🐍 Python – Part 1: Understanding Variables and Data Types
If you're new to Python, one of the first things you'll learn is how to work with variables and data types. Python makes it super easy and beginner-friendly. This post will walk you through the basics with clear examples, rules, naming conventions, and bonus tips.
✅ What Is a Variable?
A variable in Python is like a box that stores a value. You give it a name and use it later in your code.
pythonCopyEditname = "Lorenzo"
age = 25
height = 5.9
is_happy = True
Here’s what’s happening:
nameis astr(string)ageis anint(integer)heightis afloatis_happyis abool(Boolean)
✅ Quick Rules for Variables
Strings go in quotes:
"text"No need to declare variable types like in Java or C++
Variable names can’t start with a number
Python is case-sensitive:
ageandAgeare two different variables
🧠 Python Variable Naming Conventions
| Rule | Example |
| Must start with a letter or an underscore | ✅ name, _value |
| Cannot start with a number | ❌ 2value, 3name |
Can only contain letters, numbers, _ | ✅ user_name1, age2 |
| Are case-sensitive | Name ≠ name |
| Avoid using Python keywords as variable names | ❌ class, if, while |
✅ Recommended Naming Style (PEP 8)
Use snake_case for variable names:
user_name,total_amountKeep names descriptive but short:
age,user_idConstants should be ALL_CAPS:
PI = 3.14,MAX_USERS = 100
🧠 What are constants?
Python doesn’t have real constants, but by convention, if you name a variable in ALL CAPS, it means "don’t change this later in the program."
pythonCopyEditPI = 3.14
MAX_USERS = 100
📘 Common Python Variable Types
| Type | Example | Description |
int | 5 | Whole numbers |
float | 5.8 | Numbers with decimals |
str | "hello" | Text (strings) |
bool | True, False | Boolean values |
list | [1, 2, 3] | Ordered, changeable collection |
tuple | (1, 2, 3) | Ordered, unchangeable collection |
set | {1, 2, 3} | Unordered, no duplicates |
dict | {"name": "Lorenzo"} | Key-value pairs |
NoneType | None | Represents "nothing" or "no value" |
complex | 2 + 3j | Complex numbers (used in advanced math) |
bytes | b'hello' | Immutable sequence of bytes |
bytearray | bytearray(b'hello') | Mutable sequence of bytes |
range | range(5) | Represents a sequence of numbers |
✅ Built-in Python Data Types (Official Categories)
| Category | Data Types Included |
| Text Type | str |
| Numeric Types | int, float, complex |
| Sequence Types | list, tuple, range |
| Set Types | set, frozenset |
| Mapping Type | dict |
| Boolean Type | bool |
| Binary Types | bytes, bytearray, memoryview |
| None Type | NoneType (only value is None) |
🧠 Fun Fact
You can check the type of any variable using the type() function:
pythonCopyEditx = 5.8
print(type(x)) # Output: <class 'float'>
This is really useful when you’re debugging or just curious about what kind of data you're working with.
🔁 Dynamic vs Static Typing
Python is a dynamically-typed language.
✅ You don’t need to declare the type of a variable.
🧠 Python figures it out when the program runs (at runtime).
🔍 Example:
pythonCopyEditx = 5 # int
x = "text" # Now it's a string
This is allowed in Python, unlike in Java or C++.
📊 Comparison Table
| Language | Type System | Example |
| Python | Dynamically typed | x = 10 |
| Java | Statically typed | int x = 10; |
| C++ | Statically typed | float x = 10.5; |