Python Variables
Understanding Variables and Literals in Python
Variables and literals are the foundation of any Python program. Whether you're storing user input, configuring logic, or displaying output, you'll be using these concepts constantly. This article explains what they are, how they work, and how to use them effectively.
What is a Variable?
A variable is a named container used to store data in memory. Think of it like a label on a box: the label (variable name) tells you what's inside the box (value). You can later change the content or use it in operations.
Creating and Using Variables
To create a variable, you simply assign it a value using the =
operator.
username = "Alice"
age = 30
height = 1.75
username
stores a string ("Alice"
)age
stores an integer (30
)height
stores a float (1.75
)
Python automatically detects the type of data based on the value you assign. This is called dynamic typing.
Displaying Variable Values
print(username)
print(age)
print(height)
Output:
Alice
30
1.75
Reassigning Variables
Variables can be reassigned to new values at any time.
username = "Alice"
print(username)
username = "Bob"
print(username)
Output:
Alice
Bob
The variable username
was updated from "Alice"
to "Bob"
.
Multiple Assignment
You can assign multiple variables in a single line:
x, y, z = 1, 2.5, "Hello"
print(x)
print(y)
print(z)
Output:
1
2.5
Hello
You can also assign the same value to multiple variables:
a = b = c = "Python"
print(a)
print(b)
print(c)
Output:
Python
Python
Python
Rules for Naming Variables
When naming variables in Python:
- Names must begin with a letter or an underscore (
_
) - Names can contain letters, digits, and underscores
- They cannot start with a digit
- They are case-sensitive (
myVar
is different frommyvar
) - Avoid using Python keywords (like
if
,for
,class
)
Valid names: user_name
, _temp
, age2
Invalid names: 2name
, user-name
, class
What is a Literal?
A literal is a fixed value that appears directly in your code. It's the actual data you're assigning to a variable.
name = "David" # "David" is a string literal
price = 9.99 # 9.99 is a float literal
is_active = True # True is a boolean literal
Types of Literals in Python
Numeric Literals
These represent numbers and come in three forms:
- Integers – Whole numbers, with or without a minus sign.
a = 42
b = -7
- Floating-point numbers – Numbers with a decimal point.
pi = 3.1415
temp = -2.0
- Complex numbers – Numbers with a real and imaginary part (
j
represents the imaginary unit).
c = 2 + 3j
print(c.real) # 2.0
print(c.imag) # 3.0
String Literals
Text surrounded by quotes is a string literal. You can use either single or double quotes.
message1 = "Hello, world!"
message2 = 'Python is fun.'
Multiline strings can be created using triple quotes:
note = """This is
a multiline
string."""
print(note)
Output:
This is
a multiline
string.
Boolean Literals
Python has two boolean literals: True
and False
.
is_valid = True
is_expired = False
These are often used in conditions and control flow.
Special Literal: None
None
represents the absence of a value.
result = None
print(result)
Output:
None
Continue Learning
Python Operators
Popular
### Python Operators Operators in Python are special symbols or keywords used to perform operations
Personalized Recommendations
Log in to get more relevant recommendations based on your reading history.