Python Data Science Jobs & Interviews
18.6K subscribers
148 photos
3 videos
17 files
260 links
Your go-to hub for Python and Data Science—featuring questions, answers, quizzes, and interview tips to sharpen your skills and boost your career in the data-driven world.

Admin: @Hussein_Sheikho
Download Telegram
Python Data Science Jobs & Interviews
Question 65: #python
What is the purpose of the 'abstractmethod' decorator in Python?
from abc import ABC, abstractmethod

# Define an abstract base class using ABC
class Shape(ABC):
@abstractmethod
def area(self):
pass

@abstractmethod
def perimeter(self):
pass

# Concrete subclass Circle inheriting from Shape
class Circle(Shape):
def init(self, radius):
self.radius = radius

def area(self):
return 3.14 * self.radius * self.radius

def perimeter(self):
return 2 * 3.14 * self.radius

# Concrete subclass Rectangle inheriting from Shape
class Rectangle(Shape):
def init(self, width, height):
self.width = width
self.height = height

def area(self):
return self.width * self.height

def perimeter(self):
return 2 * (self.width + self.height)

# Attempting to instantiate Shape directly will raise TypeError
try:
s = Shape()
except TypeError as e:
print(f"TypeError: {e}")

# Instantiate Circle and Rectangle objects
circle = Circle(5)
rectangle = Rectangle(4, 6)

# Calculate and print area and perimeter of Circle and Rectangle
print(f"Circle - Area: {circle.area()}, Perimeter: {circle.perimeter()}")
print(f"Rectangle - Area: {rectangle.area()}, Perimeter: {rectangle.perimeter()}")
👏1
Python Data Science Jobs & Interviews
from abc import ABC, abstractmethod # Define an abstract base class using ABC class Shape(ABC): @abstractmethod def area(self): pass @abstractmethod def perimeter(self): pass # Concrete subclass Circle inheriting from…
1⃣The Shape class is defined as an abstract base class using ABC from the abc module.

2⃣area and perimeter methods in Shape are marked as abstract using @abstractmethod, which means any subclass of Shape must implement these methods.

3⃣ Circle and Rectangle are concrete subclasses of Shape that provide implementations for area and perimeter.

4⃣ Attempting to instantiate Shape directly raises a TypeError because abstract classes cannot be instantiated.

5⃣ Circle and Rectangle demonstrate how abstract methods enforce a contract that subclasses must follow, ensuring consistent behavior across different shapes.

The abstractmethod decorator in Python is used to mark a method as abstract, meaning it must be implemented by subclasses. Classes containing abstract methods cannot be instantiated directly; they serve as blueprints for subclasses to provide concrete implementations of the abstract.

https://t.iss.one/DataScienceQ
👍1🥰1
Python Data Science Jobs & Interviews
Question 66: #python
What is the purpose of the 'getattr' function in Python?
class Person:
def init(self, name, age):
self.name = name
self.age = age

# Create an instance of the Person class
person = Person('Alice', 30)

# Using getattr to dynamically retrieve attributes
name = getattr(person, 'name')
age = getattr(person, 'age')
city = getattr(person, 'city', 'Unknown') # Providing a default value if attribute doesn't exist

print(f"Name: {name}")
print(f"Age: {age}")
print(f"City: {city}")



https://t.iss.one/DataScienceQ
👍1
Python Data Science Jobs & Interviews
class Person: def init(self, name, age): self.name = name self.age = age # Create an instance of the Person class person = Person('Alice', 30) # Using getattr to dynamically retrieve attributes name = getattr(person, 'name') age = getattr(person…
1⃣We define a Person class with attributes name and age.

2⃣An instance person of the Person class is created.

3⃣We use getattr to dynamically retrieve the values of name and age attributes from the person object.

4⃣ The third usage of getattr attempts to retrieve the city attribute, which does not exist in the Person class, so it defaults to 'Unknown'.

This demonstrates how getattr can be used to fetch attribute values from objects dynamically, handling cases where attributes may or may not exist.


https://t.iss.one/DataScienceQ
Python Data Science Jobs & Interviews
Question 67: #python
What is the purpose of the 'sys.argv' list in Python?
import sys

# Print all command-line arguments
print("All arguments:", sys.argv)

# Print the script name
print("Script name:", sys.argv[0])

# Print the command-line arguments excluding the script name
for i in range(1, len(sys.argv)):
print(f"Argument {i}:", sys.argv[i])

