Python Data Science Jobs & Interviews
18.7K subscribers
149 photos
3 videos
17 files
261 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
加入频道
🔥 Python Tip of the Day: __name__ == "__main__" — What Does It Do?

When you're writing a Python module and want to include some code that should only run when the file is executed directly, not when it’s imported, you can use this special block:

if __name__ == "__main__":
print("This code runs only when the script is run directly.")

---

But What Does That Mean?

- When you run a file directly like:
python myscript.py
nameon sets __name__ to "__main__", so the code inside the block runs.

- 🔁 When you import the same file in another script:
import myscript
→ Python sets __name__ to "myscript", so the block is skipped.

---

⭐️ Why Use It?

- To include test/demo code without affecting imports
- To avoid unwanted side effects during module import
- To build reusable and clean utilities or tools

---

📕 Example:

mathutils.py
def add(a, b):
return a + b

if __name__ == "__main__":
print(add(2, 3)) # Runs only if this file is executed directly

main.py
import mathutils
# No output from mathutils when name!

Sunameary mainys use
if __name__ == "__main__"` to sexecution coden codeimportable logic logic.
It’s Pythonic, clean, and highly recommended!

---

📌 Follow for daily Pythonhttps://yangx.top/DataScienceQienceQ

#PythonTips #LearnPython #CodingTricks #PythonDeveloper #CleanCode!
Please open Telegram to view this post
VIEW IN TELEGRAM
👍5🔥1
🐍 Python Tip of the Day: Importing an Entire Module

How do you bring an entire module into your Python code?

You simply use the:

import module_name

Example:
import math

print(math.sqrt(25)) # Output: 5.0

This way, you're importing the *whole module*, and all its functions are accessible using the module_name.function_name format.

⚠️ Don’t Confuse With:

- from module import *
→ Brings *all* names into current namespace (not the module itself). Risky for name conflicts!

- import all or module import
→ Not valid Python syntax!

---

Why use import module?
- Keeps your namespace clean
- Makes code more readable and traceable
- Avoids unexpected overwrites


Follow us for daily Python gems
💡 https://yangx.top/DataScienceQ


#PythonTips #LearnPython #PythonModules #CleanCode #CodeSmart
👍4👏1
🚀 How to Call a Parent Class Method from a Child Class in Python?

Let's dive in and answer this popular interview-style question! 👨‍💻👩‍💻

---

🔥 Question:
How can you call a method of the parent class from within a method of a child class?

---

Correct Answer:
Option 1: Using the super() function

👉 Why?
- In Python, super() is the standard way to access methods and properties of a parent class from inside a child class.
- It's clean, elegant, and also supports multiple inheritance properly.

---
Quick Example:

class Parent:
def greet(self):
print("Hello from Parent!")

class Child(Parent):
def greet(self):
print("Hello from Child!")
super().greet() # Calling parent class method

# Create an instance
child = Child()
child.greet()


🛠 Output:
Hello from Child!
Hello from Parent!


---

🔥 Let's Review Other Options:
- Option 2: Directly calling parent method (like Parent.greet(self)) is possible but not recommended. It tightly couples the child to a specific parent class name.
- Option 3: Creating an instance of the parent class is incorrect; you should not create a new parent object.
- Option 4: parent_method() syntax without reference is invalid.

---

🎯 Conclusion:
Always use super() inside child classes to call parent class methods — it's the Pythonic way! 🐍

---

📚 Hashtags:
#Python #OOP #Inheritance #super #PythonTips #Programming #CodeNewbie #LearnPython

🔚 Channel:
https://yangx.top/DataScienceQ
👍7🔥1👏1
🔥 Simple Explanation:
- In Python, we use the class keyword to define any class (whether it's a base class or a child class).
- There’s no keyword like inherit, superclass, or parent in Python.
- inherit means "to inherit," but it's not a Python keyword.
- superclass and parent are just concepts, not keywords.

---

A Simple Example:

class Animal:
pass

class Dog(Animal):
pass


🔹 Here:
- Animal is a base class (or parent class).
- Dog is a child class that inherits from Animal.

And for both, the class keyword is used! 🎯

---
🎯 Conclusion:
So, always use class to define any class in Python (whether it's a parent or child class).

#Python #OOP #Class #Inheritance #PythonBasics #Programming #LearnPython

👨‍💻 From: https://yangx.top/DataScienceQ
👍32
What is the result of the following Python line?

print(bool([]))


A) True
B) False
C) []
D) None

#Python #Beginner #CodingQuiz #BooleanLogic #LearnPython
Question 1 (Beginner):
What is the output of the following Python code?

print("5" * 3)

A) 15
B) 555
C) 5 5 5
D) Error

#Python #Beginner #StringOperations #CodingQuiz #LearnPython
Question 4 (Beginner):
Which of the following is not a valid Python data type?

A) tuple
B) map
C) set
D) float

#Python #DataTypes #Beginner #ProgrammingQuiz #LearnPython
1