Complete study material – click a topic below to jump to its notes
What is Python? Python is a high-level, interpreted, general-purpose programming language created by Guido van Rossum and first released in 1991. It emphasizes code readability with its notable use of significant indentation. Python supports multiple programming paradigms, including procedural, object-oriented, and functional programming.
Key Features:
Basic Structure of a Python Program: Python programs are just text files with a .py extension. They are executed line by line by the Python interpreter.
पाइथन क्या है? पाइथन एक उच्च-स्तरीय, इंटरप्रेटेड, सामान्य प्रयोग वाली प्रोग्रामिंग भाषा है। इसे 1991 में गुइडो वैन रोसुम ने बनाया था। यह कोड को पढ़ने में आसान बनाने के लिए इंडेंटेशन (खाली स्थान) का उपयोग करती है।
मुख्य विशेषताएँ:
पाइथन प्रोग्राम की संरचना: पाइथन प्रोग्राम .py फाइल में लिखे जाते हैं। इंटरप्रेटर इसे लाइन-दर-लाइन चलाता है।
# My first Python program
print("Hello, World!")
Explanation: Python doesn't need a main function; execution starts from the first line. print() is a built-in function that outputs text. Comments start with #.
व्याख्या: पाइथन में main() की जरूरत नहीं होती; प्रोग्राम पहली लाइन से शुरू होता है। print() एक बिल्ट-इन फंक्शन है जो टेक्स्ट प्रिंट करता है। कमेंट # से शुरू होते हैं।
Key syntax rules:
#. Multi-line strings can be used as comments but are not technically comments.name and Name are different).Identifiers (names): Must start with a letter or underscore, followed by letters, digits, or underscores. Cannot use keywords (like if, for, while).
महत्वपूर्ण सिंटैक्स नियम:
# का इस्तेमाल करते हैं।age और Age अलग-अलग हैं।आइडेंटिफायर (नाम): नाम अक्षर या अंडरस्कोर से शुरू होना चाहिए, उसके बाद अक्षर, अंक या अंडरस्कोर हो सकते हैं। कीवर्ड (जैसे if, else) का इस्तेमाल नाम के रूप में नहीं कर सकते।
# Program to show syntax
age = 25 # variable assignment
if age >= 18: # colon indicates start of block
print("Adult") # indented block
print("You can vote")
print("Always executed") # outside block
Explanation: Notice the colon : after the if condition, and the consistent indentation (4 spaces) inside the if block.
व्याख्या: if के बाद कोलन : और ब्लॉक के अंदर एक समान इंडेंटेशन (4 स्पेस) का ध्यान दें।
Python has several built-in data types. The main ones are:
int (integers), float (decimals), complex.str (string), list, tuple, range.dict (dictionary).set, frozenset.bool (True / False).NoneType (represents absence of value).Python is dynamically typed – you don't need to declare the type; it is inferred at runtime.
पाइथन में कई बिल्ट-इन डेटा टाइप होते हैं। मुख्य इस प्रकार हैं:
int (पूर्णांक – 10, -3), float (दशमलव – 3.14, -0.5), complex (जटिल संख्या)।str (स्ट्रिंग – टेक्स्ट), list (सूची), tuple (टपल), range (श्रेणी)।dict (डिक्शनरी – की-वैल्यू जोड़े)।set, frozenset (अद्वितीय तत्वों का संग्रह)।bool (True / False – सही/गलत)।NoneType (जब कुछ भी वैल्यू न हो)।पाइथन डायनामिकली टाइप्ड है – वेरिएबल का टाइप पहले से बताने की जरूरत नहीं; वह रनटाइम पर अपने आप समझ लिया जाता है।
# Data type examples
x = 10 # int
y = 3.14 # float
name = "HITCOM" # str
is_active = True # bool
colors = ["red","green","blue"] # list
coordinates = (10,20) # tuple
person = {"name":"Alice","age":25} # dict
print(type(x)) # <class 'int'>
print(type(colors)) # <class 'list'>
Explanation: Use type() to check the data type of a variable.
व्याख्या: type() फंक्शन से किसी वेरिएबल का डेटा टाइप देख सकते हैं।
Variables: In Python, variables are created the moment you assign a value. No explicit declaration needed. Variable names are case‑sensitive and must follow identifier rules.
Constants: Python doesn't have true constants, but by convention, names in ALL_CAPS are treated as constants (should not be changed).
Multiple assignment: You can assign values to multiple variables in one line.
वेरिएबल: पाइथन में जैसे ही आप किसी नाम को वैल्यू देते हैं, वह वेरिएबल बन जाता है। पहले से डिक्लेयर करने की जरूरत नहीं। नाम केस-सेंसिटिव होते हैं।
कॉन्स्टैंट्स: पाइथन में स्थिरांक (जिन्हें बदलना न हो) के लिए कोई विशेष नियम नहीं, लेकिन परंपरा अनुसार नाम को पूरे बड़े अक्षरों में लिखते हैं (जैसे PI = 3.14) और उसे बदलने से बचते हैं।
एक साथ कई वेरिएबल असाइन करना: x, y, z = 10, 20, 30 – इस तरह एक लाइन में कई वेरिएबल को वैल्यू दे सकते हैं।
# Variables student_name = "Raj" age = 21 age = 22 # reassign # Multiple assignment x, y, z = 10, 20, 30 # Constant convention PI = 3.14159 MAX_SIZE = 100
Output: print() prints values to the console. You can print multiple items separated by commas.
Input: input(prompt) reads a line from the user, always returns a string. Convert to other types using int(), float(), etc.
आउटपुट: print() का इस्तेमाल स्क्रीन पर कुछ छापने के लिए करते हैं। एक से ज्यादा चीजें कॉमा से अलग करके छाप सकते हैं।
इनपुट: input() यूजर से इनपुट लेता है। यह हमेशा एक स्ट्रिंग (टेक्स्ट) लौटाता है। अगर नंबर चाहिए तो int() या float() से बदलना पड़ता है।
name = input("Enter your name: ")
age = int(input("Enter your age: "))
print("Hello", name, "you are", age, "years old.")
Python supports various operators:
पाइथन में कई प्रकार के ऑपरेटर होते हैं:
a, b = 15, 4
print(a + b) # 19
print(a / b) # 3.75
print(a // b) # 3 (floor division)
print(a ** b) # 50625 (15^4)
# Logical
print(a > 10 and b < 5) # True
# Membership
fruits = ["apple", "banana"]
print("apple" in fruits) # True
if, elif, else: Python uses if, elif (else if), and else for decision making. Blocks are defined by indentation.
if, elif, else: निर्णय लेने के लिए। अगर शर्त सही है तो एक ब्लॉक चलता है, नहीं तो दूसरा। elif का इस्तेमाल कई शर्तों के लिए करते हैं। इंडेंटेशन से ब्लॉक बनते हैं।
num = int(input("Enter a number: "))
if num > 0:
print("Positive")
elif num == 0:
print("Zero")
else:
print("Negative")
while loop: Repeats while a condition is true.
for loop: Used to iterate over sequences (list, string, range, etc.).
break exits loop; continue skips to next iteration.
while लूप: जब तक शर्त सही है, ब्लॉक को बार-बार चलाता है।
for लूप: किसी सूची, स्ट्रिंग या रेंज के सभी आइटम पर एक-एक करके चलता है।
break लूप को तुरंत खत्म कर देता है; continue मौजूदा इटरेशन छोड़कर अगले पर चला जाता है।
# while
i = 1
while i <= 5:
print(i)
i += 1
# for
for i in range(1,6):
print(i)
fruits = ["apple","banana","cherry"]
for f in fruits:
print(f)
Strings are sequences of characters, immutable. Many built-in methods: len(), upper(), lower(), strip(), split(), join(), find(), etc.
स्ट्रिंग अक्षरों का क्रम होती है। इसे बदला नहीं जा सकता (immutable)। कई उपयोगी मेथड्स हैं: len() – लंबाई, upper() – बड़े अक्षर, lower() – छोटे अक्षर, strip() – आगे-पीछे के स्पेस हटाना, split() – टुकड़ों में बाँटना, join() – जोड़ना, find() – ढूँढ़ना।
s = " Hello, Python! "
print(s.strip()) # remove spaces
print(s.lower())
print(s.replace("Python","World"))
words = s.split(",") # split into list
print(words)
Lists are ordered, mutable collections. Can hold mixed types. Common methods: append(), insert(), remove(), pop(), sort(), reverse(), slicing.
लिस्ट एक क्रमबद्ध सूची होती है जिसे बदला जा सकता है (mutable)। इसमें किसी भी टाइप के आइटम रख सकते हैं। मुख्य मेथड: append() – अंत में जोड़ना, insert() – बीच में डालना, remove() – हटाना, pop() – अंतिम हटाना, sort() – क्रम में लगाना, reverse() – उल्टा करना। स्लाइसिंग [start:end] से हिस्सा निकाल सकते हैं।
nums = [10,20,30,40] nums.append(50) nums.insert(2,25) print(nums[1:4]) # slicing nums.sort() print(nums)
Tuples are ordered, immutable sequences. Useful for fixed data. Defined with parentheses.
टपल भी लिस्ट की तरह होता है, लेकिन इसे बदला नहीं जा सकता (immutable)। इसे () में लिखते हैं। यह उन आइटम्स के लिए उपयोगी है जो नहीं बदलने वाले हों।
point = (10,20) print(point[0]) # point[0] = 5 # Error, immutable
Dictionaries store key-value pairs. Mutable. Keys must be immutable (strings, numbers, tuples).
डिक्शनरी में की (key) और वैल्यू (value) के जोड़े होते हैं। की के जरिए वैल्यू ढूँढ़ी जाती है। डिक्शनरी को बदला जा सकता है। की हमेशा अपरिवर्तनशील (immutable) होनी चाहिए, जैसे स्ट्रिंग, नंबर, या टपल।
student = {"name":"Anjali", "age":22, "course":"B.Tech"}
print(student["name"])
student["grade"] = "A"
for key,val in student.items():
print(key, val)
Sets are unordered collections of unique elements. Support mathematical operations like union, intersection, difference.
सेट अद्वितीय (unique) तत्वों का अनियंत्रित (unordered) संग्रह है। इसमें एक ही मान दो बार नहीं हो सकता। गणित के सेट ऑपरेशन जैसे union (मिलान), intersection (समान) आदि कर सकते हैं।
s1 = {1,2,3,3,4}
print(s1) # {1,2,3,4}
s2 = {3,4,5}
print(s1 | s2) # union -> {1,2,3,4,5}
print(s1 & s2) # intersection -> {3,4}
Functions are defined with def. They can take parameters and return values. Python supports default arguments, keyword arguments, and variable-length arguments (*args, **kwargs).
फंक्शन को def कीवर्ड से बनाते हैं। यह पैरामीटर ले सकता है और वैल्यू लौटा सकता है। डिफॉल्ट पैरामीटर भी दे सकते हैं। return से वैल्यू लौटाते हैं।
def add(a,b):
return a + b
def greet(name="Guest"):
print(f"Hello, {name}!")
result = add(5,3)
print(result)
greet("Amit")
Use open() to open a file. Modes: 'r' read, 'w' write (overwrite), 'a' append, 'r+' read/write. Always close the file or use with block for automatic closing.
open() से फ़ाइल खोलते हैं। मोड: 'r' पढ़ने के लिए, 'w' लिखने के लिए (पुराना डेटा मिट जाता है), 'a' जोड़ने के लिए। फ़ाइल को बंद करना जरूरी है, with ब्लॉक अपने आप बंद कर देता है।
# Writing
with open("sample.txt","w") as f:
f.write("Hello, file!\nSecond line")
# Reading
with open("sample.txt","r") as f:
content = f.read()
print(content)
# Append
with open("sample.txt","a") as f:
f.write("\nAppended line")