Docsity

Comandi Python Computer science, Schemi e mappe concettuali di Informatica gestionale

Toggle sidebar
bg1
🧾
BASICS
● 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
➗
OPERATORS
● + → 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.0
● ** → raises a number to a power
2 ** 3 # 8
● // → integer division (removes decimals, rounds down)
7 // 2 # 3
● % → returns the remainder of a division
pf3
pf4
pf5
pf8

Anteprima parziale del testo

Scarica Comandi Python Computer science e più Schemi e mappe concettuali in PDF di Informatica gestionale solo su Docsity!

🧾 BASICS

● 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

➗ OPERATORS

● + → 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

🔢 TYPES & CONVERSION

● 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

🔧 FUNCTIONS

● 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

🔗 LOGIC

● 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

🔀 CONDITIONS

● 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")

🔁 LOOPS

● 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 & ITERATION

● 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")

📚 LISTS

● [] → creates a list

l = [1,2,3]

● in → checks if value exists

2 in [1,2,3]

List methods

● .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])

🧠 LIST COMPREHENSION

● quick way to build lists

[x*2 for x in [1,2,3]]

🧪 TYPE CHECKING

● isinstance() → checks data type

isinstance(3, int) # True

💬 COMMENTS

● # → note for humans (ignored by Python)

x = 5 # number

● """ """ → longer explanation (docstring)

def f(): """function description"""