In Python, casting refers to the process of changing the data type of a variable or value from one type to another.
This can be useful when you need to perform operations on different types of data or when you need to ensure that the data you are working with is in the correct format.
Python provides several built-in functions for casting:
You can also use casting to convert between different types of collections, such as lists, tuples, and dictionaries.
When casting, it’s important to be aware of potential issues that may arise, such as the loss of precision when converting from a floating-point number to an integer.
It’s also important to be mindful of the type of data you are working with and ensure that the casting operation is appropriate for your specific use case.
Here are some examples of Python casting:
string_num = "10" int_num = int(string_num) print(int_num) # Output: 10
float_num = 3.14 int_num = int(float_num) print(int_num) # Output: 3
int_num = 10 string_num = str(int_num) print(string_num) # Output: "10"
string_bool = "True" bool_val = bool(string_bool) print(bool_val) # Output: True
my_list = [1, 2, 3] my_tuple = tuple(my_list) print(my_tuple) # Output: (1, 2, 3)
my_dict = {"name": "John", "age": 30} my_list = list(my_dict.items()) print(my_list) # Output: [("name", "John"), ("age", 30)]
string_num = "3.14" float_num = float(string_num) print(float_num) # Output: 3.14
bool_val = True int_num = int(bool_val) print(int_num) # Output: 1
my_tuple = (1, 2, 3) my_list = list(my_tuple) print(my_list) # Output: [1, 2, 3]
my_list = ["1", "2", "3"] int_list = [int(num) for num in my_list] print(int_list) # Output: [1, 2, 3]
my_list = [1, 2, 3] string_list = [str(num) for num in my_list] string_nums = ", ".join(string_list) print(string_nums) # Output: "1, 2, 3"
my_dict = {"apple": 1, "banana": 2, "orange": 3} my_set = set(my_dict.values()) print(my_set) # Output: {1, 2, 3}
my_set = {1, 2, 3} my_list = list(my_set) print(my_list) # Output: [1, 2, 3]
my_string = "Hello, World!" my_list = list(my_string) print(my_list) # Output: ['H', 'e', 'l', 'l', 'o', ',', ' ', 'W', 'o', 'r', 'l', 'd', '!']
my_list = [1, 2, 3, 1, 2, 3] my_set = set(my_list) print(my_set) # Output: {1, 2, 3}
my_tuple = ("name", "John", "age", "30") my_dict = dict(zip(my_tuple[::2], my_tuple[1::2])) print(my_dict) # Output: {"name": "John", "age": "30"}
my_string = "True" my_bool = my_string.lower() == "true" print(my_bool) # Output: True
my_list = [("name", "John"), ("age", 30)] my_dict = dict(my_list) print(my_dict) # Output: {"name": "John", "age": 30}
a. Creating new variables
b. Changing the data type of a variable or value from one type to another
c. Performing mathematical operations
d. Printing values to the console
a. toint()
b. int()
c. convert_int()
d. integer()
a. Creating new variables
b. Changing the format of a string
c. Performing operations on different types of data
d. Both b and c
a. tofloat()
b. float()
c. convert_float()
d. floating()
a. Loss of precision
b. Increased precision
c. No issues
d. Loss of decimal points
a. bool(str_value)
b. tobool(str_value)
c. boolean(str_value)
d. bool()
a. list_to_tuple()
b. tuple()
c. convert_to_tuple()
d. to_tuple()
a. int(bool_value)
b. toint(bool_value)
c. integer(bool_value)
d. int()
a. Loss of precision
b. No considerations needed
c. Increase in precision
d. Automatic rounding
a. convert_list_to_int()
b. int_list()
c. [int(num) for num in my_list]
d. toint(my_list)
Answers:
1-b
2-b
3-d
4-b
5-a
6-d
7-b
8-a
9-a
10-c
a. list_dict()
b. convert_dict_to_list()
c. list(my_dict.items())
d. dict_to_list()
a. float()
b. tofloat()
c. convert_float()
d. string_to_float()
a. str_list()
b. [str(num) for num in my_list]
c. convert_list_to_str()
d. str()
my_set = set([1, 2, 3, 1, 2, 3])
print(my_set)
a. [1, 2, 3]
b. (1, 2, 3)
c. {1, 2, 3}
d. [1, 2, 3, 1, 2, 3]
a. list_set()
b. set_to_list()
c. list(my_set)
d. convert_set_to_list()
a. Joins two strings together
b. Combines elements of a list into a string
c. Concatenates two dictionaries
d. Converts a string to a boolean
a. dict(my_tuple)
b. tuple_to_dict()
c. convert_tuple_to_dict()
d. {key: value for key, value in my_tuple}
my_string = “Hello, World!”
my_list = list(my_string)
print(my_list)
a. Hello, World!
b. [‘H’, ‘e’, ‘l’, ‘l’, ‘o’, ‘,’, ‘ ‘, ‘W’, ‘o’, ‘r’, ‘l’, ‘d’, ‘!’]
c. [‘Hello’, ‘World’]
d. H e l l o , W o r l d !
a. dict_from_tuples()
b. convert_to_dict()
c. dict(my_list)
d. {key: value for key, value in my_list}
a. Converts a list to a tuple
b. Combines multiple iterables into tuples
c. Converts a dictionary to a set
d. Checks if two variables have the same type
Quiz 3
a. str(bool_val)
b. tostr(bool_val)
c. string(bool_val)
d. str()
my_tuple = (“name”, “John”, “age”, “30”)
my_dict = dict(zip(my_tuple[::2], my_tuple[1::2]))
print(my_dict)
a. {“name”: “John”, “age”: “30”}
b. {“name”: “John”, “age”: 30}
c. {(“name”, “John”), (“age”, “30”)}
d. [(“name”, “John”), (“age”, “30”)]
a. No potential issues
b. Loss of precision
c. Case sensitivity
d. Unexpected values may result in errors
a. set_list()
b. convert_list_to_set()
c. set(my_list)
d. toset(my_list)
a. list_set()
b. set_to_list()
c. list(my_set)
d. convert_set_to_list()
my_dict = {“apple”: 1, “banana”: 2, “orange”: 3}
my_set = set(my_dict.values())
print(my_set)
a. {“apple”: 1, “banana”: 2, “orange”: 3}
b. {1, 2, 3}
c. [1, 2, 3]
d. (“apple”, “banana”, “orange”)
a. convert_list_to_int()
b. int_list()
c. [int(num) for num in my_list]
d. toint(my_list)
python
Copy code
my_list = [“1”, “2”, “3”]
int_list = [int(num) for num in my_list]
print(int_list)
a. [“1”, “2”, “3”]
b. [1, 2, 3]
c. (“1”, “2”, “3”)
d. (“1”, 2, “3”)
a. str_list()
b. [str(num) for num in my_list]
c. convert_list_to_str()
d. str()
a. list_to_tuple()
b. tuple()
c. convert_to_tuple()
d. to_tuple()
Answers: