Python has several built-in data types that allow you to store and manipulate different kinds of data. The number of data types available in Python includes integers, floating-point numbers, complex numbers, strings, booleans, lists, tuples, sets, dictionaries, and None.
What is the use of 'union()' method in Python set?
The union()
method in Python set is used to return a new set containing all the unique elements from all the input sets or iterables. It combines elements from multiple sets or iterables without any repetition.
It can be used in two ways:
- set.union(*other_sets): It takes multiple sets as arguments and returns a new set containing unique elements from all the sets.
- set.union(iterable): It takes an iterable, such as a list or tuple, as an argument and returns a new set containing unique elements from the set and the iterable.
Here is an example to illustrate the use of union()
method:
1 2 3 4 5 6 7 8 9 10 11 12 |
set1 = {1, 2, 3} set2 = {3, 4, 5} set3 = {5, 6, 7} # Using union() with multiple sets new_set1 = set1.union(set2, set3) print(new_set1) # Output: {1, 2, 3, 4, 5, 6, 7} # Using union() with an iterable my_list = [7, 8, 9] new_set2 = set1.union(my_list) print(new_set2) # Output: {1, 2, 3, 7, 8, 9} |
In both cases, the union()
method returns a new set that contains all the unique elements from the original set(s) and the input set(s) or iterables.
How to import a module in Python?
To import a module in Python, you can use the import
statement.
There are three ways to import a module:
- Import the entire module:
1
|
import module_name
|
In this case, you can access any function or variable defined in the module using the module_name
followed by a dot (.) and the function or variable name.
- Import specific functions or variables from a module:
1
|
from module_name import function_name, variable_name
|
This method allows you to import only specific functions or variables from the module, and you can directly use them without using the module name.
- Import the module with an alias:
1
|
import module_name as alias_name
|
This allows you to import the module with a different name, which can be helpful if the module name is lengthy or if there is a naming conflict.
Note: The module name can be a standard library module or it can be a custom module that you have created. You need to ensure that the module is installed or available in the same directory as your Python script.
Here is an example to import the math
module and use the sqrt
function from it:
1 2 3 4 |
import math result = math.sqrt(16) print(result) # Output: 4.0 |
Alternatively, you can import just the sqrt
function from the math
module as follows:
1 2 3 4 |
from math import sqrt result = sqrt(16) print(result) # Output: 4.0 |
How to convert a string to an integer in Python?
To convert a string to an integer in Python, you can use the built-in int()
function. Here's an example:
1 2 3 4 |
string_num = "123" integer_num = int(string_num) print(integer_num) # Output: 123 print(type(integer_num)) # Output: <class 'int'> |
In the above code, the int()
function is used to convert the string "123" to an integer. The resulting integer value is then stored in the variable integer_num
. Finally, the converted integer value is printed along with its type.
What is a dictionary in Python?
A dictionary in Python is an unordered collection of key-value pairs. It is used to store and retrieve data efficiently. In a dictionary, keys are unique and immutable (meaning they cannot be changed), while values can be a variety of data types, such as strings, numbers, lists, or even other dictionaries. Dictionaries are also known as associative arrays or hash tables in other programming languages. They are defined using curly braces ({}) and colons (:), with a comma separating each key-value pair.
How to handle exceptions in Python?
In Python, exceptions are handled using try-except blocks. Here's how you can handle exceptions in Python:
- Write the code that might raise an exception inside a try block.
- Specify the type of exception you want to handle using except block(s).
- Write the code that will be executed if the specified exception occurs inside the except block.
- Optionally, you can use an else block to write code that will be executed if no exception occurs.
- Optionally, you can use a finally block to write code that will be executed regardless of whether an exception occurs or not.
Here's an example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
try: # Code that might raise an exception result = 10 / 0 # This will raise a ZeroDivisionError print("Result:", result) # This line will not be executed except ZeroDivisionError: # Code to handle ZeroDivisionError exception print("Cannot divide by zero!") except Exception as e: # General exception handler print("An exception occurred:", str(e)) else: # Code to be executed if no exception occurs print("No exception occurred.") finally: # Code to be executed regardless of whether an exception occurs or not print("Finally block executed.") |
Output:
1 2 |
Cannot divide by zero! Finally block executed. |
In the above example, a ZeroDivisionError occurs because we are trying to divide by zero. The except block for ZeroDivisionError is executed, and the appropriate error message is printed. Finally, the code inside the finally block is executed.