⚠ यह कार्रवाई अनुमति नहीं है!
🐍 NIELIT O Level · M3-R5 Python · Hindi Notes

Chapter 1: Introduction to Programming — Languages, Algorithms, Flowcharts व Python की शुरुआत

M3-R5 (Programming and Problem Solving through Python) का foundation chapter — यहाँ आप सीखेंगे programming languages की 5 generations, Compiler vs Interpreter, Model of Computation, Algorithms, Flowcharts, Compilation Process, Testing, Debugging व Documentation — 30+ Python examples, output-based questions व practice programs के साथ, पूरी आसान हिंदी में।

📑 12 Sections 💻 30+ Python Examples 📋 Copy Button ❓ 50 MCQs + Output Qs 🗓 Updated 2026
👉 सभी chapters देखने के लिए swipe करें
📑 Table of Contents — किसी भी topic पर सीधे जाएँ
1.0

Programming Languages — Computer से बात करने का माध्यम

Programming Language = वह माध्यम जिसके द्वारा हम computer को निर्देश (instructions) देते हैं। जैसे मनुष्य आपस में हिंदी/English में बात करते हैं, वैसे computer से बात करने के लिए Python, C, Java जैसी languages चाहिए। Definition: "A programming language is a formal language comprising a set of instructions that produce various kinds of output."

🤔 Programming Language की ज़रूरत क्यों?

  • Computer केवल binary language (0 और 1) समझता है — इसे machine language कहते हैं।
  • मनुष्य के लिए binary में लिखना लगभग असंभव है (कल्पना कीजिए — "Hello" print करने के लिए हज़ारों 0/1!)।
  • Programming languages instructions को human-readable (English जैसा) बनाती हैं।
  • फिर Compiler या Interpreter उन्हें machine code में translate कर देता है।

🧬 Languages की 5 Generations (Evolution)

Machine Language से AI Languages तक का सफ़र

1GL — Machine Language (0s & 1s) 10110100 00001111 — CPU सीधे समझता है, इंसान के लिए कठिन 2GL — Assembly Language (Mnemonics) MOV, ADD, SUB — Assembler से translate होती है 3GL — High-Level Languages (English जैसी) Python, C, C++, Java, BASIC — Compiler/Interpreter से translate 4GL — Fourth Generation (काम बताओ, तरीका नहीं) SQL, MATLAB, R — programming effort बहुत कम 5GL — Fifth Generation (AI & Logic-based) Prolog, Mercury, AI frameworks — Machine Learning व Expert Systems

1️⃣ Machine Language (1st Generation)

सबसे नीचले स्तर (lowest level) की भाषा — instructions केवल 0 और 1 में। यह hardware द्वारा सीधे समझी जाती है, कोई translation नहीं चाहिए।

Machine Language Example▶ Run
10110100 00001111
// yah kisi CPU instruction ka binary roop hai
// jaise "register me value load karo"
Drawbacks: याद रखना असंभव-सा, debug करना बहुत कठिन, और portable नहीं — हर CPU की machine language अलग होती है।

2️⃣ Assembly Language (2nd Generation)

Machine language की readability बढ़ाने के लिए mnemonics (छोटे English words) आए — जैसे MOV (move), ADD (जोड़ो), SUB (घटाओ)। इसे machine code में बदलने के लिए Assembler चाहिए।

Assembly Example — दो numbers जोड़ना▶ Run
MOV A, 05H    ; Register A me 5 daalo
MOV B, 06H    ; Register B me 6 daalo
ADD A, B      ; A = A + B (yaani 11)

3️⃣ High-Level Languages (3rd Generation)

मनुष्यों के लिए आसान — syntax English जैसा। Compiler/Interpreter translate करता है। Python, C, C++, Java, BASIC इसी category में हैं।

🐍 Python Example — वही जोड़, कितना आसान!▶ Run
a = 5
b = 10
sum = a + b
print("Sum =", sum)
OUTPUTSum = 15
समझें: Assembly की 3 cryptic lines की जगह Python में साफ़-साफ़ पढ़ने लायक code! High-level languages इसीलिए popular हैं — likhna, padhna व debug karna आसान।

4️⃣ Fourth Generation Languages (4GL)

और भी high-level — आप बताते हैं "क्या चाहिए", यह नहीं कि "कैसे करना है"। SQL, MATLAB, R जैसे। उद्देश्य: programming effort कम करना।

SQL Example — 18+ students की list▶ Run
SELECT name, age FROM students WHERE age > 18;
-- humne sirf bataya KYA chahiye,
-- HOW (loop/compare) database khud karega

5️⃣ Fifth Generation Languages (5GL)

Logic-based व AI-oriented languages — Machine Learning, Artificial Intelligence व Expert Systems में उपयोग। उदाहरण: Prolog, Mercury और Python के AI frameworks (TensorFlow आदि)।

🧠 Key Programming Concepts (Exam में पूछे जाते हैं)

Termमतलब
Source CodeProgrammer द्वारा लिखा high-level code (जैसे .py file)
Object CodeMachine-readable translated version
Executable CodeFinal file जो directly run होती है (जैसे .exe)
SyntaxLanguage के grammatical rules (लिखने का सही तरीका)
SemanticsCode का meaning या behavior (क्या करेगा)

🚀 Python ही क्यों? (M3-R5 की language)

  • Simple, readable syntax — beginners के लिए सबसे आसान।
  • Interpreter-based — code लिखो और तुरंत run करो, compile step नहीं।
  • Free, open source व platform-independent — Windows/Mac/Linux सबपर।
  • Huge libraries — Data Science, AI, Web Development सबके लिए ready-made।
  • O Level syllabus के practical implementation के लिए perfect।
Exam Point: Generations का क्रम — Machine (1GL) → Assembly (2GL) → High-Level (3GL) → 4GL → 5GL। Assembly के लिए Assembler, high-level के लिए Compiler/Interpreter चाहिए। Machine language ही एकमात्र language है जिसे CPU बिना translation के समझता है।
1.1

Compiler vs Interpreter — Translation के दो तरीके

