0% found this document useful (0 votes)
15 views45 pages

PYTHON

Ffidsihddbbhhd

Uploaded by

mithunsiva8
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views45 pages

PYTHON

Ffidsihddbbhhd

Uploaded by

mithunsiva8
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Python Programming

P
Y by
T
H Dr.T.DHEEPAK
O Assistanrt Professor
N Department of Computer Science
P Government arts and Science College
R Perambalur
O
G
R
A
M
M
I
N 1
30/03/2022 Dr.V.Priya/Gov/Perambalur 1
G
P
Y
T
H
O
N

P
R
O
G
R
A
M
M
I
N
30/03/2022 Dr.V.Priya/Gov/Perambalur 2
G
Versions Releasing Months and Dates
Python 0.9.0 February 1991

Python 1.0 January 1994

Python 2.0 October 2000

Python 2.7.0 - EOL - Jan 2020 July 2010

Python 3 December 2008

Python 3.6 December 2016

Python 3.6.5 March 2018

Python 3.7.0 May 2018

Python 3.8 October 2019

Python 3.9 October 2020

Python 3.10-Current Version March 2022

30/03/2022 Dr.V.Priya/Gov/Perambalur 3
Programming Paradigm

P I mp e r a t i v e Object Oriented F u n c t i o na l Procedural


Y
T Computation is Relies o n d a t a fie l d s Every s t a t e m e n t is Tas ks a r e t r e a t e d a s
H performed as a that are treated as called a s a n step -by-st e p
O objects a n d iterations where
direct change to m a n i p u l a t e d only
equation and state c o m m o n tasks are
N program state. t h r o u g h prescri b ed o r m u t a b l e d a t a is placed i n f u n c t i o n s
This style is methods avoided. I.e t h a t a r e called a s
P especially useful advantage for needed. Thi s codi ng
R when manipulating parallel style favors i t erat i on ,
s e q u e n c i n g,
O data structures and processing select ion, a n d
G produces elegant modularization.
R yet simple code. P yt h o n excels i n
A Python fully implementing this
M part i cu l a r p a r a d i g m
implements this
M
paradigm.
I
N
30/03/2022 Dr.V.Priya/Gov/Perambalur 4
G
What is Python?

● A Simple, Object Oriented, i n t e r p r e t e d , H i g h Level


P
Y P r o g r a m m i n g Language.
T ● It h a s efficient h i g h level d a t a s t r u c t u r e s w i t h d y n a m i c
H
O typing.
N ● It is u s ef u l f o r Rapid Application D ev el o p men t a s well a s
P f o r s cr i p t i ng p u r p o s es .
R
O ● It h a s ex t en sive library a n d s u p p o r t m o d u l e s a n d
G p ack ag es w i t h code reusability.
R
A ● F u r t h e r m o d e it is free distributable .
M
M
I
N
30/03/2022 Dr.V.Priya/Gov/Perambalur 5
G
Why Python?

P
Y ● I n c r e a s e d productivity
T
H ● No Co mp ila tion p r o c e s s
O
N ● Edit - t e s t - d e b u g cycle s o f a s t
P
R
● D e b u gging e a s y
O
G
R
A
M
M
I
N 30/03/2022 Dr.V.Priya/Gov/Perambalur 6
G
Conit…
So f tw a r e Quality : r e a d a b l e cod e, r e u s e , m a i n t a i n a b l e
P Developer Prod uctiv ity : s m a l l e r cod e, ea s y t e s t d e b u g
Y
T mechanism
H
O P r o g r a m portability : R u n o n a n y p l a t f o r m
N S u p p o r t libraries : v a s t a m o u n t of p a c k a g e s , t h i r d p a r t y
P libraries
R
O C o m p o n e n t i n t e g r a t i o n : In v o k e C, C++ a n d Java Libraries,
G communicate with any framework
R
A
M E n j o y me n t : Love t o P r o g r a m
M
I
N
30/03/2022 Dr.V.Priya/Gov/Perambalur 7
G
P
Y Guido Van Rossum who started a hobby
T
H p r o g r a m m i n g proj ect i n 1989 d u r i n g t h e holiday s
O
N developed P y t h o n L an g u ag e ( n a m e d i n m e m o r y of

P “ M o n t y Flying Circus”). H e i n s p i r e d f r o m ABC


