Get The Learn to Code Course Bundle!
https://josephdelgadillo.com/product/learn-to-code-course-bundle/
Enroll in The Complete Python Course on Udemy!
https://www.udemy.com/python-complete/?couponCode=PYTHONWP
In this video we were going to discuss arguments a bit further, and we were going to move on to keyword arguments. Before we do that however, it is going to make more sense to cover default arguments first. So, let’s go ahead and write a function.
def print_something(name, age):
print("My name is ", name, + " and my age is " + age)
print_something("Nick", 27)
This won’t run. Remember, when we concatenate strings we must convert integers to strings as well. While we could use the str function to fix our code, it is much easier to separate the four pieces of data with commas.
def print_something(name, age):
print("My name is", name, "and my age is", age)
print_something("Nick", 27)
The results will be the same as if we converted the integer to a string and concatenated the four strings together. So, what happens if we only want to pass our function 1 of the 2 values? This is where we can use default arguments.
def print_something(name = "Someone", age = "Unknown"):
print("My name is", name, "and my age is", age)
print_something("Nick")
When we run this bit of code, Python will print out My name is Nick and my age is Unknown. As you can see, Python will default to the argument we provided it if no data is given.
Web – https://josephdelgadillo.com/
Subscribe – https://goo.gl/tkaGgy
Follow for Updates – https://steemit.com/@jo3potato