7 Days Analytics Course 3feiz7 2
7 Days Analytics Course 3feiz7 2
The CASE statement in SQL is used to create different outputs based on different conditions.
The COALESCE function in SQL is used to return the first non-NULL value in a list.
The NULLIF function in SQL is used to compare two expressions. If the expressions are equal,
the function returns NULL; otherwise, it returns the first expression.
To handle duplicate records in a query result, you can use the DISTINCT keyword in a SELECT
statement to retrieve unique values from a column or set of columns. Alternatively, you can use
the GROUP BY clause to group the records and perform aggregation functions to eliminate
duplicates.
10
Example:
python
def greet(name):
print("Hello, " + name)
What is the purpose of using 'if' statements in Python?
'If' statements in Python allow for conditional execution of code. They help control the flow of the
program by executing specific blocks of code based on certain conditions being true or false.
Example:
python
x = 10
if x > 5:
print("x is greater than 5")
Explain the concept of a dictionary in Python.
A dictionary in Python is an unordered collection of key-value pairs, enclosed in curly braces {}.
It allows you to store and retrieve data using a unique key for each value, making it easier to
access and manipulate data based on specific keys.
Example:
python
11
student = {
"name": "John",
"age": 25,
"grade": "A"
}
What are the different types of loops in Python, and how are they used?
Python supports various types of loops, including 'for' loops and 'while' loops. 'For' loops are
used to iterate over a sequence, while 'while' loops are used to execute a block of code
repeatedly as long as a specified condition is true.
python
python
i=1
while i < 6:
print(i)
i += 1
Example:
python
Example:
12
python
try:
age = int(input("Enter your age: "))
except ValueError:
print("Invalid input. Please enter a valid age.")
How do you import modules in Python?
You can import modules in Python using the 'import' keyword followed by the module name. You
can also use the 'from' keyword to import specific functions or variables from a module.
Example:
python
import math
from datetime import datetime
Explain the purpose of the 'range()' function in Python.
The 'range()' function in Python is used to generate a sequence of numbers within a specified
range. It can be used with 'for' loops to iterate over a specific sequence of numbers.
Example:
python
for x in range(5):
print(x)
What are lambda functions in Python, and how are they used?
Lambda functions in Python are small, anonymous functions that can have any number of
arguments but only one expression. They are often used when a small function is required for a
short period.
Example:
python
x = lambda a, b: a * b
print(x(5, 6))
How do you use the 'map()' and 'filter()' functions in Python?
The 'map()' function is used to apply a specified function to each item in an iterable, while the
'filter()' function is used to filter out elements from an iterable based on a specified condition.
Example (map):
13
python
def square(x):
return x * x
numbers = [1, 2, 3, 4]
squares = list(map(square, numbers))
print(squares)
Example (filter):
python
def is_even(x):
return x % 2 == 0
numbers = [1, 2, 3, 4, 5, 6]
even_numbers = list(filter(is_even, numbers))
print(even_numbers)
Explain the purpose of the 'split()' function in Python.
The 'split()' function in Python is used to split a string into a list of substrings based on a
specified delimiter. By default, the delimiter is a space.
Example:
python
Example:
python
try:
age = int(input("Enter your age: "))
except ValueError:
print("Invalid input. Please enter a valid age.")
finally:
print("Execution complete.")
What is the purpose of the 'set' data type in Python?
14
The 'set' data type in Python is an unordered collection of unique elements. It is used to perform
mathematical set operations such as union, intersection, and difference.
Example:
python
Example:
python
str1 = "Hello"
str2 = "world"
result = str1 + " " + str2
print(result)
Explain the concept of list comprehension in Python.
List comprehension in Python is a concise way to create lists. It allows you to create a new list
by applying an expression to each item in an existing list.
Example:
python
numbers = [1, 2, 3, 4, 5]
squares = [x * x for x in numbers]
print(squares)
What is the difference between deep copy and shallow copy in Python?
In Python, a shallow copy creates a new object but does not duplicate the nested objects, while
a deep copy creates a new object and recursively duplicates the nested objects.
Example:
python
15
Example:
python
Example:
python
Example:
python
name = "John"
age = 30
text = "My name is {} and I am {} years old.".format(name, age)
print(text)
How do you convert a string to lowercase or uppercase in Python?
You can convert a string to lowercase or uppercase in Python using the 'lower()' and 'upper()'
methods, respectively. These methods return a new string and do not modify the original string.
Example:
python
16
Example:
python
my_list = [1, 2, 3]
my_list.append(4)
print(my_list)
What is the purpose of the 'pop()' method in Python lists?
The 'pop()' method in Python lists is used to remove and return an element from a specific index
or the last element if no index is specified. It modifies the original list.
Example:
python
my_list = [1, 2, 3, 4]
my_list.pop()
print(my_list)
How do you reverse a string in Python?
You can reverse a string in Python using slicing. By specifying a step of -1, you can reverse the
string.
Example:
python
Example: