Learn Python Episode #17: Default Arguments

Опубликовано: 15 Август 2017
на канале: 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  


На этой странице сайта вы можете посмотреть видео онлайн Learn Python Episode #17: Default Arguments длительностью часов минут секунд в хорошем качестве, которое загрузил пользователь Joseph Delgadillo 15 Август 2017, поделитесь ссылкой с друзьями и знакомыми, на youtube это видео уже посмотрели 3,133 раз и оно понравилось 43 зрителям. Приятного просмотра!