def functionname():
→function_body - Answers #creates a function
def (for "define")
after def goes the name of the function
Remember - Python reads your code from top to bottom. It's not going to look ahead in order to find a
function you forgot to put in the right place ("right" means "before invocation")
You mustn't have a function and a variable of the same name. Assigning a value to the name message
causes Python to forget its previous role. The function named message becomes unavailable.
def message():
→print("Enter next value")
print("We start here")
message()
print("The end is here") - Answers We start here
Enter next value
The end is here
def message(who,no):
→print("'s favorite number is", no)
message("Satan", 666) - Answers Satan's favorite number is 666
def introduction(first,last):
→print("Hello, my name is ", first, last, ".")
, introduction(first="James", last="Bond")
introduction(last="Skywalker", first="Luke") - Answers Hello, my name is James Bond.
Hello, my name is Luke Skywalker.
#You can mix both fashions if you want - there is only one unbreakable rule: you have to put positional
arguments before keyword ones.
def introduction(first, last="Adams"):
→print("Hello, my name is ", first, last, ".")
introduction("James")
introduction("Hunter", "Skywalker") - Answers Hello, my name is James Adams .
Hello, my name is Hunter Skywalker .
def strange (n):
→if(n % 2 == ):
→→return True
print(strange(2))
print(strange(1)) - Answers True
None
def sumoflist(l):
→sum = 0
→for el in l:
→→sum += el
→return sum
print(sumoflist([5,4,3])) - Answers 12