Fundamentals 5 min read

Understanding Variable Definition and Usage in Python

This article introduces Python variable fundamentals, explaining how to define, assign, and manipulate variables of various types—including integers, floats, strings, booleans, lists, and dictionaries—through ten clear examples that illustrate common programming scenarios.

Test Development Learning Exchange
Test Development Learning Exchange
Test Development Learning Exchange
Understanding Variable Definition and Usage in Python

In Python, understanding how to define and use variables is a fundamental programming skill, as variables act as containers for data such as numbers, strings, lists, and dictionaries.

Variable Definition

Defining a variable in Python is straightforward: you simply assign a value to a name without declaring its type, because Python is dynamically typed and determines the type at runtime.

Syntax:

variable_name = value

Here, variable_name is the identifier, = is the assignment operator, and value is the data stored.

Example 1: Define and print an integer variable

# Define an integer variable
age = 20
# Print the variable
print("年龄是:", age)

Example 2: Define and operate two float variables

# Define two float variables
height = 1.75
weight = 68.5
# Calculate BMI
bmi = weight / (height ** 2)
# Print BMI
print("体重指数(BMI)是:", bmi)

Example 3: String variable definition and concatenation

# Define two string variables
first_name = "张"
last_name = "三"
# Concatenate strings
full_name = first_name + last_name
# Print full name
print("全名是:", full_name)

Example 4: Boolean variable definition and conditional

# Define a boolean variable
is_student = True
# Conditional output
if is_student:
    print("是一个学生")
else:
    print("不是一个学生")

Example 5: List variable definition and element access

# Define a list variable
scores = [90, 85, 88, 92]
# Access first element
first_score = scores[0]
# Print first score
print("第一个成绩是:", first_score)

Example 6: Dictionary variable definition and key access

# Define a dictionary variable
person_info = {"姓名": "李四", "年龄": 25, "职业": "工程师"}
# Access age
age = person_info["年龄"]
# Print age
print("年龄是:", age)

Example 7: Variable reassignment

# Define an initial variable
counter = 0
# Reassign variable
counter = counter + 1
# Print updated count
print("更新后的计数值是:", counter)

Example 8: Swap values of two variables

# Define two variables
a, b = 1, 2
# Swap values
a, b = b, a
# Print swapped values
print("交换后a的值:", a)
print("交换后b的值:", b)

Example 9: Using input to receive user input

# Prompt user for name
name = input("请输入你的名字:")
# Print greeting
print("你好,", name)

Example 10: Multiple variables assignment

# Multiple assignment
x, y, z = 1, 2, 3
# Print values
print("x的值:", x)
print("y的值:", y)
print("z的值:", z)

These ten examples cover basic variable definitions and common operations, helping beginners grasp Python's core concepts.

pythonTutorialVariablesProgramming Basics
Test Development Learning Exchange
Written by

Test Development Learning Exchange

Test Development Learning Exchange

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.