Everything you need to know about storing and working with data in Python

When stepping into the world of Python programming, two concepts you’ll encounter right from the beginning are variables and data types. Mastering these basics not only lays a strong foundation for coding but also simplifies the process of writing efficient, bug-free code. In this article, we’ll explore what variables are, how data types work in Python, and why they matter.
In simple terms, variables are containers for storing data values. Think of a variable as a labelled jar you can put anything in it, give it a name, and later use or change what’s inside by referencing the jar’s label.
In Python, you don’t need to declare the variable type beforehand. You assign a value to a name, and Python figures out the rest:
python
age = 25 name = "Alice" price = 19.99 Here, age holds an integer value, name stores a string, and price is a floating-point number.
Variable names can only contain letters, numbers, and underscores (_).
They cannot begin with a number.
Python is case-sensitive: Name and name are different.
Avoid using Python keywords (like for, class, if) as variable names.
Good examples:
user_name
totalAmount
item_1
Bad examples:
1stUser (starts with a number)
for (reserved keyword)
A data type defines what kind of value a variable holds and what operations you can perform with it. Python has several built-in data types, each serving a different purpose.
Here are some of the most common ones:
int)Whole numbers, positive or negative, without decimals.
python
count = 10 temperature = -5 float)Numbers with decimal points.
python
weight = 65.5 average = 3.14 str)Any sequence of characters, enclosed in single or double quotes.
python
greeting = "Hello, World!" bool)True or False values are commonly used for conditional statements.
python
is_active = True has_permission = False list)An ordered, changeable collection of items. Lists can hold multiple types.
python
fruits = ["apple", "banana", "cherry"] marks = [90, 85, 78] tuple)An ordered, unchangeable (immutable) collection.
python
coordinates = (10, 20) colors = ('red', 'blue', 'green') dict)A collection of key-value pairs, useful for mapping relationships.
python
student = {"name": "Bob", "age": 21, "grade": "A"} Python uses dynamic typing, meaning you can change the type of value assigned to a variable at any time:
python
variable = 10 # int variable = "text" # now a str This flexibility makes Python very beginner-friendly, but it can also lead to unexpected bugs if you’re not careful with types.
You can always check what data type a variable holds using the type() function:
python
value = 10 print(type(value)) # Output: <class 'int'> Understanding data types helps you:
Prevent bugs and errors
Write more optimised and readable code
Make use of appropriate methods for each type (e.g., string methods, list methods)
Variables and data types are the backbone of any Python program. Understanding how to use them correctly will make your journey as a Python developer much smoother. Remember, practice is key experiment with different types, create your own variables, and see how Python handles them!
0
8
0