R
O L a n g u a g e a n d AMOeBa O perating S y s t e m
G
R
A
M
M
I
N 30/03/2022 Dr.V.Priya/Gov/Perambalur 8
G
•Simple • Interpreted
P
Y •Easy to learn •Object oriented
T
H •Free and Open •Extensible
O
N Source •Embedded
P •High level Language •Rapid application
R
O development
G
R
A
M
M
I
N 30/03/2022 Dr.V.Priya/Gov/Perambalur 9
G
● D yn a mic Typ in g
P
P ● No variable d e c l a r a ti o n
Y
T O ● A u to ma tic allocation a n d g a r b a g e
H
O W collection
N E
P R ● S u p p o r t s classes, m o d u l e s a n d
R
O F e x c e p ti o ns
G U
R ● Reusability a n d s t r u c t u r e d
A L
M ● Data C o n t ai n er s
M
I
N
30/03/2022 Dr.V.Priya/Gov/Perambalur 10
G
E
P
Y a
T s ● Type a n d r u n
H
O y ● No c o mp i l a t i o n a n d link
N
● I n te rac tive p r o g r a m m i n g
P T
R
o ● Rapid d e v e l o p m e n t
O
G
R
● Simple, s ma l l a n d m o r e flexible
A u
M
M
s
I e
N
30/03/2022 Dr.V.Priya/Gov/Perambalur 11
G
P
Y
statically typed language
T A language in which types are fixed at compile time. Most
H
O
N
statically typed languages enforce this by requiring you to
P declare all variables with their datatypes before using them.
R
O Java and C are statically typed languages.
G
R
A
M
M
I
N 30/03/2022 Dr.V.Priya/Gov/Perambalur 12
G
P dynamically typed language
Y
T A language in which types are discovered at execution time; the
H
O opposite of statically typed. VBScript and Python are dynamically
N
typed, because they figure out what type a variable is when you
P
R
O
first assign it a value.
G
R
A
M
M
I
N 30/03/2022 Dr.V.Priya/Gov/Perambalur 13
G
P
Y
T
strongly typed language
H A language in which types are always enforced. Java and
O
N
Python are strongly typed. If you have an integer, you can't
P
R treat it like a string without explicitly converting it.
O
G
R
A
M
M
I
N 30/03/2022 Dr.V.Priya/Gov/Perambalur 14 18
G
P
Y weakly typed language
T
H A language in which types may be ignored; the opposite of strongly
O
N typed. VBScript is weakly typed. In VBScript, you can concatenate
P the string '12' and the integer 3 to get the string '123', then treat that
R
O as the integer 123, all without any explicit conversion.
G
R
A
M
M
I
N
30/03/2022 Dr.V.Priya/Gov/Perambalur 15
G
P
So Python is both dynamically typed
Y
T
H
(because it doesn't use explicit data type
O
N
declarations) and strongly typed (because
P
R
O once a variable has a data type, it actually
G
R
A matters).
M
M
I
N
30/03/2022 Dr.V.Priya/Gov/Perambalur 16
G
I
P D ● N a m e f o r variable, f u n c t i o n s , class,
Y
T
E m o d u l e s a n d o t h e r obj ects
H N ● Begin w i t h alphabets( A - Z, a - z)
O
N T and with numbers, letters or
I underscore.
P
R F ● Special c h a r a c t e r s n o t allowed
O
G
I ● Case Sensitive
R E ● Class N a m e s t a r t s w i t h Capital L et t er
A
M R
M
I
N
30/03/2022 Dr.V.Priya/Gov/Perambalur 17
G -
● Multiline c o m m e n t s w i t h “ ” ” o r ‘’’
P
Y ● C o m m e n t s b e g in w i t h # ( b e g i n n i n g o r
T sideways)
H
O
N
● H ere block is i d e nt i fi e d w i t h
i n d e n t a t i o n a n d n o t w i t h {}
P
R ● Len gt hy s t a t e m e n t s shall be s e p a rate d
O
G with \
R
A ●● No
S t attyepm
e deenctlsa rwaitti ho ni n {} [] () n o n e e d w i t h \
M
M
I
N
30/03/2022 Dr.V.Priya/Gov/Perambalur 18
G -
A
P
Y s
T s ● a = 10
H
O i ● Pi = 3.14
N g ● n a m e = ‘murali’
P n ● a = 10; b = 2 0
R
O e ● a, b = 10, 2 0
G m ● a=b=c=5
R
A n
M t
M
I s
N
30/03/2022 Dr.V.Priya/Gov/Perambalur 19
G -
D
P a
Y
T t ● N u me r i c
H
O
a ● String
N T ● List
P y ● Tuple
R p ● Dictionary
O
G e ● Boolean
R s
A
M
M
I
N
30/03/2022 Dr.V.Priya/Gov/Perambalur 20
G -
N
P u
Y
T m ● int
H
O
b ● float
N e ● c o mp l e x
P r ● bool
R s ● string
O
G ● b y t es
R
A
M
M
I
N
30/03/2022 Dr.V.Priya/Gov/Perambalur 21
G -
+ Addition Adds values o n e i t h e r side of t h e o p e r a t o r

