7 Days Analytics Course 3feiz7 3
7 Days Analytics Course 3feiz7 3
python
text1 = "Hello"
text2 = "123"
print(text1.isalpha())
print(text2.isdigit())
How do you create a dictionary from two lists in Python?
You can create a dictionary from two lists in Python using the 'zip()' function to combine the lists
and the 'dict()' function to convert the result into a dictionary.
Example:
python
Example:
python
list1 = [1, 2, 3]
list2 = ["a", "b", "c"]
zipped = list(zip(list1, list2))
print(zipped)
python
my_list = [1, 2, 2, 3, 4, 4, 5]
new_list = list(set(my_list))
print(new_list)
Example (using list comprehension):
18
python
my_list = [1, 2, 2, 3, 4, 4, 5]
new_list = []
[new_list.append(x) for x in my_list if x not in new_list]
print(new_list)
Explain the use of the 'sorted()' function in Python.
The 'sorted()' function in Python is used to sort elements in an iterable in ascending order and
returns a new list. It can also accept a 'reverse' argument to sort in descending order.
Example:
python
my_list = [3, 1, 4, 1, 5, 9, 2, 6]
sorted_list = sorted(my_list)
print(sorted_list)
How do you find the maximum and minimum values in a list in Python?
You can find the maximum and minimum values in a list in Python using the 'max()' and 'min()'
functions, respectively.
Example:
python
my_list = [3, 1, 4, 1, 5, 9, 2, 6]
print(max(my_list))
print(min(my_list))
What is the purpose of the 'enumerate()' function in Python?
The 'enumerate()' function in Python is used to add a counter to an iterable and returns it as an
enumerate object. This can be useful for obtaining an indexed list while iterating.
Example:
python
Example:
python
Example:
python
x=5
print(type(x))
What is the purpose of the 'replace()' method in Python strings?
The 'replace()' method in Python strings is used to replace occurrences of a specified substring
with another substring. It returns a new string and does not modify the original string.
Example:
python
Example:
python
x=5
20
y=3
print(x + y) # Addition
print(x - y) # Subtraction
print(x * y) # Multiplication
print(x / y) # Division
print(x % y) # Modulo
Explain the use of the 'input()' function in Python.
The 'input()' function in Python is used to accept user input from the console. It prompts the user
with a message and waits for the user to enter a value, which is then returned as a string.
Example:
python
Example:
python
class MyClass:
def __init__(self, name, age):
self.name = name
self.age = age
What is the purpose of inheritance in Python classes?
Inheritance in Python allows a class to inherit attributes and methods from another class. It
facilitates code reusability and helps create a hierarchy of classes. Subclasses can override or
extend the functionality of the superclass.
You can create a module in Python by saving a Python script with the '.py' extension, which
contains various functions, classes, and variables. You can then import this module into other
Python scripts to use its functionality.
python
try:
# Code that may raise exceptions
pass
except ValueError:
# Handling ValueError
pass
except KeyError:
# Handling KeyError
pass
Example (handling multiple exceptions together):
python
try:
# Code that may raise exceptions
pass
except (ValueError, KeyError) as e:
# Handling ValueError and KeyError
pass
Explain the purpose of the 'is' and '== ' operators in Python.
The 'is' operator in Python checks if two variables refer to the same object, while the '=='
operator checks if two variables have the same value. The 'is' operator checks for object
identity, whereas the '==' operator checks for equality.
You can use the 'random' module in Python to generate pseudo-random numbers, select
random elements from a sequence, and shuffle sequences. It provides various functions for
different randomization tasks.
python
import random
print(random.randint(1, 100)) # Generates a random integer between 1 and 100
What is the purpose of the 'with' statement in Python?
The 'with' statement in Python is used to wrap the execution of a block of code within a context
manager. It simplifies resource management by ensuring that acquired resources are properly
released, even in the case of exceptions.
Example:
python
import pandas as pd
python
import pandas as pd
python
import pandas as pd
python
import pandas as pd
python
import pandas as pd
python
import pandas as pd
python