- What are Data Types ā Understanding how Python stores data
- Numeric Data Types ā Integers, floats, and complex numbers
- Sequence Data Types ā Strings, lists, and tuples
- Dictionary ā Key-value pairs for structured data
- Boolean ā True and False values
- Set ā Unordered collections of unique items
- Hands-on Practice ā Write and run Python code
- Interactive Quiz ā Test your knowledge
So, What Exactly Are Data Types?
In programming, a data type is a classification that specifies what kind of value a variable can hold and what operations can be performed on it. Think of it like a container ā some containers hold numbers, others hold text, and some hold complex structures.
Python is a dynamically typed language, which means you don't need to declare the data type of a variable. Python automatically determines the type based on the value you assign.
# Python automatically detects data types a = 25 # Python knows this is an integer b = 3.14 # Python knows this is a float c = "Python" # Python knows this is a string print(type(a)) # <class 'int'> print(type(b)) # <class 'float'> print(type(c)) # <class 'str'>
Python's built-in data types can be grouped into several categories:
š¢ Numeric
int, float, complex
š Sequence
str, list, tuple
š Dictionary
dict
ā” Boolean
bool
šÆ Set
set
š” Fun fact: Everything in Python is an object, and data types in Python are actually classes. Variables are instances of these classes! That's why you can use methods like .upper() on strings.
1. Numeric Data Types
Numeric data types store numbers. Python has three numeric types:
a. Integer (int)
Integers are whole numbers ā positive, negative, or zero ā without any decimal point. Python integers have unlimited precision, meaning they can be as large as your memory allows.
a = 6 b = 456577 c = 6700000000000000000000000000000000000000000000000 print(type(a)) # <class 'int'>
b. Float (float)
Floats are numbers with a decimal point. They can represent real numbers and are written with a decimal point or using scientific notation.
g = 9.8 pi = 3.14159 temperature = -1.55 print(type(g)) # <class 'float'>
c. Complex (complex)
Complex numbers have a real part and an imaginary part. In Python, the imaginary part is denoted by j.
c = 1 + 2j # complex number print(type(c)) # <class 'complex'>
# Numeric Data Type Examples a = 6 # int print(a) print(type(a)) g = 9.8 # float print(g) print(type(g)) c = 1 + 2j # complex print(c) print(type(c)) Output: 6 <class 'int'> 9.8 <class 'float'> (1+2j) <class 'complex'>
2. Sequence Data Types
Sequence data types store collections of items in an ordered manner. Python has three sequence types:
a. String (str)
Strings are sequences of characters. They can be defined using single quotes, double quotes, or triple quotes.
s = "Welcome to Python" print(s) # Welcome to Python print(type(s)) # <class 'str'>
b. List (list)
Lists are ordered, mutable collections that can hold items of different data types. They are defined using square brackets [].
my_list = [1, "two", 3.5] print(my_list) # [1, 'two', 3.5] print(type(my_list)) # <class 'list'>
c. Tuple (tuple)
Tuples are ordered, immutable collections. Once created, they cannot be modified. They are defined using parentheses ().
my_tuple = (1, 2, "three") print(my_tuple) # (1, 2, 'three') print(type(my_tuple)) # <class 'tuple'>
3. Dictionary
Dictionaries are unordered collections of key-value pairs. Each key is unique and is used to access its corresponding value.
my_dict = {"name": "Python", "age": 40}
print(my_dict) # {'name': 'Python', 'age': 40}
print(type(my_dict)) # <class 'dict'>
4. Boolean
The Boolean data type represents one of two values: True or False. It's used in conditional statements and comparisons.
is_python_fun = True print(is_python_fun) # True print(type(is_python_fun)) # <class 'bool'>
5. Set
Sets are unordered collections of unique elements. They are mutable and are defined using curly braces {}.
my_set = {"Python", "C", "CPP"}
print(my_set) # {'Python', 'C', 'CPP'}
print(type(my_set)) # <class 'set'>
Try It Yourself!
Now it's your turn! Write and run Python code directly in your browser:
NUMERIC DATA TYPES
========================================
Integer: 42 (type: int)
Float: 3.14159 (type: float)
Complex: (1+2j) (type: complex)
========================================
SEQUENCE DATA TYPES
========================================
String: Hello Python (type: str)
List: [1, 'two', 3.5] (type: list)
Tuple: (1, 2, 'three') (type: tuple)
========================================
DICTIONARY
========================================
Dictionary: {'name': 'Python', 'age': 40} (type: dict)
========================================
BOOLEAN
========================================
Boolean: True (type: bool)
========================================
SET
========================================
Set: {'Python', 'C', 'CPP'} (type: set)
ā Explore different data types!
šÆ Challenge Yourself!
⢠Create a variable with your favorite number and check its type
⢠Create a list of your favorite programming languages
⢠Create a dictionary with your name and age
š You've Mastered Python Data Types!
You now understand the fundamental data types in Python ā numeric, sequence, dictionary, boolean, and set. These are the building blocks of every Python program!
Quick Quiz - Test Your Knowledge
Let's see what you've learned about Python data types:
Frequently Asked Questions
š¤ What is the difference between a list and a tuple? ā¼
[] and tuples use parentheses ().
š» Why is Python called a dynamically typed language? ā¼
šÆ Can a variable change its data type in Python? ā¼
x = 10 creates an integer, but later x = "Hello" changes it to a string. This flexibility is one of Python's key features.
š How do I check the data type of a variable? ā¼
type() function. For example, type(10) returns <class 'int'>. This is very useful for debugging and understanding what type of data you're working with.
š Where to Go From Here
Now that you understand Python data types, here are some related topics to explore:
š£ Python Tokens
Learn about keywords, identifiers, and operators
š§ Operators
Learn about Python operators and expressions
š Constants
Learn about constants in Python