Lesson 1
Your own functions
Wrap up code with def, give it inputs, and get answers back with return.
def makes a function
A function is a block of code with a name, so you can run it whenever you need it.
def (short for define), the name, brackets and a colon, then the indented lines inside. Nothing happens when Python reads the def: the code runs when you call the function, by writing its name with brackets.
def greet():
print("Hello!")
print("Welcome to Python School.")
greet()
greet()
Parameters and return
Parameters are the function’s inputs, named inside the brackets. When you call it, you give a value for each one.
return sends a value back to wherever the function was called, and ends the function.
def add(a, b):
return a + b
result = add(3, 4)
print(result)
print(add(10, 5) * 2)
print is not return
print() shows something to the person using the program. return hands a value back to the program, so it can store it or use it.
A function that never says return gives back None, Python’s word for “nothing”.
def show(n):
print(n * 2)
def give(n):
return n * 2
x = show(5)
y = give(5)
print(x)
print(y)
Lesson 2
Smarter functions
Default values, giving back two things, and where variables live.
Default values
A parameter can have a default value, used when the call leaves it out: greeting="Hello".
You can also give values by name, in any order: greet(greeting="Hey", name="Mia").
def greet(name, greeting="Hello"):
return f"{greeting}, {name}!"
print(greet("Priya"))
print(greet("Lucas", "Hi"))
print(greet(greeting="Hey", name="Mia"))
Giving back two things
return low, high gives back two values at once. low, high = min_max(...) puts them in two variables.
And print() can show several things: put commas between them, and it adds a space between each.
def min_max(nums):
return min(nums), max(nums)
low, high = min_max([4, 9, 1, 7])
print(low, high)
Variables inside stay inside
A variable made inside a function is local: it only exists while the function runs, and the code outside can’t see it.
A variable with the same name outside is a different variable, so the function doesn’t change it.
def make_total():
total = 100
return total
total = 5
make_total()
print(total)
Lesson 3
String power tools
Change case, split sentences into words, join them back, and more.
Methods: tools built into strings
Strings come with methods, functions you call with a dot after the string:
.upper()and.lower()change the case
.strip()removes spaces from both ends
.replace("cats", "birds")swaps one piece of text for another
They don’t change the original string: they give you a new one.
word = " Hello Python "
print(word.strip())
print(word.upper())
print(word.strip().lower())
print("cats and dogs".replace("cats", "birds"))
Splitting and joining
.split()breaks a string into a list of words. Give it something to split on, like.split(","), to split there instead
"-".join(words)joins a list back into one string, with-between
- strings have positions like lists:
word[0]is the first letter, andword[:3]the first three
sentence = "the quick brown fox"
words = sentence.split()
print(words)
print(len(words))
print("-".join(words))
print(sentence[:3])
Recap
What Level 5 covers, in a few lines.
def name(parameters):makes a function: a named block of code you can run again and again by calling it, likename(3).returnsends a value back to the code that called the function. A function withoutreturngives backNone.print()shows something to people;returnhands a value to the rest of your program.- Parameters can have default values, like
def greet(name, greeting="Hello"):. A function can return two values:return low, high. - Strings have methods:
.upper(),.lower(),.strip(),.replace(),.split()and"-".join(words). They give back new strings.
Now try it in Python School
Write real Python and run it right there, with a check after every step and a test at the end of each level. Free.