Intro to programming
>>> prompt marker on interactive interpreter
x = 2
print(x)
#output is
#2
x=2
print("x")
#output is
#x
x=5
y=3
#output is
#3
#5
x=5
y=3
print (y, x)
#output is
#3 5
x=5
y=3
print(y, end ="")
print(x)
#output is
#35
x=5
y=3
print(y, end =" ")
print(x)
#output is
#3 5
"\n" new line
"\t" makes tab
print("the\nend")
#output is
#the
#end
print("\tThe end")
#output is
# The end
input()
#will wait for user input and assign input to a variable
#default to a string
int(input())
#will allow the user's input to be saved as an integer instead of a string
def
#defines functionsGood habit- write 3-5 lines, then run to check for errors. Incremental development
Division (/)→ divides, returns float regardless of if input is integers
Floor Division (//)→ rounds down answer to nearest whole number returns integer
Modulo (%)→ remainder, returns integer
By default, input is stored as a string
Module: a file containing python code that can be used by other modules or scripts. Use import statement and dot notation. (ex. import abc to bring in abc.py)
Script: a file of Python code run directly by the interpreter
randint(min,max) between min and max
randrange(min,max) between min and max-1
#string literals
adj = "yummy"
f"Food is {adj}"
#prints: "food is yummy"
f"{2*4=}"
#prints: "2*4 = 8"
f"{{Jeff Bezos}}: Amazon"
#prints:"{Jeff Bezos}: Amazon"Strings are immutable
Lists ARE mutable
//adding items to the end of a list
name_of_list.append(value)
//adding element in specified spot
//remove elemnt at index i
name_of_list.pop(i)
//remove value from list
name_of_list.remove(value)
//index of a value
name_of_list.index(value)
//how many times a value shows up
name_of_list.count(value)
//divide a string into a list
split("string")List | String |
Mutable | Immutable |
Indexed | Indexed |
concatenation | concatenation |
get length | get length |
Sets
set()
name = {1, 2, 3}
//collection of unique elements from a and b
setA.union(setB)
//items in common
setA.intersection(setB)
//items only in set A
setA.difference(setB)
//items in A or B but not both
setA.symmetric_differene(setB)Dictionaries (Paired items)
dict = {key:value}
//ex
players = {"Lionel Messi": 10,"Christiano Ronaldo":7}
//values accessed by key
dict[key] //outputs value assigned to the key
//add new entries (or modify if key already exists)
dict[k] = v
//delete
del dict[k]Range
range(y)
//sequence of every non-negative integer less than y
range(a,b)
//sequence of every integer from a (inclusive) to b (exclusive)
range(x, y, z)
//sequence of integers from x (inc) to y (exc) with a step size of z