Python Variable
What is a Variable in Python?
A variable in Python is a named container that stores a value. You can think of it as a label for a memory location.
Creating Variables in Python
Python uses dynamic typing, which means you don’t need to declare the data type of a variable explicitly.
Syntax:
variable_name = value
Example:
x = 5
name = "Alice"
pi = 3.14
print(x) # 5
print(name) # Alice
print(pi) # 3.14
Output:
5 Alice 3.14
Variable Naming Rules
Rule | Example |
---|---|
Must start with a letter or underscore _ | _x, name |
Can’t start with a number | 1x (invalid) |
Can contain letters, digits, and underscores | user_1, data2 |
Case-sensitive (Age ≠ age) | Name, name are different |
Reserved keywords are not allowed | if = 5 (invalid) |
Reassigning Variables
You can change the value and type of a variable at any time.
x = 10
x = "Now I’m a string"
print(x)
Output:
Now I’m a string
Multiple Assignments
Python allows multiple assignments in one line.
a, b, c = 1, 2, 3
print(a, b, c) # 1 2 3
x = y = z = 100
print(x, y, z) # 100 100 100
Output:
1 2 3 100 100 100
Best Practices for Using Variables
Tip | Description |
---|---|
Use meaningful names | score is better than s |
Stick to lowercase (snake_case) | user_name, not UserName |
Use constants in ALL_CAPS | PI = 3.1415 |
Avoid reserved words | Like class, def, return, etc. |
Keep variable scope clear | Declare inside functions if not needed globally |
Bad vs Good Examples
# Bad
a = 100
b = 0.3
# Good
discount_rate = 100
tax_rate = 0.3
Quick Recap
Feature | Example |
---|---|
Variable assignment | x = 5 |
Data types | int, float, str, etc. |
Dynamic typing | x = 10 → x = "ten" |
Multiple assignment | a, b = 1, 2 |
Best practices | Use meaningful names |
What Are Data Types in Python?
In Python, data types define the kind of value a variable can hold. Python is dynamically typed, meaning you don't have to declare the type explicitly — Python infers it at runtime.
Categories of Python Data Types
1. Numeric Types
a) int – Whole numbers
x = 10
print(type(x)) # <class 'int'>
Output:
<class 'int'>
b) float – Decimal numbers
pi = 3.14
print(type(pi)) # <class 'float'>
Output:
<class 'float'>
c) complex – Complex numbers
z = 2 + 3j
print(type(z)) # <class 'complex'>
Output:
<class 'complex'>
2. Text Type
a) str – Strings (sequence of Unicode characters)
name = "Python"
print(type(name)) # <class 'str'>
Output:
<class 'str'>
3. Sequence Types
a) list – Ordered, mutable, allows duplicates
fruits = ["apple", "banana", "cherry"]
print(type(fruits)) # <class 'list'>
Output:
<class 'list'>
b) tuple – Ordered, immutable, allows duplicates
coordinates = (10.5, 20.5)
print(type(coordinates)) # <class 'tuple'>
Output:
<class 'tuple'>
c) range – Immutable sequence of numbers
r = range(5)
print(list(r)) # [0, 1, 2, 3, 4]
Output:
[0, 1, 2, 3, 4]
4. Mapping Type
a) dict – Key-value pairs, unordered (as of Python 3.7+, insertion ordered)
person = {"name": "Alice", "age": 25}
print(type(person)) # <class 'dict'>
Output:
<class 'dict'>
5. Set Types
a) set – Unordered, no duplicates
numbers = {1, 2, 2, 3}
print(numbers) # {1, 2, 3}
Output:
{1, 2, 3}
b) frozenset – Immutable set
frozen = frozenset([1, 2, 3])
print(type(frozen)) # <class 'frozenset'>
Output:
<class 'frozenset'>
6. Boolean Type
a) bool – True or False
is_active = True
print(type(is_active)) # <class 'bool'>
Output:
<class 'bool'>
7. Binary Types
a) bytes
b = b"hello"
print(type(b)) # <class 'bytes'>
Output:
<class 'bytes'>
b) bytearray
ba = bytearray(5)
print(ba) # bytearray(b'\x00\x00\x00\x00\x00')
Output:
bytearray(b'\x00\x00\x00\x00\x00')
c) memoryview
mv = memoryview(bytes(5))
print(type(mv)) # <class 'memoryview'>
Output:
<class 'memoryview'>
Best Practices for Working with Data Types
Tip | Description |
---|---|
Know your data | Choose the right type (e.g., list vs tuple) |
Avoid type errors | Use type conversion wisely |
Use type() for debugging | Helps confirm variable types |
Use immutable types for constants | E.g., tuple or frozenset |
Don't use mutable types as default args in functions | Avoid issues with list, dict, etc. |