Kelsey Chan and Kelsey Wong
Brought to you by Kelsey Chan (she/her) and Kelsey Wong (she/her)
Your program will only run if it follows all the rules of Python syntax.
For example, Python uses indentation to specify blocks of code.
if 10 > 0:
print('Dog!')
else:
print('Cat!')
if 10 > 0:
print('Dog!')
else:
print('Cat!')
Reserved words have special meaning in Python. Later, we'll teach you how to add your own words to Python's vocabulary.
and del global not with
as elif if or yield
assert else import pass
break except in raise
class finally is return
continue for lambda try
def from nonlocal while
You can download Python onto your computer, but we'll be using Repl.it to run our code online.
Save your code in a file
You can download Python onto your computer, but we'll be using Repl.it to run our code online.
Use the interactive console for one-time use code
We'll be giving you links to view code in Python Tutor, which is a tool to visualize what each line of code does.
Variables are containers for storing values. Simple examples of values are:
Python's input function allows you to collect input from the user.
An expression is a combination of:
4 * 5 # evaluates to 20
"tri" + "angle" # evaluates to "triangle"
my_variable + 1
A statement is like an instruction. We've already seen some:
print("Hello world!")
new_variable = 10
In upcoming slides, we'll learn about more types of statements.
A boolean expression is a specific type of expression that evaluates
to a boolean value - either true or false.
View Example on Python Tutor
The logical operators and, or, and not allow us to write more complicated boolean expressions.
View Example on Python Tutor
If/else statements let you tell the program how to make decisions. Every if/else statement has a boolean expression as its condition.
if number_of_points >= 10:
print("You win!")
else:
print("You still need more points")
A function is a group of statements that together do a particular task. Functions help to organize, reuse, and clarify our code.
def double(number):
doubled = number * 2
return doubled
After you define a function, you can use it by calling it, which requires the
function name with the correct number of parameters.
View Example on Python Tutor
Functions can optionally include return statements, with 0 or more expressions to return.
View Example on Python Tutor
Data structures are used to organize and store data.
Used to store an ordered sequence of values
View Example on Python TutorLike lists, but they do not store duplicate entries, and they are unordered
View Example on Python TutorUsed to store values associated with keys
View Example on Python Tutorfor loop and the while loop.
For loopsThe for loop is useful for iterating over a sequence.
While loops
The while loop is used to repeatedly execute instructions
as long as a boolean condition is met.
for or while loop.
for or
while statement.
Strings are sequences of letters surrounded by double or single quotes.
View example on Python TutorRemember you can ask questions in the chat!