दोनों का काम एक — high-level code को machine code में translate करना — लेकिन तरीका अलग। Compiler = पूरी किताब का एक साथ अनुवाद; Interpreter = साथ-साथ चलने वाला दुभाषिया (एक line सुनो → translate करो → बोलो)।

⚖️ Master Comparison Table

FeatureCompilerInterpreter
Translationपूरे program को एक बार में translate करता हैएक-एक line translate व execute करता है
Execution SpeedFast (pre-compiled)Slow (line-by-line)
Error Detectionसभी errors compile time पर एक साथ दिखते हैंपहली error पर execution रुक जाता है
Object Codeबनता है (save होता है)नहीं बनता
Memoryज़्यादा (object code store)कम
Example LanguagesC, C++Python, JavaScript
🐍 Python Interpreter — कोई compile step नहीं▶ Run
print("Hello, World!")
OUTPUTHello, World!
समझें: Python interpreter इस line को पढ़ते ही तुरंत output दे देता है — अलग से compile करके .exe बनाने की ज़रूरत नहीं। C में पहले gcc program.c से compile करना पड़ता, फिर run करते।
Exam Point: C/C++ = Compiled; Python/JavaScript = Interpreted। Compiler सभी errors एक साथ दिखाता है; Interpreter पहली error पर रुक जाता है। Execution speed: Compiler > Interpreter।
1.2

Basic Model of Computation — Computer सोचता कैसे है?

Computation = किसी समस्या को step-by-step हल करना। Model of Computation = वह theoretical framework जो बताता है कि computer/program data को कैसे process करता है। Python जैसी languages इन्हीं models का practical रूप हैं। उपयोग: algorithm की correctness, performance व feasibility परखने में।

📘 Computational Models के 3 Types

  • 1. Mathematical Model: Computation को equations व logic में दर्शाता है।
  • 2. Physical Model: बताता है CPU, Memory व Storage computation को वास्तव में कैसे चलाते हैं।
  • 3. Abstract Model: Logical steps व data flow पर केंद्रित — जैसे Turing MachineRAM Model

1️⃣ Turing Machine — Theory का बादशाह

Alan Turing (father of computer science) द्वारा दिया गया theoretical model। इसके 3 parts:

  • Infinite Tape: जिस पर symbols लिखे जाते हैं (memory की तरह)।
  • Head: जो symbols को पढ़ता और लिखता है।
  • Control Unit: जो next step तय करती है।
🐍 Python Analogy — Turing Machine जैसा sequential execution▶ Run
x = 2        # Step 1: memory me 2 rakha
y = 3        # Step 2: memory me 3 rakha
z = x + y    # Step 3: dono padhe, joda, result rakha
print(z)     # Step 4: result dikhaya
OUTPUT5
समझें: Python interpreter बिल्कुल Turing Machine की तरह — instructions को क्रम से (sequentially) execute करता है और memory में values update करता रहता है।

2️⃣ Finite Automata (FA) — Limited Memory वाला Model

Input symbols को पढ़कर states बदलता है और final state तक पहुँचता है। Pattern matching में उपयोग होता है।

  • DFA (Deterministic FA): हर symbol के लिए केवल एक next state।
  • NFA (Non-Deterministic FA): एक symbol के लिए कई possible states।
🐍 Python — Pattern Checking (FA जैसा logic)▶ Run
s = "10101"
if "101" in s:
    print("Pattern found!")
else:
    print("Pattern not found!")
OUTPUTPattern found!

3️⃣ RAM Model (Random Access Machine) — Practice का Model

Computation का practical रूप — जैसा असली computers में होता है:

  • हर instruction को 1 unit time माना जाता है।
  • Memory access O(1) time में (किसी भी location पर सीधे पहुँच)।
  • Algorithm analysis के लिए सबसे उपयुक्त model।
🐍 Python — Random Access का जादू▶ Run
arr = [10, 20, 30, 40]
arr[2] = arr[2] + 5   # index 2 par SEEDHE pahunche (koi loop nahi)
print(arr)
OUTPUT[10, 20, 35, 40]

⚙️ Computational Model के 5 Components

ComponentकामPython में
InputRaw datainput(), variables
ProcessOperations व logicCalculations, loops
OutputFinal resultprint()
StorageTemporary/permanent memoryVariables, lists
Control UnitExecution order manage करनाInterpreter
🐍 Input → Process → Output — तीनों एक साथ▶ Run
num = int(input("Enter a number: "))   # INPUT
square = num * num                      # PROCESS
print("Square is:", square)             # OUTPUT
OUTPUT (input = 7)Square is: 49
Exam Point: Turing Machine = theory (Alan Turing); RAM Model = practice (memory access O(1)); FA = pattern matching (DFA = 1 next state, NFA = कई)। Python interpreter = real-world RAM model implementation।
1.3

Algorithms — समस्या हल करने के Steps

Algorithm = किसी problem को हल करने के लिए well-defined steps का finite sequence। यह programming से पहले की planning है — जैसे चाय बनाने की recipe: पानी उबालो → पत्ती डालो → दूध-चीनी डालो → छानो → परोसो। हर step clear, क्रम में, और अंत निश्चित!

⭐ Algorithm की 5 Characteristics (बहुत Important)

Characteristicमतलब
1. FinitenessFinite steps के बाद खत्म ज़रूर हो (अनंत नहीं चले)
2. Definitenessहर step बिल्कुल clear व unambiguous हो
3. Input0 या अधिक inputs हों
4. Outputकम से कम 1 output ज़रूर हो
5. Effectivenessहर step इतना simple हो कि practically किया जा सके

📝 Example 1 — दो Numbers का Sum

Algorithm:
Step 1: Start
Step 2: Input A और B
Step 3: Sum = A + B
Step 4: Print Sum
Step 5: Stop
🐍 Python Implementation▶ Run
a = int(input("Enter first number: "))
b = int(input("Enter second number: "))
sum = a + b
print("Sum =", sum)
OUTPUT (inputs: 12, 8)Sum = 20

