Bạn đang xem thử công khai
Đăng nhập để lưu tiến độ, làm bài tập, thảo luận và nhận phản hồi.
Variables, Data Types, and Operators
Variables, Data Types, and Operators
Review lesson. A summary table of the variables, data types, and operators part of Module 1, for quick lookup and self-checking after studying units 1.2 → 1.6. If anything is still unclear, go back to the corresponding unit and rewatch the video.
Data types
| Type | Example | Ordered | Mutable | Notes |
|---|---|---|---|---|
int, float | 42, 3.14 | — | — | / always gives a float, // is floor division |
str | 'Python' | Yes | No | Indexing starts at 0, slice s[a:b] stops before b |
bool | True, False | — | — | True + True equals 2 |
list | [1, 2, 3] | Yes | Yes | b = a does not copy; use a.copy() |
tuple | (1, 2) | Yes | No | A single element needs a comma: (50,) |
set | {1, 2, 3} | No | Yes | Duplicate elements are not allowed |
dict | {"a": 1} | Yes | Yes | Keys are unique; d.get(k, default) does not raise an error when the key is missing |
Operators and precedence
From highest to lowest: ** → * / % // → + - → comparison → is, in → not and or.
Four easily confused points
input()always returns a string; to do arithmetic you must convert the type:int(input(...)).==compares values,ischecks for the same object (commonly used forNone).- Falsy values:
False None 0 0.0 '' [] () {} set(); the string"False"is still truthy. int(3.99)is 3 —int()truncates the decimal part, it does not round.
Key points
- Variables/functions use
snake_case, constants useUPPER_SNAKE_CASE, classes usePascalCase. - Mutable types (list, set, dict) and immutable types (str, tuple) determine how they behave when assigned and passed into functions.