Introduction to Python – Chapter 3 | O Level M3-R5 | Boosting Skills

Introduction to Python – NIELIT O Level (M3-R5)

इस chapter में हम Python language की मूल बातें सीखेंगे जैसे — Python Interpreter, Comments, Literals, Constants, Built-in Data Types, Numbers, Strings, Input/Output और कुछ Simple Programs

👉 Swipe to see more
Python Introduction

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 मानी जाती है।

👉 Python का उपयोग आज के समय में Web Development, Data Science, Machine Learning, Artificial Intelligence, Automation और Software Development जैसे क्षेत्रों में बहुत अधिक किया जाता है।

🔹 Key Features of Python

  • Easy to Learn: इसका syntax simple होता है इसलिए आसानी से सीखा जा सकता है
  • Interpreted Language: Python code line-by-line execute होता है
  • Platform Independent: एक बार लिखा गया code किसी भी Operating System पर run हो सकता है
  • Open Source: यह free और open source language है
  • Readable Code: Python code को आसानी से पढ़ा और समझा जा सकता है

🔹 Why Python is Popular?

  • Code लिखने में कम समय लगता है
  • Complex problems को आसानी से solve किया जा सकता है
  • Large number of libraries और frameworks उपलब्ध हैं
  • हर field में use होने वाली versatile language है
Example (Simple Python Program):
print("Hello World")
👉 यह program screen पर Hello World print करता है। यह Python का सबसे basic और पहला program माना जाता है।
Features & Characteristics of Python

Python एक powerful और versatile programming language है जिसमें कई important features होते हैं जो इसे beginners और professionals दोनों के लिए उपयुक्त बनाते हैं।

🔹 Important Features of Python

  • Simple & Easy to Learn: Python का syntax बहुत ही simple और readable होता है, जिससे इसे जल्दी सीखा जा सकता है।
  • High-Level Language: Python में low-level details (जैसे memory management) को handle करने की जरूरत नहीं होती।
  • Interpreted Language: Python code line-by-line execute होता है, जिससे debugging आसान हो जाती है।
  • Dynamically Typed: Python में variable का data type पहले से declare करने की जरूरत नहीं होती।
  • Platform Independent: Python का code किसी भी Operating System (Windows, Linux, Mac) पर run हो सकता है।
  • Open Source: Python free में उपलब्ध है और इसे कोई भी उपयोग कर सकता है।
  • Object-Oriented: Python Object-Oriented Programming (OOP) concepts को support करता है जैसे class और object।
  • Large Standard Library: Python में पहले से कई built-in libraries उपलब्ध होती हैं जिससे development आसान हो जाता है।
  • Extensible & Embeddable: Python को अन्य languages (जैसे C, C++) के साथ integrate किया जा सकता है।
  • Portable: Python programs को एक system से दूसरे system पर आसानी से ले जाया जा सकता है।
👉 यही सभी features Python को most popular programming language बनाते हैं।
Example (Dynamic Typing):

x = 10
x = "Hello"
      
👉 यहाँ variable x पहले integer value store कर रहा है और बाद में string, बिना किसी type declaration के।
Interpreted vs Compiled Languages

Programming languages को execution के आधार पर दो भागों में बाँटा जाता है: Interpreted Languages और Compiled Languages

🔹 Interpreted Language

  • Code line-by-line execute होता है
  • Compilation की जरूरत नहीं होती
  • Error तुरंत पता चल जाता है
  • Example: Python

🔹 Compiled Language

  • पूरे program को पहले compile किया जाता है
  • Executable file generate होती है
  • Execution fast होता है
  • Example: C, C++
👉 Python एक interpreted language है, इसलिए debugging आसान होती है।
Dynamic Typing vs Static Typing

Programming languages में variables के data type handle करने के दो तरीके होते हैं: Dynamic Typing और Static Typing

🔹 Dynamic Typing

  • Variable का type runtime पर decide होता है
  • Type declare करने की जरूरत नहीं होती
  • Example: Python

