पायथन का परिचय
Python is a high-level, interpreted programming language.
It emphasizes code readability and supports multiple programming paradigms.
It is used in web development, automation, data science, and more.
Example:
print("Welcome to Python!")
print(type(10)) # Output: <class 'int'>
वेरिएबल्स और डेटा प्रकार
Variables are containers for storing data values.
Python has dynamic typing: you don't need to declare types explicitly.
Data types include:
- int
- float
- str
- bool
Example:
age = 20
price = 99.99
name = "Alice"
is_student = True
print(type(name), type(price))
ऑपरेटर
Python supports the following operators:
- Arithmetic: +, -, *, /, %, **, //
- Comparison: ==, !=, >, <, >=, <=
- Logical: and, or, not
- Assignment: =, +=, -=, *=
Example:
a = 10
b=3
print(a + b, a % b)
print(a > b and b < 5)
नियंत्रण कथन
Python uses if-elif-else for conditional branching.
The blocks are defined by indentation.
Example:
marks = 75
if marks >= 90:
print("Grade A")
elif marks >= 60:
print("Grade B")
else:
print("Grade C")
लूप्स
Loops are used to repeat a block of code.
- for loop: iterate over a sequence
- while loop: continue as long as a condition is true
Example:
for i in range(1, 6):
print(i)
i=1
while i <= 5:
print(i)
i += 1
फ़ंक्शन
Functions allow code reuse and better organization.
Use def to define a function and return to return a value.
Example:
def greet(name):
return "Hello, " + name
print(greet("John"))
def square(n):
return n * n
print(square(4))
डेटा स्ट्रक्चर
Python includes built-in data structures:
- List: ordered, mutable
- Tuple: ordered, immutable
- Set: unordered, unique items
- Dictionary: key-value pairs
Example:
fruits = ["apple", "banana"]
colors = ("red", "blue")
nums = {1, 2, 3}
info = {"name": "John"}
print(fruits[0], info["name"])
स्ट्रिंग्स और मेथड्स
Strings are sequences of characters.
Common methods: .upper(), .lower(), .replace(), .split()
Example:
text = "hello python"
print(text.upper())
print(text.replace("python", "world"))
फ़ाइल हैंडलिंग
Python can read and write files using open().
Modes: 'r' (read), 'w' (write), 'a' (append)
Example:
f = open("demo.txt", "w")
f.write("This is Python")
f.close()
f = open("demo.txt", "r")
print(f.read())
f.close()
पायथन में OOP
Object-Oriented Programming uses classes and objects.
Classes define a blueprint; objects are instances.
Example:
class Student:
def __init__(self, name):
self.name = name
def greet(self):
print("Hello,", self.name)
s = Student("Alice")
s.greet()
मॉड्यूल्स और पैकेजेस
Modules are Python files with functions/classes.
Packages are directories with __init__.py containing modules.
Example:
import math
print(math.sqrt(25))
# Custom module
# utils.py:
def add(x, y): return x + y
अपवाद प्रबंधन
Use try-except to handle runtime errors.
Optional: else for no-exception block, finally to run always.
Example:
try:
x = int(input("Enter a number: "))
print(10 / x)
except ZeroDivisionError:
print("Cannot divide by zero")
except ValueError:
print("Invalid input")
नमूना प्रोग्राम
1. Swap Two Numbers:
a, b = 5, 10
a, b = b, a
print(a, b)
2. Check Even or Odd:
num = 7
if num % 2 == 0:
print("Even")
else:
print("Odd")
3. Factorial using loop:
n=5
fact = 1
for i in range(1, n+1):
fact *= i
print("Factorial:", fact)