Coding in Python: a comprehensive Beginners Guide to Learn the Realms of Coding in Python


Download 1.25 Mb.
Pdf ko'rish
bet33/48
Sana30.01.2023
Hajmi1.25 Mb.
#1140552
1   ...   29   30   31   32   33   34   35   36   ...   48
Bog'liq
Coding in Python A Comprehensive Beginners Guide to Learn the Realms

Returning Values
A function does not have to display the output in a direct form. You can
make the function process a bunch of data and return the value in an indirect
form. The return statement picks up a value from the function and forwards it
to the line that made a function call.
def user_info(username, email_id ):
info = "My name is " + username.title() + " and I am a new user, and my
email ID is " + email_id.title() + "."
return info.title()
newuser = user_info('Dora', 'dora@gmail.com')
print(newuser)
newuser1 = user_info('John', 'john@gmail.com')
print(newuser1)
newuser2 = user_info('Jimmy', 'jimmy@gmail.com')
print(newuser2)
= RESTART: C:/Users/saifia computers/Desktop/sample.py
My Name Is Dora And I Am A New User, And My Email Id Is
Dora@Gmail.Com.
My Name Is John And I Am A New User, And My Email Id Is
John@Gmail.Com.


My Name Is Jimmy And I Am A New User, And My Email Id Is
Jimmy@Gmail.Com.
>>>
In the next code sample, I will add another argument to the code. I will also
experiment on making an argument optional so that the users who do not
want to fill in a value, can leave it without running an error in the program.
def user_info(username, email_id, gender ):
info = "My name is " + username.title() + " and I am a new user, and my
email ID is " + email_id.title() + ". My gender is " + gender.title() + "."
return info.title()
newuser = user_info('Dora', 'dora@gmail.com', 'female')
print(newuser)
newuser1 = user_info('John', 'john@gmail.com', 'male')
print(newuser1)
newuser2 = user_info('Jimmy', 'jimmy@gmail.com', 'male')
print(newuser2)
= RESTART: C:/Users/saifia computers/Desktop/sample.py
My Name Is Dora And I Am A New User, And My Email Id Is
Dora@Gmail.Com. My Gender Is Female.
My Name Is John And I Am A New User, And My Email Id Is
John@Gmail.Com. My Gender Is Male.
My Name Is Jimmy And I Am A New User, And My Email Id Is
Jimmy@Gmail.Com. My Gender Is Male.
>>>
Suppose someone wants to leave the email option aside. You can add a
conditional statement to the existing code to allow users to make a choice at
will.
def user_info(username, gender, email_id='' ):
if email_id:


info = "My name is " + username.title() + " and I am a new user, and
my email ID is " + email_id.title() + ". My gender is " + gender.title() + "."
else:
info = "My name is " + username.title() + " and my gender is " +
gender.title() + "."
return info.title()
newuser = user_info('Dora', 'female')
print(newuser)
newuser1 = user_info('John', 'john@gmail.com', 'male')
print(newuser1)
newuser2 = user_info('Jimmy', 'jimmy@gmail.com', 'male')
print(newuser2)
= RESTART: C:/Users/saifia computers/Desktop/sample.py
My Name Is Dora And My Gender Is Female.
My Name Is John And I Am A New User, And My Email Id Is Male. My
Gender Is John@Gmail.Com.
My Name Is Jimmy And I Am A New User, And My Email Id Is Male. My
Gender Is Jimmy@Gmail.Com.
>>>

Download 1.25 Mb.

Do'stlaringiz bilan baham:
1   ...   29   30   31   32   33   34   35   36   ...   48




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling