1/51
Looks like no tags are added yet.
Name | Mastery | Learn | Test | Matching | Spaced |
---|
No study sessions yet.
integer
whole number, 5
float
decimal number, 21.4
string
text in quotes, “hi”
boolean
true/false
+ - * **
addition, subtraction, multiplication, exponentiation
/
float division
will have decimals
40/4 is 10.0
//
floor division, no decimals and rounding
41//4 is 10
%
modulus
remainder
41 % 4 = 1
assignment statement
stores a value in a variable
high_score = 10
print(x)
displays value x
input(x)
prompts the user
abs(x)
returns absolute value
round(x)
returns x rounded to nearest integer
round(x,y)
returns x rounded to yth decimal place
min(x,y)
returns smallest of values
max(x,y)
returns largest of values
len(x)
returns number of characters in string
typecasting
converting values from one data type to another
int() converts number to integer
float() converts it to a float
str() converts value to string
bool() converts to boolean
==
equal to
! =
not equal to
< > <= >=
less than
greater than
less than or equal to
greater than or equal to
and
is true if both conditions evaluate to true
or
is true if at least one conditions is true
not
is true if condition is false
if
starts a conditional
elif
branch of conditional
chains a condition onto an existing conditional
computer checks each branch until it finds a true one and executed it, skipping other branches left over
else
ends a conditional, used to fulfill other remaining cases
only if all other cases are false is it executed
import
import random
used to load an external module into a python file
to call it, write module name with . before writing function
random.randint(1,6)
while loops
repeats a block of code as long as condition is true
for loops
repeats for a specific amount of time
uses range function:
for count in range(6):
break
exits a loop early
execution skips remaining lines
continue
skips to next loop iteration, jumping to top
function
named block of code that performs a specific task
only executed when the function is called
some have return statements, others have print
to create, do: def function(parameters):
to call it: use name and parentheses and input arguments
add_tax(receipt)
lists
ordered collection or sequence of elements
can be any type of data, separated by commas
organize with brackets
indexing
describes an element based on its position in the list
location
first is always 0
last can be found with len(list) - 1
len determines number of items in an object, or list in this case
list = [1, 2, 3]
print(len(list))
result is 3
slicing
subset of list using start and end index
start is inclusive, end is exclusive
scores = [1, 2, 3, 4]
top_scores = scores[0:3]
top_scores= [1, 2, 3]
list assignment
updates element at specific index (location)
dress_colors = [“blue”, “red”, “green”]
dress_colors[2] = “brown”
dress_colors = [“blue”, “red”, “brown"]
in operator
checks if specific element exists in a list
evaluates to True or False
if “blue” in dress_colors:
(indent) print(“Yes”)
list iteration
for loops can iterate over every element of a list in order
will print the list out
iteration by index requires range, starting at 0 to end of list
for i in range
used to create a loop that iterates for a certain amount of itmes
for i in range(start, stop, step):
do the code
for i in range(2, 10, 2)
print(I)
you will get 2 4 6 8 printed
append(element)
adds element to end of list
insert(i, element)
inserts element at index I, only shifting, not replacing
pop()
removes last item of list
pop(I)
removes element at certain spot, NOT a specific value
upper()
makes characters uppercase
lower()
makes characters lowercase
strip()
removes leading or trailing spaces from a string
strip(chars)
removes leader or trailing chars from a string
split()
splits string based on spaces in a list of words
split(chars)
splits a string based on chars in a list of words
join(list)
inserts the string between each part of a list and connects it all into one string
remove()
removes the value in the parenthesis from the list