02: Operații și expresii
Work with numbers and text — arithmetic operations, f-strings, and type conversions.
python
operators
arithmetic
f-strings
conversions
Lesson 02 · Operations and expressions¶
What you'll learn
- Arithmetic operators and the order of operations
- String operations
- F-strings — the modern way to format text
- Conversion between types:
int(),float(),str()
Arithmetic operators¶
| Operator | Operation | Example | Result |
|---|---|---|---|
+ |
addition | 3 + 4 |
7 |
- |
subtraction | 10 - 3 |
7 |
* |
multiplication | 3 * 4 |
12 |
/ |
division (float result) | 7 / 2 |
3.5 |
// |
integer division | 7 // 2 |
3 |
% |
remainder (modulo) | 7 % 2 |
1 |
** |
exponentiation | 2 ** 8 |
256 |
print(10 + 3) # 13
print(10 - 3) # 7
print(10 * 3) # 30
print(10 / 3) # 3.3333...
print(10 // 3) # 3
print(10 % 3) # 1
print(2 ** 10) # 1024
Order of operations¶
Python follows standard math rules: ** → * / // % → + -
The % operator — what's it for?¶
% returns the remainder of division. Useful for checking parity:
String operations¶
Concatenation with +¶
Repetition with *¶
Useful functions¶
message = " Welcome! "
print(len(message)) # 12
print(message.upper()) # " WELCOME! "
print(message.lower()) # " welcome! "
print(message.strip()) # "Welcome!"
print(message.strip().upper()) # "WELCOME!"
F-strings¶
F-strings let you insert variables directly into text:
name = "Michael"
age = 14
print(f"My name is {name} and I am {age} years old.")
# My name is Michael and I am 14 years old.
Number formatting¶
Calculations inside f-strings¶
Type conversions¶
| Function | What it does |
|---|---|
int(x) |
converts to whole number |
float(x) |
converts to decimal number |
str(x) |
converts to text |
print(int(3.9)) # 3 (truncates decimals, doesn't round!)
print(int("42")) # 42
print(float(5)) # 5.0
print(float("3.14")) # 3.14
print(str(100)) # "100"
Exercises¶
Exercise 1 — Calculate¶
Without running the code, calculate:
Answer
17 % 5→22 ** 3 + 1→915 // 4→3
Exercise 2 — F-string¶
Display:4 notebooks cost 14.0 lei.
Exercise 3 — Even or odd?¶
Display the remainder of dividing 47 by 2.
Mini-project: Temperature converter¶
Convert 100 °C to Fahrenheit and Kelvin.
Formulas: F = C × 9/5 + 32 and K = C + 273.15
Output:
Solution
Summary¶
- Arithmetic operators:
+,-,*,/,//,%,** %returns the remainder — useful for parity- F-strings
f"text {variable}"— modern formatting - Conversions:
int(),float(),str()
Next step: → Lesson 03: Input and Output