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
Function and Dictionary
You can pair up a dictionary with a function. Take the example of the following function. def user_info(username, gender, email_id): user = {'uname': username, 'gender': gender, 'email address': email_id} return user newuser = user_info('Johnson', 'johnson@gmail.com', 'male') print(newuser) newuser1 = user_info('John', 'john@gmail.com', 'male') print(newuser1) newuser2 = user_info('Jimmy', 'jimmy@gmail.com', 'male') print(newuser2) newuser3 = user_info('Dora', 'dora@gmail.com', 'female') print(newuser3) = RESTART: C:/Users/saifia computers/Desktop/sample.py {'uname': 'Johnson', 'gender': 'johnson@gmail.com', 'email address': 'male'} {'uname': 'John', 'gender': 'john@gmail.com', 'email address': 'male'} {'uname': 'Jimmy', 'gender': 'jimmy@gmail.com', 'email address': 'male'} {'uname': 'Dora', 'gender': 'dora@gmail.com', 'email address': 'female'} >>> The function user_info takes the requisite information about the name, gender and email address of a user, and fill them up into a dictionary. Each value is stored in the designated key. The function receives information in raw form and turns it into textual information in a meaningful data structure. Up till now, I have stored and processed the information in the form of strings. You may confront situations where you have to store data in numerical form as well. There is an easy way out. def user_info(username, gender, email_id, age=''): user = {'uname': username, 'gender': gender, 'email address': email_id} if age: user['age'] = age return user newuser = user_info('Johnson', 'johnson@gmail.com', 'male', age=55) print(newuser) newuser1 = user_info('John', 'john@gmail.com', 'male', age=33) print(newuser1) newuser2 = user_info('Jimmy', 'jimmy@gmail.com', 'male', age= 54) print(newuser2) newuser3 = user_info('Dora', 'dora@gmail.com', 'female', age= 24) print(newuser3) = RESTART: C:/Users/saifia computers/Desktop/sample.py {'uname': 'Johnson', 'gender': 'johnson@gmail.com', 'email address': 'male', 'age': 55} {'uname': 'John', 'gender': 'john@gmail.com', 'email address': 'male', 'age': 33} {'uname': 'Jimmy', 'gender': 'jimmy@gmail.com', 'email address': 'male', 'age': 54} {'uname': 'Dora', 'gender': 'dora@gmail.com', 'email address': 'female', 'age': 24} >>> I have added a new parameter to the function's definition and have also assigned this parameter a kind of empty default value. 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