They are
1 Numeric Types:
- int: Integer values, e.g., 10, -3, 0.
- float: Floating-point values with decimal points, e.g., 3.14, -2.5, 0.0.
- complex: Complex numbers with real and imaginary parts, e.g., 2 + 3j, -1j.
- # Numeric Typesinteger_var = 10float_var = 3.14complex_var = 2 + 3jprint(integer_var) # output 10print(float_var) #output 3.14print(complex_var) #output (2+3j)
2 Text Type:
- str: String values enclosed in single quotes ('') or double quotes (""). For example, "Hello, World!".
- # Text Typestring_var = "Hello, World!"print(string_var) #output Hello, World!
3 Sequence Types:
- list: Ordered and mutable sequences of elements, enclosed in square brackets ([]). For example, [1, 2, 3].
- tuple: Ordered and immutable sequences of elements, enclosed in parentheses (()). For example, (1, 2, 3).
- # Sequence Typeslist_var = [1, 2, 3]tuple_var = (4, 5, 6)print(list_var) #output [1, 2, 3]print(tuple_var) #output (4, 5, 6)
4 Mapping Type:
- dict: Unordered key-value pairs, enclosed in curly braces ({}). For example, {"name": "John", "age": 25}.
- # Mapping Typedictionary_var = {"name": "John", "age": 25}print(dictionary_var) #output {'name': 'John', 'age': 25}
5 Set Types:
- set: Unordered collections of unique elements, enclosed in curly braces ({}). For example, {1, 2, 3}.
- frozenset: Immutable sets, enclosed in curly braces ({}). For example, frozenset({1, 2, 3}).
- # Set Typesset_var = {1, 2, 3}frozenset_var = frozenset({4, 5, 6})print(frozenset_var) #output frozenset({4, 5, 6})
6 Boolean Type:
- bool: Represents either True or False.
- # Boolean Typebool_var = Trueprint(bool_var) #output True
7 None Type:
- None: Represents the absence of a value or null.
- # None Typenone_var = Noneprint(none_var) #output None
Here's an example code snippet that demonstrates the different types of variables in Python:
# Numeric Types
integer_var = 10
float_var = 3.14
complex_var = 2 + 3j
# Text Type
string_var = "Hello, World!"
# Sequence Types
list_var = [1, 2, 3]
tuple_var = (4, 5, 6)
# Mapping Type
dictionary_var = {"name": "John", "age": 25}
# Set Types
set_var = {1, 2, 3}
frozenset_var = frozenset({4, 5, 6})
# Boolean Type
bool_var = True
# None Type
none_var = None
# Printing the variables
print(integer_var)
print(float_var)
print(complex_var)
print(string_var)
print(list_var)
print(tuple_var)
print(dictionary_var)
print(set_var)
print(frozenset_var)
print(bool_var)
print(none_var)
OUTPUT :
When you run this code, it will output the values of the variables:
10
3.14
(2+3j)
Hello, World!
[1, 2, 3]
(4, 5, 6)
{'name': 'John', 'age': 25}
{1, 2, 3}
frozenset({4, 5, 6})
True
None
<< Previous Next >>