Understanding different types of data is a fundamental first step in any programming journey. In Python, data is the information that our functions and programs operate on. While the real world presents a vast variety of information, a computer breaks it all down into a few basic types that we can use creatively to represent anything we need. Mastering these core data types is essential for building robust and effective applications.
Table of Contents
- 1.1.1 💻 Core Data Types in Python
- 1.1.2 💻 Variables, Operators, and Methods
- 1.1.3 💻 Tuples and Dictionaries
- 1.2 Different Types of Data
- 1.3 1. Numbers
- 1.4 2. Text
- 1.5 3. Boolean
- 1.6 4. Lists
- 1.7 5. Tuples
- 1.8 6. Dictionaries
- 1.9 7. Sets
- 1.10 8. NoneType
- 1.11 9. Bonus Round: Custom Data
- 1.12 TL;DR Table
- 1.13 More Topics
💻 Core Data Types in Python
Python provides several built-in data types to handle information. The most common are numbers, which include integers like `10` or `2580` (ints) and numbers with decimal points like `10.35` (floats). Another core type is strings, which are sequences of characters enclosed in either single (`’Hello’`) or double (`”World”`) quotes. Finally, we have lists, which are ordered collections of items enclosed in square brackets, such as `[‘Bananas’, ‘Oranges’, ‘Fish’]`. A key feature of lists is that they can hold items of different types, including other lists.
💻 Variables, Operators, and Methods
To manage data, we assign it to variables. A variable is simply a name that refers to a piece of data, created with an assignment statement like `word = “Hello”`. Once assigned, we can use the variable name to access the data. Python’s operators, such as `+` and `*`, behave differently depending on the data type. For numbers, they perform addition and multiplication. For strings and lists, `+` performs concatenation (joining them together), while `*` performs repetition. Data types also have special operations called methods, like `.append()` to add an item to a list or `.lower()` to convert a string to lowercase.
💻 Tuples and Dictionaries
Beyond the basics, Python offers two other powerful data types: tuples and dictionaries. A tuple is similar to a list but is immutable, meaning it cannot be changed once created. They are identified by round brackets, for example, `(‘bananas’, ‘tiffin’)`. A dictionary is a collection of key-value pairs, created with curly brackets: `{‘free’: ‘as in beer’}`. Much like a real dictionary, you use a unique ‘key’ (the word) to look up its associated ‘value’ (the definition). This structure is incredibly useful for storing related pieces of information.
Different Types of Data
1. Numbers
- int → whole numbers.
x = 42
- float → decimals.
pi = 3.14159
- complex → numbers with real + imaginary parts.
z = 2 + 3j
2. Text
- str → strings (characters, words, emojis).
name = "Yaman"
shout = "🔥 Python is cool!"
3. Boolean
is_logged_in = True
has_access = False
4. Lists
nums = [1, 2, 3, 4]
random = [42, "hello", True, 3.14]
5. Tuples
coords = (10.0, 20.0)
6. Dictionaries
user = {"name": "Maya", "age": 25, "is_admin": False}
7. Sets
fruits = {"apple", "banana", "apple"}
8. NoneType
x = None
9. Bonus Round: Custom Data
class Dog:
def __init__(self, name):
self.name = name
TL;DR Table
Type | Example | Use Case |
---|---|---|
int | 42 | Counting, IDs |
float | 3.14 | Math with decimals |
complex | 2+3j | Advanced math |
str | "hello" | Text |
bool | True | Logic |
list | [1,2,3] | Ordered data |
tuple | (10,20) | Fixed grouping |
dict | {"a":1, "b":2} | Key/value storage |
set | {1,2,3} | Unique values |
NoneType | None | Absence |
More Topics
- Python Coding Essentials: Reliability by Abstraction
- Python Coding Essentials: Embrace Storage and Persistence
- Python Coding Essentials: Neater Code with Modules
- Python Coding Essentials: Lock Down with Data Encryption
- Python Coding Essentials: Files and Modules Done Quickly
- Python’s Itertools Module – How to Loop More Efficiently
- Python Multithreading – How to Handle Concurrent Tasks