Learning Guides
Data Types in Python: Complete Guide
Quick answer: Learn Python's core data types including int, float, string, boolean, list, tuple, dictionary and set, with type checking and conversion examples.
Overview of Python Data Types
| Type | Example | Mutable? |
|---|---|---|
| int | 10 | N/A (immutable value) |
| float | 10.5 | N/A (immutable value) |
| str | "Fireblaze" | No |
| bool | True, False | N/A |
| list | [1, 2, 3] | Yes |
| tuple | (1, 2, 3) | No |
| dict | {"a": 1} | Yes |
| set | {1, 2, 3} | Yes |
Numeric Types
age = 25 # int
price = 89000.50 # float
complex_num = 2 + 3j # complex, rarely needed outside specialised maths
Strings
name = "Fireblaze AI School"
print(type(name)) #
Boolean
is_active = True
is_deleted = False
print(type(is_active)) #
Booleans are technically a subtype of int in Python, where True behaves as 1 and False behaves as 0 in arithmetic.
Collection Types
scores = [85, 90, 78] # list: ordered, mutable
point = (4, 5) # tuple: ordered, immutable
student = {"name": "Priya", "age": 22} # dict: key-value pairs
unique_ids = {101, 102, 103} # set: unordered, no duplicates
Checking a Variable's Type
x = 10
print(type(x)) #
print(isinstance(x, int)) # True
Type Conversion
age_str = "25"
age_int = int(age_str) # 25 (as an integer)
price = 89000
price_str = str(price) # "89000" (as a string)
value = "3.14"
value_float = float(value) # 3.14
Converting a non-numeric string, such as int("abc"), raises a ValueError, so real-world code should validate the string first or handle the exception.
Mutable vs Immutable Types
Immutable types, such as int, float, str and tuple, cannot be changed in place. Any operation that appears to modify them actually creates a new object. Mutable types, such as list, dict and set, can be changed in place without creating a new object.
Common Interview Questions
What is the difference between a mutable and an immutable type?
A mutable type's contents can be changed in place after creation, such as a list. An immutable type cannot be changed, so any apparent modification actually creates a new object, such as with strings.
How would you check a variable's data type at runtime?
Use the built-in type() function, or isinstance() when checking against a specific expected type.
FAQ
Frequently Asked Questions
What are Python's core data types?
int, float, str and bool for single values, and list, tuple, dict and set for collections of values.
What is the difference between a mutable and an immutable data type?
A mutable type, like a list, can be changed in place after creation. An immutable type, like a string or tuple, cannot, so any modification creates a new object.
How do you convert a string to an integer in Python?
Use the int() function, such as int('25'), which raises a ValueError if the string does not represent a valid whole number.
Is a boolean a separate type from an integer in Python?
Technically bool is a subtype of int, so True behaves as 1 and False behaves as 0 in arithmetic operations.
Keep Reading
Related Articles
Learning Guides
INNER JOIN in SQL with Examples: Complete Beginner's Guide
Learn INNER JOIN in SQL with practical examples, syntax, interview questions, and real-world use cases. A complete beginner-friendly SQL JOI
Learning Guides
OUTER JOIN in SQL: Complete Guide with Examples for Beginners
Master OUTER JOIN in SQL with practical examples and real-world scenarios. Learn LEFT JOIN, RIGHT JOIN, FULL OUTER JOIN, syntax, use cases,
Learning Guides
Multi-Row Functions in SQL: Complete Guide with Examples
Learn multi-row (aggregate) functions in SQL including SUM, AVG, COUNT, MIN, MAX, GROUP BY and HAVING, with practical examples, NULL handlin