In a recent interview, SLK software asked Python questions that can help you prepare for your next job.

10 SLK Software Python interview questions
01. How do we sort a list in Python?
a=[20,40, 10, 5]
print(sorted(a))
Output
[5, 10, 20, 40]
** Process exited - Return Code: 0 **
Press Enter to exit terminal
02. How do we get index and value using the enumerate function?
a=[20,40, 10, 5]
print(list(enumerate(a)))
e=enumerate(a)
for i, j in e:
print(i, j)
Output
[(0, 20), (1, 40), (2, 10), (3, 5)]
0 20
1 40
2 10
3 5
** Process exited - Return Code: 0 **
Press Enter to exit terminal
03. Pandas’ categorical method, how to use on a list?
The categorical method basically counts unique categories.
# 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 ("myc1 : ", c1)
c2 = pd.Categorical(['e', 'm', 'f', 'i','f', 'e', 'h', 'm' ])
print ("myc2 : ", 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']
04. What is the Coalesce method in PySpark?
#RDD
rdd.getNumPartitions()
#For DataFrame, convert to RDD first
df.rdd.getNumPartitions()
df.coalesce(9)
Shuffling:
repartitioninvolves a full shuffle, making it more computationally expensive thancoalesce, which avoids a full shuffle.
When to Use:Usecoalescewhen decreasing the number of partitions, especially when the data is already evenly distributed.
Userepartitionwhen you need to increase the number of partitions or want a more balanced distribution of data. Which is crucial for operations likejoin.
Performance:coalesceis generally faster and more efficient thanrepartitionwhen reducing the number of partitions. Because it minimizes data movement.
05. How to find mean and Average in Python?
###Average
a = [1, 2, 4, 5]
avg=sum(a)/len(a)
print(avg)
###Mean
import numpy as np
a=[1,2,3,45]
print(np.mean(a))
Output
3.0
12.75
** Process exited - Return Code: 0 **
Press Enter to exit terminal
06. How to write simple inheritance examples in Python?
# 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!
Output
Buddy says Woof!
Whiskers says Meow!
** Process exited - Return Code: 0 **
Press Enter to exit terminal
07. Decorator simple 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!
Output
HELLO, ALICE!
** Process exited - Return Code: 0 **
Press Enter to exit terminal
08. What is quality & data governance in AWS?
- Data quality - Cleaning data for accuracy
- Goevrnace - Security, encryption
09. What are various errors in Python?
- NameError
- ValueError
- TypeError
- IndexError
- SyntaxError
- KeyError
- AttributeError
- EOFError
- ZeroDivisionError
- OverflowError
- ModuleNotFoundError
10. How to calculate the 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)
Output
Average: 30.0
** Process exited - Return Code: 0 **
Press Enter to exit terminal







You must be logged in to post a comment.