Python Book PDF in Hindi
Python Book PDF in Hindi
1 (Introduction)
n Python
Python Skeâ high level leLee interpreted programming language nw; peesefkeâ meved 1989 ceW Guido Van
Rossum Éeje develop keâer ieF& leLee ØeLece cej meved 1991 ceW release keâer ieF& Leer~ heefjCeecemJe™he Ùen Ssmeer
programming language ngF& pees software development leLee web development kesâ efueS yengle GheÙeesieer meeefyele
ngF&~
Python ceW kegâue oes Version nQ—Python 2 SJeb Python 3 leLee oesveeW ner Deeheme ceW efyeukegâue Deueie nÝb~
meeceevÙeleÙee Python file keâe extension .py neslee nw leLee Command kesâ ceeOÙece mes Yeer nce keâesF& Øees«eece
Deemeeveer mes run keâj mekeâles nQ~
efkeâmeer Yeer Python Program keâes efvecveefueefKele leerve Øekeâej mes run keâj mekeâles nQ—
1. Interactive Interprefer kesâ Éeje
2. Script from command line kesâ Éeje
3. Integrated Development Environment (IDLE) kesâ Éeje
Source Code
(Program)
Computer
Running an Outputs
Inputs Interpreter
Problem Description
Logical
Solution
. obj . exe
efÛe$e : Assembler and Linker
High Level Language : meYeer Øekeâej keâer High Level Language (HLL), procedure oriented
language leLee machine independent nesleer nw~ Ùen upper language leLee third generation language kesâ veece
mes Yeer peeveer peeleer nw~ Ùen nceW program keâes meeOeejCe English language efueKeves, Mathematical SJeb DevÙe efmeiveue
keâe ØeÙeesie keâjves keâer megefJeOee Øeoeve keâjleer nw~ efpememes Øees«eece, Deemeeve redable leLee mecePeves ÙeesiÙe nes peelee nw~ Ssmes Øees«eece
keâes Computer keâes mecePeves kesâ efueS Gmes Machine instruction ceW translate keâjvee heÌ[lee nw~ efpemekesâ efueS nceW Skeâ
mee@heäšJesÙej keâer DeeJeMÙekeâlee nesleer nw~ efpemes compiler Ùee interpreter keânles nQ~ Fmes compiler SJeb translator keâe
cegKÙe keâeÙe& source keâes[ keâes machine understandable code ceW heefjJee|lele keâjvee neslee nw~
Source Machine
Compiler Output
Code Code
Source
Interpreter Output
Code
Source code
Processor
Compiler
Interpreter
Compiler
Processor
Bytecode
Python, meved 1980 kesâ ueieYeie Guido Van Rossum Éeje develop keâer ieÙeer Leer efpemes Netherland keâer
“National Research Institute for Mathematics and Computer Science” ØeÙeesieMeeuee ceW efJekeâefmele efkeâÙee ieÙee
Lee~
Python keâes otmejer Øees«eeeEceie ueQiJespe pewmes ABS Modula-3, C, C++, Afgol, Unix Shell Deeefo mes derive
efkeâÙee ieÙee nw~
heefjÛeÙe y 7
Python, copyrighted nw~ Python keâe source code GNU General Public Licence (GPL) kesâ Devleie&le
heefyuekeâ [escesve ceW GheueyOe nw~
Python DeYeer Yeer Skeâ keâesj šerce Éeje maintain keâer peeleer nw leLee Guido Van Rossum DeYeer Yeer Gmekeâer
progress keâes [eÙejskeäš keâjles nQ leLee Ùen DeefOekeâej Gvekesâ heeme megjef#ele nw~
veJecyej 1994 ceW Python 1.0 keâes peejer (release) efkeâÙee ieÙee Lee leLee meved 2000 ceW Python 2.0 keâes peejer
(release) efkeâÙee ieÙee Lee~ Python 2.7.11 DeYeer Yeer Python 2.0 keâe meyemes veJeervelece version nw~
meved 2000 ceW Python 3.0 keâes peejer efkeâÙee ieÙee Lee~ Fmekeâe veJeervelece version Python 3.5.1 DeYeer GheueyOe nw~
Python 3, Python 2 oes Deueie-Deueie Python kesâ version nQ~
Evaluation of Python
Sun Microsystem
1995 Java (Now Oracle)
2000 C# Microsoft
n Why Python
Python Skeâ General purpose programming language nw~ pees cegKÙele: scripting kesâ efueS ØeÙeesie keâer peeleer
nw~ Fmes object oriented scripting language Yeer keâne peelee nw~ Ùen oop kesâ concent keâes support keâjleer nw~
Python codef ile keâes program kesâ yepeeÙe Script keâne peelee nw~
Python kesâ yengle mes ueeYeoeÙekeâ features nesves kesâ keâejCe Øees«eecej Fmes ØeÙeesie ceW ueevee Ûeenles nQ~ Fmekesâ kegâÚ
cenlJehetCe& iegCe efvecveJele nQ, pees Øees«eece keâes Deekeâe|<ele keâjles nQ—
1. Simple and Easy to learn 6. Extendable
2. Interpreted and Interactive 7. Dynamic
3. Object Oriented 8. GUI Programming and Data Bases
4. Portable 9. Board Standard Library
5. Scalable
meceÙe-meceÙe hej Python software keâes update efkeâÙee peelee nw~ leLee kegâÚ megOeejeW leLee kegâÚ veÙes features kesâ
meeLe Gmekeâe veÙee version peejer efkeâÙee peelee nw~
8 y keâchÙetšj Øees«eeeEceie ÙeteEpeie heeÙeLeve
Python keâe henuee version Python 1.0 V, Jan 1994 ceW peejer efkeâÙee ieÙee Lee~ Fmekesâ yeeo Oct. 2000 ceW
Python 2.0 V leLee Dec. 2008 ceW Python 3.0 V keâes peejer efkeâÙee ieÙee Lee~ Jele&ceeve ceW GheueyOe Python keâe
Python 3.9.0 SJeb Python 2.7.18 mes veJeerve version nw~
veesš—Python 3 kesâ Jepe&ve ceW (Python 2 mes) Backward compatability veneR nesleer nw DeLee&led Python 2 keâe
Program pe™jer veneR nw~ Python 3 ceW ncesMee run keâjW~
Python Documentation Released
1. Python 3.9.0 05 Oct 2020
2. Python 3.8.6 23 Sept 2020
3. Python 3.8.5 20 July 2020
4. Python 3.8.4 13 July 2020
5. Python 3.8.3 13 May 2020
6. Python 3.8.2 24 Feb 2020
7. Python 3.8.1 18 Dec 2019
8. Python 3.8.0 14 Oct 2019
9. Python 3.7.0 24 June 2018
10. Python 3.6.0 23 Dec 2016
11. Python 3.0 03 Dec 2008
1. Python 2.7.18 20 April 2020
2. Python 2.7.17 19 Oct 2019
3. Python 2.7.16 02 March 2019
4. Python 2.7.15 30 April 2018
5. Python 2.7.14 17 Dec 2016
6. Python 2.7 04 July 2010
7. Python 2.6 01 Oct 2008
8. Python 2.5 19 Sept 2006
9. Python 2.4 30 Nov 2004
10. Python 2.0 16 Oct 2000
Flavors of Python
1. C Python : Ùen Python keâe Standard Flavor nw~ Fmekeâe ØeÙeesie C language Applications kesâ efueS
neslee nw~
2. Jython or JPython : Ùen cegKÙele: Java Applications kesâ efueS nw~ Ùen Jum hej run keâjlee nw~
3. Iron Python : Ùen C# leLee .Net Platform hej ØeÙeesie ceW ueeÙee peelee nw~
4. P 4 P 4 : Ùen JIT Compiler hej keâeÙe& keâjlee nw leLee PVM ceW GheueyOe neslee nw~
5. Ruby Python : Ùen Ruby Platform hej keâeÙe& kesâ efueS neslee nw~
6. Anaconda Python : Ùen cegKÙe ™he mes large volume kesâ data processing, machine learing leLee
mathematical Calculation kesâ efueS ØeÙeesie ceW ueeÙee peelee nw~
heefjÛeÙe y 9
Where We Can Use Python
Jele&ceeve ceW Python keâes cegKÙe leewj efvecve keâeÙe& nsleg ØeÙeesie keâj jns nQ—
1. For Developing Desktop Applications
2. For Developing Web Application
3. For Developing Database Applications
4. For Network Programming
5. For Developing Games
6. For Data Analysis Application
7. For Machine Learning Application
8. For Developing Artificial Intelligence Applications
9. For Internal of Things (IOT) Application
10. For Image Processing and its Application
Python Skeâ Cross platform programing language nw~ DeLee&led Ùen efkeâmeer Yeer Platform pewmes Windows,
Macox, Linux Deeefo hej Deemeeveer mes run keâjleer nw~ Ùen free leLee open source nw~
How to Install Python on Windows [Pycharm IDE]
PyCharm is a cross-platform editor developed by JetBrains. Pycharm provides all the tools you
need for productive Python developmen. Below are the detailed steps for installing Python and
PyCharm.
n Installing Python
Step 1 : To download and install Python visit the official website of Python
http://www.python.org/downloads/and choose your version. We have chosen Python version
3.6.3
10 y keâchÙetšj Øees«eeeEceie ÙeteEpeie heeÙeLeve
Step 2 : Once the download is complete, run the axe for install Python. Now click on Install
Now.
n Installing Pycharm
Step 1 : To download PyCharm visit the website
http://www.jetbrains.com/pycharm/download/and click the “DOWNLOAD” link under the
Community Section.
12 y keâchÙetšj Øees«eeeEceie ÙeteEpeie heeÙeLeve
Step 2 : Once the download is complete, run the exe for install PyCharm. The stepup wizard
should have started. Click "Next".
Step 5 : Choose the start menu folder. Keep selected Jet Brains and click on "Install".
14 y keâchÙetšj Øees«eeeEceie ÙeteEpeie heeÙeLeve
Step 6 : Wait for the installation to finish.
Step 7 : Once installation finished, you should receive a message screen that PyCharm is
installed. If you want to go ahead and run it, click the "Run PyCharm Community Edition" box first
and click "Finish".
heefjÛeÙe y 15
Step 8 : After you click on "Finish", the Following screen will appear.
Variable Description
1. PYTHONPATH Ùen PATH kesâ meceeve ner neslee nw~ Ùen yeleelee nw efkeâ Interpereter keâes Øees«eece ceW import
keâer ieÙeer module file keâes keâneB locate keâjvee nw~ PYTHONPATH keâYeer-keâYeer
Python installer Éeje Yeer Øemlegle efkeâÙee peelee nw~
2. PYTHONSTARTUR Ùen Gme file kesâ path keâes jKelee nw pees Python Source keâes[ keâes initilize keâjleer
nw~ peye Yeer interpreter start neslee nw, leye Ùen execute neslee nw~ Ùen
PYTHONPATH keâes modify Ùee utilities keâes uees[ keâjves keâe command Deheves
heeme jKelee nw~
3. PYTHONCASEOK Ùen imported statement ceW first case insensitive match kesâ efueS Python keâes
efveoxefMele keâjlee nw~ Fmes activate keâjves kesâ efueS Fmes efkeâmeer Yeer value kesâ meeLe set efkeâÙee
peelee nw~
4. PYTHONHOME Ùen Search Path keâe efJekeâuhe nw, pees PYTHONSTARTUR Ùee PYTHONPATH
ceW embedded jnlee nw~
16 y keâchÙetšj Øees«eeeEceie ÙeteEpeie heeÙeLeve
File Extensions in Python
1.6 IDLE
Python Program keâes run keâjves keâe Skeâ ceeOÙece IDLE (Integrated Development Environment) nw~
Development Environment Ssmee tools keâe mesš nw pees Program writing keâes Deemeeve yeveelee nw~ Ùen Program
kesâ efueS word processor keâer lejn keâeÙe& keâjlee nw~ Ùen Deehekesâ Program keâes Write keâjves, save keâjves, edit keâjves
ceW ceooieej neslee nw~ IDLE ceW keâeÙe& keâjves kesâ oes ØecegKe cees[ (Mode) efoÙes ieÙes nQ~
1. Interactive Mode
2. Script Mode
1. Interactive Mode : efkeâmeer Yeer Python Program keâes efueKeves keâe yengle ner DeÛÚe leLee lespe (quick)
lejerkeâe neslee nw FmeceW Deehe Python keâes yeleeles nQ efkeâ Deehe keäÙee keâjvee Ûeenles nQ Deewj Ùen Gmekesâ Devegmeej respond keâjlee
nw~
Interactive Session ceW keâesF& Øees«eece efueKeves kesâ efueS meyemes henues (IDEL) pees Python keâe GUI neslee nw, Gmes
open keâjvee nesiee~
® Start Menu ¿
® All Programs ¿
® Python 3
® IDLE
Deye Deehekesâ Screen hej Skeâ efJev[es Kegue peeÙesieer~ Gme efJev[es (Window) keâes Python Shell keâne peelee nw~
Deye Ùeefo Deehe Command promot keâes open keâjkesâ
print ("Hello Python")
šeFhe keâjkesâ Enter Key keâes Press keâjles gSa lees Interpreter respond keâjsiee Deewj Deehekesâ efJev[es hej
Hello Python
efoKesiee (Hello Python)~ Ùen Skeâ Øekeâej keâe Hello Word print keâjves keâe Øees«eece nw~
Python Skeâ Case sensitive language nw~ Ùeefo Deehe print kesâ mLeeve hej Print Ùee PRINT efueKeWies lees Ùen
Øees«eece keâeÙe& veneR keâjsiee~
2. Script Mode : pewmee efkeâ nceves osKee Interactive mode Deehekeâes lelkeâeue (immediate) Feedback oslee
nw~ Ùen cenlJehetCe& nw keäÙeeWefkeâ Deehe legjvle Øees«eece keâe heefjCeece (result/output) osKe mekeâles nQ~ uesefkeâve Interactive
mode ceW Ùen megefJeOee veneR nesleer nw efkeâ Deeheves pees Øees«eece efueKee nw, Gmes mesJe (Save) keâjkesâ yeeo ceW run keâj mekeWâ~
Fmekesâ efueS Python's IDLE keâe Script mode yeveeÙee ieÙee nw, efpemeceW Deehe keâesF& Øees«eece efueKekeâj Gmes Save
keâj mekeâles nQ~ yeeo ceW edit, load Ùee run keâj mekeâles nQ~
Ùen Deehekesâ efueS Word processor keâer lejn nw, efpemeceW keâesF& Øees«eece efueKeles meceÙe GmeceW find and replace,
copy, cut, paste Deeefo functions keâe ØeÙeesie keâj mekeâles nQ~
keâesF& Øees«eece efueKeves kesâ efueS Deehe Skeâ Script Mode Window keâes open keâj mekeâles nQ~ File Menu mes Deehe
Skeâ veF& window open keâj mekeâles nQ~ Ùen window efyeukegâue note pad Ùee word pad keâer lejn nesleer nw~ Deye FmeceW
Yeer Skeâ Program efueKeles nQ leLee
Print ("Hello Python")
18 y keâchÙetšj Øees«eeeEceie ÙeteEpeie heeÙeLeve
Deye Enter Key press keâjles nQ~ uesefkeâve Deye kegâÚ veneR neslee nw Deewj ve ner keâesF& DeeGšhegš nceW efoKeeF& oslee nw~ Deye Fme
Øees«eece keâes meyemes henues Save keâjvee nesiee~ efheâj Deehe Fmes run keâj mekeâles nQ~ Fmekesâ efueS :
goto file menu
¯
Save As
¯
Hello.py
Deye Deehe keâe Øees«eece keâchÙetšj ceW mesJe (save) nes peeÙesiee~
Note : Python keâe keâesF& Yeer Øees«eece Save keâjles meceÙe Gmekeâe file extension .py ner jKevee neslee nw, efpememes IDLE
Python keâer program keâer file keâes Deemeeveer mes recognize keâj mekeWâ~
Deye Øees«eece keâes run keâjves kesâ efueS run module hej click keâjvee nesiee, leye Deehekeâer window hej Øees«eece keâe
heefjCeece (output) efoKeeF& osiee~
Hello Python
Python interpreter ceW Skeâ built-in function help Skeâ object kesâ ™he ceW efoÙee ieÙee neslee nw~ Ùen
interactive use kesâ efueS GheueyOe neslee nw, pees help system keâes invoke keâjlee nw~ Fmes invoke keâjves kesâ efueS help
Ùee help ('object') type keâjvee neslee nw~
leLee Fmemes exit keâjves kesâ efueS 'q' keâes press keâjvee neslee nw~
>>> help ('for') – This display the help for function.
otmejer Programming languages keâer YeeBefle Python ceW Yeer kegâÚ reserved Ùee keyword nesles nQ,efpevnW nce
meeceevÙe identifier kesâ ™he ceW ØeÙeesie veneR keâj mekeâles nQ~
heefjÛeÙe y 19
and del for is raise
assert elit from lambda return
break else global not try
class except it or while
continue exec import pass yield
det finally in print false
true nonlocal with
Table : Python Reserved Words
Python ceW kegâue 33 keyword nQ~ uesefkeâve Gvekeâer mebKÙee Ieš Ùee yeÌ{ mekeâleer nw~
True, False, None keâes ÚesÌ[keâj meYeer keywords lowercase Ùeeefve Úesšs De#ejeW ceW nesles nQ~
# Keywords in Python :
v Keywords are the reserved words.
v They are used to define the syntax and structure of Python language.
v We cannot use a keyword as a Variable name, Function name or any other identifier.
v In Python, keywords are case sensitive.
v All the keywords except True, False and None are in lowercase.
v There are 35 keywords in Python.
Keyword.is key word (X)
v Return true if X is a Python keyword.
# What is identifier in Python :
v An idenfifier is a name used to identify a variable, function, class, module or other
object.
Ex. – X = 10 X
10
Y = 20 Y
20
"Names" efkeâmeer Yeer Øees«eeeEceie ueQiJespe keâe yengle ner cenòJehetCe& efnmmee neslee nw~ nceW Module keâes Skeâ veece (GÅee0
Covert) leLee Module kesâ Devoj Function osvee neslee nw~
Variable : Variable keâe ØeÙeesie efkeâmeer Names keâes Value assign keâjves kesâ efueS efkeâÙee peelee nw~ pewmes C, F
Deeefo~
Identifier : Python ceW efkeâmeer variable, function, class, module Ùee DevÙe object keâe pees veece (Name)
jKee peelee nw Ùee ØeÙeesie efkeâÙee peelee nw, Gmes ner nce identifier keânles nQ~
uesefkeâve identifier yeveeÙes peeves kesâ kegâÚ efveÙece efveOee&efjle efkeâÙes ieÙes nQ pees De«eJeled nQ—
20 y keâchÙetšj Øees«eeeEceie ÙeteEpeie heeÙeLeve
1. keâesF& identifier, lowercase (a to z), uppercase (A to Z), digit (0 mes 9), underscore (_) keâe
Skeâ efceßeCe (combination) neslee nw~ Ùee Fvemes ner efceuekeâj yevee neslee nw~ GoenjCe—my_car, vor_1,
print123, x_19 Deeefo~
2. keâesF& Yeer identifier keâYeer Yeer digit (0 mes 9) mes veneR Meg™ nesiee~ pewmes 1var Skeâ invalid leLee var1
Skeâ valid identifier nw~
3. Keywords Ùee Reserved word keâe ØeÙeesie nce identifier kesâ ™he ceW veneR keâj mekeâles nQ~
4. Python ceW efkeâmeer Yeer identifier kesâ Devoj !, #, @, $, % Deeefo efJeMes<e character keâes veneR jKee
peevee ÛeeefnS~
5. efkeâmeer Yeer identifier kesâ yeerÛe ceW peien (Space) veneR nesveer ÛeeefnS~
6. Identifier keâer length keâer keâesF& meercee efveOee&efjle veneR keâer ieF& nw~
7. Python Skeâ case sesitive programming language nesves kesâ keâejCe Name SJeb name oes
Deueie-Deueie identifier ceeves peeles nQ~
Identifier Validity Reason
1. Roundoff Valid
2. ‘Round off’ Invalid –Use of ‘ ’
3. Sum x Invalid –Space
4. 2020 worldcup Invalid –Digit first
5. Covid-19 Valid
6. True Invalid –Keyword
7. My@name Invalid –Special char–@
8. Java2Shore Valid
9. Cash Invalid –Special char $
10. def Invalid –Keyword
z
z=x+y 30
– It is a user defined name.
# Rules for Writing Identifiers :
1. Identifiers can be combination of letters in lowercase (a to z) or uppercase (A to Z) or digit (0
to 9) or an underscore :
a = 10 3
A = 10 3
a1 = 20 3
Aa = 20 3
– = 20 3
a_ = 20 3
x–y = 100 7
heefjÛeÙe y 21
2. An identifier cannot start with a digit.
Ex : 1x = 100 7
x1 = 100 3
3. Keywords cannot be used as identifiers.
Ex. : If = 10 7
break = 20 7
Identifier naming convention for Python
v Class, name, start with an uppercase letter and all other identifiers with a lowercase
letter.
v Starting an identifier with a single leading underscore indicates by convention that the
identifier is meant to be private.
v Starting an identifier with two leading underscores indicates a strongly private
identifier.
v If the identifier also ends with two trailing underscores, the identifier is a
language-defined special name.
Use of Python Language in Industry
v GUI based Desktop applications
v Image processing and graphic design applications
v Scientific and computational applications
v Games
v Web frameworks and web applications
v Enterprise and business applications
v Operating systems
v Language Development
v For Artificial Intelligence and Machine learning
v DIY projects such as Robots, Remote-controlled toys and video game consoles
v Prototyping
heefjefMe<š-I
n Keywords in Python
Keywords reserved words nesles nQ~
Computer programming language ceW predefined words kesâ set Meeefceue nesles nQ efpevnW keywords keâne
peelee nw~ ØelÙeskeâ keyword kesâ use kesâ efueS efveOee&efjle efveÙece keâes syntax keâne peelee nw~
ØelÙeskeâ keyword keâe Skeâ special DeLe& Deewj Skeâ special operation neslee nw keäÙeeWefkeâ efkeâmeer Yeer keyword kesâ
veece keâe ØeÙeesie python program ceW efkeâmeer identifier pewmes—variable, function, class name Deeefo kesâ efueS veneR
efkeâÙe pee mekeâlee nw~
Python ceW ueieYeie 33 keywords predefined nQ~
22 y keâchÙetšj Øees«eeeEceie ÙeteEpeie heeÙeLeve
as and from
non local
for
assent
finally
break
class raise
continue import
del in
PYTHON KEYWORDS
def lambda
elif none
else not
pass or
except while
it with
global true yield
x y x and y
0 0 0
0 1 1
1 0 1
1 1 1
Source Code :
Print(5! = 6 and 5 = = 6) # False
Print(5! = 6 and 5 = = 5) # True
Print(5! = 6 and 5 = = 6 and 4 < 6) # False
Output : False
True
False
2. ‘as’ Keyword
‘as’ keyword keâe Fmlesceeue module keâes keâesF& veece Ùee Gheveece (alias) veece oskeâj import efkeâÙee peelee nw~
Source Code :
import keyword as pykey
print(pykey.kwlist)
Output : [‘False’, ‘None’, ‘True’, ‘and’, ‘assert’, ‘break’, ‘class’, ‘continue’, ‘def’, ‘del’, ‘elif’, ‘else’,
‘except’, ‘finally’, ‘for’, ‘from’, ‘global’, ‘if’, ‘import’, ‘in’, ‘lambda’, ‘nonlocal’, ‘not’, ‘or’, ‘pass’, ‘raise’,
‘return’, ‘try’, ‘while’, ‘with’, ‘yield’]
heefjÛeÙe y 23
3. ‘assert’ Keyword
Python program ceW debugging kesâ efueS ‘assert’ keyword keâe use efkeâÙee peelee nw~
‘assert’ keyword kesâ meeLe peye condition true nesleeR nw leye kegâÚ veneR neslee Deewj peye condition false nesleer
nw leye ‘Assertion Error’ occur neslee nw~
Source Code :
assert 4 = = 4 #No Exception
assert 4 = = 5 # Assertion Error Exception occured
Ouput : assert 4 = = 5 #Assertion Error Exception Occured Assertion Error
4. ‘break’ Keyword
‘break’ keyword, loop execution Deewj transfer control keâes loop kesâ Devle lekeâ meceehle keâjves kesâ efueS use
efkeâÙee peelee nw~
Source Code :
for i in range (5) :
if (i = = 3) :
break
print(i)
print(“End of execution”)
Output : 0
1
2
End of execution
5. ‘class’ Keyword
User kesâ Éeje classes keâes define keâjves kesâ efueS ‘class’ keyword keâe use efkeâÙee peelee nw~
Classes ceW kegâÚ attributes Deewj kegâÚ methods nesles nQ~ Classes ‘OOP’ ceW meyemes Important part nw~
Source Code :
Class my Class :
def func ( ) :
print(“Hello”)
Myclass.func()
Output : Hello
6. ‘Continue’ Keyword
‘Continue’ keyword keâe ØeÙeesie iterate nes jns kegâÚ statements keâes skip keâjves kesâ efueS efkeâÙee peelee nw~
‘Continue’ keâe ØeÙeesie iterate ve nes jns loop keâes iterate keâjves kesâ efueS efkeâÙee peelee nw~
Source Code :
a=0
while (a < 10) :
if (a = = s) :
24 y keâchÙetšj Øees«eeeEceie ÙeteEpeie heeÙeLeve
print (“skipped value of a is” a)
a=a+1
continue
print (“value of a is”, a)
a=a+1
Output : Value of a is 0
Value of a is 1
Value of a is 2
Value of a is 3
Value of a is 4
Value of a is 5
Value of a is 6
Value of a is 7
Value of a is 8
Value of a is 9
7. ‘def’ Keyword
‘def’ keyword keâe use user-defined function yeveeves kesâ efueS efkeâÙee peelee nw~
Source Code :
def func(a) :
print(“value of a is”, a)
fun(5)
Output : Value of a is 5
8. ‘del’ Keyword
Python ‘del’ keyword keâe DeLe& delete neslee nw~
‘del’ keyword keâe ØeÙeesie list, tuple, dictionary Ùee efkeâmeer Deewj collection ceW element keâes delete keâjves
kesâ efueS efkeâÙee peelee nw~
Source Code :
list = [1, 4, 3, 5, 7]
del list [2]
print (list)
Ouput : [1, 4, 5, 7]
‘del’ keyword kesâ DeueeJee remove () function keâe Yeer ØeÙeesie efkeâÙee peelee nw~
Source Code :
list = [1, 4, 3, 5, 7]
list.remove (3)
print(list)
Output : [1, 4, 5, 7]
heefjÛeÙe y 25
9. ‘elif’ Keyword
‘elif’ keyword keâe ØeÙeesie if Deewj else keyword kesâ meeLe efkeâÙee peelee nw~ Skeâ mes DeefOekeâ conditions keâes
check keâjves kesâ efueS ‘elif’ keâe ØeÙeesie efkeâÙee peelee nw~
Source Code :
a = 11
if (a < 10)
print (“a is less than 10”)
elif (a > 10) :
print(“a is greater than 10”)
else :
print(“a is greater than 10”)
Output : a is greater than 10
10. ‘else’ Keyword
‘else’ keyword keâe ØeÙeesie if, elif Ùee efmehe&â if kesâ meeLe efkeâÙee peelee nw~ Deiej if Deewj elif keâer condition
false nesleer nw lees else keâe statement execute neslee nw~
Source Code :
a = 11
if (a%2 = = 0) :
print (“Number of even.”)
else :
print (“Number of odd.”)
Output : Number is odd.
11. ‘except’ Keyword
‘except’ Keyword keâe ØeÙeesie exception/error handling kesâ efueS efkeâÙee peelee nw~ try clause kesâ meeLe
except keâe ØeÙeesie efkeâÙee peelee nw~
Example ceW try block ceW pees Yeer exception occur nesiee, Gmekeâe legjvle block execute nes peeÙesiee~
Example 1 :
Source Code :
try :
a
except Zero Division Error :
print (“Divided byZero Error”)
except Name Error
print (“as is not found”)
Output : a is not found
Example 2 :
Source Code :
try :
26 y keâchÙetšj Øees«eeeEceie ÙeteEpeie heeÙeLeve
4/0
except ZeroDivisionError :
print (“Divided by ZeroError”)
except NameError :
print(“a is not found”)
Output : Divided by Zero Error
12. ‘nonlocal’ Keyword
‘nonlocal’ keyword keâe ØeÙeesie nested function kesâ efueS efkeâÙee peelee nw~
‘nonlocal’ variable global veneR nesles~ Deiej inner function ceW Gvekeâes efueÙee peelee nw lees outer functions ceW
Gvekeâer value heefjJee|lele veneR nesleer nw~
Without nonlocal
Source Code :
Var = 2
def main func () :
var = 5
def subfunc ( ):
var = 10
print("SubFun C :", var)
SubFunc()
‘False’ keyword boolean false keâe JeCe&ve keâjlee nw~ Deiej oer ieF& condition Ùee statement ieuele nes peelee
nw lees False; return efkeâÙee peelee nw~ False ‘0’ kesâ yejeyej neslee nw~
Source Code :
print(False = = 0)
print(4 = = 5)
Output : True
True
14. ‘Finally’ Keyword
‘Finally’ keyword keâe ØeÙeesie ‘try-except’ Ùee ‘try’ block kesâ meeLe efkeâÙee peelee nw~
exception/error nes Ùee vee nes lees finally block execute neslee nw~
Source Code :
try :
a
except NameError :
print(“NameError Exception Occured”)
Finally :
print(“Always executed”)
Output : NameError Exception Occured
Always executed
heefjÛeÙe y 27
print(“mainFunC :”, var)
mainFunc()
print(“global : ”, var)
Output : SubFunC : 10
mainFunC : 5
global : 2
With nonlocal
Source Code :
Var = 2
def mainFunc() :
Var = 5
def SubFunc() :
nonlocal var
Var = 10
print (“SubFunC :”, var)
SubFunc()
print (“global”, var)
Output :
SubFunc : 10
MainFunc : 10
Global : 2
15. ‘for’ Keyword
‘for’ Keyword keâe ØeÙeesie looping kesâ efueS efkeâÙee peelee nw~ Collection kesâ elements keâes iterate keâjves kesâ
efueS ‘for’ loop keâe ØeÙeesie efkeâÙee peelee nw~
Source code :
list = [“Rakesh”, “Ramesh”, “Suresh”]
for n in list
print(n)
Output : Rakesh
Ramesh
Suresh
16. ‘from’ Keyword
‘from’ Keyword keâe ØeÙeesie ‘import’ keyword kesâ meeLe efkeâÙee peelee nw~ kegâÚ special function Ùee
attribute keâes import keâjves kesâ efueS ‘from’ keâe ØeÙeesie efkeâÙee peelee nw~
Deiej efmehe&â ‘Module’ keâes import efkeâÙee peelee nw Deewj GmeceW mes fun1 () keâes uesvee heÌ[lee nw lees +
‘Module.Func ()’ efueKevee heÌ[lee nw~
Module.p4
28 y keâchÙetšj Øees«eeeEceie ÙeteEpeie heeÙeLeve
(i) Source Code :
def funC1() :
print(“I am in funC1”)
def funC2() :
print(“I am in funC2”)
Sample.py
Source Code :
from Module import FunC1
funC(1)
Output : I am in fun 1
Example 2.
Source Code :
From math import sqrt
print (sqrt(25))
Output : 5
17. ‘Global’ Keyword
‘Global’ Keyword keâe ØeÙeesie function kesâ variable ceW global yeveeves kesâ efueS efkeâÙee peelee nw~
Differnce between Local and Global variable in fun
test1.py
Source Code :
def funC( ) :
string = “Local variable”
print(string)
funC()
print(string)
Output : Local variable
print(string)
Name Error : name ‘string’ is not defined
18. ‘if’ Keyword
‘if’ keyword keâe ØeÙeesie if statement, if_else statement Deewj if_elif_else statement ceW efkeâÙee peelee nw~
Source Code :
var = 0
if (var = = 0):
print (“var is equal to 0”)
Output : var is equal to 0
19. ‘Import’ Keyword
‘Import’ keyword keâe ØeÙeesie Module keâes Current program hej import keâjves kesâ efueS efkeâÙee peelee nw~
Example hej math module keâes import efkeâÙee peelee nw~
heefjÛeÙe y 29
Source Code :
Import math
print(Math.sqrt (16))
Output : 4.0
20. ‘In’ Keyword
‘In’ keyword keâe ØeÙeesie collections (list, tuple, string, dictionary) ceW element nw Ùee veneR, Ùen check
keâjkesâ boolean value return keâjlee nw~ For loop ceW Yeer ‘in’ keyword keâe nesvee DeefveJeeÙe& neslee nw~
Source Code :
list = [6, 8, 4, 7, 8]
print = (7 in list) # True
print = (10 in list) # False
Output : True
False
21. ‘is’ Keyword
‘is’ keyword keâe ØeÙeesie object equality kesâ efueS efkeâÙee peelee nw~ Ùes keyword ‘= =’ operator kesâ
LeesÌ[e-yengle meceeve neslee nw~ Deiej oes objects equal nesles nQ lees ‘True’ return neslee nw~ Ùeefo equal veneR nesles nQ lees
‘false’ return neslee nw~
Source Code :
print(“one” is “are”) # True
print (“one” is 1) # False
print (“” is “”) # True
print (“” = = “”) # True
print ([] is []) # False
print ([] = = []) # False
Output : True
False
True
True
True
False
True
22. ‘lambda’ Keyword
‘lambda’ Keyword mes anonymous (without name) function keâes create efkeâÙee peelee nw~ name Jeeues
function ceW ‘def’ keyword keâe ØeÙeesie efkeâÙee peelee nw Deewj anonymous function ceW ‘def’ kesâ yepeeÙe ‘lambda’
keyword keâe ØeÙeesie efkeâÙee peelee nw~ lambda function kesâ efueS return statement veneR neslee nw~ Function kesâ
arguments function nesves kesâ keâejCe Fvekeâes efkeâmeer variable hej assign efkeâÙee peelee nw Deewj Gme variable kesâ Éeje
call efkeâÙee peelee nw~
Deeies efoS ieS oesveeW Examples keâe celeueye Skeâ ner nw~
30 y keâchÙetšj Øees«eeeEceie ÙeteEpeie heeÙeLeve
Normal Function Example
Source Code :
def funC(a, b) :
return a*b
print (funC (5, 5))
Output : 25
23. ‘None’ Keyword
Python ceW ‘None’ keyword keâe ØeÙeesie efkeâÙee peelee nw~ uesefkeâve Fmekeâer programming languages ceW ‘null’
keyword keâe ØeÙeesie efkeâÙee peelee nw~ ‘None’ keâe celeueye keâesF& value veneR nesleer~ Deiej function keâesF& value retrun
veneR keâjlee nw lees ‘None’ return keâjlee nw~
Source Code :
print (“None = =”)
print (None = = 0)
print (None = = None)
print (type (None))
Output : False
False
False
<class ‘none type’>
24. ‘not’ (Logical NOT) Keyword
‘not’ keyword keâe ØeÙeesie efoS ieS operand keâes True mes False Ùee False mes true ceW convert keâjlee nw~
0 = True
1 = False
True
Table of
x not x
0 1
1 0
Source Code :
print (not (5! = 6) # False
print (not (3! = 5) # True
Output : False
True
25. ‘or’ (Logical OR) Keyword
‘or’ keyword keâe ØeÙeesie peye efoS ieS oprands ceW mes Skeâ Yeer oprand true neslee nw, leye true return efkeâÙee
peelee nw~
heefjÛeÙe y 31
0 = True
1 = False
True Table
of
‘OR’
x y x or y
0 0 0
0 1 0
1 0 0
1 1 1
Source Code :
print(5! = 6 or 5 = = 6) # True
print(5! = 6 or 5 = = 5) # True
print(5! = 6 or 5 = = 6 or 4 < 6) # True
print(5! = 6 or 5 > 6 or 6 < 6) # False
Output : True
True
True
False
26. ‘pass’ Keyword
‘pass’ keyword keâe ØeÙeesie loops, functions Deewj classes keâes efjkeäle jKeves kesâ efueS neslee nw~ Ùes Skeâ null
statement/block neslee nw~ ‘pass’ keâe Fmlesceeue peye efkeâÙee peelee nw, leye Ùes kegâÚ veneR keâjlee nw~
pass statement keâe Fmlesceeue Skeâ ner line hej Ùee seperate line hej Yeer efkeâÙee peelee nw~
Source Code :
def func() : pass
while 4 < 5 :
pass
Output :
27. ‘Raise’ Keyword
‘raise’ keyword keâe ØeÙeesie Exception handing kesâ efueS efkeâÙee peelee nw~ raise keyword mes custom
exception/error keâe efvecee&Ce efkeâÙee peelee nw~ pÙeeoelej programming languages ceW ‘throw’ keyword keâe
Fmlesceeue efkeâÙee peelee nw~ uesefkeâve Python ceW ‘raise’ keâe Fmlesceeue efkeâÙee peelee nw~
Source Code :
1. a = 5
2. if (a > 2) :
3. raise Exception (‘a should not exceed 2’)
32 y keâchÙetšj Øees«eeeEceie ÙeteEpeie heeÙeLeve
Output :
Exception : a should not exceed 2
28. return keyword
return keyword keâe ØeÙeesie function kesâ DeeefKej ceW efkeâÙee peelee nw~ Function keâes Deiej keâesF& value return
veneR keâer peeleer nw lees null/none return efkeâÙee peelee nw~
Source Code :
def func(a) :
b = a*a
return b
print (func(5))
Output : 25
29. ‘True’ Keyword
‘True’ keyword Skeâ Boolen value nw~ logical Ùee comparism operations ceW boobean value (True,
False) return efkeâÙee peelee nw~ True keâer value ‘1’ Deewj False keâer value ‘0’ nesleer nw~
Source Code :
print (True = =1)
print (false = = 0)
print (1 = = 1)
print (True + True)
Output : True
True
True
2
30. ‘Try’ Keyword
‘Try’ keyword keâe ØeÙeesie excepting handing ceW efkeâÙee peelee nw pees code exception check keâjlee nw~
FmeceW code keâes try block hej efueKee peelee nw~
Source Code :
Try :
a
except Name Error :
print (“value of a is not defined”)
Output : Value of a is not defined
31. ‘While’ Keyword
‘While’ keyword keâe ØeÙeesie looping kesâ efueS efkeâÙee peelee nw~ peye lekeâ while condition true nesleer nw, leye
lekeâ statement iterate neslee jnlee nw Deewj Deiej condition false Ùee loop break efkeâÙee peelee nw, leye loop keâe
iteration yevo nes peelee nw~
heefjÛeÙe y 33
Source Code :
a=0
while a < 10 :
print (“value of a is”, a)
a=a+1
Output :
Value of a is 0
Value of a is 1
Value of a is 2
Value of a is 3
Value of a is 4
Value of a is 6
Value of a is 7
Value of a is 8
Value of a is 9
32. ‘with’ Keyword
‘with’ keyword keâe ØeÙeesie File handling kesâ efueS efkeâÙee peelee nw~ peye file kesâ efueS ‘with-as’ statement
keâe ØeÙeesie efkeâÙee peelee nw leye file keâes close keâjves keâer pe™jle veneR heÌ[leer nw~
veerÛes efoS ieS oesveeW examples keâe celeueye Skeâ ner nw~
(1) without ‘with’
Source code :
file = open (“text file.text”, “w”)
file.write (“Hellow world”)
file.close ()
textfile.txt
Hellow world
(2) with ‘with’
Source Code :
with open (“text file.text”, “w”) as file :
file.write (“Hello world”)
text file.txt
Hello world
33. ‘yield’ Keyword
‘yield’ keyword keâe ØeÙeesie function kesâ efueS return statement pewmes efkeâÙee peelee nw~ ‘yield’ keyword
generator keâes return keâjlee nw~ return statement; function kesâ DeeefKej ceW function keâes suspend keâjkesâ JeneB mes
Deeies Jeeheme start neslee nw~ Normal function peneB hej Kelce neslee nw, JeneB hej Jeeheme veneR Deelee nw~
Source Code :
def func-name ( ) :
34 y keâchÙetšj Øees«eeeEceie ÙeteEpeie heeÙeLeve
yield 5
yield 10
yield 15
for y in func-Name ( ) :
print (4)
Output : 5
10
15
next () function keâe ØeÙeesie ‘yield’ kesâ efueS efkeâÙee peelee nw~ next () function generate object keâes ueskeâj
Deieueer value return keâjlee nw~
Source Code :
def func-name ( ) :
yield 5
yield 10
yield 15
y = func-name ( )
print (next (4))
print (next (4))
print (next (4))
Output : 5
10
15
cenòJehetCe& Øees«eece
n Program 1
#Write a Program to print Hello World and print your name
print(“Hello World”)
print(“Rajshree”)
#output:
heefjÛeÙe y 35
DeYÙeeme ØeMveeJeueer
- A. Keeueer mLeeve Yejes (Fill in the blanks)
1. Python was created by ……… .
2. To start python from the command prompt, use the command ……… .
3. To run python script file named t.py, use the command ……… .
4. A python paragraph comment uses the style ……… .
5. In python, a syntax error is detected by the ……… at ……… .