इस chapter में हम Python language की मूल बातें सीखेंगे जैसे — Python Interpreter, Comments, Literals, Constants, Built-in Data Types, Numbers, Strings, Input/Output और कुछ Simple Programs।
Python एक high-level, interpreted, general-purpose programming language है जिसे Guido van Rossum ने 1991 में develop किया था। इसका मुख्य उद्देश्य programming को simple, readable और easy बनाना है।
Python का syntax बहुत ही साफ और आसान होता है, जो काफी हद तक English language जैसा लगता है। इसी कारण यह beginners के लिए सबसे popular programming language मानी जाती है।
print("Hello World")
👉 यह program screen पर Hello World print करता है।
यह Python का सबसे basic और पहला program माना जाता है।
Python एक powerful और versatile programming language है जिसमें कई important features होते हैं जो इसे beginners और professionals दोनों के लिए उपयुक्त बनाते हैं।
x = 10
x = "Hello"
👉 यहाँ variable x पहले integer value store कर रहा है और बाद में string,
बिना किसी type declaration के।
Programming languages को execution के आधार पर दो भागों में बाँटा जाता है: Interpreted Languages और Compiled Languages।
Programming languages में variables के data type handle करने के दो तरीके होते हैं: Dynamic Typing और Static Typing।
x = 10
x = "Hello"
High-Level Programming Language ऐसी language होती है जो human language के करीब होती है और समझने में आसान होती है।
print("Hello")
👉 यह simple statement high-level language का उदाहरण है।
Keywords वे reserved words होते हैं जिनका Python में पहले से ही special meaning होता है। इन्हें हम variable या identifier के रूप में use नहीं कर सकते।
False True None and or not
if else elif for while
break continue pass
def return class import
Identifier वह नाम होता है जो हम variables, functions, classes आदि को देते हैं।
total_marks)my_variable_namePI = 3.14
name = "Rahul" # valid
_age = 20 # valid
2num = 10 # invalid
class = 5 # invalid (keyword)
Tokens Python program के सबसे छोटे unit होते हैं। यानी program छोटे-छोटे parts में divide होता है जिन्हें tokens कहते हैं।
Reserved words जिनका special meaning होता है (जैसे: if, else, for)
Variables या functions के नाम (जैसे: name, total_marks)
Fixed values जो directly code में लिखी जाती हैं
x = 10 # integer literal
name = "Ram" # string literal
Operations perform करने के लिए use होते हैं
a = 5 + 3 # + is operator
Special symbols जो structure define करते हैं
(), {}, [], :, ;, ,
Python Program Structure से मतलब है कि एक program को किस तरह लिखा और organize किया जाता है। Python में syntax simple होता है और indentation बहुत important role निभाता है।
# simple python program
name = input("Enter your name: ")
if name:
print("Hello", name)
👉 यहाँ indentation (4 spaces) block को define कर रहा है।
Memory Management का मतलब है कि Python program के दौरान memory को कैसे allocate (देना) और deallocate (हटाना) किया जाता है।
x = 10
x = "Hello"
👉 यहाँ Python automatically नई memory allocate करता है।
Garbage Collection वह process है जिसमें Python उन objects की memory को free कर देता है जो अब use में नहीं हैं।
x = 10
x = None
👉 पहले object का reference था, बाद में हट गया → memory free हो सकती है।
Variable एक नाम होता है जो किसी value को store करने के लिए use किया जाता है। लेकिन Python में variable value को directly store नहीं करता, बल्कि वह object (value) का reference hold करता है।
a = 10
b = a
👉 यहाँ a और b दोनों same object (10) को refer कर रहे हैं।
x = 5
👉 पहले 5 object create होता है, फिर x उसे refer करता है।
x = 10
x = "Hello"
👉 यहाँ x पहले integer था, फिर string बन गया।
Python में हम एक साथ कई variables को value assign कर सकते हैं, इसे multiple assignment कहते हैं।
a, b, c = 1, 2, 3
👉 a = 1, b = 2, c = 3 assign हो जाता है।
x = y = z = 5
👉 तीनों variables same value को refer करते हैं।
Swapping का मतलब है दो variables की value को आपस में बदलना।
a = 10
b = 20
temp = a
a = b
b = temp
a = 10
b = 20
a, b = b, a
👉 Python में swapping बिना extra variable के हो जाती है।
Constant ऐसी value होती है जो program के दौरान change नहीं होती। जैसे: PI, GRAVITY आदि।
PI = 3.14
GRAVITY = 9.8
👉 ये constant माने जाते हैं (by convention)
Data Type यह बताता है कि variable में किस प्रकार की value store है और उस value पर कौन-कौन से operations perform किए जा सकते हैं।
x = 10 # int
y = 3.5 # float
z = "Hello" # string
flag = True # boolean
Python में numeric data types का उपयोग numbers को represent करने के लिए होता है।
a = 10
b = -5
x = 3.14
y = 1.2e3 # 1200.0
z = 2 + 3j
Python में integer को अलग-अलग number systems में represent किया जा सकता है।
x = 0b1010 # 10
x = 0o12 # 10
x = 0xA # 10
Floating point numbers exact precision में store नहीं होते क्योंकि computer binary system का उपयोग करता है।
print(0.1 + 0.2)
👉 Output: 0.30000000000000004
Boolean data type केवल दो values रखता है: True और False।
x = 10
print(x > 5) # True
String characters का sequence होता है, जिसे quotes (' ' या " ") में लिखा जाता है।
name = "Python"
print(name[0]) # P
print(name[1:4]) # yth
NoneType एक special data type है जो no value को represent करता है।
x = None
Python में data types को दो categories में divide किया जाता है: Mutable और Immutable।
Immutable का मतलब है — जिस object की value create होने के बाद change नहीं की जा सकती। अगर आप value बदलने की कोशिश करते हैं, तो Python नया object बना देता है।
x = 10
print(id(x))
x = x + 5
print(id(x))
👉 Explanation:
Mutable का मतलब है — जिस object की value directly modify की जा सकती है। इसमें नया object नहीं बनता, existing object ही change होता है।
x = [1, 2, 3]
print(id(x))
x.append(4)
print(id(x))
👉 Explanation:
Python में variable का data type check करने के लिए दो main functions होते हैं: type() और isinstance()।
type() function किसी variable का exact data type return करता है।
x = 10
print(type(x))
👉 Output: <class 'int'>
isinstance()
x = 10
print(isinstance(x, int))
👉 Output: True
Type Conversion का मतलब है एक data type को दूसरे data type में बदलना।
जब Python खुद automatically data type change कर देता है, उसे implicit conversion कहते हैं।
x = 10
y = 2.5
result = x + y
print(result)
print(type(result))
👉 Explanation:
जब programmer खुद type change करता है, उसे explicit conversion कहते हैं।
x = "10"
y = int(x)
print(y)
print(type(y))
👉 Explanation:
int("abc") # Error
Python में numbers को represent करने के लिए mainly int, float और complex data types का उपयोग होता है। ये mathematical operations के लिए use होते हैं।
a = 10
b = 3
print(a + b) # 13
print(a / b) # 3.33
print(a // b) # 3
print(a % b) # 1
print(a ** b) # 1000
print(abs(-5)) # 5
print(round(3.6)) # 4
print(pow(2,3)) # 8
String characters का sequence होता है जो quotes में लिखा जाता है। यह Python में सबसे ज्यादा use होने वाला data type है।
name = "Python"
हर character का index होता है (0 से start)।
name = "Python"
print(name[0]) # P
print(name[1:4]) # yth
a = "Hello"
b = "World"
print(a + b) # HelloWorld
print(a * 2) # HelloHello
text = " python "
print(text.strip()) # python
print(text.upper()) # PYTHON
Python में user से input लेने के लिए input() function का use होता है। यह हमेशा string return करता है।
name = input("Enter your name: ")
age = int(input("Enter age: "))
👉 input string होता है, इसलिए int() से convert करना जरूरी है।
Output दिखाने के लिए print() function का use होता है।
print("Hello World")
name = "Rahul"
print(f"My name is {name}")
Python में program लिखना बहुत आसान होता है। हर program step-by-step instructions का combination होता है।
a = int(input("Enter first number: "))
b = int(input("Enter second number: "))
sum = a + b
print("Sum =", sum)
👉 Explanation:
num = int(input("Enter number: "))
if num % 2 == 0:
print("Even")
else:
print("Odd")
👉 Explanation: