Data Type Conversions
int()
Converts a value to an integer.
Syntax:
int(value[, base])
Example:
print(int("123")) # Output: 123
print(int("1010", 2)) # Output: 10
float()
Converts a value to a floating-point number.
Syntax:
float(value)
Example:
print(float("3.14")) # Output: 3.14
str()
Converts a value to a string.
Syntax:
str(value)
Example:
print(str(100)) # Output: "100"
list()
Converts a value to a list.
Syntax:
list(iterable)
Example:
print(list("hello")) # Output: ['h', 'e', 'l', 'l', 'o']
tuple()
Converts a value to a tuple.
Syntax:
tuple(iterable)
Example:
print(tuple([1, 2, 3])) # Output: (1, 2, 3)
set()
Converts a value to a set.
Syntax:
set(iterable)
Example:
print(set("hello")) # Output: {'h', 'e', 'l', 'o'}
dict()
Creates a dictionary from a sequence of key-value pairs.
Syntax:
dict(iterable)
Example:
print(dict([("name", "Alice"), ("age", 30)])) # Output: {'name': 'Alice', 'age': 30}