📝 Example 2 — दो Numbers में बड़ा (Largest of Two)

Algorithm:
Step 1: Start
Step 2: Input A, B
Step 3: यदि A > B है तो Print A
Step 4: वरना Print B
Step 5: Stop
🐍 Python Implementation▶ Run
a = int(input("Enter A: "))
b = int(input("Enter B: "))
if a > b:
    print("Largest =", a)
else:
    print("Largest =", b)
OUTPUT (inputs: 15, 42)Largest = 42

📝 Example 3 — Even या Odd

Algorithm:
Step 1: Start
Step 2: Input N
Step 3: यदि N % 2 == 0 → Print "Even"
Step 4: वरना → Print "Odd"
Step 5: Stop
🐍 Python Implementation▶ Run
n = int(input("Enter number: "))
if n % 2 == 0:
    print(n, "is Even")
else:
    print(n, "is Odd")
OUTPUT (input: 7)7 is Odd

🧩 Algorithms के Types

Typeक्या करता हैExample
SequentialSteps सीधे क्रम मेंSum of two numbers
Conditional (Selection)Condition के आधार पर रास्ता चुननाLargest of two, Even/Odd
Iterative (Loop)Steps बार-बार दोहराना1 से 10 तक print, Factorial
RecursiveFunction खुद को call करेFactorial (recursion), Fibonacci
🐍 Iterative Example — 1 से 5 तक Print▶ Run
for i in range(1, 6):
    print(i, end=" ")
OUTPUT1 2 3 4 5

⏱️ Algorithm Complexity (Efficiency मापना)

  • Time Complexity: Algorithm कितना समय लेगा — Big-O notation में: O(1) constant, O(n) linear, O(n²) quadratic।
  • Space Complexity: कितनी memory लेगा।
💡 समझें: 100 नामों की list में कोई नाम ढूँढना — एक-एक करके देखें तो 100 comparisons तक (O(n))। यही algorithm 1 करोड़ नामों पर slow हो जाएगा — इसीलिए complexity मायने रखती है।
Exam Point: 5 characteristics याद रखें — FDIOE (Finiteness, Definiteness, Input, Output, Effectiveness)। Algorithm हमेशा language-independent होता है — वही algorithm Python, C, Java किसी में भी लिख सकते हैं।
1.4

Flowcharts — Algorithm की तस्वीर

Flowchart = Algorithm का graphical (चित्रात्मक) representation — standard symbols से logic का flow दिखाया जाता है। Algorithm शब्दों में है, flowchart चित्र में — देखते ही समझ आ जाता है program कैसे चलेगा।

🔣 Flowchart Symbols (Exam का पक्का प्रश्न)

Symbolनामउपयोग
⬭ (Oval)TerminalStart / Stop
▭ (Rectangle)ProcessCalculation या assignment (Sum = A+B)
◇ (Diamond)DecisionCondition check (Yes/No)
▱ (Parallelogram)Input/OutputData लेना या result दिखाना
→ (Arrow)Flow LinesSteps की दिशा
◯ (Circle)ConnectorFlowchart के हिस्सों को जोड़ना

📊 Example Flowchart — दो Numbers में बड़ा

Flowchart: Largest of Two Numbers

Start Input A, B Is A > B ? Print A Print B Stop Yes No
समझें: Oval से शुरुआत → parallelogram में input → diamond पर condition (A > B?) — Yes तो बाएँ Print A, No तो दाएँ Print B → दोनों रास्ते Stop पर मिलते हैं। यही ऊपर वाले Python if-else का चित्र है!

✔️ Flowchart बनाने के Rules

  • हमेशा Start से शुरू और Stop पर खत्म।
  • Flow की दिशा ऊपर से नीचे या बाएँ से दाएँ।
  • Decision (diamond) से हमेशा 2 रास्ते निकलते हैं — Yes व No।
  • Arrows एक-दूसरे को cross न करें; हर symbol में short, clear text।

⚖️ Algorithm vs Flowchart

आधारAlgorithmFlowchart
रूपText/steps मेंSymbols/diagram में
समझनापढ़ना पड़ता हैदेखते ही समझ आता है
बनानाआसान व तेज़Symbols से समय लगता है
Modificationआसानपूरा redraw करना पड़ सकता है
Exam Point: Diamond = Decision; Parallelogram = Input/Output; Rectangle = Process; Oval = Start/Stop — symbols की matching exam में लगभग हर बार आती है!
1.5

Compilation Process — Code से Execution तक का सफ़र

Compilation = high-level source code को machine-understandable code में बदलने की प्रक्रिया। C जैसी languages में यह 7 stages से गुज़रती है; Python में यह हल्के रूप में bytecode + PVM के ज़रिए होती है।

🏭 Compilation की 7 Stages (C जैसी compiled languages)

Stageक्या होता है
1. Lexical AnalysisCode को tokens में तोड़ना (keywords, identifiers, operators)
2. Syntax Analysis (Parsing)Grammar rules के अनुसार structure check — parse tree बनता है
3. Semantic AnalysisMeaning check — जैसे string को int से जोड़ नहीं सकते
4. Intermediate Code GenerationMachine-independent बीच का code बनना
5. Code OptimizationCode को fast व memory-efficient बनाना
6. Code GenerationFinal machine code बनना
7. Linking & LoadingLibraries जोड़कर executable बनाना व memory में load करना

🐍 Python का Execution Model — Bytecode + PVM

Python Program कैसे Run होता है

📄 Source Codeprogram.py ⚙️ CompileBytecode (.pyc) 🖥️ PVMPython Virtual Machine ✅ OutputResult on screen
  • Step 1: आप .py file लिखते हैं (source code)।
  • Step 2: Python इसे internally bytecode (.pyc) में compile करता है — platform-independent बीच की भाषा।
  • Step 3: PVM (Python Virtual Machine) bytecode को line-by-line execute करती है।
  • Step 4: Output screen पर आता है।
Exam Point: Python = Interpreted + Compiled दोनों (पहले bytecode compile, फिर PVM interpret)। Compilation की 7 stages का क्रम याद रखें — Lexical → Syntax → Semantic → Intermediate → Optimization → Code Gen → Linking। Lexical Analysis में code tokens में टूटता है।
1.6

