01: Variabile și tipuri
Understand variables and the four basic data types in Python.
Lesson 01 · Variables and data types¶
What you'll learn
- What a variable is and how to create one
- The 4 basic types:
int,float,str,bool - How to find the type of a value with
type() - The rules for naming variables
What is a variable?¶
A variable is a labeled box in which you store a value. The label is the name of the variable, and the contents of the box is its value.
Every time you write age, Python gives you the value 14.
Creating a variable¶
The = sign is the assignment operator — it doesn't mean "equal", but "store the value on the right into the variable on the left".
The 4 basic types¶
1. int — whole numbers¶
No decimal point, no quotes.
2. float — decimal numbers¶
Period, not comma
Python uses the period as the decimal separator: 9.5 ✓, not 9,5 ✗
3. str — strings of characters (text)¶
Single '...' and double "..." quotes work the same.
4. bool — logical values¶
Only two possible values: True or False (capital letter is mandatory!).
The type() function¶
print(type(14)) # <class 'int'>
print(type(3.14)) # <class 'float'>
print(type("hello")) # <class 'str'>
print(type(True)) # <class 'bool'>
Naming rules¶
| Rule | Correct | Wrong |
|---|---|---|
Letter or _ at start |
age, _x |
1age, @score |
| No spaces | student_count |
student count |
Letters, digits, _ |
game_score2 |
game-score |
| Case-sensitive | Age ≠ age |
— |
| No reserved words | — | if, for, while |
Writing conventions¶
# snake_case — recommended for variables
number_of_students = 32
max_temperature = 37.5
# UPPERCASE — for constants (values that don't change)
PI = 3.14159
SPEED_OF_LIGHT = 299792458
Displaying variables¶
Exercises¶
Exercise 1 — Define variables¶
Create variables for: your name (str), your age (int), your math grade average (float), and whether you are in 9th grade (bool).
Exercise 2 — Which type?¶
What type is each value?
Answer
x = 42→inty = "42"→str(the quotes make the difference!)z = 42.0→float(the decimal point matters)w = False→bool
Exercise 3 — Track the value¶
What does the following program display?
Exercise 4 — Find the errors¶
What's wrong?
Answer
student count→ space not allowed →student_count2score→ cannot start with a digit →score2if→ Python reserved word → choose another name, e.g.word
Mini-project: Personal record card¶
Create variables for a person and display them in a formatted way.
Expected output:
=== Personal record card ===
Name: Robert Popescu
Age: 13
Grade: 7
Overall average: 9.8
Is honor student: True
Solution
Summary¶
- A variable stores a value under a name you choose
- Basic types:
int,float,str,bool type(x)tells you the type of valuex- Use
snake_casefor naming variables
Next step: → Lesson 02: Operations and expressions