PyData Careers
20.7K subscribers
196 photos
4 videos
26 files
342 links
Python Data Science jobs, interview tips, and career insights for aspiring professionals.
Download Telegram
๐Ÿ† 150 Design Patterns: A Complete Guide

๐Ÿ“ข Dive into 150 essential Software Design Patterns! Learn practical solutions with code examples to build robust, efficient, and scalable applications.

โšก Tap to unlock the complete answer and gain instant insight.

โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”
By: @DataScienceQ โœจ
โค2
A generator in Python is a function that returns not a single value, but an iterator object.

Generators differ from regular functions in that they use yield instead of return.
The next value from the iterator is obtained by calling next(generator).

Example:

def multiple_generator(x, n):
    for i in range(1, n + 1):
        yield x * i

multiples_of_5 = multiple_generator(5, 3)

print(next(multiples_of_5))  # 5
print(next(multiples_of_5))  # 10
print(next(multiples_of_5))  # 15


๐Ÿ‘‰  @DataScience4
Please open Telegram to view this post
VIEW IN TELEGRAM
โค1
๐Ÿ† Unlock 150 SQL Clean Code Principles

๐Ÿ“ข Unlock cleaner, more maintainable SQL! Explore 150 essential clean code principles to write readable, consistent queries that reduce bugs and simplify collaboration.

โšก Tap to unlock the complete answer and gain instant insight.

โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”
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
โ” Interview Question

How to view an object's methods?

Answer: To see all methods and attributes associated with a specific object, you can use the dir() function. It takes the object as an argument and returns a list of all attribute and method names of the object.

tags: #interview

โžก @DataScienceQ
Please open Telegram to view this post
VIEW IN TELEGRAM
โค2๐Ÿ”ฅ1
โ” Interview Question

What is the GIL (Global Interpreter Lock) in Python, and how does it impact the execution of multi-threaded programs?

Answer: The Global Interpreter Lock (GIL) is a mutex (or a lock) that allows only one thread to hold the control of the Python interpreter at any one time. This means that in a CPython process, only one thread can be executing Python bytecode at any given moment, even on a multi-core processor.

This has a significant impact on performance:

โ€ข For CPU-bound tasks: Multi-threaded Python programs see no performance gain from multiple CPU cores. If you have a task that performs heavy calculations (e.g., image processing, complex math), creating multiple threads will not make it run faster. The threads will execute sequentially, not in parallel, because they have to take turns acquiring the GIL.

โ€ข For I/O-bound tasks: The GIL is less of a problem. When a thread is waiting for Input/Output (I/O) operations (like waiting for a network response, reading from a file, or querying a database), it releases the GIL. This allows another thread to run. Therefore, the threading module is still highly effective for tasks that spend most of their time waiting, as it allows for concurrency.

How to achieve true parallelism?

To bypass the GIL and leverage multiple CPU cores for CPU-bound tasks, you must use the multiprocessing module. It creates separate processes, each with its own Python interpreter and memory space, so the GIL of one process does not affect the others.

tags: #Python #Interview #CodingInterview #GIL #Concurrency #Threading #Multiprocessing #SoftwareEngineering

โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”
By: @DataScienceQ โœจ
โค1
โ” Interview question

What objects can be put into a set?

Answer: In Python, a set can only contain hashable (i.e., immutable) objects. This means you can put numbers, strings, tuples (if all their elements are also hashable), boolean values, and other immutable types into a set.

Objects like list, dict, set, and other mutable structures cannot be put in: they do not have a hash function (hash) and will cause a TypeError.

ta
gs: #interview

โžก @DataScienceQ
Please open Telegram to view this post
VIEW IN TELEGRAM
โค1
Python tip:

Use the Path class from the pathlib module to work with file paths cross-platform.

from pathlib import Path

p = Path('/usr/local/bin')


๐Ÿ‘‰  @DataScienceQ
Please open Telegram to view this post
VIEW IN TELEGRAM
โค3
๐Ÿ† NumPy Python Tips

๐Ÿ“ข Boost your Python skills with NumPy! Learn quick tips for efficient array creation and manipulation to level up your data handling.

โšก Tap to unlock the complete answer and gain instant insight.

โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”
By: @DataScienceQ โœจ
โค1
Tired of missing perfect GOLD entries?
Last week alone: 1,740 pips profit, 10/12 trades wonโ€”all shared live in our channel.
Ready to catch the next move? Stay ahead with premium signals & real-time updatesโ€”donโ€™t just watch, start profiting!
Join GOLD PIPS SIGNALS before the next trade drops!

#ad InsideAds
โค1
๐ŸŒ Remote Work โ€” India Only ๐Ÿ‡ฎ๐Ÿ‡ณ
๐Ÿ’ฐ Earn $800โ€“$2000/month
๐Ÿ“† Weekly payments โ€” card or crypto
๐Ÿ•’ Work 3โ€“4 hours/day
๐Ÿ  Fully remote โ€” no office, no experience needed
๐ŸŽ“ Training provided
๐Ÿ‘‰ Indian citizens aged 25+ only
๐Ÿ“ฉ Message our HR manager to apply today!

