Python is a versatile and beginner-friendly programming language. One of the first concepts you need to understand is Variables and Data Types, as they form the foundation of any program.
In this chapter, we will cover:
- What are Variables?
- Rules for Naming Variables
- Data Types in Python
- Type Conversion
Let’s dive in!
1. What are Variables?
A variable is like a container that stores data values. Unlike some other programming languages, Python does not require explicit declaration of variables—they are created the moment you assign a value to them.
Example:
name = "Alice"
age = 25
height = 5.9
Here:
name
stores a string ("Alice"
)age
stores an integer (25
)height
stores a float (5.9
)
Variables can be reassigned to different values anytime.
age = 25
age = 26 # Updated value
print(age) # Output: 26
2. Rules for Naming Variables
Python has some rules and conventions for variable names:
Rules:
✔ Must start with a letter (a-z, A-Z) or underscore (_
).
✔ Can contain letters, numbers (0-9), and underscores.
✔ Case-sensitive (Age
and age
are different).
✔ Cannot use Python keywords (if
, for
, while
, etc.).
Examples:
✅ Valid:
name = "Bob"
_age = 30
user1 = "Alice"
❌ Invalid:
1user = "John" # Starts with a number
for = 5 # Uses a keyword
first-name = "Sam" # Hyphen not allowed
Best Practices:
- Use snake_case (e.g.,
user_name
). - Choose meaningful names (e.g.,
student_age
instead ofsa
).
3. Data Types in Python
Python has several built-in data types:
Data Type | Example | Description |
---|---|---|
int | 5 , -10 | Whole numbers (positive/negative) |
float | 3.14 , -0.5 | Decimal numbers |
str | "Hello" , 'Python' | Text (enclosed in quotes) |
bool | True , False | Boolean (True/False) |
list | [1, 2, 3] | Ordered, mutable collection |
tuple | (1, 2, 3) | Ordered, immutable collection |
dict | {"name": "Alice"} | Key-value pairs |
set | {1, 2, 3} | Unordered, unique elements |
Examples:
# Integer
age = 25
# Float
price = 19.99
# String
greeting = "Hello, Python!"
# Boolean
is_student = True
# List
fruits = ["apple", "banana", "cherry"]
# Tuple
coordinates = (10.0, 20.0)
# Dictionary
person = {"name": "Alice", "age": 25}
# Set
unique_numbers = {1, 2, 3, 3} # {1, 2, 3} (duplicates removed)
4. Type Conversion
Sometimes, we need to convert one data type to another. Python provides built-in functions for this:
Function | Example | Converts to |
---|---|---|
int() | int("5") → 5 | Integer |
float() | float(3) → 3.0 | Float |
str() | str(100) → "100" | String |
bool() | bool(1) → True | Boolean |
Examples:
# String to Integer
num_str = "10"
num_int = int(num_str) # 10
# Integer to String
age = 25
age_str = str(age) # "25"
# Float to Integer
price = 9.99
price_int = int(price) # 9 (truncates decimal)
# Boolean Conversion
print(bool(1)) # True
print(bool(0)) # False
Summary
✔ Variables store data and can be reassigned.
✔ Follow naming rules for variables.
✔ Python has multiple data types (int
, float
, str
, bool
, etc.).
✔ Type conversion allows changing one data type to another.
In the next chapter, we’ll explore Operators and Expressions in Python!