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
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()