Coding in Python: a comprehensive Beginners Guide to Learn the Realms of Coding in Python
Download 1.25 Mb. Pdf ko'rish
|
Coding in Python A Comprehensive Beginners Guide to Learn the Realms
- Bu sahifa navigatsiya:
- Default Values
Keyword Arguments
There is another way out. You can use keyword arguments to avoid this kind of mix up. A keyword argument is like a name-value pair that is passed to a function. A keyword argument allows you to create a link between the name and the value inside an argument. When you pass the argument to the function, Python cannot mistake it. It eliminates the confusion and you do not have to worry about bringing your arguments in order. def user_info(username, email_id): print("My name is " + username.title() + " and I am a new user.") print("My email ID is " + email_id.title() + ".") user_info(email_id = 'joe@gmail.com', username = 'Joe') user_info( email_id = 'jimmy@outlook.com', username = 'Jimmy' ) user_info( email_id = 'emily@gmail.com', username = 'Emily') user_info(username = 'Emilia', email_id = 'emilia@yahoo.com') user_info(username = 'Mark', email_id = 'mark@outlook.com') = RESTART: C:/Users/saifia computers/Desktop/sample.py My name is Joe and I am a new user. My email ID is Joe@Gmail.Com. My name is Jimmy and I am a new user. My email ID is Jimmy@Outlook.Com. My name is Emily and I am a new user. My email ID is Emily@Gmail.Com. My name is Emilia and I am a new user. My email ID is Emilia@Yahoo.Com. My name is Mark and I am a new user. My email ID is Mark@Outlook.Com. >>> I have changed the positions of the arguments and it hardly affected the results. Keyword arguments help you create a functional program. Default Values When you are writing a program, you may come up with information that you have to use repeatedly. This means that you will have to fill in the function call with the required arguments each you need that information to be executed. This may result in a waste of time and energy, and may also cause frustration. If you create default values for the function, you will be able to execute the excessively used information fast and efficiently. When you leave the function call empty, it will use the default arguments. You can use the default information as many times as you want to. The default values tend to simply a program and declutter the code. I will fill in the same example with the default arguments and also use the default values multiple times. def user_info(username = 'Dora', email_id = 'dora@outlook.com'): print("My name is " + username.title() + " and I am a new user.") print("My email ID is " + email_id.title() + ".") user_info(email_id = 'joe@gmail.com', username = 'Joe') user_info() user_info( email_id = 'jimmy@outlook.com', username = 'Jimmy' ) user_info() user_info( email_id = 'emily@gmail.com', username = 'Emily') user_info(username = 'Emilia', email_id = 'emilia@yahoo.com') user_info(username = 'Mark', email_id = 'mark@outlook.com') user_info() = RESTART: C:/Users/saifia computers/Desktop/sample.py My name is Joe and I am a new user. My email ID is Joe@Gmail.Com. My name is Dora and I am a new user. My email ID is Dora@Outlook.Com. My name is Jimmy and I am a new user. My email ID is Jimmy@Outlook.Com. My name is Dora and I am a new user. My email ID is Dora@Outlook.Com. My name is Emily and I am a new user. My email ID is Emily@Gmail.Com. My name is Emilia and I am a new user. My email ID is Emilia@Yahoo.Com. My name is Mark and I am a new user. My email ID is Mark@Outlook.Com. My name is Dora and I am a new user. My email ID is Dora@Outlook.Com. >>> You can use the keyword arguments, the positional arguments and the default values at the same time. def user_info(username = 'Dora', email_id = 'dora@outlook.com'): print("My name is " + username.title() + " and I am a new user.") print("My email ID is " + email_id.title() + ".") user_info(email_id = 'joe@gmail.com', username = 'Joe') user_info() user_info( username = 'Jimmy' ) user_info() user_info( email_id = 'emily@gmail.com', username = 'Emily') user_info('Emilia', email_id = 'emilia@yahoo.com') user_info('Mark', 'mark@outlook.com') user_info() = RESTART: C:/Users/saifia computers/Desktop/sample.py My name is Joe and I am a new user. My email ID is Joe@Gmail.Com. My name is Dora and I am a new user. My email ID is Dora@Outlook.Com. My name is Jimmy and I am a new user. My email ID is Dora@Outlook.Com. My name is Dora and I am a new user. My email ID is Dora@Outlook.Com. My name is Emily and I am a new user. My email ID is Emily@Gmail.Com. My name is Emilia and I am a new user. My email ID is Emilia@Yahoo.Com. My name is Mark and I am a new user. My email ID is Mark@Outlook.Com. My name is Dora and I am a new user. My email ID is Dora@Outlook.Com. >>> The most important thing to note in the code mentioned above is that in one function call when I missed out on writing the email ID, the program picked it up from the default values and ran it. If you leave one argument in the function call but have a default argument in place, you will have it covered by the default values. You may run an error if you fail to fill in the function call with the arguments. def user_info(username, email_id ): print("My name is " + username.title() + " and I am a new user.") print("My email ID is " + email_id.title() + ".") user_info() = RESTART: C:/Users/saifia computers/Desktop/sample.py Traceback (most recent call last): File "C:/Users/saifia computers/Desktop/sample.py", line 6, in user_info() TypeError: user_info() missing 2 required positional arguments: 'username' and 'email_id' >>> Download 1.25 Mb. Do'stlaringiz bilan baham: |
Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling
ma'muriyatiga murojaat qiling