Python Programming Tutorial #17 - Optional Parameters

Tech With Tim · Beginner ·🛠️ AI Tools & Apps ·9y ago

Key Takeaways

This video tutorial covers the use of optional parameters in Python functions, allowing developers to set default values for parameters and make their code more efficient.

Full Transcript

hey guys and welcome back to another YouTube video this is the 17th video in my Python programming series and today we're going to be talking about functions more advanced functions and something called optional parameters so let's just give a quick refresher of what a function is I'm just going to write one very quickly I did do another video on basic function so if you haven't seen that yet go check that one out first now here we've defined what we're going to call the function func and then inside of these brackets we put something called parameters if you remember that from the other video so in this case I'm just going to put X as my only parameter and then I'm simply going to print X to the screen just like that so a very very basic function but it's just to give you a quick refresher of what it does now when we wanted to call our function all we had to do was write our function name and then put a brackets or the brackets like this and then inside of our brackets if we had a parameter set we had to do we had to give it that parameter so we could do three we could do hello let's just do hello in this case and we'll click f5 and we get hello on the screen just like that now we can change this again to maybe Tim which is my name just like that and we get to M printed the screen very basic very easy just like that okay so that's pretty much what a function is now what we're going to talk about today is more in-depth on these parameters so you remember we could give it multiple parameters so we do X we could do maybe text like this so we had X and text and we could print X to the screen and maybe we'll say if text is equal to UM let's say 1 so with a string 1 then maybe we'll print text is 1 otherwise so else we will print text is not 1 just like that very easy ok so now what we have to do when we call the function is we have to give it another parameter in this case I'm going to give it 2 and we'll see what happens we get Tim and then texture I misspelled that my bad is not 1 now if I change this parameter to 1 again we'll see that we get text is 1 now again that's just another example but in some cases when we call a function and we have many parameters and maybe we don't have many but we don't want to constantly be rewriting these parameters we want to have something called the default value for our parameters so X maybe we always want to give our function the value of X but in text here we always want it to be set to something unless we otherwise specify so in this case I'm going to say text and then the equal sign I'm going to put two just like that now here I'm going to remove the one and you'll see what will happen here we'll just run it to show you and then we'll go in what's actually happening so we get Tim and then text is not one so what actually happened here is since I didn't specify a value for text so I didn't do a comma and write something else here it defaulted to the value of two now we can prove that by just simply printing out text and showing you that it is actually two so we have Tim and two just like that if I wanted to change this value now all I would have to do is simply put a comma here in the function and change it to whatever I want so in this case I'll do 67 and if we print it again you'll see we get 67 so that's how the optional parameter works now all you have to do to set an optional parameter is simply type an equal sign after your parameter so if I have X and I'll set it equal to three like that now when I call the function I actually don't have to give it any parameters because it already has the optional parameter of three and of two which are going to be the default values if I don't write anything in my function argument here so inside of these brackets and you'll see you again we get three and two because those are the default values now there is one tricky thing that you have to understand about these optional parameters say you wanted to have X stay at the default value so you want it to be three and you didn't want to type three in the brackets like that but you wanted to change the text optional parameter - something like seven or eight or maybe another string well you can't actually do that in order to change this text optional parameter you would have to also set a value for X here so if I try to I'll say I just want to set the text to three so I'll do like three here it's actually not going to change I'm sorry and what do you call it isn't I'll change it something else so we can see it better - five let's do five it's actually not going to change that second variable right it's going to change the first one automatically because that's just the order in which they're defined so if you wanna if you have more than one optional parameter you will have to set a value for the one before in order to change the one afterwards like this so that's the only tricky part about these optional parameters other than that they're extremely useful again you would use them if you're constantly using a function and you don't want to have to keep writing in parameters you only want to give it a parameter in a very specific case then you would have that optional parameter already set so you don't have to keep typing a certain word you don't have to keep giving it a value like that so this has been a shorter video but this is the video on how to do an optional parameter they look just like this they're very simple and pretty basic and I hope you learned something today if you did please like and subscribe I will see you again in another video

Original Description

This is the 17th video in my python programming series and in today's video I talk about how to use optional parameters. These are useful if you are constantly calling a function passing the same value and want to set a default value for a given parameter. Text-Based Tutorial: https://techwithtim.net/tutorials/python-programming/beginner-python-tutorials/error-handling-try-except/ Please LIKE and SUBSCRIBE for more content :)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from Tech With Tim · Tech With Tim · 19 of 60

