# greet_user
def greet_user():
print("Hello!")
#calling
greet_user()
Hello!
Python basics
Kunal Khurana
October 6, 2023
# passing information to the function
def greet_user(username): #username would accept the value that i provide
print(f"Hello, {username.title()}!")
# calling with username
greet_user('kunal') #don't forget to call the function as string
Hello, Kunal!
# practice
def display_message(content):
print(f"Hope you are feeling energized this monday, and you enjoyed your weekend well. I'm curious about {content. title()}!")
# calling
display_message("your prepration for the maths test!!")
Hope you are feeling energized this monday, and you enjoyed your weekend well. I'm curious about Your Prepration For The Maths Test!!!
def get_formatted_name(first_name, last_name):
"Return a full name, neatly formatted."
full_name = f"{first_name} {last_name}"
return full_name.title()
# infinite loop
while True:
print("\nPlease tell me your name:")
f_name = input("First name: ")
l_name = input("Last name: ")
formatted_name = get_formatted_name(f_name, l_name)
print(f"\nHello, {formatted_name}!")
def get_formatted_name(first_name, last_name):
"Return a full name, neatly formatted."
full_name = f"{first_name} {last_name}"
return full_name.title()
# adding break to stop the loop
while True:
print("\nPlease tell me your name:")
print("(enter 'q' at any time to qutit)")
f_name = input("First name: ")
if f_name == 'q':
break
l_name = input("Last name: ")
if l_name == 'q':
break
formatted_name = get_formatted_name(f_name, l_name)
print(f"\nHello, {formatted_name}!")
Please tell me your name:
(enter 'q' at any time to qutit)
First name: Kunal
Last name: Khurana
Hello, Kunal Khurana!
Please tell me your name:
(enter 'q' at any time to qutit)
First name: q
# capital, country
def get_capital_country (capital, country):
cap_c = f"{capital} {country}"
return cap_C.title()
while True:
print("\nPlease print capital name and country name)
print("enter q anytime to quit")
capital = input("Capital name : ")
if capital == 'q':
break
country = input("Country name : ")
if country == 'q':
break
# city_country
def city_country (city, country):
formatted_string = f"{city.title()}, {country.title()}"
return formatted_string
# INFINITE LOOPL; HOW TO STOP
count = 0
while count < 3:
print("\nThe name of the city and capital are-")
print(city_country('newyork', 'united states'))
print(city_country('delhi', 'india'))
count += 1 #increment the count
print ("loop has ended")
The name of the city and capital are-
Newyork, United States
Delhi, India
The name of the city and capital are-
Newyork, United States
Delhi, India
The name of the city and capital are-
Newyork, United States
Delhi, India
loop has ended
def print_models(unprinted_designs, completed_models): #defining with two parameters
while unprinted_designs:
current_design = unprinted_designs.pop()
print(f"Printing model: {current_design}")
completed_models.append(current_design)
def show_completed_models(completed_models): #defining with one parameter
print("\nThe following models have been printed:")
for completed_model in completed_models:
print(completed_model)
unprinted_designs = ['ferrari', 'lancer', 'accord']
completed_models = []
print_models(unprinted_designs, completed_models) #don't forget to print
show_completed_models(completed_models)
Printing model: accord
Printing model: lancer
Printing model: ferrari
The following models have been printed:
accord
lancer
ferrari
def make_pizza(*toppings):
print(toppings)#prints requested toppings
make_pizza('pepperoni')
make_pizza('mushrooms', 'green peppers', 'extra cheese')
('pepperoni',)
('mushrooms', 'green peppers', 'extra cheese')
The asterisk in the parameter name *toppings tells Python to make an empty tuple called toppings and pack whatever values it receives into this tuple.
def make_pizza(*toppings):
print("\nMake the pizza with following toppings-")
for topping in toppings:
print(f"-{topping}")
make_pizza('pepperoni')
make_pizza('mushrooms', 'green peppers', 'extra cheese')
Make the pizza with following toppings-
-pepperoni
Make the pizza with following toppings-
-mushrooms
-green peppers
-extra cheese
### adding an additional paramter 'size'
def make_pizza(size, *toppings):
print(f"\nMake a {size}-inch pizza with the following toppings-")
for topping in toppings:
print(f"-{topping}")
make_pizza(12, 'pepperoni')
make_pizza(16, 'mushrooms', 'green peppers', 'extra cheese')
Make a 12-inch pizza with the following toppings-
-pepperoni
Make a 16-inch pizza with the following toppings-
-mushrooms
-green peppers
-extra cheese
def user_profile(first, last, **user_info):
user_info['first_name'] = first
user_info['last_name'] = last
return user_info
more_info = user_profile('Kunal', 'Khurana',
location= 'montréal',
field= 'agirculture')
print(more_info)
{'location': 'montréal', 'field': 'agirculture', 'first_name': 'Kunal', 'last_name': 'Khurana'}