1/38
Looks like no tags are added yet.
Name | Mastery | Learn | Test | Matching | Spaced | Call with Kai |
|---|
No analytics yet
Send a link to your students to track their progress
what is python
high level programming language
easy to write and read (simple syntax for beginners)
used in web development, data science, ai, automation etc
What do variables do
used to store, manipulate and display info within a program
memory unit that can be accessed by typing the variable name
name cannot start with a number
variable (contains) value
ex: variable_name = value
ex: age = 16
int
integer, whole numbers
ex: 2
float
real numbers. (a decimal turns integer into a float)
ex: 1.3
str
string
consists of multiple chars (single characters)
str ( “variable” ) turns it into a sentence, displaying the variable as text not math
bool
boolean
only true/false
basic operations
arithmetic : + - & / %
comparison == != >. < >= <=
logical: and, or, not
conditional statements example
variable = value
if value>= 18:
print (“ you are an adult)
else:
print (“you are a minor”)
** dont forget colons and quotes
if statements
if condition:
statement1
statement 2
statement 3
if condition is valid, all indented lines will run. if not valid then execution will be skipped and continue after indent
nested blocks
blocks within other blocks
if x> 5
print (“x is greater than 5”)
if x>10
print (“x is also greater than 10”)
operators
= = does this equate? check/compare (boolean expression to evaluate if its true)
= this is equal to ___ (assignment)
! = is not equal to (returns the opposite of = = )
logical operators
and, &
when both A and B are true
or, l
either A is true or B is true
not, !
Not A (opposite of A) is true if A is false
else clauses
if condition:
codecodecode1
else:
codecodecode2
** anything besides the condition. execution moves to else given condition1 is false
Elif
Else- if
if none of the previous conditions are true. goes thru elif 1, elif 2, elif 3,… then ELSE:
used when there are multiple related conditions you when to check for and execute different code depending on which one is true
When would you assign None to a variable
when the value hasnt been decided yet. not the same as 0 zero
no input yet
while loop
called indefinite loops bc used to repeat code without knowing how many times it will be repeated before hand
while a condition is true, it will keep running. when its false, it stops
need to make sure condition is checked before the block of code is executed
need to make sure that the condition will eventually become false or loop with be infinite and never finish
what is a loop
allows programmer to execute certain sections of code multiple times
makes certain programs more simple
for loop
called definite loops bc they repeat a set number of times
while loop example
num = 1
while num <5
print (num)
num = num + 1
#### this is a counter that adds one then reloops. will continue while num<5 is true
print (“finished”)
how do comments work
not relayed in the code. just for reading
#single line comment
“ “ “ this is for
multiline
comments
“ “ “
for loop example
for x in range (5)
print (x)
will run 5x. starting from 0
what number does code/computer start counting from
zero
nesting loops
where a loop contains another loop (more if structures) in itself
for outer_variable in outer_iterable:
for inner_variable in inner_iterable:
<body>what does print (“ “ ) mean
print("") inside a for loop, outputs a single empty line during each iteration
—
for i in range(2):
print("Iteration", i)
print("") # This prints an empty line
print("Continuing...")
output——
Iteration 0
—
Continuing...
Iteration 1
—
Continuing...
what does end = mean
used to get output on the same like
can do end= “ - “ or to end= “ “ get dashes or spaces between # on same line
by default, a print statment goes to the next line so an end = prevents the new line
nested for loop with end= example

how to convert/capitalize word to title
variable.title()
All First Letters Are Capitalized
variable.capitalize()
Only first word (first letter) is capitalized
how to remove leading/trailing spaces
both sides
variable.strip()
remove whitespace only from the left
variable.lstrip()
only right
varibale.rstrip()
how to remove whitespace in multiple lines
word = “ ___hello world.___ “
cleaned_word = word.strip()
output: “hello world”
Modulo operator
%
tells you whats left over after dividing one number by another REMAINDER
result = 10%3
result = 1
what do you use a modulo operator for?
checking if # is even
= number%2
if x%2 == 0 ** then even
check if # is odd
= number%2
if x%2 == 1 ** then odd
if the dividend is smaller than the divisor: result is just the dividend
ex: 5%8 = 5
what is a range
this_is_a_range= range(3)
a sequence of numbers
often used in loops
start at 0
how to check what type is a variable (weather it is ___ or not)
age=10
print(isinstance(age, float))
: False
** here shows that age is not a float, it is an integer (whole #)
how to check the class type
is_sunny= True
print(is_sunny, type(is_sunny)
: <class str>
what data types are immutable
immutable - cant be modified or altered once declared
can reassign them (will be most updated assignment)
ex: string, integer, float, boolean, range
how to combine multiple strings together
aka string concatenation
add things together using + operator
need to convert all data to strings. can add int to string
print(name+str(age))
convert all characters to uppercase
.upper()
variable= “hello”
print(variable.upper())
HELLO
convert all characters to lowercase
.lower()
variable=”Hello World”
print(variable.lower())
hello world
count substring
count(substring)
returns # of times a substring appears in a string
word = 'hello world'
o_count = word.count('o')
print(o_count)
output —— 2