Python Data Types
Level: 1 | Version: v1.0 | Author: Meptrasoft
What is a Data Type?
A data type tells Python what kind of value a name refers to and what operations can be performed on that value.
age = 21 # int (integer)
name = "Arun" # str (string)
price = 99.5 # float (decimal number)
In Python, every value has a data type. Python detects the data type automatically when you assign a value to a name.
Data Types at a Glance

Why Do We Need Data Types?
Data types tell Python how to handle data. For example, the + operator behaves differently depending on data types:
# Adding two numbers
a = 10
b = 20
print(a + b) # Output: 30
# Joining two text strings (concatenation)
a = "10"
b = "20"
print(a + b) # Output: 1020
Overview of Python Data Types
| Category | Data Type | Example | Description |
|---|---|---|---|
| Numeric | int |
10, -5 |
Whole numbers |
| Numeric | float |
10.5, 99.9 |
Numbers with decimal points |
| Numeric | complex |
2 + 3j |
Complex numbers with real and imaginary parts |
| Text | str |
"Python" |
Text enclosed in single or double quotes |
| Boolean | bool |
True, False |
Logical truth values |
| Sequence | list |
[1, 2, 3] |
Ordered, changeable list of values |
| Sequence | tuple |
(1, 2, 3) |
Ordered, unchangeable tuple of values |
| Sequence | range |
range(5) |
Generated sequence of numbers |
| Set | set |
{1, 2, 3} |
Unordered collection of unique values |
| Mapping | dict |
{"name": "Arun"} |
Key-value pairs |
| Special value | NoneType |
None |
Represents the absence of a value |
Detailed Data Types
1. Numeric Types
age = 21 # int
price = 45000.50 # float
c_num = 3 + 4j # complex
2. Text Type (str)
Strings store text data enclosed in single ('...') or double ("...") quotes:
course = "Python Programming"
3. Boolean Type (bool)
Booleans store either True or False (always capitalized):
is_enrolled = True
is_passed = False
4. Collection Types Summary
| Type | Example | Ordered? | Changeable (Mutable)? | Duplicates Allowed? |
|---|---|---|---|---|
list |
["Python", "Java", "SQL"] |
Yes | Yes | Yes |
tuple |
("Python", "Java") |
Yes | No | Yes |
set |
{1, 2, 3} |
No | Yes | No |
dict |
{"name": "Arun", "age": 21} |
Yes | Yes | Keys: No |
Type Conversion (Type Casting)
Type conversion means changing a value from one data type to another. In Python, type conversion happens in two ways: Implicit Conversion and Explicit Conversion.
1. Implicit Conversion (Automatic)
Python can automatically convert compatible numeric types during calculations.
num_int = 10 # int
num_float = 2.5 # float
total = num_int + num_float
print(total) # Output: 12.5
print(type(total)) # Output: <class 'float'>
Python treated 10 (int) as 10.0 (float) before adding, so the result became a float.
2. Explicit Conversion (Manual)
Explicit conversion is done by the programmer using built-in functions like int(), float(), and str().
Converting String to Integer / Float
age_text = "21"
age_num = int(age_text)
print(age_num + 1) # Output: 22
Converting Number to String
marks = 95
marks_text = str(marks)
print("Your marks are: " + marks_text)
Warning: Converting non-numeric text to an integer causes a
ValueError:
val = int("twenty") # Raises ValueError: invalid literal for int()
Checking Data Types with type()
Use the built-in type() function to check the data type of a value:
x = 10
y = "Hello"
print(type(x)) # <class 'int'>
print(type(y)) # <class 'str'>
Placement Quick Points
- Use
type()to check the data type of a value. int,float, andcomplexare numeric types.strstores text inside quotes.boolstoresTrueorFalse.- Lists (
list) are changeable; Tuples (tuple) are unchangeable. - Sets (
set) store unique values and remove duplicates automatically. - Dictionaries (
dict) store data askey: valuepairs. - Implicit type conversion is done automatically by Python when compatible values are used together.
- Explicit type conversion uses
int(),float(), andstr(). - Converting invalid text like
int("hello")raises aValueError.
Interview Questions
- What is a data type in Python?
- A classification that tells Python what kind of value a name refers to and what operations can be performed on that value.
- How do you check a value's data type?
- By using the built-in
type()function (e.g.,type(value)). - What is the difference between implicit and explicit type conversion?
- Implicit conversion is done automatically by Python when compatible types are used together. Explicit conversion is done manually by the programmer using functions like
int(),float(), orstr(). - What is the difference between a list and a tuple?
- A
listis mutable (can be changed after creation), while atupleis immutable (cannot be changed). - What happens if you try
int("abc")in Python? - It raises a
ValueErrorbecause"abc"is not a valid numeric string. - What output does
"5" + "5"give in Python? "55"because both operands are strings, so string concatenation takes place.
Practice
- Create variables for
int,float,str, andbool, and print their data types usingtype(). - Convert the string
"100"into an integer and add50to it. - Convert the integer
450to a string and combine it with"Total Marks: ". - Create a list of 3 colors and change the first color to
"Blue". - Create a set
{10, 20, 20, 30}and print it. Explain why20appears only once. - What error occurs when running
int("nineteen")? Fix it.
End of Python Data Types