SLK Software Python Interview Questions

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

1.

Sorting list

a=[20,40, 10, 5]

print(sorted(a))

2. Enumerate list

a=[20,40, 10, 5]

print(list(enumerate(a)))

e=enumerate(a)

for i, j in e:
print(i, j)

3. Categorical columns in Python Pandas.

# Python code explaining


# numpy.pandas.Categorical()

# importing libraries
import numpy as np
import pandas as pd

# Categorical using dtype


c = pd.Series(["a", "b", "d", "a", "d"], dtype ="category")
print ("\nCategorical without pandas.Categorical() : \n", c)

c1 = pd.Categorical([1, 2, 3, 1, 2, 3])
print ("\n\nc1 : ", c1)

c2 = pd.Categorical(['e', 'm', 'f', 'i',


'f', 'e', 'h', 'm' ])
print ("\nc2 : ", c2)
Output:

Categorical without pandas.Categorical() :


0 a
1 b
2 d
3 a
4 d
dtype: category
Categories (3, object): ['a', 'b', 'd']

c1 : [1, 2, 3, 1, 2, 3]
Categories (3, int64): [1, 2, 3]

c2 : ['e', 'm', 'f', 'i', 'f', 'e', 'h', 'm']


Categories (5, object): ['e', 'f', 'h', 'i', 'm']

We can find how many categories in each group.

4. Coalesce() in PySpark.

// RDD
rdd.getNumPartitions()

// For DataFrame, convert to RDD first


df.rdd.getNumPartitions()

df.coalesce(9)

5. Find mean and Avg in Python.

a = [1, 2, 4, 5]

avg=sum(a)/len(a)
print(avg)

Import numpy as np

a=[1,2,3,45]

print(np.mean(a))
6. Inheritance example.

# Parent class
class Animal:
def __init__(self, name):
self.name = name

def speak(self):
pass

# Child class inheriting from Animal


class Dog(Animal):
def speak(self):
return f"{self.name} says Woof!"

# Child class inheriting from Animal


class Cat(Animal):
def speak(self):
return f"{self.name} says Meow!"

# Create instances of child classes


dog = Dog("Buddy")
cat = Cat("Whiskers")

# Call the speak method of each instance


print(dog.speak()) # Output: Buddy says Woof!
print(cat.speak()) # Output: Whiskers says Meow!

7. Decorator example.

# Decorator function
def uppercase_decorator(func):
def wrapper(text):
result = func(text)
return result.upper()
return wrapper

# Function to be decorated
def greet(name):
return f"Hello, {name}!"

# Applying the decorator


greet_uppercase = uppercase_decorator(greet)
# Calling the decorated function
print(greet_uppercase("Alice")) # Output: HELLO, ALICE!

- Uses of Decorators.

8. AWS Data Quality / Governance

- Data quality - Cleaning data for accuracy


- Goevrnace - Security, encryption

9. Different Errors in Python

- NameError
- ValueError
- TypeError
- IndexError
- SyntaxError
- KeyError
- AttributeError
- EOFError
- ZeroDivisionError
- OverflowError
- ModuleNotFoundError

10. How to calculate Average using enumerate().

numbers = [10, 20, 30, 40, 50]

# Calculate sum using enumerate


total_sum = sum(num for _, num in enumerate(numbers))

# Calculate average
average = total_sum / len(numbers)

print("Average:", average)

You might also like