SlideShare a Scribd company logo
1 of 21
PYTHON PROGRAMMING
WHAT IS PYTHON??
 Python is a General Purpose object-oriented
programming language, which means that it can
model real-world entities.
 The distinctive feature of Python is that it is
an interpreted language.
 The Python IDLE (Integrated Development
Environment) executes instructions one line at a
time.
PYTHON FEATURES
-STRINGS ARE IMMUTABLE
Code Comment Result
print str # prints complete string Hello, World!
print str[0] # prints first character of the string H
print str[-1] #prints last character of the string !
print str[1:5] # prints character starting from index 1 to 4
# prints str[start_at : end_at-1]
ello
print str[2:] #prints string starting at index 2 till end of the llo, World!
print str * 2 # asterisk (*) -is the repetition operator. Prints
string two times.
Hello,
Hello, World!
print str + # prints concatenated string Hello, World! Hai
STRINGS IN PYHON
PYTHON LIST,TUPLE AND DICTIONARY
 A List can hold items of different data types.
- The list is enclosed by square brackets [] where
the items are separated by commas
 Tuple is another sequence data type similar to list.
-A tuple consists of a number of values separated
by commas and enclosed within parentheses( )
Unlike list, the tuple values cannot be updated
 Dictionary is a kind of hash table. It contains key-value
pairs.
-Dictionaries are enclosed by curly braces {}
LIST IN PYTHON
Code Comment Result
print list1 # prints complete list [‘abcd’, 345,
3.2,’python’,
3.14]
print list1[0] # prints first element of the list abcd
print list1[-1] #prints last element of the list 3.14
print list1[1:3] # prints elements starting from index 1
2
# prints list1[start_at : end_at-1]
[345, 3.2]
print list1[2:] #prints list starting at index 2 till end of
the list
[3.2, ‘python’, 3.14]
print list 2 * 2 # asterisk (*) -is the repetition
Prints the list two times.
[‘abcd’, 345, 3.2,
‘python’,
3.14, 234, ‘xyz’]
print list1 + # prints concatenated lists [‘abcd’, 345,
3.2,’python’,
3.14, 234, ‘xyz’]
TUPLES IN PYTHON
Code Comment Result
print tuple1 # prints complete tuple1 (‘abcd’, 345, 3.2,
3.14)
print tuple1[0] # prints first element of the tuple1 abcd
print tuple1[-1] #prints last element of the tuple1 3.14
print # prints elements starting from index 1
# prints tuple1[start_at : end_at-1]
(345, 3.2)
print tuple1[2:] #prints tuple1 starting at index 2 till the
end
(3.2, ‘python’, 3.14)
print tuple 2 * 2 # asterisk (*) -is the repetition operator.
Prints the tuple2 two times.
(234, 'xyz', 234, 'xyz')
print tuple1 +
tuple2
# prints concatenated tuples (‘abcd’, 345,
3.14, 234, ‘xyz’)
DICTIONARY IN PYTHON
Code Comment Result
print dict1 # prints complete dictionary { ‘dept’: ‘Engg’,
‘code’:6734,
‘name’ : ‘ABCD’}
print dict1.keys() # prints all keys of dictionary { ‘dept’, ‘code’, ‘name’}
print dict1.values #prints all values of dictionary {‘Engg’, 6734, ‘ABCD’}
print dict2[‘rollno’] # print the value for the key
rollno
II-ITA24
OPERATORS –SUPPORTED BY PYTHON
 Python language supports the following types of operators.
• Arithmetic Operators
• Comparison (Relational) Operators
• Assignment Operators
• Logical Operators
• Bitwise Operators
• Membership Operators
• Identity Operators
MEMBERSHIP & IDENTITY OPERATORS
 Membership operators test for membership in a
sequence, such as strings, lists, or tuples and
explained below:
 Identity operators compare the memory locations of
two objects. MEMBERSHIP
OPERATOR
IDENTITY
OPERATOR
in is
not in is not
Membership Operartor
a = 6 b = 2
list = [1, 2, 3, 4, 5 ];
print a in list
print a not in list
print b in list
print b not in list
Output
False
True
True
False
IdentityOperartor
a = 20 b = 20
print a is b
print a is not b
b=30
print a is b
Output
True
False
False
ALIASING/ CLONNING
Aliasing
a=[1,2,3,4,5]
b=a
print(b)
 Cloning
