Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # define a function
- def my_function():
- print("Hello in my function")
- my_function() # function call
- # define a function with parameters
- def say_hello(name, name2): # 'name' is a parameters
- print("Hello " + name + " and " + name2)
- say_hello("Ola", "Artur") # 'Ola' and 'Artur' are arguments
- # define a function with parameters and keywords!
- def say_hi(name3, name2, name1): # 'name1', 'name2' and 'name3' are keywords
- print("Hello " + name1 + ", " + name2 + " and " + name3)
- print("I like the first person so much " + name1)
- say_hi(name2="Ania", name1="Hania", name3="Kasia")
- # define a function with type hint
- def person_(name: str, age: int): # ': str' and ': int' are type hints
- print("Name: " + name)
- print("Age: " + str(age))
- person_("Ola", 18)
- def greet_people(people: list) -> list: # '-> list[str]' you can find sth like that too
- return [f"Hello {person}! How are you doing today?" for person in people]
- result = greet_people(["James", "Matthew", "Claire"])
- for item in result:
- print(item)
- # function aliasing
- def fruits(fruit: str):
- print(f"I like {fruit}")
- food = fruits # the same function, but with another name
- print("ID of food " + str(id(food)))
- print(f"ID of fruits {id(fruits)}")
- fruits("apple")
- food("apple")
Advertisement
Add Comment
Please, Sign In to add comment