Python Data Science Jobs & Interviews
20.5K subscribers
191 photos
4 videos
25 files
332 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
How to Dynamically Create a Class at Runtime in Python?

You can dynamically create a class in Python using the built-in type() function. This is one of the simplest ways to leverage metaclasses.

Example:

# Create a new class dynamically
MyDynamicClass = type('MyDynamicClass', (object,), {
'say_hello': lambda self: print("Hello!")
})

# Use the dynamically created class
obj = MyDynamicClass()
obj.say_hello()

Explanation:

* 'MyDynamicClass': Name of the new class
* (object,): Tuple of base classes (here, just inheriting from object)
* {'say_hello': ...}: Dictionary of attributes/methods for the class

Output:

Hello!

This is a powerful feature used in metaprogramming and framework design.



#PythonTips #Metaclass #PythonOOP #DynamicClass #typeFunction #AdvancedPython #CodingTips

🌺https://t.iss.one/DataScienceQ
👍2🔥2
💡 Python Lists: Adding and Extending

Use .append() to add a single item to the end of a list. Use .extend() to add all items from an iterable (like another list) to the end.

# Create a list of numbers
my_list = [10, 20, 30]

# Add a single element
my_list.append(40)
# my_list is now [10, 20, 30, 40]
print(f"After append: {my_list}")

# Add elements from another list
another_list = [50, 60]
my_list.extend(another_list)
# my_list is now [10, 20, 30, 40, 50, 60]
print(f"After extend: {my_list}")


Code explanation: The code first initializes a list. .append(40) adds the integer 40 to the end. Then, .extend() takes each item from another_list and adds them individually to the end of my_list.

#Python #PythonLists #DataStructures #CodingTips #PythonCheatsheet

━━━━━━━━━━━━━━━
By: @DataScienceQ
The Walrus Operator := (Assignment Expressions)

Introduced in Python 3.8, the "walrus operator" := allows you to assign a value to a variable as part of a larger expression. It's a powerful tool for writing more concise and readable code, especially in while loops and comprehensions.

It solves the common problem where you need to compute a value, check it, and then use it again.

---

#### The Old Way: Repetitive Code

Consider a loop that repeatedly prompts a user for input and stops when the user enters "quit".

# We have to get the input once before the loop,
# and then again inside the loop.
command = input("Enter command: ")

while command != "quit":
print(f"Executing: {command}")
command = input("Enter command: ")

print("Exiting program.")

Notice how input("Enter command: ") is written twice.

---

#### The Pythonic Way: Using the Walrus Operator :=

The walrus operator lets you capture the value and test it in a single, elegant line.

while (command := input("Enter command: ")) != "quit":
print(f"Executing: {command}")

print("Exiting program.")

Here, (command := input(...)) does two things:
• Calls input() and assigns its value to the command variable.
• The entire expression evaluates to that same value, which is then compared to "quit".

This eliminates redundant code, making your logic cleaner and more direct.

#Python #PythonTips #PythonTricks #WalrusOperator #Python3 #CleanCode #Programming #Developer #CodingTips

━━━━━━━━━━━━━━━
By: @DataScienceQ
2