Learn Python Episode #17: Default Arguments

Publié le: 15 août 2017
sur la chaîne: Joseph Delgadillo
3,133
43

Learn Python programming and more with our Learn to Code course Bundle!
⁠⁠⁠⁠⁠⁠⁠⁠⁠⁠⁠⁠https://josephdelgadillo.com/product/...

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://bit.ly/SubscribeJTD
Facebook -   / delgadillojt  


Sur cette page du site, vous pouvez voir la vidéo en ligne Learn Python Episode #17: Default Arguments durée heure minute seconde en bonne qualité , qui a été Téléchargé par l'utilisateur Joseph Delgadillo 15 août 2017, Partagez le lien avec vos amis et connaissances, sur youtube cette vidéo a déjà été regardée 3,133 fois et il a aimé 43 téléspectateurs. Bon visionnage!