Understanding Python Variables and Data Types — A Beginner-Friendly Guide
What is a Variable in Python?
A variable is like a labeled box that holds information. You give the box a name and put a value inside it. For example:
name = "John"
age = 25
Here, name holds text (a string) and age holds a number (an integer). In Python you do not have to tell the language the type — Python figures it out. This feature is called dynamically typed.
Simple Rules for Naming Variables
- Start with a letter or underscore (_).
- Do not start with a number.
- Use only letters, numbers, and underscores.
- Names are case-sensitive:
Nameandnameare different.
user_name or total_price so your code is easy to read.Common Python Data Types (with examples)
Python has several built-in data types. Below are the most common ones you will use as a beginner.
1. Numbers (int, float, complex)
int is for whole numbers, float is for decimals, and complex is for complex numbers.
x = 5
y = 3.14
z = 2 + 3j
2. Strings (str)
Strings hold text and are placed inside quotes. You can use single or double quotes:
message = 'Hello, Python'
name = "Alice"
3. Boolean (bool)
Boolean values are True or False. They are useful for decisions in code.
is_active = True
4. List
A list stores multiple items in order. Lists are changeable (mutable).
fruits = ["apple", "banana", "cherry"]
5. Tuple
A tuple is like a list but cannot be changed (immutable).
coords = (10, 20)
6. Dictionary (dict)
Dictionaries store data as key-value pairs. This is useful when you want to look up values by name.
student = {"name": "John", "age": 21}
7. Set
A set is an unordered collection of unique items. Duplicates are removed automatically.
numbers = {1, 2, 3, 3} # becomes {1,2,3}
Check a Variable’s Type
Use the type() function to see what type a variable holds:
x = 5
print(type(x)) # > <class 'int'>
Why Data Types Matter
Data types help you avoid errors. For example, you cannot add text and numbers directly:
age = 25
print("I am " + age) # This will cause an error
Convert the number to a string first:
print("I am " + str(age))
Quick Reference Table
| Data Type | Example | Description |
|---|---|---|
| int | 10 | Whole number |
| float | 3.14 | Decimal number |
| str | “Hello” | Text |
| bool | True | True or False |
| list | [1,2,3] | Ordered, changeable collection |
| tuple | (1,2,3) | Ordered, fixed collection |
| dict | {“a”:1} | Key-value pairs |
| set | {1,2,3} | Unique items |
Conclusion
Variables and data types are the foundation of Python programming. Practice by creating different variables and trying small examples. Over time, understanding these basics will make building larger programs much easier.
