Python is a dynamically typed language, meaning you do not need to declare the type of a variable when defining it; the interpreter automatically determines the type based on the assigned value.
1. Numeric Types
| Type | Description | Example |
|---|---|---|
| int | Integer (positive, negative, zero) | 10, -5, 0 |
| float | Floating-point number (decimal) | 3.14, -0.01 |
| complex | Complex number (less commonly used) | 1 + 2j |
2. String (str)
name = "Alice"
message = 'Hello!'
text = """
Strings support many operations, such as concatenation (+), repetition (*), indexing, slicing, etc.
3. Boolean (bool)
There are only two values
-
True
-
False
Commonly used for conditional statements
is_active = True
has_permission = False
Note: True and False are keywords and should be capitalized.
4. List
A collection that is ordered, mutable, and can contain duplicates, represented bysquare brackets [] with elements separated by commas.
fruits = ["apple", "banana", "cherry"]
numbers = [1, 2, 3]
mixed = [1, "hello", True] // Lists can contain different types
Lists support operations such as adding, deleting, modifying, and querying.
5. Tuple
A collection that is ordered, immutable, and can contain duplicates, represented byparentheses ().
point = (10, 20)
colors = ("red", "green", "blue")
single = (42,) // Note: If there is only one element, a comma must follow
Once created, the contents of a tuple cannot be changed (immutable).
6. Dictionary (dict)
A collection that is unordered and consists of key-value pairs, represented bycurly braces {}.
person = {
"name": "Alice",
"age": 25,
"is_student": False
}
-
Key: Usually a string or number, must be an immutable type (cannot be a list).
-
Value: Can be of any type.
7. Set
A collection that is unordered and contains no duplicates, created usingcurly braces {} or set().
unique_numbers = {1, 2, 3, 4} // Actually {1, 2, 3}, the duplicate 3 will be removed
empty_set = set()
Variables
Variables
1. What is a variable: A variable is a name you give to data, used to reference that data in your program.
x = 10
name = "Tom"
is_valid = False
2. Variable naming rules
-
Variable names can consist of letters, numbers, and underscores.
-
Cannot start with a number.
-
Case-sensitive (age and Age are two different variables).
-
Cannot use keywords (Python, if, for, def, class, etc.).
-
It is recommended to use meaningful names, such as username, total_price, and to use lowercase letters with underscores (snake_case).
Dynamic Typing
Dynamic typing
Python is a dynamically typed language, meaning:
-
No need to declare variable types in advance.
-
The type of a variable is determined by the value assigned to it.
-
The same variable can change type during program execution.
x = 10
x = "hello"
x = [1, 2, 3] // List
Although you can change types freely, for clarity and maintainability of code, it is recommended not to change variable types frequently and to have a clear purpose.
How to check variable type
– Use the built-in function: type(variable_name)
x = 42
print(type(x)) // int
y = "python"
print(type(y)) // str
z = [1, 2, 3]
print(type(z)) // list
Summary of Common Data Types
| Data Type | Keyword | Description | Example |
|---|---|---|---|
| Integer | int | Integer value | 10, -5, 0 |
| Float | float | Decimal | 3.14, -0.5 |
| Boolean | bool | True/False | True, False |
| String | str | Text | “Hello”, “Python” |
| List | list | Ordered, mutable collection | [1, 2, 3], [“a”, “b”] |
| Tuple | tuple | Ordered, immutable collection | (1, 2), (“x”, “y”) |
| Dictionary | dict | Key-value pair collection | {“name”: “Tom”, “age”: 20} |
| Set | set | Unordered, unique collection | {1, 2, 3}, set([1, 2, 2]) |
Conclusion
Summary
| Topic | Description |
|---|---|
| Data Types | Python supports various built-in data types, such as numbers, strings, lists, dictionaries, etc., each with different characteristics and uses. |
| Variables | Used to store names for data, no need to declare types, can point to different types of data at any time (dynamic typing). |
| Dynamic Typing | The type of a variable is determined by the assigned value and can change at any time (but frequent type changes are not recommended). |
| Checking Type | Use type(variable) to check the current data type of a variable. |
| Variable Naming | Must follow rules, be meaningful, and it is recommended to use lowercase letters with underscores. |