#ad InsideAds
โค2
โ” Interview Question

What can be a key in a dictionary?

Answer: Dictionaries in Python are hash tables. Instead of keys, hashes are used in the dictionary. Accordingly, any hashable data type can be a key in the dictionary, which means all immutable data types.

tags: #interview

โžก๏ธ @DataScienceQ
Please open Telegram to view this post
VIEW IN TELEGRAM
โค3
๐Ÿ† Master Vue.js for Modern UIs

๐Ÿ“ข Master Vue.js, the intuitive JavaScript framework, and confidently build modern, reactive user interfaces. Your journey to dynamic web apps starts here!

โšก Tap to unlock the complete answer and gain instant insight.

โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”
By: @DataScienceQ โœจ
๐Ÿ† Python MCP Server: Create & Learn

๐Ÿ“ข Unlock Minecraft's secrets! Build your own custom Minecraft Protocol (MCP) server with Python for ultimate game customization and deep dives into mechanics.

โšก Tap to unlock the complete answer and gain instant insight.

โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”
By: @DataScienceQ โœจ
โค1
When an attribute is defined both in the instance and in the class, Python always takes the value from the instance.

The class attribute is used only if it is not present in the instance.

class Warehouse:
    purpose = "storage"
    region = "west"

w1 = Warehouse()
print(w1.purpose, w1.region)   # storage west

w2 = Warehouse()
w2.region = "east"
print(w2.purpose, w2.region)   # storage east


๐Ÿ‘‰  @DataScienceQ
Please open Telegram to view this post
VIEW IN TELEGRAM
โ” Interview Question

What is a deep copy?

Answer: A deep copy is a complete duplication of an object along with all nested structures. Changes to the original do not affect the copy, and vice versa.

In Python, this is done using copy.deepcopy(), which creates a fully independent data structure, including nested lists, dictionaries, and other objects.


tags: #interview

โžก @DataScienceQ
Please open Telegram to view this post
VIEW IN TELEGRAM
๐Ÿ‘4โค1
Tip for clean tests in Python:

Structure your tests with the Arrange-Act-Assert pattern to improve readability and maintainability.

โ€ข Arrange: Set up the test. Initialize objects, prepare data, and configure any mocks or stubs.
โ€ข Act: Execute the code being tested. Call the specific function or method.
โ€ข Assert: Check the outcome. Verify that the result of the action is what you expected.

import pytest
from dataclasses import dataclass, field


# Code to be tested
@dataclass
class Product:
name: str
price: float


@dataclass
class ShoppingCart:
items: list[Product] = field(default_factory=list)

def add_item(self, product: Product):
if product.price < 0:
raise ValueError("Product price cannot be negative.")
self.items.append(product)

def get_total_price(self) -> float:
return sum(item.price for item in self.items)


# Tests using the Arrange-Act-Assert pattern
def test_get_total_price_for_multiple_items():
# Arrange
product1 = Product(name="Mouse", price=25.50)
product2 = Product(name="Keyboard", price=75.50)
cart = ShoppingCart()
cart.add_item(product1)
cart.add_item(product2)

# Act
total_price = cart.get_total_price()

# Assert
assert total_price == 101.00


def test_get_total_price_for_empty_cart():
# Arrange
cart = ShoppingCart()

# Act
total_price = cart.get_total_price()

# Assert
assert total_price == 0.0


def test_add_item_with_negative_price_raises_value_error():
# Arrange
cart = ShoppingCart()
product_with_negative_price = Product(name="Invalid Item", price=-50.0)

# Act & Assert
with pytest.raises(ValueError, match="Product price cannot be negative."):
cart.add_item(product_with_negative_price)


#Python #Testing #CleanCode #SoftwareEngineering #Pytest #DeveloperTips #AAA

โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”
By: @DataScienceQ โœจ
โ” Interview Question

Why does list.sort() return None instead of the sorted list?

Answer: The list.sort() method modifies the list in place and intentionally returns None to clearly indicate that the sorting was done but no new list was created. This prevents confusion between modifying the object and creating its copy.

If a new sorted list is needed, the built-in sorted() function is used, which returns the result without changing the original.


tags: #interview

โžก @DataScienceQ
Please open Telegram to view this post
VIEW IN TELEGRAM
Hello! How can I assist you today?

By: @DataScienceQ ๐Ÿš€
โค2
โ” Interview Question

Are there generics in Python like in Java or C++?

Answer: Yes, but only at the annotation level. Since Python 3.5, generic types (List[T], Dict[K, V]) have appeared through the typing module, but they are intended for static checking and do not affect the program's behavior at runtime.

tags: #interview

โžก @DataScienceQ
Please open Telegram to view this post
VIEW IN TELEGRAM