Python Data Types Explained
Beginner to advanced guide to Python data types with real examples. Learn integers, floats, strings, lists, dictionaries & more with easy and advanced snippets.
Let's dive into the core concept of data types in Python, explore each type with examples, and understand how they help in building real-world applications.
What Are Data Types?
A data type tells Python what kind of value a variable holds—such as a number, text, or a list of items. Every value in Python has a data type, and the interpreter uses it to know how the value should behave.
Why Are Data Types Important?
- Help Python understand how to store and process data.
- Allow developers to organize and structure data properly.
- Prevent errors by enforcing correct data usage.
Built-in Data Types in Python
Python has several built-in data types, categorized into:
Text Type – str
Numeric Types – int, float, complex
Sequence Types – list, tuple, range
Mapping Type – dict
Set Types – set
Boolean Type – bool
Binary Types – bytes, bytearray, memoryview
None Type – NoneType
Let’s look at each with examples
1.Text Type: str:
Use it to store text (characters, words, sentences).
Strings are Immutable
Once a string is created, its characters cannot be modified directly.
If you want to change it, you must create a new string.
Example:
a = “Python is great”
print ( a ) #Output: Python is great
Example:
String=”Python”
for i in string:
print( i )
#Output:
P
y
t
h
o
n
Key Features:
- Defined using single (') or double quotes (")
- Supports string operations: slicing, concatenation, repetition
String Indexing in Python
Each character in a string has a position (index). Python uses zero-based indexing, which means the first character is at position 0.
Example:
text = "Python"
print(text[0]) # Output: P
print(text[1]) #Output: y
Positive Indexing:
- Starts from 0 for the first character.
- Goes left to right.
|
Character |
P |
y |
t |
h |
o |
n |
|
Index |
0 |
1 |
2 |
3 |
4 |
5 |
Negative Indexing:
- Starts from -1 for the last character.
- Goes right to left.
|
Character |
P |
y |
t |
h |
o |
n |
|
Index |
-6 |
-5 |
-4 |
-3 |
-2 |
-1 |
Example:
text=”Python”
print(text[-1]) #Output: n
print(text[-3]) #Output: h
String Slicing in Python
Slicing allows you to extract a portion (substring) of a string using the colon : operator.
Syntax:
string[start:stop]
Includes the start index.
Excludes the stop index.
Example:
text = "Python"
print(text[0:4]) # Output: Pyth (index 0 to 3 as it excludes stop index 4)
Omitting Indexes
- string[:stop] → from beginning to stop - 1
- string[start:] → from start to end
- string[:] → entire string
Example:
print(text[:3]) # Output: 'Pyt'
print(text[-3:-1 ]) # Output: 'ho'
print(text[:]) # Output: 'Python'
Slicing with Steps
You can add a step to skip characters using string[ start : stop: step].
Example:
text = "Python"
print(text[0:6:2]) # Output: 'Pto'
· Start from index 0 to 5 (since 6 is excluded).
· Step = 2 means take every second character.
2. Numeric Types: int, float
· These types handle numbers.
int – Whole numbers (positive or negative):
Example:
A=2
Print(A) # Output: 2
float – Decimal numbers
Example:
price = 19.99
print(price) #Output: 19.99
3. Boolean Type: bool
Used to represent True or False values.
Example:
is_active = True
print(type(is_active)) # Output:
Often used in:
- Conditional statements
- Loop control
- Comparisons
Example:
age = 20
if age<18:
print(“age greater than 18”)
else:
print(“age is less than 18”)
#Output: age greater than 18
4. None Type: NoneType
Represents the absence of a value.
Example:
x = None
print(x) #Output: None
print(type(x)) #Output:
Often used as a default return value or placeholder in functions.

