Hello everyone, today let’s talk about variables and data types in Python. As a Python tutorial author, I’ve found that many beginners feel confused about these basic concepts. Don’t worry, follow me to learn, and I will help you understand these “seemingly complex” concepts in the simplest way.
Variables: The “Containers” in Python
In my opinion, variables are like labeled boxes where you can put various things inside. For example:
name = "Xiao Ming"
age = 18
height = 1.75
is_student = True
Here, name, age, height, and is_student are the labels of the boxes (variable names), and the content on the right side of the equal sign is what is placed inside the boxes (variable values).
Tip: Python variable names can include letters, numbers, and underscores, but cannot start with a number. For example,<span>1_name</span> is illegal, but<span>name_1</span> is fine.
Basic Data Types in Python
1. Numeric Types: Integers (int) and Floats (float)
# Integer
my_age = 25
population = 1400000000
# Float
pi = 3.14159
weight = 62.5
# Numeric Operations
print(my_age + 10) # Output: 35
print(pi * 2) # Output: 6.28318
2. Strings (str): The Best Friend of Text
# Strings can be enclosed in single or double quotes
name = 'Python'
message = "I love coding"
# String Concatenation
full_message = name + " - " + message
print(full_message) # Output: Python - I love coding
# Formatted Strings (f-string)
age = 25
intro = f"I am {age} years old"
print(intro) # Output: I am 25 years old
3. Boolean Values (bool): The World of True and False
is_sunny = True
is_raining = False
# Comparison operations return boolean values
age = 18
is_adult = age >= 18
print(is_adult) # Output: True
Special Operations on Variables
1. Multiple Assignments
# Assigning values to multiple variables at once
x, y, z = 1, 2, 3
print(x, y, z) # Output: 1 2 3
# Swapping values
a = 5
b = 10
a, b = b, a # Elegant way to swap
print(a, b) # Output: 10 5
2. Type Conversion
# Converting string to number
age_str = "25"
age_int = int(age_str)
print(age_int + 1) # Output: 26
# Converting number to string
number = 100
number_str = str(number)
print("Number " + number_str) # Output: Number 100
Tip: Use<span>type()</span> function to check the type of a variable
x = 100
print(type(x)) # Output: <class 'int'>
Common Misconceptions and Precautions
- In Python, variables do not need to declare a type; it infers automatically:
x = 100 # Integer
x = "hello" # Changing to string is also fine
- Floating-point calculations may have precision issues:
0.1 + 0.2 # Output: 0.30000000000000004
- Strings and numbers cannot be added directly:
age = 25
# print("I am " + age) # Error!
print("I am " + str(age)) # Correct: convert to string first
Practice Time
Try the following small exercises:
- Create two numeric variables and perform addition, subtraction, multiplication, and division
- Use f-string to format a string and output a self-introduction
- Try various type conversions, such as converting “3.14” to a float
Summary
Today we learned about variables and basic data types in Python, including:
- Definition and naming rules of variables
- Numeric types (integers and floats)
- Basic operations on strings
- Usage of boolean values
- Multiple assignments and type conversions of variables
Remember, the most important thing in programming is hands-on practice. I suggest you open the Python interpreter and try the examples above. Don’t worry if you encounter problems; debugging and fixing errors are also part of learning.
Next time we will explain more interesting features of Python, so be sure to keep an eye on my updates! If you have any questions, feel free to leave a comment for discussion.
Let’s improve together and explore the world of Python!