If this script is executed with command-line arguments like:

python script.py arg1 arg2 arg3

The output will be:

All arguments: ['script.py', 'arg1', 'arg2', 'arg3']
Script name: script.py
Argument 1: arg1
Argument 2: arg2
Argument 3: arg3



https://t.iss.one/DataScienceQ
2👍2
In your opinion, in what direction should we continue the questions in the coming week?
Anonymous Poll
35%
Numpy
38%
Pandas
20%
Matplotlib
7%
Other, say in comment
🔥8👍2🥰1
Python Data Science Jobs & Interviews
Question 68: #python
What is the purpose of the 'str' method in Python classes?
class Point:
def init(self, x, y):
self.x = x
self.y = y

def str(self):
return f"Point({self.x}, {self.y})"

# Creating an instance of the Point class
p = Point(3, 4)


# Printing the instance as a string
print(str(p)) # Output: Point(3, 4)



https://t.iss.one/DataScienceQ
1
Python Data Science Jobs & Interviews
class Point: def init(self, x, y): self.x = x self.y = y def str(self): return f"Point({self.x}, {self.y})" # Creating an instance of the Point class p = Point(3, 4) # Printing the instance as a string print(str(p)) #…
❤️ The str method in Python classes is a special method used to define the behavior of the class when it is converted to a string representation.

1⃣ The Point class has a str method that is used to return a string representation of an instance.

2⃣ When we call print(str(p)), the str method of the instance p is invoked, and the desired string representation (here "Point(3, 4)") is returned.

The str method allows customizing the string representation of class instances, which is useful when you want a readable and meaningful representation of an object.

https://t.iss.one/DataScienceQ
👍2
Thank God,

I have had my first baby girl and named her LAYA ❤️
26👍2
Forwarded from ُEng. Hussein Sheikho
This channels is for Programmers, Coders, Software Engineers.

0️⃣ Python
1️⃣ Data Science
2️⃣ Machine Learning
3️⃣ Data Visualization
4️⃣  Artificial Intelligence
5️⃣ Data Analysis
6️⃣ Statistics
7️⃣ Deep Learning
8️⃣ programming Languages

https://t.iss.one/addlist/8_rRW2scgfRhOTc0

https://t.iss.one/codeprogrammer
Please open Telegram to view this post
VIEW IN TELEGRAM
👍3
Python Data Science Jobs & Interviews
Question 69: #MachineLearning
What is the main difference between a hyperparameter and a parameter in machine learning models?
https://t.iss.one/DataScienceQ
Everyone should provide an example of a hyperparameter or parameter in the comments
Python Data Science Jobs & Interviews
Question 69: #MachineLearning
What is the main difference between a hyperparameter and a parameter in machine learning models?
https://t.iss.one/DataScienceQ
from sklearn.linear_model import LogisticRegression
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score

# Load dataset
data = load_iris()
X = data.data
y = data.target

# Split dataset into training and testing sets
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42)

# Create a logistic regression model
# Hyperparameters: C (regularization strength) and max_iter (number of iterations)
model = LogisticRegression(C=1.0, max_iter=100)

# Fit the model to the training data
model.fit(X_train, y_train)

# Predict on the test data
y_pred = model.predict(X_test)

# Evaluate the model
accuracy = accuracy_score(y_test, y_pred)
print(f"Model Accuracy: {accuracy:.2f}")

# Display parameters learned by the model
print(f"Model Coefficients: {model.coef_}")
print(f"Model Intercept: {model.intercept_}")


https://t.iss.one/DataScienceQ
👍3👏21
Python Data Science Jobs & Interviews
from sklearn.linear_model import LogisticRegression from sklearn.datasets import load_iris from sklearn.model_selection import train_test_split from sklearn.metrics import accuracy_score # Load dataset data = load_iris() X = data.data y = data.target # Split…
1⃣ Hyperparameters: C (regularization strength) and max_iter (number of iterations) are set by the user before training.

2⃣ Parameters: coef_ (weights) and intercept_ are learned by the model during training.

3⃣ The model’s performance is evaluated using accuracy, and the learned parameters are displayed.

In this example, hyperparameters (such as C and max_iter) are specified by the user, while parameters (such as weights and intercept) are learned by the model during training.

https://t.iss.one/DataScienceQ
👏1
Is hard this question?
Anonymous Poll
43%
Yes
57%
No