a=[1,2,3,4,5]
b=a[:]
print(b)
 Another method
b=a.copy()
MODULES –USED TO REUSE THE CODE IN
MORE THAN ONE PROGRAM
 Math Module
c=math.sqrt(64)
d=math.log10(100.2)
print c
print d
Import Random
r=random.range(30)
print r
r=random.randint(0,5)
print r
FILE HANDLING
 Read(),write(),Open(),Close()..
Read Operation
f=open(“test.txt”,’r’)
data=f.read(10)
print(data)
f.close()
Write Operation
f=open(“test.txt”,’r’)
f.write(“python program”)
f.close()
PYTHON IN XML
 Extensible Markup Language (XML) is a markup
language which encodes documents by defining a
set of rules in both machine-readable and human-
readable format.
 Extended from SGML (Standard Generalized
Markup Language), it lets us describe the structure
of the document.
 In XML, we can define custom tags.
 XML is used as a standard format to exchange
information.
PYTHON XML PARSER
 <?xml version="1.0"?>
 <genre catalogue="Pop">
 <song title="No Tears Left to Cry">
 <artist>Ariana Grande</artist>
 <year>2018</year>
 <album>Sweetener</album>
 </song> Python XML file
 <song title="Delicate">
 <artist>Taylor Swift</artist>
 <year>2018</year>
 <album>Reputation</album>
 </song>
WE WILL MAKE USE OF TWO APIS TO DEAL WITH
PYTHON XML PARSER HERE- SAX AND DOM.
DOM PARSER
>>> from xml.dom.minidom import parse
>>> import xml.dom.minidom
>>> import os
>>> os.chdir('C:UserslifeiDesktop')
>>> DOMTree = xml.dom.minidom.parse("songs.xml") #Opening the XML
document
>>> genre=DOMTree.documentElement
>>> if genre.hasAttribute('catalogue'):
print(f'Root: {genre.getAttribute("catalogue")}')
Root: Pop
>>> songs=genre.getElementsByTagName('song') #Get all songs in the genre
Pop
>>> for song in songs: #Print each song’s details
print('Song:')
if song.hasAttribute('title'):
print(f'Title: {song.getAttribute("title")}')
artist=song.getElementsByTagName('artist')[0]
print(f'Artist: {artist.firstChild.data}')
year=song.getElementsByTagName('year')[0]
print(f'Release Year: {year.firstChild.data}')
album=song.getElementsByTagName('album')[0]
print(f'Album: {album.firstChild.data}')
PYTHON MATH LIBRARIES
 The most basic math module that is available in
Python.
 It covers basic mathematical operations like sum,
exponential, modulus, etc.
>>> from math import exp
>>> exp(3) #Calculates Exponential
USING NUMPY- ITS ABILITY TO PERFORM
LIGHTNING SPEED CALCULATIONS.
The numpy library in Python is most widely used for
carrying out mathematical operations that involve
matrices.
>>> import numpy as np
>>> mat1 = np.array([[1,2],[3,4]])
>>> mat2 = np.array([[5,6],[7,8]])
>>> np.dot(mat1,mat2)
array([[19, 22],
[43, 50]])
USING MATPLOTLIB
from matplotlib import pyplot as plt
#Plotting to our canvas
plt.plot([1,2,3],[4,5,1])
#Showing what we plotted
plt.show()
PYTHON  PROGRAMMING for first year cse students

More Related Content

Similar to PYTHON PROGRAMMING for first year cse students

Introduction to Python Programming
Introduction to Python ProgrammingIntroduction to Python Programming
Introduction to Python ProgrammingVijaySharma802
 
Introduction to Python Programming | InsideAIML
Introduction to Python Programming | InsideAIMLIntroduction to Python Programming | InsideAIML
Introduction to Python Programming | InsideAIMLVijaySharma802
 
Python 培训讲义
Python 培训讲义Python 培训讲义
Python 培训讲义leejd
 
1. python programming
1. python programming1. python programming
1. python programmingsreeLekha51
 
Introduction to python programming ( part-3 )
Introduction to python programming ( part-3 )Introduction to python programming ( part-3 )
Introduction to python programming ( part-3 )Ziyauddin Shaik
 
Python basic
Python basic Python basic
Python basic sewoo lee
 
2022-23TYBSC(CS)-Python Prog._Chapter-1.pptx
2022-23TYBSC(CS)-Python Prog._Chapter-1.pptx2022-23TYBSC(CS)-Python Prog._Chapter-1.pptx
2022-23TYBSC(CS)-Python Prog._Chapter-1.pptxsangeeta borde
 