1 A* Path Finding Algorithm(Visualization)
A* Path Finding Algorithm(Visualization)
Tech With Tim
2 Python Programming Tutorial #1 - Variables and Data Types
Python Programming Tutorial #1 - Variables and Data Types
Tech With Tim
3 Python Programming Tutorial #2 - Basic Operators and Input
Python Programming Tutorial #2 - Basic Operators and Input
Tech With Tim
4 Python Programming Tutorial #3 - Conditions
Python Programming Tutorial #3 - Conditions
Tech With Tim
5 Python Programming Tutorial #4 - IF/ELIF/ELSE
Python Programming Tutorial #4 - IF/ELIF/ELSE
Tech With Tim
6 Python Programming Tutorial #5 - Chained Conditionals and Nested Statements
Python Programming Tutorial #5 - Chained Conditionals and Nested Statements
Tech With Tim
7 Python Programming Tutorial #6 - For Loops
Python Programming Tutorial #6 - For Loops
Tech With Tim
8 Python Programming Tutorial #7 - While Loops
Python Programming Tutorial #7 - While Loops
Tech With Tim
9 Python Programming Tutorial #8 - Lists and Tuples
Python Programming Tutorial #8 - Lists and Tuples
Tech With Tim
10 Python Programming Tutorial #9 - Iteration by Item (For Loops Continued...)
Python Programming Tutorial #9 - Iteration by Item (For Loops Continued...)
Tech With Tim
11 Python Programming Tutorial #10 - String Methods
Python Programming Tutorial #10 - String Methods
Tech With Tim
12 How to Overclock a NVIDIA GPU
How to Overclock a NVIDIA GPU
Tech With Tim
13 Python Programming Tutorial #11 - Slice Operator
Python Programming Tutorial #11 - Slice Operator
Tech With Tim
14 Python Programming Tutorial #12 - Functions
Python Programming Tutorial #12 - Functions
Tech With Tim
15 Python Programming Tutorial #13 - How to Read a Text File
Python Programming Tutorial #13 - How to Read a Text File
Tech With Tim
16 Python Programming Tutorial #14 - Writing to a Text File
Python Programming Tutorial #14 - Writing to a Text File
Tech With Tim
17 Python Programming Tutorial #15 - Using .count() and .find()
Python Programming Tutorial #15 - Using .count() and .find()
Tech With Tim
18 Python Programming Tutorial #16 - Introduction to Modular Programming
Python Programming Tutorial #16 - Introduction to Modular Programming
Tech With Tim
Python Programming Tutorial #17 - Optional Parameters
Python Programming Tutorial #17 - Optional Parameters
Tech With Tim
20 Python Programming Tutorial #18 - Try and Except (Python Error Handling)
Python Programming Tutorial #18 - Try and Except (Python Error Handling)
Tech With Tim
21 Python Programming Tutorial #19 - Global vs Local Variables
Python Programming Tutorial #19 - Global vs Local Variables
Tech With Tim
22 Python Programming Tutorial #20 - Classes and Objects
Python Programming Tutorial #20 - Classes and Objects
Tech With Tim
23 Cool VBS Script to Prank Your Friends!
Cool VBS Script to Prank Your Friends!
Tech With Tim
24 How to Overclock an AMD GPU
How to Overclock an AMD GPU
Tech With Tim
25 Best GPU'S For Mining Ethereum (2018)
Best GPU'S For Mining Ethereum (2018)
Tech With Tim
26 Recursion and Memoization Tutorial Python
Recursion and Memoization Tutorial Python
Tech With Tim
27 Ethereum Mining Rig - Hardware Guide
Ethereum Mining Rig - Hardware Guide
Tech With Tim
28 Pygame Tutorial #1 - Basic Movement and Key Presses
Pygame Tutorial #1 - Basic Movement and Key Presses
Tech With Tim
29 How to Install Pygame (Windows 8/10)
How to Install Pygame (Windows 8/10)
Tech With Tim
30 How to Trade Your Cryptocurrency (Bitcoin, Ethereum etc.) For Cash!
How to Trade Your Cryptocurrency (Bitcoin, Ethereum etc.) For Cash!
Tech With Tim
31 How to Mine Ethereum 2018 - WORKING (Super-Easy)
How to Mine Ethereum 2018 - WORKING (Super-Easy)
Tech With Tim
32 Microphone Comparison - $10 Mic vs $150 Mic (Blue Yeti USB)
Microphone Comparison - $10 Mic vs $150 Mic (Blue Yeti USB)
Tech With Tim
33 Pygame Tutorial #2 - Jumping and Boundaries
Pygame Tutorial #2 - Jumping and Boundaries
Tech With Tim
34 Pygame Tutorial #3 - Character Animation & Sprites
Pygame Tutorial #3 - Character Animation & Sprites
Tech With Tim
35 Pygame Tutorial #4 - Optimization & OOP
Pygame Tutorial #4 - Optimization & OOP
Tech With Tim
36 OBS Studio Tutorial - Best OBS Settings
OBS Studio Tutorial - Best OBS Settings
Tech With Tim
37 Linear Search Algorithm - Python Example and Code
Linear Search Algorithm - Python Example and Code
Tech With Tim
38 Make Any Mic Sound AMAZING! (WITH OBS)
Make Any Mic Sound AMAZING! (WITH OBS)
Tech With Tim
39 Binary Search Algorithm - Python Example & Code
Binary Search Algorithm - Python Example & Code
Tech With Tim
40 Pygame Tutorial #5 - Projectiles
Pygame Tutorial #5 - Projectiles
Tech With Tim
41 Pygame Game - Mini Golf
Pygame Game - Mini Golf
Tech With Tim
42 Pygame Tutorial - Projectile Motion (Part 1)
Pygame Tutorial - Projectile Motion (Part 1)
Tech With Tim
43 Pygame Tutorial - Projectile Motion (Part 2)
Pygame Tutorial - Projectile Motion (Part 2)
Tech With Tim
44 Pygame Tutorial #6 - Enemies
Pygame Tutorial #6 - Enemies
Tech With Tim
45 Pygame Tutorial #7 - Collision and Hit Boxes
Pygame Tutorial #7 - Collision and Hit Boxes
Tech With Tim
46 Pygame Tutorial #8 - Scoring and Health Bars
Pygame Tutorial #8 - Scoring and Health Bars
Tech With Tim
47 Cloud Mining vs. Hardware Mining - 2018
Cloud Mining vs. Hardware Mining - 2018
Tech With Tim
48 How to Install Pygame on Mac OSX (Fast-Simple)
How to Install Pygame on Mac OSX (Fast-Simple)
Tech With Tim
49 Pygame Tutorial #9 - Sound Effects, Music & More Collision
Pygame Tutorial #9 - Sound Effects, Music & More Collision
Tech With Tim
50 Pygame Tutorial #10 - Finishing Touches & Next Steps
Pygame Tutorial #10 - Finishing Touches & Next Steps
Tech With Tim
51 How to Fade Your Screen in Pygame [CODE IN DESCRIPTION]
How to Fade Your Screen in Pygame [CODE IN DESCRIPTION]
Tech With Tim
52 How to Create a Button in Pygame [CODE IN DESCRIPTION]
How to Create a Button in Pygame [CODE IN DESCRIPTION]
Tech With Tim
53 Pygame Side-Scroller Tutorial #1 - Scrolling Background/Character Movement
Pygame Side-Scroller Tutorial #1 - Scrolling Background/Character Movement
Tech With Tim
54 Pygame Side-Scroller Tutorial #2 - Random Object Generation
Pygame Side-Scroller Tutorial #2 - Random Object Generation
Tech With Tim
55 Pygame Side-Scroller Tutorial #3 - Collision
Pygame Side-Scroller Tutorial #3 - Collision
Tech With Tim
56 Pygame Side-Scroller Tutorial #4 - Scoring and End Screen
Pygame Side-Scroller Tutorial #4 - Scoring and End Screen
Tech With Tim
57 How to Create A Message Box in Python - Tkinter
How to Create A Message Box in Python - Tkinter
Tech With Tim
58 Is Ethereum Mining Still Profitable - Is It Worth It (April 2018)
Is Ethereum Mining Still Profitable - Is It Worth It (April 2018)
Tech With Tim
59 How to Run MAC OSX on a WINDOWS PC (Clover Boot-loader)
How to Run MAC OSX on a WINDOWS PC (Clover Boot-loader)
Tech With Tim
60 Programming Problem #1 - Alphabet Soup (Beginner/Novice)
Programming Problem #1 - Alphabet Soup (Beginner/Novice)
Tech With Tim