Testing — Program सही चल रहा है या नहीं?

Testing = program को अलग-अलग inputs देकर check करना कि वह expected output दे रहा है या नहीं। उद्देश्य: errors को users तक पहुँचने से पहले पकड़ना। "Testing shows the presence of bugs, not their absence!"

🧪 Testing के 4 मुख्य Types

Typeक्या test होता हैExample
1. Unit Testingएक-एक function/module अलग सेसिर्फ add() function check करना
2. Integration TestingModules आपस में मिलकर सही चलते हैं या नहींadd() + display() साथ में
3. System Testingपूरा system एक साथपूरी calculator app
4. Acceptance TestingUser की requirements पूरी हुईं या नहींClient द्वारा final approval
🐍 Simple Testing Example — assert से▶ Run
def add(a, b):
    return a + b

# Testing different cases
assert add(2, 3) == 5      # Pass
assert add(-1, 1) == 0     # Pass
assert add(0, 0) == 0      # Pass
print("All tests passed!")
OUTPUTAll tests passed!
समझें: assert statement condition false होने पर error देता है — तीनों test cases pass हुए इसलिए आखिरी print चला। अगर add() में गलती होती (जैसे a - b), तो पहला ही assert fail करके AssertionError देता।
Exam Point: Testing का क्रम — Unit → Integration → System → Acceptance (छोटे से बड़े की ओर)। Unit testing सबसे पहले, Acceptance सबसे आखिरी (user द्वारा)।
1.7

Debugging & Error Handling — गलतियाँ ढूँढना व सुधारना

Debugging = program की errors (bugs) को ढूँढकर ठीक करने की प्रक्रिया। नाम की कहानी — 1947 में Harvard के Mark II computer में सच में एक कीड़ा (moth) फँसा मिला था, तभी से errors को "bugs" कहते हैं! 🐛

❌ Errors के 3 Types (सबसे Important Topic)

1️⃣ Syntax Error — Grammar की गलती

Language के rules टूटने पर — program चलता ही नहीं