P - S ubt ract i on S ubt ract s r i gh t h a n d o p e r a n d f r o m


Y left h a n d o p e r a n d
T
* Multiplicat ion Multiplies values o n e i t h e r side of t h e
H operator
O
N / Division Divides left h a n d o p e r a n d by r i gh t h a n d
operand
P ** Exponent Performs expo-
R n e n t i a l ( p o w e r ) calcul at ion o n o p e r a t o r s
O
G // Floor Division T h e division of o p e r a n d s w h e r e t h e
R r e s u l t is t h e q u o t i e n t i n w h i c h t h e digits aft er
t h e deci m al p o i n t a r e rem oved.
A
M % Modul o R e m a i n d e r of t h e division
M Division
I
N 30/03/2022 Dr.V.Priya/Gov/Perambalur 22 26
G -
D

+= Addition
P
Y -= S ubt ract i on
T
H *= Multiplicat ion
O
N /= Division

P **= Exponent
R
//= Floor Division
O
G %= Modul o
R Division
A
M
M
I
N 27
30/03/2022 Dr.V.Priya/Gov/Perambalur 23
G
-
int() ( fl o a t / n u m e r i c s t r i n g)
P ( n u m e r i c s t r i n g , base)
Y
T float() ( i n t / n u m e r i c st ri nc)
(int)
H
O com pl ex () (int/float)
N
bool() (int/float)
P
R byt es() (int)
O
G str() (int/float/bool)
R
A chr() (int)
M
M
I
N
30/03/2022 Dr.V.Priya/Gov/Perambalur 24
G -
n o t Boolean NOT
P
Y
== equal t o
T != n o t equal t o
H
O < l e s s than
N
<= l e s s than or eq
P > greater than
R
O >= greater than or e
G
R << >> & | ^ ~
A [ bit w i s e operators]
M
M
I
N
30/03/2022 Dr.V.Priya/Gov/Perambalur 25
G -
P
Y
T
H
O
N

P
R
O
G
R
A
M
M
I
N 30/03/2022 Dr.V.Priya/Gov/Perambalur 26
G -
P
Y
T
H
O Control Structures in Python
N

P
R
O
G
R
A
M
M
I
N
30/03/2022 Dr.V.Priya/Gov/Perambalur 27
G
Control Structures
if condition: while condition:
P
Y statements statements
T
[elif condition:
H
O statements] ... for var in sequence:
N
else: statements
P statements
R
O break
G continue
R
A
M
M
I
N
G
Important Note: Indentation

P
Y •In Python, all the statements indented
T
H
by the same number of character spaces
O after a programming construct are
N
considered to be part of a single block of
P
R code.
O
G
R
A
•Python uses indentation as its method of
M grouping statements.
M
I
N
30/03/2022 Dr.V.Priya/Gov/Perambalur 29
G
If - Structures

P • The if statement of Python is similar to that of other