Introduction to Python 01-08-2023.pon by everyone else. . Hence, they must be...
Introduction to Python 01-08-2023.pon by everyone else. . Hence, they must be...Introduction to Python 01-08-2023.pon by everyone else. . Hence, they must be...
Introduction to Python 01-08-2023.pon by everyone else. . Hence, they must be...DRVaibhavmeshram1
 
mooc_presentataion_mayankmanral on the subject puthon
mooc_presentataion_mayankmanral on the subject puthonmooc_presentataion_mayankmanral on the subject puthon
mooc_presentataion_mayankmanral on the subject puthongarvitbisht27
 
Chapter 2 Python Language Basics, IPython.pptx
Chapter 2 Python Language Basics, IPython.pptxChapter 2 Python Language Basics, IPython.pptx
Chapter 2 Python Language Basics, IPython.pptxSovannDoeur
 
Python Interview Questions | Python Interview Questions And Answers | Python ...
Python Interview Questions | Python Interview Questions And Answers | Python ...Python Interview Questions | Python Interview Questions And Answers | Python ...
Python Interview Questions | Python Interview Questions And Answers | Python ...Simplilearn
 

Similar to PYTHON PROGRAMMING for first year cse students (20)

Introduction to Python Programming
Introduction to Python ProgrammingIntroduction to Python Programming
Introduction to Python Programming
 
Introduction to Python Programming | InsideAIML
Introduction to Python Programming | InsideAIMLIntroduction to Python Programming | InsideAIML
Introduction to Python Programming | InsideAIML
 
Python 培训讲义
Python 培训讲义Python 培训讲义
Python 培训讲义
 
MODULE. .pptx
MODULE.                              .pptxMODULE.                              .pptx
MODULE. .pptx
 
1. python programming
1. python programming1. python programming
1. python programming
 
Introduction to python programming ( part-3 )
Introduction to python programming ( part-3 )Introduction to python programming ( part-3 )
Introduction to python programming ( part-3 )
 
Python 3.pptx
Python 3.pptxPython 3.pptx
Python 3.pptx
 
Python basic
Python basic Python basic
Python basic
 
CPPDS Slide.pdf
CPPDS Slide.pdfCPPDS Slide.pdf
CPPDS Slide.pdf
 
2022-23TYBSC(CS)-Python Prog._Chapter-1.pptx
2022-23TYBSC(CS)-Python Prog._Chapter-1.pptx2022-23TYBSC(CS)-Python Prog._Chapter-1.pptx
2022-23TYBSC(CS)-Python Prog._Chapter-1.pptx
 
Introduction to Python 01-08-2023.pon by everyone else. . Hence, they must be...
Introduction to Python 01-08-2023.pon by everyone else. . Hence, they must be...Introduction to Python 01-08-2023.pon by everyone else. . Hence, they must be...
Introduction to Python 01-08-2023.pon by everyone else. . Hence, they must be...
 
Let's golang
Let's golangLet's golang
Let's golang
 
Python basics
Python basicsPython basics
Python basics
 
Standard data-types-in-py
Standard data-types-in-pyStandard data-types-in-py
Standard data-types-in-py
 
Python course Day 1
Python course Day 1Python course Day 1
Python course Day 1
 
Python basic
Python basicPython basic
Python basic
 
Python for Beginners(v1)
Python for Beginners(v1)Python for Beginners(v1)
Python for Beginners(v1)
 
mooc_presentataion_mayankmanral on the subject puthon
mooc_presentataion_mayankmanral on the subject puthonmooc_presentataion_mayankmanral on the subject puthon
mooc_presentataion_mayankmanral on the subject puthon
 
Chapter 2 Python Language Basics, IPython.pptx
Chapter 2 Python Language Basics, IPython.pptxChapter 2 Python Language Basics, IPython.pptx
Chapter 2 Python Language Basics, IPython.pptx
 
Python Interview Questions | Python Interview Questions And Answers | Python ...
Python Interview Questions | Python Interview Questions And Answers | Python ...Python Interview Questions | Python Interview Questions And Answers | Python ...
Python Interview Questions | Python Interview Questions And Answers | Python ...
 

Recently uploaded

Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfSumit Tiwari
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxmanuelaromero2013
 
भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,Virag Sontakke
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxiammrhaywood
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformChameera Dedduwage
 
Painted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of IndiaPainted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of IndiaVirag Sontakke
 
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting DataJhengPantaleon
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTiammrhaywood
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Celine George
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityGeoBlogs
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxGaneshChakor2
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptxVS Mahajan Coaching Centre
 
Science 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsScience 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsKarinaGenton
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxSayali Powar
 
Pharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfPharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfMahmoud M. Sallam
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Sapana Sha
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsanshu789521
 
Class 11 Legal Studies Ch-1 Concept of State .pdf
Class 11 Legal Studies Ch-1 Concept of State .pdfClass 11 Legal Studies Ch-1 Concept of State .pdf
Class 11 Legal Studies Ch-1 Concept of State .pdfakmcokerachita
 

Recently uploaded (20)

Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptx
 
भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy Reform
 
Painted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of IndiaPainted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of India
 
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptx
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
 
Science 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsScience 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its Characteristics
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
 
Pharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfPharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdf
 
9953330565 Low Rate Call Girls In Rohini Delhi NCR
9953330565 Low Rate Call Girls In Rohini  Delhi NCR9953330565 Low Rate Call Girls In Rohini  Delhi NCR
9953330565 Low Rate Call Girls In Rohini Delhi NCR
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
 
Staff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSDStaff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSD
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha elections
 
Class 11 Legal Studies Ch-1 Concept of State .pdf
Class 11 Legal Studies Ch-1 Concept of State .pdfClass 11 Legal Studies Ch-1 Concept of State .pdf
Class 11 Legal Studies Ch-1 Concept of State .pdf
 