x = 10
x = "Hello"
      

🔹 Static Typing

  • Variable का type पहले से declare करना होता है
  • Compile time पर type check होता है
  • Example: C, Java
👉 Python एक dynamically typed language है।
High-Level Programming Language Concept

High-Level Programming Language ऐसी language होती है जो human language के करीब होती है और समझने में आसान होती है।

🔹 Characteristics

  • Readable और easy syntax
  • Hardware details से abstraction
  • Program लिखना और समझना आसान

🔹 Examples

  • Python
  • Java
  • C++
👉 Python एक high-level language है इसलिए इसे सीखना आसान है।
Example:

print("Hello")
      
👉 यह simple statement high-level language का उदाहरण है।
Python Keywords (Reserved Words)

Keywords वे reserved words होते हैं जिनका Python में पहले से ही special meaning होता है। इन्हें हम variable या identifier के रूप में use नहीं कर सकते।

👉 Keywords का meaning fixed होता है, इसलिए इन्हें redefine नहीं किया जा सकता।

🔹 Important Points

  • Keywords case-sensitive होते हैं
  • इनका predefined meaning होता है
  • इनका use programming logic में होता है

🔹 Examples of Python Keywords


False   True   None   and   or   not  
if   else   elif   for   while  
break   continue   pass  
def   return   class   import  
      
Identifiers in Python (Rules & Naming Conventions )

Identifier वह नाम होता है जो हम variables, functions, classes आदि को देते हैं।

