




Studia grazie alle numerose risorse presenti su Docsity
Guadagna punti aiutando altri studenti oppure acquistali con un piano Premium
Questa pagina non è visibile nell’anteprima
Non perderti parti importanti!





● print() → used to display something on the screen. It’s how you see results from your code.
print("Hello")
● = → used to store a value inside a variable (like putting something into a box).
x = 5
● + → adds two numbers
2 + 3 # 5
● - → subtracts one number from another
5 - 2 # 3
● * → multiplies numbers
3 * 4 # 12
● / → divides numbers (result is a decimal)
10 / 2 # 5.
● ** → raises a number to a power
2 ** 3 # 8
● // → integer division (removes decimals, rounds down)
7 // 2 # 3
● % → returns the remainder of a division
● int() → converts a value to an integer
int("5") # 5
● float() → converts to a decimal number
float(3) # 3.
● str() → converts to text (string)
str(10) # "10"
● type() → tells you what kind of data something is
type(3.14) # float
● def → used to create a function , a reusable block of code
def greet(): print("Hello")
● return → sends a result back from a function so you can use it later
def double(x): return x * 2
● () → used to call (run) a function
double(4) # 8
● and → true only if both conditions are true
True and False # False
● or → true if at least one condition is true
True or False # True
● not → reverses a boolean value
not True # False
● if → runs code only if a condition is true
if x > 0: print("positive")
● else → runs when the condition is false
if x > 0: print("positive") else: print("negative")
● elif → checks another condition
if x > 0: print("positive") elif x == 0: print("zero")
● for → repeats code for each item in a sequence
for i in [1,2,3]: print(i)
● while → repeats code while a condition is true
x = 3 while x > 0: x -= 1
● break → stops the loop immediately
for i in range(5): if i == 3: break
● continue → skips the current iteration
for i in range(5): if i == 2: continue print(i)
● range() → generates a sequence of numbers
for i in range(3): print(i)
● enumerate() → gives index + value
for i, v in enumerate(["a","b"]): print(i, v)
● zip() → combines multiple sequences
for a, b in zip([1,2], [3,4]):
"hello".replace("h","y")
● [] → creates a list
l = [1,2,3]
● in → checks if value exists
2 in [1,2,3]
● .append() → adds item at the end
l.append(4)
● .pop() → removes item by index
l.pop(0)
● sorted() → returns a sorted list
sorted([3,1,2])
● quick way to build lists
[x*2 for x in [1,2,3]]
● isinstance() → checks data type
isinstance(3, int) # True
● # → note for humans (ignored by Python)
x = 5 # number
● """ """ → longer explanation (docstring)
def f(): """function description"""