1/31
Looks like no tags are added yet.
Name | Mastery | Learn | Test | Matching | Spaced |
---|
No study sessions yet.
Integer (int)
whole numbers 5, 2, -20
float(float)
decimal numbers, 2.0, 3.7
String (str)
text in quotes "Hello World", "CPSC150"
Containers
set, list, dictionary, tuple
Strings
a collection of letters/characters
Immutable
you cannot change a character in a string. Any function returns a new string (.upper(), .lower())
Concatenation
adding strings together
Length
gives how many characters are in a string
String Formatting
use f"{var_name}"
Lists
-an ordered collection of items that can be edited (mutable)
- created using square brackets []
- accessed based on the index of the element
- index starts at 0
.append(item)
adds to end of list
.remove(item)
removes this item
Dictionary
- a collection of key - value pairs
- created using curly braces
- my_dict = { key : value }
Adding to a Dictionary
my_dict[key] = value
Accessing a Dictionary
my_dict[key] -> value
Tuples
- collection of ordered items that are immutable
- created using parentheses ()
- Accessed based on the index of the element
- Use Square Brackets to Access
Sets
- Mutable, Unordered collection of unique elements
- created using curly braces {}
Scope
- Passing a primitive (int, string, character) to a function does not change the original USE RETURN
- Passing a list to a function does not update the original list
User Input
- Use input() function
- Make sure you convert the correct type
Booleans
statements that evaluate to be True or False
If-else statements
-If Statements only execute if the statement is true
- Elif (Else-If) executes if the previous if statement is not true and it is true
- else executes only if everything above it has not
Functions
always start with the def notation and then any params in ()
- the parameter names are what is used in the function, these do not necessarily have to be the variable names outside of the function
- A variable that is defined inside the function is a local variable. If you want to use it outside then need to use return
this only prints the variable/output to the console
Return
this returns the variable to where the function was called so that you can use it
Default parameters
assigned if no parameters are given
For Loop
used when you know how many iterations you want to go through
2 main types of for loops
using range an iterating through the sequence
While Loop
used when you are unsure how long you want the loop to run for
Nested Loops
-for every iteration of the outer loop the inner loop fully runs
-can be good for iterating through 2D lists
break
fully exists out of the loop
continue
exists the current iteration, but continues the loop
Files
- make sure to use with open() so that you don't have to worry about closing the file
- 'r' to read, 'w' to write, 'a' to append