🔹 Rules for Identifiers

  • Name alphabet (a-z, A-Z) या underscore (_) से शुरू होना चाहिए
  • Digit (0-9) से start नहीं कर सकते
  • Special symbols (@, #, $, %) allowed नहीं होते
  • Keywords को identifier नहीं बना सकते
  • Case-sensitive होते हैं (name ≠ Name)

🔹 Naming Conventions (Best Practices)

  • Meaningful names use करें (जैसे total_marks)
  • Snake case use करें → my_variable_name
  • Constant के लिए uppercase → PI = 3.14
Example:

name = "Rahul"     # valid
_age = 20          # valid
2num = 10          # invalid
class = 5          # invalid (keyword)
      
Python Tokens (Keywords, Identifiers, Literals, Operators, Punctuators)

Tokens Python program के सबसे छोटे unit होते हैं। यानी program छोटे-छोटे parts में divide होता है जिन्हें tokens कहते हैं।

👉 Python program = Tokens का combination

🔹 Types of Tokens

1. Keywords

Reserved words जिनका special meaning होता है (जैसे: if, else, for)

2. Identifiers

Variables या functions के नाम (जैसे: name, total_marks)

3. Literals

Fixed values जो directly code में लिखी जाती हैं


x = 10        # integer literal
name = "Ram"  # string literal
      

4. Operators

Operations perform करने के लिए use होते हैं


a = 5 + 3     # + is operator
      

5. Punctuators

Special symbols जो structure define करते हैं


(), {}, [], :, ;, , 
      
👉 हर Python program इन सभी tokens से मिलकर बना होता है।
Structure of a Python Program

Python Program Structure से मतलब है कि एक program को किस तरह लिखा और organize किया जाता है। Python में syntax simple होता है और indentation बहुत important role निभाता है।

🔹 Basic Structure

  • Statements (instructions)
  • Indentation (spacing)
  • Comments
  • Input / Output

🔹 Important Points

  • Python में indentation (space/tab) block define करता है
  • Curly braces { } का use नहीं होता
  • हर statement नई line में लिखी जाती है
Example:

# simple python program

name = input("Enter your name: ")

if name:
    print("Hello", name)
      
👉 यहाँ indentation (4 spaces) block को define कर रहा है।
Python Memory Management (Basic Concept)

Memory Management का मतलब है कि Python program के दौरान memory को कैसे allocate (देना) और deallocate (हटाना) किया जाता है।

🔹 Key Concepts

  • Automatic Memory Allocation: Python खुद memory allocate करता है
  • Dynamic Typing: Variable का type change होने पर memory automatically manage होती है
  • Heap Memory: Objects heap memory में store होते हैं
👉 Python में programmer को manually memory manage करने की जरूरत नहीं होती।
Example:

x = 10
x = "Hello"
      
👉 यहाँ Python automatically नई memory allocate करता है।
Garbage Collection in Python (Introduction)

Garbage Collection वह process है जिसमें Python उन objects की memory को free कर देता है जो अब use में नहीं हैं।

🔹 Why Garbage Collection?

  • Memory waste होने से बचाता है
  • Program performance improve करता है
  • Unused objects को remove करता है

🔹 How it Works?

  • Python reference counting use करता है
  • जब object का reference 0 हो जाता है → memory free हो जाती है
Example:

x = 10
x = None
      
👉 पहले object का reference था, बाद में हट गया → memory free हो सकती है।
👉 Python automatic garbage collection provide करता है।
Variables in Python (Object Reference & Assignment)

Variable एक नाम होता है जो किसी value को store करने के लिए use किया जाता है। लेकिन Python में variable value को directly store नहीं करता, बल्कि वह object (value) का reference hold करता है।

👉 Python में variable = label (tag) होता है जो memory में stored object को point करता है।

🔹 Object Reference Concept

  • जब हम value assign करते हैं, Python memory में object बनाता है
  • Variable उस object को refer करता है
  • एक ही object को multiple variables refer कर सकते हैं

a = 10
b = a
      
👉 यहाँ a और b दोनों same object (10) को refer कर रहे हैं।

🔹 Assignment in Python

  • = operator assignment के लिए use होता है
  • Right side की value पहले create होती है
  • फिर variable उसे refer करता है

x = 5
      
👉 पहले 5 object create होता है, फिर x उसे refer करता है।

🔹 Important Points

  • Variable declare करने की जरूरत नहीं होती
  • Type automatically assign होता है
  • Same variable different type की value hold कर सकता है

x = 10
x = "Hello"
      
👉 यहाँ x पहले integer था, फिर string बन गया।
Multiple Assignment & Variable Swapping

🔹 Multiple Assignment

Python में हम एक साथ कई variables को value assign कर सकते हैं, इसे multiple assignment कहते हैं।


a, b, c = 1, 2, 3
      
👉 a = 1, b = 2, c = 3 assign हो जाता है।

🔹 Same Value Assignment


x = y = z = 5
      
👉 तीनों variables same value को refer करते हैं।

🔹 Variable Swapping

Swapping का मतलब है दो variables की value को आपस में बदलना।

✔ Traditional Method


a = 10
b = 20

temp = a
a = b
b = temp
      

✔ Python Method (Best Way)


a = 10
b = 20

a, b = b, a
      
👉 Python में swapping बिना extra variable के हो जाती है।
👉 Python swapping interview और exam में बहुत important होता है।
Constants in Python (Conceptual Understanding)

Constant ऐसी value होती है जो program के दौरान change नहीं होती। जैसे: PI, GRAVITY आदि।

🔹 Important Concept

  • Python में real constant keyword नहीं होता
  • Convention (rule) से constant बनाते हैं
  • Constant name uppercase में लिखा जाता है

PI = 3.14
GRAVITY = 9.8
      
👉 ये constant माने जाते हैं (by convention)

🔹 Important Note

  • Technically value change हो सकती है
  • लेकिन programmer इसे change नहीं करता
👉 Constant का use readability और clarity बढ़ाने के लिए किया जाता है।
Python Data Types Overview

Data Type यह बताता है कि variable में किस प्रकार की value store है और उस value पर कौन-कौन से operations perform किए जा सकते हैं।

👉 Python एक dynamically typed language है, इसलिए data type automatically detect हो जाता है।

🔹 Main Built-in Data Types

  • Numeric: int, float, complex
  • Boolean: True, False
  • Sequence: string, list, tuple
  • NoneType: None

x = 10        # int
y = 3.5       # float
z = "Hello"   # string
flag = True   # boolean
      
Numeric Data Types (int, float, complex)

Python में numeric data types का उपयोग numbers को represent करने के लिए होता है।

🔹 int (Integer)

  • Whole numbers (positive, negative, zero)
  • Unlimited size

a = 10
b = -5
      

🔹 float (Floating Point)

  • Decimal numbers
  • Scientific notation support

x = 3.14
y = 1.2e3   # 1200.0
      

🔹 complex

  • Real + Imaginary part
  • j का उपयोग imaginary part के लिए होता है

z = 2 + 3j
      
Integer Types (Binary, Octal, Hexadecimal)

Python में integer को अलग-अलग number systems में represent किया जा सकता है।

🔹 Binary (Base 2)


x = 0b1010   # 10
      

🔹 Octal (Base 8)


x = 0o12   # 10
      

🔹 Hexadecimal (Base 16)


x = 0xA   # 10
      
👉 Prefix (0b, 0o, 0x) number system को define करता है।
Floating Point Precision Issues

Floating point numbers exact precision में store नहीं होते क्योंकि computer binary system का उपयोग करता है।


print(0.1 + 0.2)
      
👉 Output: 0.30000000000000004

🔹 Why this happens?

  • Binary representation में decimal values exact store नहीं होती
  • Approximation की वजह से error आता है
👉 Comparison करते समय rounding का use करना चाहिए।
Boolean Data Type

Boolean data type केवल दो values रखता है: True और False

🔹 Usage

  • Conditions (if, while)
  • Logical operations

x = 10
print(x > 5)   # True
      
👉 True = 1 और False = 0 के बराबर माना जाता है।
String Data Type

String characters का sequence होता है, जिसे quotes (' ' या " ") में लिखा जाता है।

🔹 Features

  • Immutable होता है (change नहीं किया जा सकता)
  • Indexing और slicing support करता है

name = "Python"
print(name[0])   # P
print(name[1:4]) # yth
      
👉 String operations programming में बहुत important हैं।
NoneType in Python

NoneType एक special data type है जो no value को represent करता है।

🔹 Key Points

  • None एक special constant है
  • यह empty या null value को दिखाता है

x = None
      
👉 None का उपयोग तब किया जाता है जब value अभी assign नहीं हुई हो।
Mutable vs Immutable Data Types

Python में data types को दो categories में divide किया जाता है: Mutable और Immutable

🔹 Immutable Data Types (Change नहीं होते)

Immutable का मतलब है — जिस object की value create होने के बाद change नहीं की जा सकती। अगर आप value बदलने की कोशिश करते हैं, तो Python नया object बना देता है।

  • int
  • float
  • str (string)
  • tuple
  • bool

x = 10
print(id(x))

x = x + 5
print(id(x))
      
👉 Explanation:
पहले x = 10 एक object बनाता है।
जब हम x = x + 5 करते हैं, तो value change नहीं होती बल्कि नया object (15) बनता है।
इसलिए दोनों बार id अलग होगी
👉 Immutable = value change नहीं होती, नया object बनता है।

🔹 Mutable Data Types (Change हो सकते हैं)

Mutable का मतलब है — जिस object की value directly modify की जा सकती है। इसमें नया object नहीं बनता, existing object ही change होता है।

  • list
  • dict
  • set

x = [1, 2, 3]
print(id(x))

x.append(4)
print(id(x))
      
👉 Explanation:
list में नया element add हुआ लेकिन object वही रहा।
इसलिए दोनों बार id same होगी
👉 Mutable = value change होती है, object same रहता है।
Type Checking (type() and isinstance())

Python में variable का data type check करने के लिए दो main functions होते हैं: type() और isinstance()

🔹 type() Function

type() function किसी variable का exact data type return करता है।


x = 10
print(type(x))
      
👉 Output: <class 'int'>
मतलब x एक integer है।

🔹 isinstance() Function

isinstance()


x = 10
print(isinstance(x, int))
      
👉 Output: True
मतलब x integer type का है।

🔹 Difference (Important)

  • type() exact type बताता है
  • isinstance() True/False देता है
  • isinstance() inheritance में ज्यादा useful होता है
👉 Exam में पूछा जाता है: type() vs isinstance()
Type Conversion (Implicit & Explicit)

Type Conversion का मतलब है एक data type को दूसरे data type में बदलना।

🔹 Implicit Type Conversion (Automatic)

जब Python खुद automatically data type change कर देता है, उसे implicit conversion कहते हैं।


x = 10
y = 2.5

result = x + y
print(result)
print(type(result))
      
👉 Explanation:
int + float → result float बन गया (12.5)
Python ने खुद conversion किया।

🔹 Explicit Type Conversion (Manual)

जब programmer खुद type change करता है, उसे explicit conversion कहते हैं।


x = "10"
y = int(x)

print(y)
print(type(y))
      
👉 Explanation:
string "10" को int में convert किया गया।

🔹 Common Conversion Functions

  • int()
  • float()
  • str()
  • bool()
👉 Wrong conversion error दे सकता है (जैसे "abc" → int)

int("abc")   # Error
      
Numbers in Python (Deep Concepts)

Python में numbers को represent करने के लिए mainly int, float और complex data types का उपयोग होता है। ये mathematical operations के लिए use होते हैं।

🔹 Arithmetic Operators

  • + (Addition)
  • - (Subtraction)
  • * (Multiplication)
  • / (Division)
  • // (Floor Division)
  • % (Modulus)
  • ** (Exponent)

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
      

🔹 Built-in Functions

  • abs(x) → absolute value
  • round(x) → rounding
  • pow(x, y) → power

print(abs(-5))      # 5
print(round(3.6))   # 4
print(pow(2,3))     # 8
      
👉 Numbers Python में immutable होते हैं।
Strings and String Operations in Python

String characters का sequence होता है जो quotes में लिखा जाता है। यह Python में सबसे ज्यादा use होने वाला data type है।

🔹 String Creation


name = "Python"
      

🔹 Indexing

हर character का index होता है (0 से start)।


name = "Python"
print(name[0])  # P
      

🔹 Slicing


print(name[1:4])   # yth
      

🔹 String Operations

  • Concatenation (+)
  • Repetition (*)

a = "Hello"
b = "World"
print(a + b)     # HelloWorld
print(a * 2)     # HelloHello
      

🔹 Important String Methods

  • upper()
  • lower()
  • strip()
  • replace()
  • find()

text = " python "
print(text.strip())   # python
print(text.upper())   # PYTHON
      
👉 String immutable होता है (change नहीं होता, नया string बनता है)
Accepting Input & Displaying Output in Python

🔹 Input लेना

Python में user से input लेने के लिए input() function का use होता है। यह हमेशा string return करता है।


name = input("Enter your name: ")
      

🔹 Type Conversion Required


age = int(input("Enter age: "))
      
👉 input string होता है, इसलिए int() से convert करना जरूरी है।

🔹 Output दिखाना

Output दिखाने के लिए print() function का use होता है।


print("Hello World")
      

🔹 Formatting Output


name = "Rahul"
print(f"My name is {name}")
      
Writing Simple Python Programs

Python में program लिखना बहुत आसान होता है। हर program step-by-step instructions का combination होता है।

🔹 Example 1: Addition Program


a = int(input("Enter first number: "))
b = int(input("Enter second number: "))

sum = a + b
print("Sum =", sum)
      
👉 Explanation:
user से दो number लिए गए → जोड़ किया → result print किया।

🔹 Example 2: Even Odd Program


num = int(input("Enter number: "))

if num % 2 == 0:
    print("Even")
else:
    print("Odd")
      
👉 Explanation:
number को 2 से divide करके check किया गया → even या odd decide हुआ।
👉 Python programs हमेशा logic + input + output पर आधारित होते हैं।