Wednesday 6 November 2024

Python OOPS Challenge | Day 10 | What is the output of following Python code?


This code snippet demonstrates runtime polymorphism. Here’s why:

1. Polymorphism allows a method in a subclass to have the same name as a method in its superclass but behave differently. In this example, the printWeight() method is defined in both the PolarAnimal superclass and the Penguin subclass.


2. Method Overriding: The Penguin subclass overrides the printWeight() method of PolarAnimal. This means that if an object of Penguin is used to call printWeight(), it will execute the print("heavy") line instead of print("light") defined in the superclass.


3. Runtime Polymorphism (also known as dynamic polymorphism) happens at runtime, where the method to execute is determined based on the actual object type (i.e., whether it’s a Penguin or PolarAnimal instance) rather than at compile time.



Since printWeight() behaves differently in Penguin than in PolarAnimal, it demonstrates runtime polymorphism.


Tuesday 5 November 2024

Python OOPS Challenge | Day 9 | What is the output of following Python code?


In this code snippet, we have two classes: OSDevice and SmartTV. The SmartTV class inherits from the OSDevice class.

Code Analysis

1. The OSDevice class has a method called printSize, which prints "medium".


2. The SmartTV class has its own printSize method that overrides the one from OSDevice and prints "large".



When we create an instance of SmartTV with obj = SmartTV() and call obj.printSize(), Python will look for the printSize method in the SmartTV class first. Since SmartTV has its own printSize method, it overrides the printSize method in OSDevice. Therefore, only "large" is printed.

Output

The output of this code will be:

large

So, the correct answer is the second option: large.


Monday 4 November 2024

Python OOPS Challenge | Day 8 |What is the output of following Python code?



In this code snippet, we have two classes: Fruit and Apple. The Apple class inherits from the Fruit class.

Code Analysis

1. The Fruit class has an __init__ method (constructor) that prints '1'.


2. The Apple class also has its own __init__ method that overrides the one from Fruit and prints '2'.



When we create an instance of Apple with obj = Apple(), Python will look for the __init__ method in the Apple class first. Since Apple has its own __init__ method, it overrides the __init__ method of Fruit. Therefore, only '2' is printed.

Output

The output of this code will be:

2

So, the correct answer is the second option: 2.



Sunday 3 November 2024

Python OOPS Challenge | Day 7 |What is the output of following Python code?


Let's go through this code snippet step-by-step:

try:
    print("1")
    raise Exception("2")
    print("3")
except Exception as e:
    print(str(e))
    print("4")

Explanation:

1. try Block Execution:

print("1"): This line executes first and outputs 1.

raise Exception("2"): This line raises an exception with the message "2". Because an exception is raised, the code execution immediately jumps to the except block, and the line print("3") is never executed.



2. except Block Execution:

print(str(e)): This line executes next, printing the exception message "2".

print("4"): This line then executes, printing 4.




Final Output:

The output is:

1
2
4

Correct Answer:

The correct answer is 124.


Saturday 2 November 2024

Automating Excel with Python

 

Automating Excel with Python 


In Automating Excel with Python: Processing Spreadsheets with OpenPyXL you will learn how to use Python to create, edit or read Microsoft Excel documents using OpenPyXL.


Python is a versatile programming language. You can use Python to read, write and edit Microsoft Excel documents. There are several different Python packages you can use, but this book will focus on OpenPyXL.

The OpenPyXL package allows you to work with Excel files on Windows, Mac and Linux, even if Excel isn't installed.

In this book, you will learn about the following:

  • Opening and Saving Workbooks

  • Reading Cells and Sheets

  • Creating a Spreadsheet (adding / deleting rows and sheets, merging cells, folding, freeze panes)

  • Cell Styling (font, alignment, side, border, images)

  • Conditional Formatting

  • Charts

  • Comments

  • and more!

Python is a great language that you can use to enhance your daily work, whether you are an experienced developer or a beginner!

Automating Excel with Python

Python OOPS Challenge | Day 6 |What is the output of following Python code?



In this code, we define a class Rectangle with an initializer method __init__ and a method calcPerimeter to calculate the perimeter of the rectangle.