This video tutorial teaches how to use optional parameters in Python functions to set default values and make code more efficient. It covers the basics of function parameters, default values, and how to use them in Python programming.

Key Takeaways
  1. Define a function with parameters
  2. Set default values for parameters using the equals sign
  3. Call the function with and without parameters
  4. Understand how to use optional parameters to make code more efficient
💡 Optional parameters in Python functions allow developers to set default values and make their code more efficient by reducing the need to constantly pass the same values to functions.

Related Reads

📰
AI Tools for Small Business Automation: One Bounded Job Before You Buy
Learn to automate small business tasks with AI tools by focusing on one bounded job and evaluating tools based on data exposure and error cost
Dev.to AI
📰
From Zero‑Budget to Interview Calls: Leveraging Free AI Bots for Tech Job Hunts
Learn how to leverage free AI bots to accelerate your tech job hunt from zero-budget to interview calls
Dev.to · Elyasians(elyasians.com)
📰
AI Content Labels: Build Trust Signals Before Users Stop Believing the Page
Learn to build trust signals for AI-generated content to improve user trust and experience
Dev.to · Jack M
📰
I Built 3 Free AI Tools That Help You Find the Right AI Stack
Discover three free AI tools to help you find the right AI stack for your needs, including a quiz, prompt generator, and comparison tool
Dev.to AI
Up next
How to Get Your Brand Cited by ChatGPT, Claude and Gemini
Arvow
Watch →