In this tutorial, you'll learn about Python variables, how to use them effectively, and practice with clear examples and beginner-friendly exercises.
What is a Variable in Python?
When developing a program, you need to work with many values. To store and manipulate these values, you use variables.
In Python, a variable is a label that you assign a value to. A variable always holds some value.
Example:
message = 'Hello, World!'
print(message)
message = 'Good Bye!'
print(message)
Output:
Hello, World!
Good Bye!
In this example:
-
We create a variable called
message
. -
First, it holds the value
'Hello, World!'
, which is printed. -
Then we assign a new value
'Good Bye!'
to it and print again.
This demonstrates that a variable can store different values during a program's execution.
Creating Variables
To define a variable in Python, use the following syntax:
variable_name = value
The =
sign is the assignment operator. It assigns a value to a variable.
Examples:
age = 25
name = 'Anna'
height = 1.68
learning_python = True
We can use those variables like this:
print('Name:', name)
print('Age:', age)
print('Height:', height)
print('Learning Python?', learning_python)
Output:
Name: Anna
Age: 25
Height: 1.68
Learning Python? True
Naming Variables
When naming variables, it’s important to follow both rules and best practices.
Rules:
-
Variable names can contain only letters, digits, and underscores (_).
-
They must begin with a letter or an underscore, not a digit.
-
They cannot contain spaces. Use underscores to separate words.
-
They must not be the same as Python keywords or built-in functions.
Best Practices:
-
Use clear and descriptive names. Example:
student_average = 8.5 # better than "sa" or "x"
-
Use lowercase and separate words with underscores (
snake_case
). -
Avoid using the lowercase letter "l" and uppercase "O" because they resemble the digits "1" and "0".
More Examples
Example 1: Future age calculation
current_age = 20
years_ahead = 5
future_age = current_age + years_ahead
print('In', years_ahead, 'years, you will be', future_age)
Example 2: Concatenating strings
first_name = 'Carlos'
last_name = 'Silva'
full_name = first_name + ' ' + last_name
print('Full name:', full_name)
Example 3: Swapping values
x = 10
y = 20
print('Before swap:', x, y)
x, y = y, x
print('After swap:', x, y)
Practice: Assignments and Exercises
Exercise 1:
Create a variable called city
and assign the name of your city. Then print:
city = 'Lisbon'
print('I live in', city)
Exercise 2:
Create three variables: product
, quantity
, and price
. Calculate the total and print the result:
product = 'Notebook'
quantity = 4
price = 2.5
total = quantity * price
print('Total cost of', product + ':', total, 'USD')
Exercise 3:
Use variables to store two grades and calculate the average:
grade1 = 7.5
grade2 = 8.0
average = (grade1 + grade2) / 2
print('Final average:', average)
Summary
-
A variable is a label that stores a value, which may change during the program.
-
Use the syntax
variable_name = value
to create a variable. -
Variable names should follow Python’s rules and be as descriptive as possible.
-
Practice with real examples helps reinforce your understanding and ability to apply variables in programs.
Copyright Statement: Unless stated otherwise, all articles are original to this site, please credit the source when sharing.
Article link:http://pybeginners.com/python-tutorial/python-variables/
License Agreement:Attribution-NonCommercial 4.0 International License