๐น Python Data Types
Python has several built-in data types that define the kind of value a variable can hold.
๐ Built-in Data Types in Python
Category | Data Type | Example |
---|---|---|
Numeric | int |
x = 10 |
float |
y = 3.14 |
|
complex |
z = 2 + 3j |
|
Boolean | bool |
status = True |
Sequence | str |
name = "Alice" |
list |
numbers = [1, 2, 3] |
|
tuple |
coords = (4, 5) |
|
range |
range(5) |
|
Set Types | set |
{1, 2, 3} |
frozenset |
frozenset([1, 2, 3]) |
|
Mapping | dict |
{"name": "Alice", "age": 25} |
Binary | bytes |
b"hello" |
bytearray |
bytearray(5) |
|
memoryview |
memoryview(bytes(5)) |
|
None Type | NoneType |
x = None |
๐น Type Conversion (Type Casting)
Python allows implicit and explicit type conversion.
1๏ธโฃ Implicit Type Conversion (Automatic)
Python automatically converts one data type to another without losing information.
โ Example: Implicit Conversion
a = 10 # int
b = 2.5 # float
result = a + b # int + float โ float
print(result) # Output: 12.5
print(type(result)) # Output: <class 'float'>
โ
Python automatically converted int
to float
.
2๏ธโฃ Explicit Type Conversion (Manual)
You can manually convert data types using type casting functions.
๐ Common Type Conversion Functions
Function | Converts To | Example |
---|---|---|
int(x) |
Integer | int(3.5) โ 3 |
float(x) |
Float | float(10) โ 10.0 |
str(x) |
String | str(100) โ "100" |
bool(x) |
Boolean | bool(1) โ True |
list(x) |
List | list("abc") โ ['a', 'b', 'c'] |
tuple(x) |
Tuple | tuple([1, 2, 3]) โ (1, 2, 3) |
set(x) |
Set | set([1, 2, 2, 3]) โ {1, 2, 3} |
dict(x) |
Dictionary | dict([(1, 'a'), (2, 'b')]) โ {1: 'a', 2: 'b'} |
frozenset(x) |
Frozen Set | frozenset([1, 2, 3]) |
๐ Examples of Explicit Conversion
Converting float
to int
(Data Loss)
x = 9.8
y = int(x) # Converts float to int
print(y) # Output: 9
โ Note: The decimal part is lost when converting float
to int
.
Converting int
to float
x = 5
y = float(x) # Converts int to float
print(y) # Output: 5.0
Converting str
to int
x = "100"
y = int(x) # Converts string to int
print(y + 10) # Output: 110
โ Invalid Example:
x = "abc"
y = int(x) # โ Error! "abc" cannot be converted to an integer.
Converting list
to tuple
my_list = [1, 2, 3]
my_tuple = tuple(my_list)
print(my_tuple) # Output: (1, 2, 3)
Converting list
to set
(Removes Duplicates)
my_list = [1, 2, 2, 3]
my_set = set(my_list)
print(my_set) # Output: {1, 2, 3}
๐น Boolean Conversion
Python treats some values as True and some as False when converting to bool
.
Truthy Values (Converted to True
)
โ Any non-zero number, non-empty string, non-empty list/set/dict.
print(bool(5)) # Output: True
print(bool(-10)) # Output: True
print(bool("hello")) # Output: True
print(bool([1, 2])) # Output: True
Falsy Values (Converted to False
)
โ 0
, None
, ""
(empty string), []
(empty list), {}
(empty dict).
print(bool(0)) # Output: False
print(bool(None)) # Output: False
print(bool("")) # Output: False
print(bool([])) # Output: False
โ Summary
Type Conversion | Example | Result |
---|---|---|
int(3.5) |
3.5 โ int |
3 |
float(10) |
10 โ float |
10.0 |
str(100) |
100 โ str |
"100" |
list("abc") |
"abc" โ list |
['a', 'b', 'c'] |
tuple([1,2,3]) |
[1,2,3] โ tuple |
(1,2,3) |
set([1,2,2,3]) |
[1,2,2,3] โ set |
{1,2,3} |
bool(0) |
0 โ bool |
False |
bool("Hello") |
"Hello" โ bool |
True |
๐ฏ Final Thoughts
- Python automatically converts types (implicit conversion) where possible.
- Explicit type conversion (type casting) is required when mixing incompatible types.
- Some conversions may result in data loss, e.g.,
float โ int
.
Would you like examples of real-world scenarios using type conversion? ๐
Leave a Reply