1. Function
- Explanation: A function is a block of reusable code that performs a specific task. Functions can take inputs (called arguments) and return outputs.
def greet(name): return "Hello, " + name
2. Class
- Explanation: A class is a blueprint for creating objects (instances). It encapsulates data (attributes) and functions (methods) that operate on the data.
class Dog: def __init__(self, name): self.name = name def bark(self): return "Woof!"
3. Object
- Explanation: An object is an instance of a class. It contains both data and methods that operate on the data.
my_dog = Dog("Buddy") # my_dog is an object of the Dog class.
4. Loop
- Explanation: Loops are used to repeatedly execute a block of code as long as a condition is true. Common loops in Python are
for
andwhile
.
for i in range(5): print(i) # Prints numbers from 0 to 4.
5. Conditional Statement
- Explanation: Conditional statements allow you to execute certain blocks of code based on whether a condition is true or false. The
if
,elif
, andelse
keywords are used for this purpose.
if age >= 18: print("Adult") else: print("Minor")
No comments:
Post a Comment