Code Breakdown:

1. __init__ method: This is the constructor of the class. When an object of the Rectangle class is created, it initializes the attributes a and b with the values provided as arguments.

def __init__(self, aVal, bVal):
    self.a = aVal
    self.b = bVal

Here, self.a and self.b are set to aVal and bVal, respectively.


2. calcPerimeter method: This method calculates the perimeter of the rectangle using the formula .

def calcPerimeter(self):
    return 2 * self.a + 2 * self.b


3. Creating an object and calling calcPerimeter:

obj = Rectangle(1, 5)
print(obj.calcPerimeter())

Here, we create an object obj of the Rectangle class with a = 1 and b = 5.

Then, calcPerimeter is called on obj, which calculates .




Output:

The output of this code is 12.


Retrieve System Information using Python

 

import wmi


c = wmi.WMI()


for os in c.Win32_OperatingSystem():

    

    print(f"OS Name: {os.Name}")

    print(f"Version: {os.Version}")

    print(f"Manufacturer: {os.Manufacturer}")

    print(f"Architecture: {os.OSArchitecture}")

Friday 1 November 2024

Python OOPS Challenge! Day 4 | What is the output of following Python code?


In this code snippet, an attempt is made to create an instance of the class SpaceObject, which inherits from ABC (Abstract Base Class) from Python's abc module. Here’s the breakdown of the code:

Explanation

1. Abstract Base Class (ABC):

The class SpaceObject inherits from ABC, which is used to define abstract base classes.

However, SpaceObject does not contain any abstract methods (methods decorated with @abstractmethod), making it a concrete class, even though it inherits from ABC.



2. Object Creation:

Since there are no abstract methods in SpaceObject, it is possible to instantiate this class directly.

The code obj = SpaceObject() will execute without any issues.



3. Output:

After creating the object, print('Object Creation') is called, which will print Object Creation to the console.




Conclusion

The correct answer is:

Object Creation



Python OOPS Challenge! Day 5 | What is the output of following Python code?


The code snippet contains a class Tablet with a static method printModel. However, there’s an issue with how this static method is defined and used.

Explanation

1. Static Method Misuse:

printModel is decorated with @staticmethod, which means it should not accept any arguments except for optional ones. However, self is being used as a parameter, which is misleading.

Static methods do not have access to instance-specific data, so they cannot use self to access instance attributes like self.model.



2. Error Triggered:

When Tablet.printModel() is called, it tries to execute print(self.model).

Because printModel is a static method, self is not automatically passed, leading to a missing argument error.

This will raise a TypeError, saying something like "printModel() takes 0 positional arguments but 1 was given."




Output

The correct answer is:

Exception


Python Code for Periodic Table

 

import periodictable


Atomic_No = int(input("Enter Atomic No :"))


element = periodictable.elements[Atomic_No]

print('Name:', element.name)

print('Symbol:', element.symbol)

print('Atomic mass:', element.mass)

print('Density:', element.density)


#source code --> clcoding.com

Name: zinc

Symbol: Zn

Atomic mass: 65.409

Density: 7.133

Popular Posts

Categories

AI (31) Android (24) AngularJS (1) Assembly Language (2) aws (17) Azure (7) BI (10) book (4) Books (146) C (77) C# (12) C++ (82) Course (67) Coursera (198) Cybersecurity (24) data management (11) Data Science (106) Data Strucures (8) Deep Learning (13) Django (14) Downloads (3) edx (2) Engineering (14) Excel (13) Factorial (1) Finance (6) flask (3) flutter (1) FPL (17) Google (20) Hadoop (3) HTML&CSS (47) IBM (25) IoT (1) IS (25) Java (93) Leet Code (4) Machine Learning (46) Meta (18) MICHIGAN (5) microsoft (4) Nvidia (1) Pandas (3) PHP (20) Projects (29) Python (885) Python Coding Challenge (284) Questions (2) R (70) React (6) Scripting (1) security (3) Selenium Webdriver (2) Software (17) SQL (42) UX Research (1) web application (8)

Followers

Person climbing a staircase. Learn Data Science from Scratch: online program with 21 courses