While Loop: Repeats a block of code while a condition is true.
x=0whilex<5:print(x)x+=1
Loop Control Statements:
break → Exits the loop early.
continue → Skips the current iteration.
pass → Placeholder for future code.
5. Functions in Python
Functions help reuse code and improve modularity.
Example:
defgreet(name):return"Hello, "+nameprint(greet("John"))# Output: Hello, John
Lambda (Anonymous) Functions:
square=lambdax:x*xprint(square(4))# Output: 16
6. Object-Oriented Programming (OOP) in Python
Key OOP Concepts:
Class & Object:
classPerson:def__init__(self,name,age):self.name=nameself.age=agedefgreet(self):returnf"Hello, my name is {self.name}"p1=Person("Alice",25)print(p1.greet())# Output: Hello, my name is Alice
Encapsulation: Hiding data inside classes.
Inheritance: Reusing features of a parent class.
Polymorphism: Using a single method name for different types.
7. Exception Handling
Used to handle runtime errors.
Example:
try:x=10/0exceptZeroDivisionError:print("Cannot divide by zero")finally:print("Execution complete")
Used to modify functions without changing their code.
Example:
defdecorator_function(original_function):defwrapper_function():print("Wrapper executed before",original_function.__name__)returnoriginal_function()returnwrapper_function@decorator_functiondefdisplay():print("Display function executed")display()
11. Python’s map(), filter(), and reduce() Functions
Using map():
Applies a function to all elements of an iterable.
Ask anything about your entire project, code and get answers and even architecture diagrams. Built to handle large projects, Amazon Q Developer works alongside you from idea to production code.
Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.
Explore a trove of insights in this engaging article, celebrated within our welcoming DEV Community. Developers from every background are invited to join and enhance our shared wisdom.
A genuine "thank you" can truly uplift someone’s day. Feel free to express your gratitude in the comments below!
On DEV, our collective exchange of knowledge lightens the road ahead and strengthens our community bonds. Found something valuable here? A small thank you to the author can make a big difference.
Top comments (0)