🐍 Syntax Error Example▶ Run
print("Hello"     # closing bracket bhool gaye!
OUTPUTSyntaxError: '(' was never closed

2️⃣ Logical Error — सोच की गलती

Program चलता है, error नहीं आती — लेकिन output गलत आता है। सबसे खतरनाक, क्योंकि पकड़ना मुश्किल!

🐍 Logical Error Example — Average निकालना▶ Run
a = 10
b = 20
average = a + b / 2     # GALAT! pehle b/2 hoga (BODMAS)
print("Average =", average)
OUTPUT (गलत!)Average = 20.0 ← सही उत्तर 15.0 था; सही code: (a + b) / 2

3️⃣ Runtime Error — चलते-चलते Crash

Execution के दौरान आने वाली error — जैसे zero से divide, गलत index।

🐍 Runtime Error Example▶ Run
a = 10
b = 0
print(a / b)     # zero se divide!
OUTPUTZeroDivisionError: division by zero

🛠️ Debugging की 4 Techniques

  • 1. Print Statement Debugging: बीच-बीच में print() लगाकर values check करना (सबसे simple)।
  • 2. Python Debugger (pdb): import pdb; pdb.set_trace() से program को रोककर step-by-step देखना।
  • 3. IDE Debugger: VS Code/PyCharm में breakpoints लगाकर।
  • 4. Exception Handling: try-except से errors को gracefully संभालना।
🐍 try-except — Error को संभालना (crash नहीं!)▶ Run
try:
    a = int(input("Enter number: "))
    result = 100 / a
    print("Result =", result)
except ZeroDivisionError:
    print("Error: Zero se divide nahi kar sakte!")
except ValueError:
    print("Error: Sahi number enter karein!")
OUTPUT (input = 0)Error: Zero se divide nahi kar sakte!
समझें: बिना try-except के input 0 देने पर program crash हो जाता। try block में risky code रखा — error आते ही matching except block चला और program शालीनता से message देकर आगे बढ़ गया। यही professional error handling है।
Exam Point: Syntax error = चलने से पहले पकड़ी जाती है; Logical error = चलता है पर output गलत; Runtime error = चलते-चलते crash (ZeroDivisionError, ValueError, IndexError)। "Bug" शब्द असली कीड़े (moth, 1947) से आया।
1.8

Documentation — Code का Description लिखना

Documentation = program के बारे में लिखी गई जानकारी — code क्या करता है, कैसे उपयोग करें, किसने बनाया। 6 महीने बाद खुद का ही code समझने के लिए, team members के लिए, और maintenance के लिए ज़रूरी। "Code बताता है 'कैसे', documentation बताती है 'क्यों'।"

📝 Python में Documentation के 3 तरीके

1️⃣ Single-line Comments (#)

🐍 Comments Example▶ Run
# Yah program area calculate karta hai
radius = 5           # circle ki radius
area = 3.14 * radius ** 2    # formula: πr²
print("Area =", area)
OUTPUTArea = 78.5

2️⃣ Multi-line Comments (''' ''')

🐍 Multi-line Comment▶ Run
'''
Program: Simple Interest Calculator
Author: Boosting Skills
Date: 2026
Purpose: SI = (P × R × T) / 100 calculate karna
'''
p, r, t = 1000, 5, 2
si = (p * r * t) / 100
print("Simple Interest =", si)
OUTPUTSimple Interest = 100.0

3️⃣ Docstrings — Functions की Official Documentation

🐍 Docstring + help() Example▶ Run
def square(n):
    """Yah function number ka square return karta hai."""
    return n * n

print(square(6))
print(square.__doc__)    # docstring ko access karna
OUTPUT36
Yah function number ka square return karta hai.

📋 Good Documentation के Rules

  • Comments "क्यों" बताएँ, "क्या" नहीं (code खुद बताता है क्या हो रहा है)।
  • Meaningful variable names रखें — student_age, न कि x
  • हर function में docstring; ज़रूरत से ज़्यादा comments भी बुरे हैं।
  • Documentation को code के साथ update रखें।
Exam Point: Python में single-line comment = #; multi-line = ''' ''' या """ """; function documentation = Docstring (function के पहले line पर, __doc__ से access)। Comments interpreter द्वारा ignore किए जाते हैं — execution पर असर नहीं।
1.9

🎯 Output-Based Questions — Exam का सबसे Scoring हिस्सा

O Level exam में "इस code का output क्या होगा?" type questions ज़रूर आते हैं। नीचे 12 questions दिए हैं — पहले खुद output सोचिए, फिर answer देखिए। यही असली practice है!

Q1. Print का basic behavior

🐍 Output बताइए▶ Run
print("5 + 3")
print(5 + 3)
ANSWER5 + 3
8
← Quotes में लिखा text वैसा ही print होता है; बिना quotes calculation होती है।

Q2. Integer vs Float Division

🐍 Output बताइए▶ Run
print(10 / 3)
print(10 // 3)
print(10 % 3)
ANSWER3.3333333333333335
3
1
← / हमेशा float देता है; // quotient (integer); % remainder।

Q3. Variable Update

🐍 Output बताइए▶ Run
x = 5
x = x + 2
x = x * 3
print(x)
ANSWER21
← Step-by-step: x=5 → x=7 → x=21। (RAM model जैसा sequential execution!)

Q4. String Concatenation vs Addition

🐍 Output बताइए▶ Run
a = "10"
b = "20"
print(a + b)
print(int(a) + int(b))
ANSWER1020
30
← Strings का + = जोड़ना नहीं, चिपकाना (concatenation)! int() से convert करने पर असली जोड़।

Q5. BODMAS Trap

🐍 Output बताइए▶ Run
result = 2 + 3 * 4 ** 2
print(result)
ANSWER50
← Order: पहले ** (4²=16), फिर * (3×16=48), फिर + (2+48=50)।

Q6. Loop Output

🐍 Output बताइए▶ Run
for i in range(1, 6):
    if i == 3:
        continue
    print(i, end=" ")
ANSWER1 2 4 5
← continue से i=3 वाला print skip हो गया; range(1,6) में 6 शामिल नहीं।

Q7. Condition का Output

🐍 Output बताइए▶ Run
a = 15
if a > 10:
    print("Big")
if a > 20:
    print("Bigger")
else:
    print("Small")
ANSWERBig
Small
← पहली if true (Big छपा); दूसरी if false इसलिए उसका else चला (Small)। दोनों अलग-अलग if हैं!

Q8. Error पहचानिए

🐍 कौन-सी error आएगी?▶ Run
x = int("hello")
print(x)
ANSWERValueError: invalid literal for int() with base 10: 'hello'
← "hello" को number में बदला नहीं जा सकता — यह Runtime Error है (Syntax नहीं!)।

Q9. While Loop Trace

🐍 Output बताइए▶ Run
n = 10
while n > 0:
    print(n, end=" ")
    n = n - 3
ANSWER10 7 4 1
← n: 10→7→4→1→(-2 पर condition false, रुक गया)।

Q10. Logical Error पकड़िए

🐍 यह code गलत output क्यों देगा?▶ Run
# 3 numbers ka average
a, b, c = 10, 20, 30
avg = a + b + c / 3
print("Average =", avg)
ANSWERAverage = 40.0 (गलत! सही 20.0 है)
← BODMAS से पहले c/3=10 हुआ, फिर 10+20+10=40। सही code: (a+b+c)/3 — यह Logical Error है (program चला, output गलत)।

Q11. Type पहचानिए

🐍 Output बताइए▶ Run
print(type(10))
print(type(10.5))
print(type("10"))
ANSWER<class 'int'>
<class 'float'>
<class 'str'>
← type() variable की class बताता है — exam में common question!

Q12. Assert का Behavior

🐍 Output बताइए▶ Run
def double(n):
    return n * 2

assert double(4) == 8
print("Test 1 passed")
assert double(5) == 11
print("Test 2 passed")
ANSWERTest 1 passed
AssertionError
← पहला assert true (8==8) — print चला; दूसरा false (10≠11) — AssertionError पर program रुका।
1.10

💻 Practice Programs — Algorithm से Code तक

हर program में पहले algorithm, फिर Python code, फिर output — यही pattern exam की answer-sheet में भी लिखना है। Copy button से code उठाकर खुद run करके देखिए!

Program 1 — Factorial निकालना (Loop से)

Algorithm: Start → Input N → fact = 1 → i को 1 से N तक: fact = fact × i → Print fact → Stop
🐍 factorial.py▶ Run
n = int(input("Enter number: "))
fact = 1
for i in range(1, n + 1):
    fact = fact * i
print("Factorial of", n, "=", fact)
OUTPUT (input = 5)Factorial of 5 = 120

Program 2 — N Numbers का Sum व Average

🐍 sum_average.py▶ Run
n = int(input("Kitne numbers? "))
total = 0
for i in range(n):
    num = float(input("Number " + str(i+1) + ": "))
    total = total + num
average = total / n
print("Sum =", total)
print("Average =", average)
OUTPUT (inputs: 3 → 10, 20, 30)Sum = 60.0
Average = 20.0

Program 3 — Prime Number Check

Algorithm: Input N → 2 से N-1 तक divide करके देखो → कहीं भी पूरा divide हुआ तो Not Prime, वरना Prime
🐍 prime_check.py▶ Run
n = int(input("Enter number: "))
is_prime = True
if n < 2:
    is_prime = False
for i in range(2, n):
    if n % i == 0:
        is_prime = False
        break
if is_prime:
    print(n, "is a Prime number")
else:
    print(n, "is NOT a Prime number")
OUTPUT (input = 17)17 is a Prime number

Program 4 — Multiplication Table

🐍 table.py▶ Run
n = int(input("Kis number ki table? "))
for i in range(1, 11):
    print(n, "x", i, "=", n * i)
OUTPUT (input = 7)7 x 1 = 7
7 x 2 = 14
... (10 lines तक)
7 x 10 = 70

Program 5 — Number Reverse करना

🐍 reverse_number.py▶ Run
n = int(input("Enter number: "))
rev = 0
while n > 0:
    digit = n % 10        # aakhri digit nikala
    rev = rev * 10 + digit
    n = n // 10           # aakhri digit hataya
print("Reversed =", rev)
OUTPUT (input = 1234)Reversed = 4321

Program 6 — Fibonacci Series

🐍 fibonacci.py▶ Run
n = int(input("Kitne terms? "))
a, b = 0, 1
for i in range(n):
    print(a, end=" ")
    a, b = b, a + b
OUTPUT (input = 8)0 1 1 2 3 5 8 13

Program 7 — Largest of Three Numbers

🐍 largest_of_three.py▶ Run
a = int(input("A: "))
b = int(input("B: "))
c = int(input("C: "))
if a >= b and a >= c:
    print("Largest =", a)
elif b >= a and b >= c:
    print("Largest =", b)
else:
    print("Largest =", c)
OUTPUT (inputs: 25, 67, 41)Largest = 67

Program 8 — Palindrome Number Check

🐍 palindrome.py▶ Run
n = int(input("Enter number: "))
temp = n
rev = 0
while temp > 0:
    rev = rev * 10 + temp % 10
    temp = temp // 10
if n == rev:
    print(n, "is a Palindrome")
else:
    print(n, "is NOT a Palindrome")
OUTPUT (input = 121)121 is a Palindrome
Exam Point: Factorial, Prime, Fibonacci, Reverse व Palindrome — ये 5 programs O Level practical व theory दोनों में बार-बार पूछे जाते हैं। हर एक का algorithm + code + dry run करना आना चाहिए।
1.11

Summary — Quick Revision (Exam से पहले पढ़ें)

  • Programming Language = computer से बात करने का माध्यम; 5 generations: Machine → Assembly → High-Level → 4GL → 5GL।
  • Compiler = पूरा program एक साथ translate (C, C++); Interpreter = line-by-line (Python, JavaScript)।
  • Models of Computation: Turing Machine (theory), Finite Automata (patterns), RAM Model (practice — O(1) memory access)।
  • Algorithm = finite, well-defined steps; 5 characteristics = Finiteness, Definiteness, Input, Output, Effectiveness।
  • Flowchart = algorithm का चित्र; Oval=Start/Stop, Rectangle=Process, Diamond=Decision, Parallelogram=I/O।
  • Compilation की 7 stages: Lexical → Syntax → Semantic → Intermediate → Optimization → Code Gen → Linking।
  • Python execution: .py → bytecode (.pyc) → PVM → Output; इसीलिए "Interpreted + Compiled"।
  • Testing: Unit → Integration → System → Acceptance (छोटे से बड़े)।
  • Errors: Syntax (चलने से पहले), Logical (गलत output), Runtime (crash — ZeroDivisionError आदि)।
  • Documentation: # comments, ''' multi-line, docstrings (__doc__) — good programming की पहचान।
1.12

Model Questions — 50 MCQs + 15 Theory Questions

❓ A. Multiple Choice Questions (50)

#प्रश्न व उत्तर
1

Computer सीधे कौन-सी language समझता है?

(a) Assembly(b) Python(c) Machine Language(d) SQL
✔ सही उत्तर: (c) Machine Language0/1 में, बिना translation।
2

Machine language किस generation की है?

(a) 1st(b) 2nd(c) 3rd(d) 4th
✔ सही उत्तर: (a) 1st
3

Assembly language में उपयोग होते हैं —

(a) Binary digits(b) Mnemonics(c) Objects(d) Queries
✔ सही उत्तर: (b) MnemonicsMOV, ADD, SUB जैसे।
4

Assembly को machine code में बदलता है —

(a) Compiler(b) Interpreter(c) Assembler(d) Linker
✔ सही उत्तर: (c) Assembler
5

Python किस generation की language है?

(a) 1GL(b) 2GL(c) 3GL(d) 5GL
✔ सही उत्तर: (c) 3GLHigh-level language।
6

SQL किसका उदाहरण है?

(a) 2GL(b) 3GL(c) 4GL(d) 5GL
✔ सही उत्तर: (c) 4GL
7

AI-oriented languages (जैसे Prolog) किस generation में आती हैं?

(a) 3GL(b) 4GL(c) 5GL(d) 1GL
✔ सही उत्तर: (c) 5GL
8

Programmer द्वारा लिखा गया high-level code कहलाता है —

(a) Object code(b) Source code(c) Byte code(d) Executable
✔ सही उत्तर: (b) Source code
9

Language के grammatical rules कहलाते हैं —

(a) Semantics(b) Syntax(c) Logic(d) Tokens
✔ सही उत्तर: (b) Syntax
10

Code का meaning/behavior कहलाता है —

(a) Syntax(b) Semantics(c) Grammar(d) Structure
✔ सही उत्तर: (b) Semantics
11

Compiler translate करता है —

(a) Line-by-line(b) पूरा program एक साथ(c) आधा-आधा(d) कुछ नहीं
✔ सही उत्तर: (b) पूरा program एक साथ
12

Interpreter की विशेषता है —

(a) Object code बनाता है(b) पहली error पर रुकता है(c) Fast execution(d) सभी errors एक साथ
✔ सही उत्तर: (b) पहली error पर रुकता है
13

इनमें से compiled language है —

(a) Python(b) JavaScript(c) C(d) HTML
✔ सही उत्तर: (c) C
14

Python है —

(a) Compiled only(b) Interpreted only(c) Interpreted + Compiled(d) Assembly
✔ सही उत्तर: (c) Interpreted + Compiledbytecode बनता है, PVM execute करती है।
15

Execution speed में तेज़ होता है —

(a) Interpreter(b) Compiler(c) दोनों बराबर(d) कोई नहीं
✔ सही उत्तर: (b) Compilerpre-compiled होने से।
16

Turing Machine किसने दी?

(a) Charles Babbage(b) Alan Turing(c) Dennis Ritchie(d) Guido van Rossum
✔ सही उत्तर: (b) Alan Turing
17

Turing Machine का part नहीं है —

(a) Infinite Tape(b) Head(c) Control Unit(d) Monitor
✔ सही उत्तर: (d) Monitor
18

DFA में एक symbol के लिए next states होती हैं —

(a) कई(b) केवल एक(c) शून्य(d) अनंत
✔ सही उत्तर: (b) केवल एकDeterministic।
19

RAM Model में memory access की complexity है —

(a) O(n)(b) O(n²)(c) O(1)(d) O(log n)
✔ सही उत्तर: (c) O(1)किसी भी location पर सीधी पहुँच।
20

Computational model के components हैं —

(a) Input, Process, Output(b) Storage(c) Control Unit(d) उपरोक्त सभी
✔ सही उत्तर: (d) उपरोक्त सभी
21

Algorithm है —

(a) Diagram(b) Steps का finite sequence(c) Hardware(d) Language
✔ सही उत्तर: (b) Steps का finite sequence
22

Algorithm की कौन-सी characteristic कहती है कि वह finite steps में खत्म हो?

(a) Definiteness(b) Finiteness(c) Effectiveness(d) Input
✔ सही उत्तर: (b) Finiteness
23

"हर step clear व unambiguous हो" — यह है —

(a) Finiteness(b) Definiteness(c) Output(d) Input
✔ सही उत्तर: (b) Definiteness
24

Algorithm में कम से कम कितने output होने चाहिए?

(a) 0(b) 1(c) 2(d) अनगिनत
✔ सही उत्तर: (b) 1
25

Algorithm में inputs हो सकते हैं —

(a) केवल 1(b) कम से कम 2(c) 0 या अधिक(d) अनिवार्य 5
✔ सही उत्तर: (c) 0 या अधिक
26

if-else पर आधारित algorithm कहलाता है —

(a) Sequential(b) Conditional(c) Iterative(d) Recursive
✔ सही उत्तर: (b) Conditional
27

Loop पर आधारित algorithm है —

(a) Sequential(b) Conditional(c) Iterative(d) Static
✔ सही उत्तर: (c) Iterative
28

Algorithm का समय मापा जाता है —

(a) Space Complexity(b) Time Complexity(c) Length(d) Weight
✔ सही उत्तर: (b) Time Complexity
29

O(n) का अर्थ है —

(a) Constant time(b) समय input के साथ linear बढ़ता है(c) समय घटता है(d) कोई संबंध नहीं
✔ सही उत्तर: (b) linear बढ़ता है
30

Algorithm लिखा जाता है —

(a) केवल Python में(b) केवल C में(c) किसी भी भाषा (language-independent) में(d) Binary में
✔ सही उत्तर: (c) language-independent
31

Flowchart है —

(a) Program की तस्वीर(b) Algorithm का graphical रूप(c) Testing tool(d) Compiler
✔ सही उत्तर: (b) Algorithm का graphical रूप
32

Start/Stop के लिए symbol —

(a) Rectangle(b) Diamond(c) Oval(d) Circle
✔ सही उत्तर: (c) Oval
33

Decision (condition) के लिए symbol —

(a) Oval(b) Diamond(c) Rectangle(d) Arrow
✔ सही उत्तर: (b) Diamond
34

Input/Output के लिए symbol —

(a) Parallelogram(b) Oval(c) Diamond(d) Rectangle
✔ सही उत्तर: (a) Parallelogram
35

Process (calculation) के लिए symbol —

(a) Circle(b) Rectangle(c) Oval(d) Diamond
✔ सही उत्तर: (b) Rectangle
36

Flowchart के दो भागों को जोड़ता है —

(a) Arrow(b) Connector (Circle)(c) Diamond(d) Oval
✔ सही उत्तर: (b) Connector (Circle)
37

Decision symbol से कितने रास्ते निकलते हैं?

(a) 1(b) 2 (Yes/No)(c) 4(d) कोई नहीं
✔ सही उत्तर: (b) 2 (Yes/No)
38

Compilation की पहली stage है —

(a) Syntax Analysis(b) Lexical Analysis(c) Code Generation(d) Linking
✔ सही उत्तर: (b) Lexical Analysis
39

Lexical Analysis में code टूटता है —

(a) Objects में(b) Tokens में(c) Files में(d) Loops में
✔ सही उत्तर: (b) Tokens में
40

Parse tree बनता है —

(a) Lexical(b) Syntax Analysis(c) Optimization(d) Loading
✔ सही उत्तर: (b) Syntax Analysis
41

Type mismatch (जैसे "abc" + 5) पकड़ी जाती है —

(a) Lexical(b) Semantic Analysis(c) Linking(d) Loading
✔ सही उत्तर: (b) Semantic Analysis
42

Python का source code internally बदलता है —

(a) .exe में(b) Bytecode (.pyc) में(c) HTML में(d) Assembly में
✔ सही उत्तर: (b) Bytecode (.pyc)
43

Python bytecode को execute करती है —

(a) CPU सीधे(b) PVM(c) Assembler(d) Browser
✔ सही उत्तर: (b) PVMPython Virtual Machine।
44

Libraries जोड़कर executable बनाने का काम है —

(a) Lexical(b) Linking(c) Parsing(d) Testing
✔ सही उत्तर: (b) Linking
45

सबसे पहले की जाने वाली testing —

(a) System(b) Acceptance(c) Unit(d) Integration
✔ सही उत्तर: (c) Unit Testing
46

User की requirements की final जाँच —

(a) Unit(b) Integration(c) Acceptance Testing(d) Lexical
✔ सही उत्तर: (c) Acceptance Testing
47

print("Hello" (bracket बंद नहीं) — कौन-सी error?

(a) Logical(b) Runtime(c) Syntax(d) कोई नहीं
✔ सही उत्तर: (c) Syntax Error
48

Program चलता है पर output गलत आता है — error है —

(a) Syntax(b) Logical(c) Runtime(d) Linker
✔ सही उत्तर: (b) Logical Error
49

10/0 करने पर आती है —

(a) SyntaxError(b) ValueError(c) ZeroDivisionError(d) TypeError
✔ सही उत्तर: (c) ZeroDivisionErrorRuntime error।
50

Python में function की documentation लिखी जाती है —

(a) # से(b) Docstring (""" """) से(c) // से(d) <!-- --> से
✔ सही उत्तर: (b) Docstring__doc__ से access होती है।

📝 B. 15 Theory Questions (Short Answers)

  1. Programming language क्या है और इसकी ज़रूरत क्यों है?Computer को instructions देने का माध्यम। Computer केवल binary समझता है, मनुष्य के लिए binary कठिन है — इसलिए human-readable languages चाहिए जिन्हें compiler/interpreter translate करे।
  2. Languages की 5 generations बताइए।1GL Machine (0/1), 2GL Assembly (mnemonics), 3GL High-Level (Python, C), 4GL (SQL, MATLAB), 5GL AI-based (Prolog)।
  3. Compiler और Interpreter में 4 अंतर लिखिए।Compiler: पूरा program एक साथ, fast, सभी errors एक साथ, object code बनता है (C)। Interpreter: line-by-line, slow, पहली error पर रुकता, object code नहीं (Python)।
  4. Source code व Object code में अंतर बताइए।Source code = programmer द्वारा लिखा high-level code (.py); Object code = translate किया हुआ machine-readable code।
  5. Turing Machine के 3 parts बताइए।Infinite Tape (symbols के लिए), Head (पढ़ने-लिखने के लिए), Control Unit (next step तय करने के लिए)।
  6. DFA और NFA में क्या अंतर है?DFA में हर symbol के लिए केवल एक next state; NFA में एक symbol के लिए कई possible states हो सकती हैं।
  7. RAM Model की विशेषताएँ लिखिए।हर instruction = 1 unit time; memory access O(1); algorithm analysis के लिए सबसे उपयुक्त practical model।
  8. Algorithm की 5 characteristics बताइए।Finiteness (finite steps), Definiteness (हर step clear), Input (0+), Output (1+), Effectiveness (steps simple व practical)।
  9. Flowchart के 5 symbols व उनके उपयोग लिखिए।Oval = Start/Stop; Rectangle = Process; Diamond = Decision; Parallelogram = Input/Output; Arrow = flow direction।
  10. Algorithm और Flowchart में अंतर बताइए।Algorithm text/steps में होता है, बनाना आसान, modify करना आसान; Flowchart symbols/diagram में, देखते ही समझ आता है पर redraw करना पड़ता है।
  11. Compilation की 7 stages क्रम से लिखिए।Lexical Analysis → Syntax Analysis → Semantic Analysis → Intermediate Code → Code Optimization → Code Generation → Linking & Loading।
  12. Python को Interpreted + Compiled क्यों कहते हैं?क्योंकि .py source पहले internally bytecode (.pyc) में compile होता है, फिर PVM उस bytecode को line-by-line interpret/execute करती है।
  13. Testing के 4 types क्रम से बताइए।Unit (एक function), Integration (modules साथ में), System (पूरा system), Acceptance (user requirements) — छोटे से बड़े की ओर।
  14. Errors के 3 types उदाहरण सहित लिखिए।Syntax (bracket missing — चलने से पहले), Logical (गलत formula — output गलत), Runtime (10/0 — ZeroDivisionError, चलते हुए crash)।
  15. Python में documentation के तरीके बताइए।# single-line comments, ''' ''' multi-line comments, और docstrings (function की पहली line पर """ """, __doc__ से access) — interpreter इन्हें ignore करता है।
Revision Tip: Flowchart symbols, compiler vs interpreter, errors के 3 types और Python का bytecode+PVM model — ये 4 topics इस chapter से exam में सबसे ज़्यादा बार आए हैं। Output-based questions (Section 1.9) को दो बार ज़रूर practice करें!
FAQ

अक्सर पूछे जाने वाले प्रश्न

Programming Language क्या है और इसकी ज़रूरत क्यों है?
Programming language वह माध्यम है जिससे हम computer को instructions देते हैं। Computer केवल binary (0/1) समझता है और मनुष्य के लिए binary लिखना कठिन है — इसलिए programming languages instructions को human-readable बनाती हैं, जिन्हें compiler/interpreter machine code में translate करता है।
Compiler और Interpreter में क्या अंतर है?
Compiler पूरे program को एक बार में translate करता है (fast execution, सभी errors एक साथ — जैसे C, C++); Interpreter एक-एक line को translate व execute करता है (slow, पहली error पर रुकता है — जैसे Python, JavaScript)।
Algorithm क्या है? इसकी characteristics बताइए।
Algorithm किसी problem को हल करने के well-defined steps का finite sequence है। 5 characteristics — Finiteness (finite steps में खत्म हो), Definiteness (हर step clear हो), Input (0 या अधिक), Output (कम से कम 1), Effectiveness (steps simple हों)।
Flowchart में कौन-कौन से symbols होते हैं?
Oval = Start/Stop; Rectangle = Process; Diamond = Decision (condition); Parallelogram = Input/Output; Arrow = Flow lines; Circle = Connector। हर flowchart में Start और Stop अनिवार्य होते हैं।
Python compiled है या interpreted?
Python दोनों है — पहले source code (.py) internally bytecode (.pyc) में compile होता है, फिर Python Virtual Machine (PVM) उस bytecode को line-by-line execute करती है। इसीलिए Python को "Interpreted + Compiled" language कहा जाता है।
Programming में कितने types की errors होती हैं?
3 types — Syntax Errors (grammar rules टूटने पर, जैसे missing parenthesis), Logical Errors (गलत logic से गलत output, program चलता है), Runtime Errors (execution के दौरान crash, जैसे ZeroDivisionError)।

🎯 Chapter 1 पूरा हुआ! अब आगे बढ़ें

Chapter 2 में Algorithms और Flowcharts को और गहराई से — decision making, loops व problem solving के साथ।

Chapter 2 पढ़ें ➜