Y languages.
T
H
O
N if expression: Eg:
P
a=int(input(“enter a=“))
R statement(s)
O if (a%2==0):
G
R print "a is Even=",a
A
M
M Output:
I
N enter a=10
30/03/2022 Dr.V.Priya/Gov/Perambalur 30
G a is Even=10
If – else Structures
P • The if statement of Python is similar to that of
Y
T other languages.
H
Eg:
O if expression:
N
statement(s) a=int(input(“enter a:”))
P
else: if (a%2==0):
R
O statement(s) print "a is even=",a
G
R
else:
A print “a is Odd=“,a
M
M Output:
I
N enter a=5
30/03/2022 Dr.V.Priya/Gov/Perambalur 31
G a is Odd=5
If – elif-else Structures
P
Y if expression1: Eg:
T
H statement(s) a=int(input(“enter a:”))
O elif expression2: if (a==0):
N
statement(s) print "a is zero”
P elif expression3:
R elif (a>0):
O statement(s) print “a is Positive“
G else:
R
A statement(s)
else:
M print “ a is Negative”
M
I
N
30/03/2022 Dr.V.Priya/Gov/Perambalur 32
G
while Structures
•The while loop continues until the expression becomes false.
• The expression has to be a logical expression and must return either a true or a
P false value
Y
T while expression: Output:
H
O statement(s) The count is: 0
N The count is: 1
The count is: 2
P
R count = 0 The count is: 3
O while (count < 5): The count is: 4
G print 'The count is:', count Good bye!
R
A count = count + 1
M
M print "Good bye!"
I
N
30/03/2022 Dr.V.Priya/Gov/Perambalur 33
G
range()
creates a list of numbers in a specified range
P range([start,] stop[, step]) -> list of integers
Y
T •When step is given, it specifies the increment (or decrement).
H
O >>> range(5)
N [0, 1, 2, 3, 4]
P >>> range(5, 10)
R [5, 6, 7, 8, 9]
O
G
>>> range(0, 10, 2)
R
A [0, 2, 4, 6, 8]
M
M
I
N
30/03/2022 Dr.V.Priya/Gov/Perambalur 34
G
The for loop
A for loop performs the same statements for each value in a list
P
Y for iterating_var in sequence:
T
statements(s)
H
O
N •If a sequence contains an expression list, it is evaluated first.
P Then, the first item in the sequence is assigned to the iterating
R
O variable iterating_var.
G
R • Next, the statements block is executed.
A
M •Each item in the list is assigned to iterating_var, and the
M
I statements(s) block is executed until the entire sequence is
N
G
30/03/2022 exhausted. Dr.V.Priya/Gov/Perambalur 35
The for loop
Example:1
for n in range(1, 4):
P
Y print "This is the number", n
T
H
O
OUTPUT:
N
This is the number 1
P This is the number 2
R
O
This is the number 3
G
R
A The for loop uses a variable (in this case, n) to hold the current value in the list
M
M
I
N
30/03/2022 Dr.V.Priya/Gov/Perambalur 36
G
Example:2
for letter in 'Python’:
P
Y print 'Current Letter :', letter
T
H
O
N
OUTPUT:
P Current Letter : P
R
Current Letter : y
O
G Current Letter : t
R Current Letter : h
A
Current Letter : o
M
M Current Letter : n
I
N
30/03/2022 Dr.V.Priya/Gov/Perambalur 37
G
The break Statement:
P
Y •The break statement in Python terminates the current loop and
T
H resumes execution at the next statement
O • just like the traditional break found in C.
N
•The most common use for break is when some external condition is
P
R triggered requiring a hasty exit from a loop.
O • The break statement can be used in both while and for loops.
G
R
A
M
M
I
N
30/03/2022 Dr.V.Priya/Gov/Perambalur 38
G
Eg:

P for letter in 'Python‘:


Y if letter == 'h':
T
H break
O print 'Current Letter :', letter
N

P
R
O Output:
G
R
A Current Letter : P
M
M
Current Letter : y
I Current Letter : t
N
30/03/2022 Dr.V.Priya/Gov/Perambalur 39
G
The continue Statement:

P •The continue statement in Python returns the control to


Y
T the beginning of the while loop.
H
O
N
•The continue statement rejects all the remaining
P
R statements in the current iteration of the loop and moves
O
G the control back to the top of the loop.
R
A
M
M •The continue statement can be used in both while and for
I
N
30/03/2022
loops. Dr.V.Priya/Gov/Perambalur 40
G
Eg:
for letter in 'Python‘:
if letter == 'h':
P
Y continue
T print 'Current Letter :', letter
H
O
N

P
Output:
R
O Current Letter : P
G
R Current Letter : y
A
M
Current Letter : t
M Current Letter : o
I
N
Current Letter : n
30/03/2022 Dr.V.Priya/Gov/Perambalur 41
G
The pass Statement:
P
Y
T •The pass statement in Python is used when a statement is
H
O required syntactically but you do not want any command or
N
code to execute.
P
R •The pass statement is a null operation;
O
G • nothing happens when it executes. The pass is also useful in
R
A places where your code will eventually go, but has not been
M
M written yet.
I
N
30/03/2022 Dr.V.Priya/Gov/Perambalur 42
G
Eg:
for letter in 'Python':
if letter == 'h':
pass
P
Y
print 'This is pass block'
T print 'Current Letter :', letter
H
O print "Good bye!"
N Output:
Current Letter : P
P
Current Letter : y
R
O Current Letter : t
G This is pass block
R Current Letter : h
A
M Current Letter : o
M Current Letter : n
I Good bye!
N
30/03/2022 Dr.V.Priya/Gov/Perambalur 43
G
Eg:2 OUTPUT:
P
Y for value in [3, 1, 4, 1, 5, 9, 2]: Checking 3
T
print "Checking", value Checking 1
H
O if value > 8: Ignoring
N print "Exiting for loop" Checking 4
break Checking 1
P
R elif value < 3: Ignoring
O print "Ignoring" Checking 5
G continue
R
Checking 9
A print "The square is", value**2 Exiting for loop
M The square is 81
M
I
N
30/03/2022 Dr.V.Priya/Gov/Perambalur 44
G
THANK YOU

You might also like