Python List MCQ

Python List MCQ Quiz (Random 10 from 50)

⏳ Time Remaining: 60 seconds

🔄 Refresh the page to get a new set of questions.

Python String MCQ

Python String MCQ Quiz (Random 10 from 50)

Python Loop MCQ

Python Loops MCQ Quiz (Random 10 from 50)

Python If Conditions MCQ

Python IF Conditions MCQ Quiz (Random 10 from 50)

Python Datatypes MCQ

Python Datatypes MCQ Quiz (Random 10 Questions)

Python Variables MCQ

Python Variables MCQ Quiz (Random 10 of 50 Questions)

Python Polymorphism

Polymorphism Definition

Polymorphism means same function name, different behaviors, depending on class or input.

Two types in Python:

TypeMeaning
Compile-time (Overloading)Same function, different parameters (simulated in Python)
Runtime (Overriding)Child class modifies parent method

Each object responds differently to the same speak() call.


Python doesn’t support true overloading, so we use default parameters.


Same function → different behavior based on object type.


Child classes provide unique logic for inherited structure.


Real-World Example (Combined)

Explanation

  • Admin & Customer override the login() method.
  • Same method name → different behaviors based on object type.

Summary

FeatureWhat It DoesExample
InheritanceReuse parent class featuresclass B(A)
Multiple InheritanceChild from multiple parentsclass C(A,B)
PolymorphismSame method, different behaviorOverriding
OverridingModify parent methodRedefine in child
OverloadingSame name, different argumentsDefault parameters

Python Inheritance

Inheritance Definition

Inheritance allows a class (child) to acquire properties and methods of another class (parent).

This promotes:

  • Code reusability
  • Hierarchy / structure
  • Cleaner and scalable programs

Explanation

  • Animal is the parent/base class.
  • Dog is the child/derived class.
  • Dog gets the speak() method without writing it again.

📝 Child gets access to both:
show() (from Parent)
display() (its own)


📝 Chain of inheritance: A → B → C
Class C inherits everything.


📝 Child receives methods of both parents.


📝 One parentMultiple children.


📝 super() is used to call parent methods/constructors.

Python Generators

Python Generators Introduction

A Generator in Python is a special type of function that yields values one at a time, instead of returning them all at once.

Generators are memory-efficient and used for working with large datasets, streams, or infinite sequences.


Why Generators?

FeatureWhy it matters
Lazy evaluationValues are produced on demand, not all at once
Memory efficientDoesn’t store entire data in memory
Improves performanceFaster for large datasets / loops
Infinite sequencesPossible to generate endless values

Regular Function vs Generator

Normal Function

Generator Function

The keyword yield makes it a generator.


How to Use a Generator


Generator with Loop


Difference Between return and yield

KeywordBehavior
returnExits function and sends a single value
yieldPauses function, saves state, returns value, resumes on next call

Generator Expression

List comprehension

Generator expression


Practical Examples





Python Decorators

Decorator Definition

A decorator in Python is a function that modifies the behavior of another function, without changing its code.

Think of a decorator as adding a layer of functionality before and/or after the original function runs.


Why Use Decorators?

PurposeExample Use Case
Add functionality without editing original codeLogging, Authorization
Reuse common logicValidation
Track performanceExecution time calculator
Restrict accessAdmin/user roles

Basic Decorator Structure


Applying a Decorator

The @decorator_function applies extra behavior to the display() function.


Basic Example

Output:


Decorator With Arguments


Decorator Returning a Value


Real-World Example – Logging



Authentication / Access Control


Decorators with Parameters (Advanced)

Sometimes we need the decorator to accept its own argument:

Output


Nesting Multiple Decorators