PYTHON PROGRAMMING for first year cse students

  • 2. WHAT IS PYTHON??  Python is a General Purpose object-oriented programming language, which means that it can model real-world entities.  The distinctive feature of Python is that it is an interpreted language.  The Python IDLE (Integrated Development Environment) executes instructions one line at a time.
  • 4. -STRINGS ARE IMMUTABLE Code Comment Result print str # prints complete string Hello, World! print str[0] # prints first character of the string H print str[-1] #prints last character of the string ! print str[1:5] # prints character starting from index 1 to 4 # prints str[start_at : end_at-1] ello print str[2:] #prints string starting at index 2 till end of the llo, World! print str * 2 # asterisk (*) -is the repetition operator. Prints string two times. Hello, Hello, World! print str + # prints concatenated string Hello, World! Hai STRINGS IN PYHON
  • 5. PYTHON LIST,TUPLE AND DICTIONARY  A List can hold items of different data types. - The list is enclosed by square brackets [] where the items are separated by commas  Tuple is another sequence data type similar to list. -A tuple consists of a number of values separated by commas and enclosed within parentheses( ) Unlike list, the tuple values cannot be updated  Dictionary is a kind of hash table. It contains key-value pairs. -Dictionaries are enclosed by curly braces {}
  • 6. LIST IN PYTHON Code Comment Result print list1 # prints complete list [‘abcd’, 345, 3.2,’python’, 3.14] print list1[0] # prints first element of the list abcd print list1[-1] #prints last element of the list 3.14 print list1[1:3] # prints elements starting from index 1 2 # prints list1[start_at : end_at-1] [345, 3.2] print list1[2:] #prints list starting at index 2 till end of the list [3.2, ‘python’, 3.14] print list 2 * 2 # asterisk (*) -is the repetition Prints the list two times. [‘abcd’, 345, 3.2, ‘python’, 3.14, 234, ‘xyz’] print list1 + # prints concatenated lists [‘abcd’, 345, 3.2,’python’, 3.14, 234, ‘xyz’]
  • 7. TUPLES IN PYTHON Code Comment Result print tuple1 # prints complete tuple1 (‘abcd’, 345, 3.2, 3.14) print tuple1[0] # prints first element of the tuple1 abcd print tuple1[-1] #prints last element of the tuple1 3.14 print # prints elements starting from index 1 # prints tuple1[start_at : end_at-1] (345, 3.2) print tuple1[2:] #prints tuple1 starting at index 2 till the end (3.2, ‘python’, 3.14) print tuple 2 * 2 # asterisk (*) -is the repetition operator. Prints the tuple2 two times. (234, 'xyz', 234, 'xyz') print tuple1 + tuple2 # prints concatenated tuples (‘abcd’, 345, 3.14, 234, ‘xyz’)
  • 8. DICTIONARY IN PYTHON Code Comment Result print dict1 # prints complete dictionary { ‘dept’: ‘Engg’, ‘code’:6734, ‘name’ : ‘ABCD’} print dict1.keys() # prints all keys of dictionary { ‘dept’, ‘code’, ‘name’} print dict1.values #prints all values of dictionary {‘Engg’, 6734, ‘ABCD’} print dict2[‘rollno’] # print the value for the key rollno II-ITA24
  • 9. OPERATORS –SUPPORTED BY PYTHON  Python language supports the following types of operators. • Arithmetic Operators • Comparison (Relational) Operators • Assignment Operators • Logical Operators • Bitwise Operators • Membership Operators • Identity Operators
  • 10. MEMBERSHIP & IDENTITY OPERATORS  Membership operators test for membership in a sequence, such as strings, lists, or tuples and explained below:  Identity operators compare the memory locations of two objects. MEMBERSHIP OPERATOR IDENTITY OPERATOR in is not in is not
  • 11. Membership Operartor a = 6 b = 2 list = [1, 2, 3, 4, 5 ]; print a in list print a not in list print b in list print b not in list Output False True True False IdentityOperartor a = 20 b = 20 print a is b print a is not b b=30 print a is b Output True False False
  • 13. MODULES –USED TO REUSE THE CODE IN MORE THAN ONE PROGRAM  Math Module c=math.sqrt(64) d=math.log10(100.2) print c print d Import Random r=random.range(30) print r r=random.randint(0,5) print r
  • 14. FILE HANDLING  Read(),write(),Open(),Close().. Read Operation f=open(“test.txt”,’r’) data=f.read(10) print(data) f.close() Write Operation f=open(“test.txt”,’r’) f.write(“python program”) f.close()
  • 15. PYTHON IN XML  Extensible Markup Language (XML) is a markup language which encodes documents by defining a set of rules in both machine-readable and human- readable format.  Extended from SGML (Standard Generalized Markup Language), it lets us describe the structure of the document.  In XML, we can define custom tags.  XML is used as a standard format to exchange information.
  • 16. PYTHON XML PARSER  <?xml version="1.0"?>  <genre catalogue="Pop">  <song title="No Tears Left to Cry">  <artist>Ariana Grande</artist>  <year>2018</year>  <album>Sweetener</album>  </song> Python XML file  <song title="Delicate">  <artist>Taylor Swift</artist>  <year>2018</year>  <album>Reputation</album>  </song> WE WILL MAKE USE OF TWO APIS TO DEAL WITH PYTHON XML PARSER HERE- SAX AND DOM.
  • 17. DOM PARSER >>> from xml.dom.minidom import parse >>> import xml.dom.minidom >>> import os >>> os.chdir('C:UserslifeiDesktop') >>> DOMTree = xml.dom.minidom.parse("songs.xml") #Opening the XML document >>> genre=DOMTree.documentElement >>> if genre.hasAttribute('catalogue'): print(f'Root: {genre.getAttribute("catalogue")}') Root: Pop >>> songs=genre.getElementsByTagName('song') #Get all songs in the genre Pop >>> for song in songs: #Print each song’s details print('Song:') if song.hasAttribute('title'): print(f'Title: {song.getAttribute("title")}') artist=song.getElementsByTagName('artist')[0] print(f'Artist: {artist.firstChild.data}') year=song.getElementsByTagName('year')[0] print(f'Release Year: {year.firstChild.data}') album=song.getElementsByTagName('album')[0] print(f'Album: {album.firstChild.data}')
  • 18. PYTHON MATH LIBRARIES  The most basic math module that is available in Python.  It covers basic mathematical operations like sum, exponential, modulus, etc. >>> from math import exp >>> exp(3) #Calculates Exponential
  • 19. USING NUMPY- ITS ABILITY TO PERFORM LIGHTNING SPEED CALCULATIONS. The numpy library in Python is most widely used for carrying out mathematical operations that involve matrices. >>> import numpy as np >>> mat1 = np.array([[1,2],[3,4]]) >>> mat2 = np.array([[5,6],[7,8]]) >>> np.dot(mat1,mat2) array([[19, 22], [43, 50]])
  • 20. USING MATPLOTLIB from matplotlib import pyplot as plt #Plotting to our canvas plt.plot([1,2,3],[4,5,1]) #Showing what we plotted plt.show()