-Data types in Python –
Python provides a variety of built-in data types that allow you to handle different kinds of data effectively. Here are the most commonly used data types in Python:
1. Numeric Types
- int: Integer numbers (e.g., 5, -3, 100)
- float: Floating-point numbers (e.g., 3.14, -0.001, 2.0)
- complex: Complex numbers (e.g., 1+2j, 3-4j)
2. Sequence Types
- str: String, a sequence of characters (e.g., “hello”, “123”)
- list: Ordered, mutable collection of items (e.g., [1, 2, 3], [“apple”, “banana”, “cherry”])
- tuple: Ordered, immutable collection of items (e.g., (1, 2, 3), (“apple”, “banana”, “cherry”))
3. Mapping Type
- dict: Dictionary, a collection of key-value pairs (e.g., {“name”: “John”, “age”: 30})
4.Set Types
- set: Unordered collection of unique items (e.g., {1, 2, 3}, {“apple”, “banana”})
- frozenset: Immutable version of a set (e.g., frozenset([1, 2, 3]))
5. Boolean Type
- bool: Boolean value (True or False)
6. None Type
- NoneType: Represents the absence of a value or a null value (None)
Examples
Here are examples demonstrating the use of each data type:
- β# Numeric Types
x_int = 5 # int
x_float = 3.14 # float
x_complex = 1 + 2j # complex
- # Sequence Types
x_str = “hello” # str
x_list = [1, 2, 3, “apple”] # list
x_tuple = (1, 2, 3, “banana”) # tuple
- # Mapping Type
x_dict = {“name”: “John”, “age”: 30} # dict
- # Set Types
x_set = {1, 2, 3, “apple”} # set
x_frozenset = frozenset([1, 2, 3, “banana”]) # frozenset
- # Boolean Type
x_bool = True # bool
- # None Type
x_none = None # NoneType
Checking Data Types
You can use the type() function to check the type of a variable:
print(type(x_int))
# Output: <class ‘int’>
print(type(x_float))
# Output: <class ‘float’>
print(type(x_complex))
# Output: <class ‘complex’>
print(type(x_str))
# Output: <class ‘str’>
print(type(x_list))
# Output: <class ‘list’>
print(type(x_tuple))
# Output: <class ‘tuple’>
print(type(x_dict))
# Output: <class ‘dict’>
print(type(x_set))
# Output: <class ‘set’>
print(type(x_frozenset))
# Output: <class ‘frozenset’>
print(type(x_bool))
# Output: <class ‘bool’>
print(type(x_none))
# Output: <class ‘NoneType’>
Understanding these data types and their properties is fundamental to effectively using Python for various programming tasks.
Social Media Links of Tech DCode
ππ» YouTube
https://www.youtube.com/channel/UCjJnEdeugftBwQ3yMuD4B_A
ππ»Instagram
https://www.instagram.com/thetechdcode/
ππ»Facebook Page
https://www.facebook.com/thetechdcode
ππ»Twitter
ππ»Telegram Channel
https://t.me/thetechdcode
ππ»Tech DCode Linktree
https://linktr.ee/thetechdcode
ππ»My Personal Handles
https://linktr.ee/virtualshivamin