1/24
Variables & types , conditionals, booleans, functions, loops, & data structures (strings, lists, dictionaries, & tuples)
Name | Mastery | Learn | Test | Matching | Spaced | Call with Kai | Chat |
|---|
No analytics yet
Send a link to your students to track their progress
String (str)
Can be letters, words, sentences, or numbers enclosed in quotes.
Ex:
message = ‘Hello, world!’
Integer (int)
A data type that can be any whole number but not decimals.
Ex:
fav_number = 9
Float
A decimal
Ex:
month = 10.0
Boolean (bool)
A variable that can either be True or False.
Ex:
will_ace_exam = True
Local Variable
A variable defined inside functions.
Global Variable
A variable defined outside functions and can be “seen”/used by any function.
List
A sequence of values.
Can be ints, floats, strings, bools, & other lists.
Lists are mutable.
Uses square brackets - [ ]
Ex:
numbers = [1, 2, 3]
Dictionary (dict)
Has key-value pairs - key : value.
Can be ints, floats, strings, & a value can be a list.
Dictionaries are mutable.
Uses curly brackets { }
Ex:
songs = {‘LOONA’ : ‘High High’, ‘DREAMCATCHER’ : ‘Jazz Bar’}
Tuple
Tuples are immutable lists. Need to create a new tuple to “edit” it.
Uses parentheses ( )
Ex:
greetings = (‘hello’, ‘hola’, ‘bonjur’)
Loops
Used to repeat something over and over again.
While Loop
Repeats until a condition is no longer True and becomes False.
Has uncertainty.
Ex:
while CONDITION:
BODY
For Loop
Repeats a condition in a pre-defined group of items.
Need to create a new variable (name is related to the singular item).
Ex:
for ELEMENT in GROUP:
BODY
Break
Used to stop a loop from continuing/leave the loop early.
Continue
Ignores the rest of the BODY for the current element. Then, skips to the next element in the body.
Range
Gives a sequence of numbers.
Starts from 0 and increments by default.
Ex:
range(3) = [0, 1, 2]
range(start, end, increment) - range(1, 7, 2) = [1, 3, 5]
range(start, end, decrement) - range(3, 0, -1) = [3, 2, 1]
Length (len)
A built-in function that returns the number of characters in a string.
Ex:
fruit = ‘banana’
len(fruit)
6
Slice
Returns a segment of a string.
Can be the front, back half, everything, or empty.
Starts at index 0 and excludes the last character.
[:9], [9:], [ ], [9:9]
Ex:
[starting letter # : ending letter #]
fruit[:3]
‘ban’
Append
Adds new element to the end of a list.
Ex:
list.append(element)
Sort
Sorts a list alphabetically or from low to high.
Ex:
list.sort() - works only w/ lists - doesn’t modify original list
sorted() - works w/ anything - makes a new list
String Concatenation +
Joins strings without adding a space in between.
String Concatenation ,
Combines strings and automatically adds a space in between.
If Statement
Evaluates a condition (True or False) and executes the specified block of code if the condition is True.
Ex:
if CONDITION:
BODY
If/Else Statement
Evaluates a condition (True or False) and executes the 1st block of code if the condition is True, if not then it executes the 2nd block.
Ex:
if CONDITION:
BODY 1
else:
BODY 2
Else If Statement (elif)
Used to check multiple conditions sequentially.
Ex:
if condition_1:
elif condition_2: - checks if condition_1 is False AND condition_2 is True
else:
Define (def)
Used to start a function definition.
Ex:
def function_name:
return result
def function_name(parameters):
return result