Master Python Variables and 4 Core Data Types in 5 Minutes
This short tutorial teaches how to define variables and use Python’s four fundamental data types—strings, integers, floats, and booleans—through concise code examples, common pitfalls, and practice exercises, enabling beginners to store and manipulate data within five minutes.
1. Variables (quick copy‑paste)
# Direct copy, run to see effect
name = "张三" # store name
age = 20 # store age
height = 175.5 # store height
is_student = True # store boolean
print(name)
print(age, height)
print(is_student)Definition rule: variable_name = data (no spaces, cannot start with Chinese characters, cannot use keywords). Variable names should be meaningful, e.g., use age for age.
2. Four core data types
String ( str): text enclosed in single or double quotes.
Integer ( int): whole numbers without a decimal point.
Float ( float): numbers with a decimal point.
Boolean ( bool): two values True or False, used for logical tests.
3. Common beginner pitfalls and fixes
Pitfall 1: Variable name syntax errors (e.g., starting with Chinese characters or containing spaces). Fix: Use only ASCII letters, digits, and underscores, and avoid leading digits.
Pitfall 2: Trying to concatenate strings and numbers directly (e.g., print("Age:" + 20)). Fix: Convert numbers to strings: print("Age:" + str(age)).
Pitfall 3: Installing packages with a mirror that fails to parse the webpage. Fix: Use the Tsinghua mirror first:
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple 包名, or fall back to Aliyun mirror if needed.
4. After‑class exercises (5 minutes)
# 1. Define three variables (name, age, weight) and print them
name = "李四"
age = 18
weight = 62.5
print("姓名:", name, "年龄:", age, "体重:", weight)
# 2. Compute weight*2 + 10, keep one decimal, and print
result = weight * 2 + 10
print(round(result, 1))
# 3. Check whether age is greater than 18 and print the boolean result
print(age > 18)Key takeaways
Variables store data; the four types map to different scenarios: text, integer, decimal, and logical judgment.
Essential today: variable definition plus common string and integer operations.
When errors occur, first check variable‑naming rules, data‑type concatenation, and package‑mirror selection.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
liandk
Seasoned Java and mobile developer with years of experience, specializing in mini‑programs, public accounts, and full‑stack front‑end development. In the AI era, I continuously learn to broaden my knowledge and evolve. I revived a public account I started a decade ago during a dessert‑startup venture, using code as a vessel and knowledge as a companion. I share personal projects, technical articles, programming tips, and growth insights—let’s improve together and set sail.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
