Python Data Science Jobs & Interviews
20.4K subscribers
188 photos
4 videos
25 files
326 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
🧠 Quiz: What is the most "Pythonic" way to create a new list containing the squares of numbers from an existing list called nums?

A) Using a for loop and the .append() method.
B) new_list = [num**2 for num in nums]
C) Using a while loop with an index counter.
D) new_list = (num**2 for num in nums)

βœ… Correct answer: B

Explanation: This is a list comprehension. It's a concise, readable, and often faster way to create a new list from an iterable compared to a traditional for loop. Option D creates a generator expression, not a list.

#Python #ProgrammingTips #PythonQuiz

━━━━━━━━━━━━━━━
By: @DataScienceQ ✨
🧠 Quiz: What is the most Pythonic way to create a new list containing the squares of numbers from 0 to 4?

A) squares = [x**2 for x in range(5)]
B) squares = list(map(lambda x: x**2, range(5)))
C) squares = []
for x in range(5):
squares.append(x**2)

βœ… Correct answer: A

Explanation: List comprehensions are a concise and highly readable way to create lists from other iterables. While the other options work, a list comprehension is generally considered the most "Pythonic" for its clarity and efficiency in this context.

#Python #ProgrammingTips #CodeQuiz

━━━━━━━━━━━━━━━
By: @DataScienceQ ✨
Python tip:

itertools.zip_longest pairs elements from multiple iterables, but unlike the built-in zip(), it continues until the longest iterable is exhausted, padding shorter ones with a specified fillvalue.

While zip() truncates its output to the length of the shortest input, zip_longest() ensures no data is lost from longer inputs by substituting None (or a custom value) for missing items.

ExampleπŸ‘‡
>>> import itertools
>>> students = ['Alice', 'Bob', 'Charlie', 'David']
>>> scores = [88, 92, 75]
>>> grades = list(itertools.zip_longest(students, scores, fillvalue='Absent'))
grades
[('Alice', 88), ('Bob', 92), ('Charlie', 75), ('David', 'Absent')]

#Python #ProgrammingTips #Itertools #PythonTips #CleanCode

━━━━━━━━━━━━━━━
By: @DataScienceQ ✨
❀1