Python For Beginners - In Hindi (Free)

Why take this course?
Let's go through each of the topics you've listed, providing a brief explanation and example where applicable.
Positional & Keyword Argument
Arguments passed to functions can be categorized as positional (required order) or keyword (named parameters).
def greet(name, message):
print(f"{message}, {name}!")
greet(name="Alice", message="Hello") # Keyword arguments
Setting Default Values
You can set default values for function parameters.
def greet(name, message="Hi"):
print(f"{message}, {name}!")
greet("Bob") # Uses default value for 'message'
Passing Arbitrary Arguments
You can accept an arbitrary number of positional or keyword arguments using *args
and **kwargs
.
def sum_numbers(*numbers):
return sum(numbers)
print(sum_numbers(1, 2, 3)) # Positional argument
print(sum_numbers(a=1, b=2)) # Keyword arguments
Passing Arbitrary Keyword Arguments
Similar to *args
and **kwargs
, you can capture all passed keyword arguments.
def configure(**settings):
for key, value in settings.items():
print(f"Setting {key} to {value}")
configure(color="blue", size=10)
Memory Diagram
Memory in Python is managed by the interpreter, and objects are stored in memory with references back to the program. The memory diagram visualizes the object's lifetime and where it's stored.
class MyClass:
pass
obj = MyClass() # Object 'obj' created, lives here
del obj # Object 'obj' is deleted, its memory is reclaimed
Lambda Expression
A lambda function is a small anonymous function. It can take any number of arguments, but can only have one expression.
sum_lambda = lambda x, y: x + y
print(sum_lambda(5, 3))
Lambda Expression with if-else
You can also include an if-elif-else
block in a lambda function.
price = 100
discount_lambda = lambda price: (price * 0.1) if price > 100 else 5
print(discount_lambda(price))
Advanced Built-in Functions
Python has many built-in functions, here are some commonly used ones with enumerate
and map
.
# enumerate provides a counter for iterating over another iterator
for index, value in enumerate(['a', 'b', 'c']):
print(index, value)
# map takes a function and applies it to each item of an iterable from the second argument
square = lambda x: x ** 2
print(list(map(square, [1, 2, 3])))
Exception Handling
Python uses try
, except
, else
, and finally
to handle exceptions.
try:
result = 10 / 0
except ZeroDivisionError as e:
print("Cannot divide by zero", e)
finally:
print("Execution continues here.")
File Handling
You can read and write to files in Python.
# Writing to a file
with open('data.txt', 'w') as file:
file.write("Hello, World!\nThis is a test.")
# Reading from a file
with open('data.txt', 'r') as file:
data = file.read()
print(data)
Encapsulation
Encapsulation is the bundling of data with the methods that operate on that data. It restricts direct access to some of an object's components, which is a means of preventing accidental interference and misuse of the data.
class Person:
def __init__(self, name, age):
self.__name = name # Private attribute
self.__age = age # Private attribute
def get_name(self):
return self.__name
def get_age(self):
return self.__age
def set_age(self, new_age):
if new_age > 0 and new_age < 150:
self.__age = new_age
else:
raise ValueError("Age must be between 1 and 150")
Exception
Exceptions are errors that occur during execution. You can define custom exceptions for better error handling in your code.
class CustomError(Exception):
pass
def risky_operation():
if some_risky_condition:
raise CustomError("An error occurred")
# Rest of the operations
try:
risky_operation()
except CustomError as e:
print(e)
Operator Overloading
In Python, you can override the special methods (often starting and ending with double underscores) to define the behavior of operators for your custom objects.
class Point:
def __init__(self, x, y):
self.x = x
self.y = y
def __add__(self, other):
return Point(self.x + other.x, self.y + other.y)
p1 = Point(1, 2)
p2 = Point(3, 4)
result = p1 + p2 # Uses the overloaded '+' operator
str
and repr
methods (Dunder Methods)
The __str__
method defines the printable string representation of an object, while __repr__
should give a concise but accurate Python-codable representation.
class Point:
def __init__(self, x, y):
self.x = x
self.y = y
def __str__(self):
return f"Point({self.x}, {self.y})"
def __repr__(self):
return f"{self.__class__.__name__}({self.x!r}, {self.y!r})"
point = Point(1, 2)
print(point) # Uses __str__
print(repr(point)) # Uses __repr__
Custom Exception
You can define your own exceptions to handle specific error conditions in your code.
class ArrayIndexError(IndexError):
pass
def get_element_at_index(array, index):
if not isinstance(index, int) or index < 0:
raise ArrayIndexError(f"Index must be an integer and non-negative. Got {index}")
return array[index]
File Handling
Python provides built-in functions to work with files, including reading and writing data, and handling different file types like text, csv, binary, etc.
# Writing to a CSV file
with open('data.csv', 'w', newline='') as csvfile:
writer = csv.writer(csvfile)
for row in [['Name', 'Age'], ['Alice', 25], ['Bob', 30]]:
writer.writerow(row)
These are the fundamental concepts and practices you should be familiar with when working with Python. Each of these topics is broad, and there's much more to explore within each area.
Loading charts...