For those new to Python programming, understanding variables and how to use them is one of the foundational topics. Variables are used to store data and perform operations on it. In this article, we’ll explain what Python variables are, how to define them, and how to use them with example code blocks.
What is a Variable?
Variables are named storage locations used to hold data. When we assign a value to a variable, the data is stored in the computer’s memory, and we can access it using the variable’s name. Variables store data temporarily and remain in memory as long as the program is running.
Defining Variables in Python
Defining a variable in Python is quite simple. You give the variable a name and assign a value to it. Python automatically determines the type of the variable, so there’s no need to specify the data type.
Example 1: Simple Variable Definition
# Defining a numeric variable
number = 10
print(number) # Output: 10
# Defining a text (string) variable
name = "Ali"
print(name) # Output: Ali
# Defining a decimal (float) variable
pi = 3.14
print(pi) # Output: 3.14
# Defining a Boolean (True/False) variable
is_correct = True
print(is_correct) # Output: True
Variable Naming Rules
When naming variables in Python, certain rules must be followed:
- Variable names can contain letters (a-z, A-Z), digits (0-9), and underscores (_).
- They must start with a letter or an underscore.
- Python is case-sensitive, so
variable
andVariable
are treated as different variables. - Reserved Python keywords (e.g.,
if
,else
,while
) cannot be used as variable names.
Example 2: Valid and Invalid Variable Names
# Valid variable names
number1 = 10
_name = "Ayşe"
last_name = "Yılmaz"
PI = 3.14
# Invalid variable names
1number = 10 # Cannot start with a digit
last-name = "Yılmaz" # Cannot contain hyphens (-)
while = True # Cannot use Python keywords
Variable Types and Type Conversion
In Python, variable types are determined automatically, but sometimes we may need to convert a value from one type to another. This process is known as “type conversion.”
Example 3: Type Conversion
# Converting a numeric variable to a string
number = 123
number_str = str(number)
print(number_str) # Output: "123"
print(type(number_str)) # Output: <class 'str'>
# Converting a string variable to a numeric variable
text_number = "456"
text_int = int(text_number)
print(text_int) # Output: 456
print(type(text_int)) # Output: <class 'int'>
# Converting a decimal to an integer
pi = 3.14
pi_int = int(pi)
print(pi_int) # Output: 3
print(type(pi_int)) # Output: <class 'int'>
Multiple Assignments
In Python, it is possible to assign values to multiple variables in a single line. This makes the code more readable and organized.
Example 4: Multiple Assignments
# Assigning values to multiple variables on the same line
a, b, c = 1, 2, 3
print(a) # Output: 1
print(b) # Output: 2
print(c) # Output: 3
# Assigning the same value to multiple variables
x = y = z = 0
print(x) # Output: 0
print(y) # Output: 0
print(z) # Output: 0
Basic Arithmetic Operations with Variables
Performing basic arithmetic operations with variables in Python is straightforward. Operations like addition, subtraction, multiplication, and division can be applied directly to variables.
Example 5: Arithmetic Operations
# Addition
x = 10
y = 5
result = x + y
print(result) # Output: 15
# Subtraction
result = x - y
print(result) # Output: 5
# Multiplication
result = x * y
print(result) # Output: 50
# Division
result = x / y
print(result) # Output: 2.0
# Modulus (remainder)
result = x % y
print(result) # Output: 0
# Exponentiation
result = x ** y
print(result) # Output: 100000
Conclusion
Variables in Python are versatile tools that allow you to store and manipulate data efficiently. Mastering the basics of variable definition, naming rules, type conversion, and arithmetic operations will serve as a strong foundation for more advanced Python programming concepts.