0% found this document useful (0 votes)
11 views

Python ppt

Uploaded by

shreyaspiano
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

Python ppt

Uploaded by

shreyaspiano
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 124

PYTHON - An Introduction

Dr RAMANANDA H S1 Dr SMITA S NAGOUDA2


Dr K SUSHAN BAIRY3 Dr K MADHUKAR4
1 Department of Mathematics, St Joseph Engineering College, Mangaluru, INDIA.
2 Department of Mathematics,CHRIST(Deemed to be University), Central Campus, Bengaluru, INDIA.
3 Department of Mathematics, School of Applied Sciences, REVA University, Bengaluru, INDIA.
4 Department of Mathematics, BMS College of Engineering, Bull Temple Road, Bengaluru, INDIA.

PYTHON - An Introduction 1 / 63
Introduction to PYTHON What is PYTHON?

W HAT IS PYTHON?

Python is a computer programming language.

PYTHON - An Introduction 2 / 63
Introduction to PYTHON What is PYTHON?

W HAT IS PYTHON?

Python is a computer programming language.


High-level general purpose scripting language.

PYTHON - An Introduction 2 / 63
Introduction to PYTHON What is PYTHON?

W HAT IS PYTHON?

Python is a computer programming language.


High-level general purpose scripting language.
Beginner-level language that supports development of wide range of applications.

PYTHON - An Introduction 2 / 63
Introduction to PYTHON What is PYTHON?

W HAT IS PYTHON?

Python is a computer programming language.


High-level general purpose scripting language.
Beginner-level language that supports development of wide range of applications.
Guido van Rossum (1987) named it after the BBC television show ‘Monty Python’s
Flying Circus.’ (Comedy show).

PYTHON - An Introduction 2 / 63
Introduction to PYTHON History

H ISTORY

Created in early 1990’s – Guido van Rossum at Stichting Mathematisch Centrum


(CWI, see https://www.cwi.nl/), Netherlands as a successor of a language called ABC.

PYTHON - An Introduction 3 / 63
Introduction to PYTHON History

H ISTORY

Created in early 1990’s – Guido van Rossum at Stichting Mathematisch Centrum


(CWI, see https://www.cwi.nl/), Netherlands as a successor of a language called ABC.
1995 – Guido continued work on Python at Corporation for National Research Initia-
tives (CNRI, see https://www.cnri.reston.va.us/) in Reston, Virginia.

PYTHON - An Introduction 3 / 63
Introduction to PYTHON History

H ISTORY

Created in early 1990’s – Guido van Rossum at Stichting Mathematisch Centrum


(CWI, see https://www.cwi.nl/), Netherlands as a successor of a language called ABC.
1995 – Guido continued work on Python at Corporation for National Research Initia-
tives (CNRI, see https://www.cnri.reston.va.us/) in Reston, Virginia.
May 2000 – Guido and the Python core development team move to BeOpen.com to
form the BeOpen PythonLabs team.

PYTHON - An Introduction 3 / 63
Introduction to PYTHON History

H ISTORY

Created in early 1990’s – Guido van Rossum at Stichting Mathematisch Centrum


(CWI, see https://www.cwi.nl/), Netherlands as a successor of a language called ABC.
1995 – Guido continued work on Python at Corporation for National Research Initia-
tives (CNRI, see https://www.cnri.reston.va.us/) in Reston, Virginia.
May 2000 – Guido and the Python core development team move to BeOpen.com to
form the BeOpen PythonLabs team.
October 2000 – PythonLabs team moved to Digital Creations (now Zope Corporation).

PYTHON - An Introduction 3 / 63
Introduction to PYTHON History

H ISTORY

Created in early 1990’s – Guido van Rossum at Stichting Mathematisch Centrum


(CWI, see https://www.cwi.nl/), Netherlands as a successor of a language called ABC.
1995 – Guido continued work on Python at Corporation for National Research Initia-
tives (CNRI, see https://www.cnri.reston.va.us/) in Reston, Virginia.
May 2000 – Guido and the Python core development team move to BeOpen.com to
form the BeOpen PythonLabs team.
October 2000 – PythonLabs team moved to Digital Creations (now Zope Corporation).
2001 – Python Software Foundation (PSF) was formed. (Zope corporation is a spon-
soring member.)

PYTHON - An Introduction 3 / 63
Introduction to PYTHON History

PYTHON USED FOR ...

Python is used in many places.

PYTHON - An Introduction 4 / 63
Introduction to PYTHON History

PYTHON USED FOR ...

Python is used in many places.


Web and Internet Development
Desktop GUI Applications
Scientific and Numeric
Software Development
Education
Business Applications
Games and 3D Graphics
Network programming
Database Access
One of the recent growing field of expertise is ‘data science’. Many data scientists use
Python for their day-to-day work.

PYTHON - An Introduction 4 / 63
Introduction to PYTHON History

P YTHON ’ S MAJOR FEATURES

It is easy to read and write.

PYTHON - An Introduction 5 / 63
Introduction to PYTHON History

P YTHON ’ S MAJOR FEATURES

It is easy to read and write.


Python is an interpreted, interactive, object-oriented programming language

PYTHON - An Introduction 5 / 63
Introduction to PYTHON History

P YTHON ’ S MAJOR FEATURES

It is easy to read and write.


Python is an interpreted, interactive, object-oriented programming language
It incorporates modules, exceptions, dynamic typing, very high level dynamic data
types, and classes.

PYTHON - An Introduction 5 / 63
Introduction to PYTHON History

P YTHON ’ S MAJOR FEATURES

It is easy to read and write.


Python is an interpreted, interactive, object-oriented programming language
It incorporates modules, exceptions, dynamic typing, very high level dynamic data
types, and classes.
It has interfaces to many system calls and libraries, as well as to various window
systems.

PYTHON - An Introduction 5 / 63
Introduction to PYTHON History

P YTHON ’ S MAJOR FEATURES

It is easy to read and write.


Python is an interpreted, interactive, object-oriented programming language
It incorporates modules, exceptions, dynamic typing, very high level dynamic data
types, and classes.
It has interfaces to many system calls and libraries, as well as to various window
systems.
It is used as an extension language for applications that need a programming interface.

PYTHON - An Introduction 5 / 63
Introduction to PYTHON History

P YTHON ’ S MAJOR FEATURES

Python is portable: it runs on many Unix variants including Linux and macOS, and on
Windows.

PYTHON - An Introduction 6 / 63
Introduction to PYTHON History

P YTHON ’ S MAJOR FEATURES

Python is portable: it runs on many Unix variants including Linux and macOS, and on
Windows.
Python includes a comprehensive base library.

PYTHON - An Introduction 6 / 63
Introduction to PYTHON History

P YTHON ’ S MAJOR FEATURES

Python is portable: it runs on many Unix variants including Linux and macOS, and on
Windows.
Python includes a comprehensive base library.
Addition to this, One can find hundreds of thousands of external packages contributed
by the enormous community. You’ll find supporting base libraries and packages for
pretty much anything you want to accomplish.

PYTHON - An Introduction 6 / 63
Introduction to PYTHON History

PYTHON - An Introduction 7 / 63
Introduction to PYTHON Anaconda

I NTRODUCTION TO A NACONDA

Anaconda is a package manager, environment manager, and Python distribution with


a collection of 1,500+ open source packages with free community support. Anaconda
is free and easy to install and can be used on Windows, macOS, or Linux.

PYTHON - An Introduction 8 / 63
Introduction to PYTHON Anaconda

I NTRODUCTION TO A NACONDA

Anaconda is a package manager, environment manager, and Python distribution with


a collection of 1,500+ open source packages with free community support. Anaconda
is free and easy to install and can be used on Windows, macOS, or Linux.
Anaconda can be downloaded from
https://www.anaconda.com/products/individual

PYTHON - An Introduction 8 / 63
Introduction to PYTHON Anaconda

I NTRODUCTION TO A NACONDA

Anaconda is a package manager, environment manager, and Python distribution with


a collection of 1,500+ open source packages with free community support. Anaconda
is free and easy to install and can be used on Windows, macOS, or Linux.
Anaconda can be downloaded from
https://www.anaconda.com/products/individual
After installing Anaconda, we use Anaconda Navigator to launch applications and
easily manage packages, environments and channels without using command-line
commands.

PYTHON - An Introduction 8 / 63
Introduction to PYTHON Anaconda

I NTRODUCTION TO A NACONDA

Anaconda is a package manager, environment manager, and Python distribution with


a collection of 1,500+ open source packages with free community support. Anaconda
is free and easy to install and can be used on Windows, macOS, or Linux.
Anaconda can be downloaded from
https://www.anaconda.com/products/individual
After installing Anaconda, we use Anaconda Navigator to launch applications and
easily manage packages, environments and channels without using command-line
commands.
Navigator is an easy, point-and-click way to work with packages and environments
without needing to type conda commands in the terminal window.

PYTHON - An Introduction 8 / 63
Introduction to PYTHON Anaconda

PYTHON - An Introduction 9 / 63
Introduction to PYTHON Anaconda

A PPLICATIONS IN NAVIGATOR

The following applications are available by default in Navigator:


JupyterLab
Jupyter Notebook
Spyder
PyCharm
VSCode
Glueviz
Orange 3 App
RStudio
Anaconda Prompt (Windows only)
Anaconda PowerShell (Windows only)

PYTHON - An Introduction 10 / 63
Introduction to PYTHON Anaconda

J UPYTER N OTEBOOK

The notebook extends the console-based approach to interactive computing in a quali-


tatively new direction, providing a web-based application suitable for capturing the whole
computation process: developing, documenting, and executing code, as well as communi-
cating the results.

PYTHON - An Introduction 11 / 63
Introduction to PYTHON Anaconda

J UPYTER N OTEBOOK

The notebook extends the console-based approach to interactive computing in a quali-


tatively new direction, providing a web-based application suitable for capturing the whole
computation process: developing, documenting, and executing code, as well as communi-
cating the results.
The Jupyter notebook combines two components:

PYTHON - An Introduction 11 / 63
Introduction to PYTHON Anaconda

J UPYTER N OTEBOOK

The notebook extends the console-based approach to interactive computing in a quali-


tatively new direction, providing a web-based application suitable for capturing the whole
computation process: developing, documenting, and executing code, as well as communi-
cating the results.
The Jupyter notebook combines two components:
A web application: a browser-based tool for interactive authoring of documents which
combine explanatory text, mathematics, computations and their rich media output.

PYTHON - An Introduction 11 / 63
Introduction to PYTHON Anaconda

J UPYTER N OTEBOOK

The notebook extends the console-based approach to interactive computing in a quali-


tatively new direction, providing a web-based application suitable for capturing the whole
computation process: developing, documenting, and executing code, as well as communi-
cating the results.
The Jupyter notebook combines two components:
A web application: a browser-based tool for interactive authoring of documents which
combine explanatory text, mathematics, computations and their rich media output.
Notebook documents: a representation of all content visible in the web application, in-
cluding inputs and outputs of the computations, explanatory text, mathematics, images,
and rich media representations of objects.

PYTHON - An Introduction 11 / 63
Introduction to PYTHON Anaconda

PYTHON - An Introduction 12 / 63
Introduction to PYTHON Anaconda

PYTHON - An Introduction 13 / 63
Introduction to PYTHON Installing Python

D OWNLOAD AND DOCUMENTATION

Python home - https://www.python.org/

Download area - https://www.python.org/downloads/

Documentation and Help - https://www.python.org/doc/

PYTHON - An Introduction 14 / 63
Introduction to PYTHON Installing Python

I NSTALLING P YTHON

N OTE :
Python 3.10 supports Windows 8.1 and newer. If you require Windows 7 support, please
install Python 3.8.

PYTHON - An Introduction 15 / 63
Introduction to PYTHON Installing Python

I NSTALLING P YTHON

N OTE :
Python 3.10 supports Windows 8.1 and newer. If you require Windows 7 support, please
install Python 3.8.

For full installation: download “Python 3.10” installer available for download.
The following dialogue box appears.

PYTHON - An Introduction 15 / 63
Introduction to PYTHON Installing Python

Select any one of the option and continue.

PYTHON - An Introduction 16 / 63
Introduction to PYTHON Installing Python

PYTHON - An Introduction 17 / 63
Introduction to PYTHON Installing Python

PYTHON - An Introduction 18 / 63
Introduction to PYTHON Installing Python

PYTHON - An Introduction 19 / 63
Introduction to PYTHON Installing Python

PYTHON - An Introduction 20 / 63
Introduction to PYTHON Installing Python

This completes the successful installation of “Python 3.10.0”.

PYTHON - An Introduction 21 / 63
Introduction to PYTHON INSTALLING PACKAGES FOR PYTHON

INSTALLING PACKAGES FOR PYTHON

Open command prompt by searching cmd


pip (package installer for Python) is used to install packages from Python Package
Index and other indexes. First we update the ‘pip’ to latest version and then use pip to
install the packages.

PYTHON - An Introduction 22 / 63
Introduction to PYTHON INSTALLING PACKAGES FOR PYTHON

INSTALLING PACKAGES FOR PYTHON

Open command prompt by searching cmd


pip (package installer for Python) is used to install packages from Python Package
Index and other indexes. First we update the ‘pip’ to latest version and then use pip to
install the packages.
Type: python -m pip install - -upgrade pip
pip install numpy
pip install sympy
pip install matplotlib
pip install statistics

PYTHON - An Introduction 22 / 63
Introduction to PYTHON Shell/Editor

S HELL /E DITOR

S HELL /E DITOR
Open IDLE (Integrated Development and Learning Environment). IDLE shell opens
and one can start working

PYTHON - An Introduction 23 / 63
Introduction to PYTHON Shell/Editor

S HELL /E DITOR

S HELL /E DITOR
Open IDLE (Integrated Development and Learning Environment). IDLE shell opens
and one can start working
Select “New File” from “File” menu.

PYTHON - An Introduction 23 / 63
Introduction to PYTHON Shell/Editor

S HELL /E DITOR

S HELL /E DITOR
Open IDLE (Integrated Development and Learning Environment). IDLE shell opens
and one can start working
Select “New File” from “File” menu.
The Editor will open. We type Python program here and then use the interpreter to
execute the content from the file.
Files to be saved with extension .py

PYTHON - An Introduction 23 / 63
Keywords and Identifiers

K EYWORDS AND I DENTIFIERS

Keywords are the reserved words in Python used by Python interpreter to recognize
the structure of the program.

PYTHON - An Introduction 24 / 63
Keywords and Identifiers

K EYWORDS AND I DENTIFIERS

Keywords are the reserved words in Python used by Python interpreter to recognize
the structure of the program.
Identifiers are the name given to entities like class, functions, variables etc. It helps
to differentiate one entity from another.

PYTHON - An Introduction 24 / 63
Keywords and Identifiers

K EYWORDS AND I DENTIFIERS

Keywords are the reserved words in Python used by Python interpreter to recognize
the structure of the program.
Identifiers are the name given to entities like class, functions, variables etc. It helps
to differentiate one entity from another.
Identifiers can be a combination of letters from “a” to “z”, from “A” to “Z” and digits from
“0” to “9” or special character _,.

PYTHON - An Introduction 24 / 63
Keywords and Identifiers

K EYWORDS AND I DENTIFIERS

Keywords are the reserved words in Python used by Python interpreter to recognize
the structure of the program.
Identifiers are the name given to entities like class, functions, variables etc. It helps
to differentiate one entity from another.
Identifiers can be a combination of letters from “a” to “z”, from “A” to “Z” and digits from
“0” to “9” or special character _,.
Keywords cannot be used as identifiers.

PYTHON - An Introduction 24 / 63
Keywords and Identifiers

K EYWORDS AND I DENTIFIERS

Keywords are the reserved words in Python used by Python interpreter to recognize
the structure of the program.
Identifiers are the name given to entities like class, functions, variables etc. It helps
to differentiate one entity from another.
Identifiers can be a combination of letters from “a” to “z”, from “A” to “Z” and digits from
“0” to “9” or special character _,.
Keywords cannot be used as identifiers.
Only special character used in identifiers is _.

PYTHON - An Introduction 24 / 63
Keywords and Identifiers

K EYWORDS AND I DENTIFIERS

Keywords are the reserved words in Python used by Python interpreter to recognize
the structure of the program.
Identifiers are the name given to entities like class, functions, variables etc. It helps
to differentiate one entity from another.
Identifiers can be a combination of letters from “a” to “z”, from “A” to “Z” and digits from
“0” to “9” or special character _,.
Keywords cannot be used as identifiers.
Only special character used in identifiers is _.
Identifier can be of any length.

PYTHON - An Introduction 24 / 63
Keywords and Identifiers

M OST IMPORTANT P YTHON KEYWORDS

False True and or

not break continue class

def if elif else

for while in is

None lambda return

PYTHON - An Introduction 25 / 63
Keywords and Identifiers

DATA TYPES

Variables can store data of different types, and different types can do different things.

PYTHON - An Introduction 26 / 63
Keywords and Identifiers

DATA TYPES

Variables can store data of different types, and different types can do different things.
Python has the following data types built-in by default, in these categories:

PYTHON - An Introduction 26 / 63
Keywords and Identifiers

DATA TYPES

Variables can store data of different types, and different types can do different things.
Python has the following data types built-in by default, in these categories:

Text Type str


Numeric Types int, float, complex
Sequence Types list, tuple, range
Mapping Type dict
Set Types set, frozenset
Boolean Type bool
Binary Types bytes, bytearray, memoryview

PYTHON - An Introduction 26 / 63
Keywords and Identifiers

E XAMPLES :

a =2.4
x=3
y= ’a ’
z =" H e l l o "
p r i n t ( type ( a ) ) # t y p e ( ) g i v e s t h e data t y p e o f t h e v a r i a b l e .
p r i n t ( type ( x ) )
p r i n t ( type ( y ) )
p r i n t ( type ( z ) )

PYTHON - An Introduction 27 / 63
Keywords and Identifiers

E XAMPLE FOR N UMERIC TYPES :

x = 1 # int
y = 2.8 # f l o a t
z = 1j # complex
X = 35e3 # e i n d i c a t e s t h e power o f 10
Y = 12E4
Z = −87.7 e100

PYTHON - An Introduction 28 / 63
Keywords and Identifiers

E XAMPLE FOR T EXT TYPE :

Strings in python are surrounded by either single quotation marks, or double quotation
marks.

PYTHON - An Introduction 29 / 63
Keywords and Identifiers

E XAMPLE FOR T EXT TYPE :

Strings in python are surrounded by either single quotation marks, or double quotation
marks.
a = " Hello "
print (a)

b = " " " H e l l o a l l , good morning ,


welcome t o t h e i n t r o d u c t i o n t o Python ,
w i l l d i s c u s s v e r y b r i e f l y how t o use
Python e d i t o r u s i n g IDLE ; J u p y t e r Notebook ( Anaconda ) . " " "
print (b)

PYTHON - An Introduction 29 / 63
Operators

O PERATORS

Python divides the operators in the following groups:


Arithmetic operators
Assignment operators
Comparison operators
Logical operators
Identity operators
Membership operators
Bitwise operators

PYTHON - An Introduction 30 / 63
Operators

A RITHMETIC O PERATORS

All the common algebraic operators presented in the following table are available in Python.

Addition +
Subtraction −
Multiplication ∗
Division /
Integer Division //
Power **
Remainder %

PYTHON - An Introduction 31 / 63
Operators

C OMPARISON O PERATORS

Comparison operators:
< less than
> greater than
<= less than or equal to
>= greater than or equal to
== equal to
!= not equal to

PYTHON - An Introduction 32 / 63
Operators

L OGICAL OPERATORS

Logical operators:

and logical and


or logical or
not logical not

PYTHON - An Introduction 33 / 63
Operators

A SSIGNMENT O PERATOR

=, is used as assignment operator. For example x = 5, means 5 is stored to variable


x.
+ =. The expression a+ = 3, give the result of a = a + 3.
− =. The expression x− = 3, give the result of x = x − 3.
∗ =. The expression a∗ = 3, give the result of a = a ∗ 3.
/ =. The expression a/ = 3, give the result of a = a/3.
// =. The expression a// = 3, give the result of a = a//3.
% =. The expression a% = 3, give the result of a = a%3.
∗∗ =. The expression a∗ = 3, give the result of a = a ∗ ∗3.

PYTHON - An Introduction 34 / 63
Operators

I DENTITY O PERATORS

Identity Operators
is Returns True if both variables are
the same object
is not Returns True if both variables are
not the same object

PYTHON - An Introduction 35 / 63
Operators

M EMBERSHIP O PERATORS

Membership Operators
in Returns True if a sequence with the
specified value is present in the ob-
ject
not in Returns True if a sequence with the
specified value is not present in the
object

PYTHON - An Introduction 36 / 63
Operators

E XERCISE :

Type the following statements and observe the output:


32+56 −74 * 2+65/5 −2 ** 3
32+(56 −74) * 2+65/5 −2 ** 3
32+(56 −74) * 2+(65/5 −2) ** 3
1254//9
487 ** 2%3
i n t (22/6)
round ( 2 2 / 6 )
round ( 2 2 / 6 , 2 )
round ( 2 2 / 6 , 6 )
round ( 2 . 4 9 )
round ( 2 . 5 )

PYTHON - An Introduction 37 / 63
Operators

E XERCISE :

Type the following statements and observe the output:


round ( 3 . 5 )
round ( 3 . 5 1 )
round ( 2 . 5 2 )
x =23145621.456872194
x
round ( x )
round ( x , 1 )
round ( x , 2 )
round ( x , 4 )
round ( x , 6 )

PYTHON - An Introduction 38 / 63
Operators

E XERCISE :

Type the following statements and observe the output:


round ( x , − 1 )
round ( x , − 2 )
round ( x , − 4 )
round ( x , − 7 )
round ( x , − 8 )
x , y , z =23 ,45 ,67
x+y , x+y+z , x * y
p r i n t ( x+y , x+y+z , x * y )

PYTHON - An Introduction 39 / 63
Operators

E XERCISE :

Type the following statements and observe the output:


x
x+=2
x
x−=4
x
x * =2
x
x%=2
x
x //=5
x

PYTHON - An Introduction 40 / 63
Lists, Tuples, Sets

L ISTS , T UPLES , S ETS

Lists
Tuples
Sets
These are used to store collections of data.

PYTHON - An Introduction 41 / 63
Lists, Tuples, Sets

L ISTS , T UPLES

are used to store multiple items in a single variable.

PYTHON - An Introduction 42 / 63
Lists, Tuples, Sets

L ISTS , T UPLES

are used to store multiple items in a single variable.


Lists are created using square brackets: for example
a=[1,2,3]

PYTHON - An Introduction 42 / 63
Lists, Tuples, Sets

L ISTS , T UPLES

are used to store multiple items in a single variable.


Lists are created using square brackets: for example
a=[1,2,3]
Tuples are created using round brackets: for example
a=(1,2,3)

PYTHON - An Introduction 42 / 63
Lists, Tuples, Sets

L ISTS , T UPLES

are used to store multiple items in a single variable.


Lists are created using square brackets: for example
a=[1,2,3]
Tuples are created using round brackets: for example
a=(1,2,3)
items are ordered, allow duplicate values.

PYTHON - An Introduction 42 / 63
Lists, Tuples, Sets

L ISTS , T UPLES

are used to store multiple items in a single variable.


Lists are created using square brackets: for example
a=[1,2,3]
Tuples are created using round brackets: for example
a=(1,2,3)
items are ordered, allow duplicate values.
List items are changeable, whereas; Tuple items are unchangeable.

PYTHON - An Introduction 42 / 63
Lists, Tuples, Sets

L ISTS , T UPLES

are used to store multiple items in a single variable.


Lists are created using square brackets: for example
a=[1,2,3]
Tuples are created using round brackets: for example
a=(1,2,3)
items are ordered, allow duplicate values.
List items are changeable, whereas; Tuple items are unchangeable.
items are indexed, the first item has index [0], the second item has index [1] etc.

PYTHON - An Introduction 42 / 63
Lists, Tuples, Sets

L ISTS ( CONTD ...)

ordered, it means that the items have a defined order, and that order will not change.

PYTHON - An Introduction 43 / 63
Lists, Tuples, Sets

L ISTS ( CONTD ...)

ordered, it means that the items have a defined order, and that order will not change.
If you add new items to a list, the new items will be placed at the end of the list.

PYTHON - An Introduction 43 / 63
Lists, Tuples, Sets

L ISTS ( CONTD ...)

ordered, it means that the items have a defined order, and that order will not change.
If you add new items to a list, the new items will be placed at the end of the list.
The list is changeable, meaning that we can change, add, and remove items in a list
after it has been created.

PYTHON - An Introduction 43 / 63
Lists, Tuples, Sets

L ISTS ( CONTD ...)

ordered, it means that the items have a defined order, and that order will not change.
If you add new items to a list, the new items will be placed at the end of the list.
The list is changeable, meaning that we can change, add, and remove items in a list
after it has been created.
Since lists/tuples are indexed, lists can have items with the same value.

PYTHON - An Introduction 43 / 63
Lists, Tuples, Sets

L ISTS ( CONTD ...)

ordered, it means that the items have a defined order, and that order will not change.
If you add new items to a list, the new items will be placed at the end of the list.
The list is changeable, meaning that we can change, add, and remove items in a list
after it has been created.
Since lists/tuples are indexed, lists can have items with the same value.
List/tuple items can be of any data type.

PYTHON - An Introduction 43 / 63
Lists, Tuples, Sets

L ISTS ( CONTD ...)

ordered, it means that the items have a defined order, and that order will not change.
If you add new items to a list, the new items will be placed at the end of the list.
The list is changeable, meaning that we can change, add, and remove items in a list
after it has been created.
Since lists/tuples are indexed, lists can have items with the same value.
List/tuple items can be of any data type.
A list/tuple can contain different data types

PYTHON - An Introduction 43 / 63
Lists, Tuples, Sets

L ISTS ( CONTD ...)

len() function is used to find the number of items in the list/tuple

PYTHON - An Introduction 44 / 63
Lists, Tuples, Sets

L ISTS ( CONTD ...)

len() function is used to find the number of items in the list/tuple


list(())/tuple(()) constructor can be used to create a list/tuple

PYTHON - An Introduction 44 / 63
Lists, Tuples, Sets

L ISTS ( CONTD ...)

len() function is used to find the number of items in the list/tuple


list(())/tuple(()) constructor can be used to create a list/tuple
List/tuple items are indexed and can be accessed them by referring to the index num-
ber. The first item has index 0.

PYTHON - An Introduction 44 / 63
Lists, Tuples, Sets

L ISTS ( CONTD ...)

len() function is used to find the number of items in the list/tuple


list(())/tuple(()) constructor can be used to create a list/tuple
List/tuple items are indexed and can be accessed them by referring to the index num-
ber. The first item has index 0.
Negative indexing means start from the end
-1 refers to the last item, -2 refers to the second last item etc.

PYTHON - An Introduction 44 / 63
Lists, Tuples, Sets

insert() is used to insert an item to the list

PYTHON - An Introduction 45 / 63
Lists, Tuples, Sets

insert() is used to insert an item to the list


append() is used to add an item to the end of the list

PYTHON - An Introduction 45 / 63
Lists, Tuples, Sets

insert() is used to insert an item to the list


append() is used to add an item to the end of the list
extend() is used to append elements from another list to the current list

PYTHON - An Introduction 45 / 63
Lists, Tuples, Sets

insert() is used to insert an item to the list


append() is used to add an item to the end of the list
extend() is used to append elements from another list to the current list
remove() used to remove specified item.

PYTHON - An Introduction 45 / 63
Lists, Tuples, Sets

insert() is used to insert an item to the list


append() is used to add an item to the end of the list
extend() is used to append elements from another list to the current list
remove() used to remove specified item.
pop() used to remove specified indes.

PYTHON - An Introduction 45 / 63
Lists, Tuples, Sets

insert() is used to insert an item to the list


append() is used to add an item to the end of the list
extend() is used to append elements from another list to the current list
remove() used to remove specified item.
pop() used to remove specified indes.
del / clear() is used to delete the complete list.

PYTHON - An Introduction 45 / 63
Lists, Tuples, Sets

insert() is used to insert an item to the list


append() is used to add an item to the end of the list
extend() is used to append elements from another list to the current list
remove() used to remove specified item.
pop() used to remove specified indes.
del / clear() is used to delete the complete list.
sort() is used to sort the items in the list.

PYTHON - An Introduction 45 / 63
Lists, Tuples, Sets

insert() is used to insert an item to the list


append() is used to add an item to the end of the list
extend() is used to append elements from another list to the current list
remove() used to remove specified item.
pop() used to remove specified indes.
del / clear() is used to delete the complete list.
sort() is used to sort the items in the list.
These functions can be used for a list. Tuple items cannot be changed using any of the
above. First, convert tuple to a list, apply changes and then again convert list to tuple.

PYTHON - An Introduction 45 / 63
Lists, Tuples, Sets

S ETS

Sets are used to store multiple items in a single variable.

PYTHON - An Introduction 46 / 63
Lists, Tuples, Sets

S ETS

Sets are used to store multiple items in a single variable.


A set is a collection which is unordered, unchangeable, and unindexed.

PYTHON - An Introduction 46 / 63
Lists, Tuples, Sets

S ETS

Sets are used to store multiple items in a single variable.


A set is a collection which is unordered, unchangeable, and unindexed.
Sets are written with curly brackets.

PYTHON - An Introduction 46 / 63
Lists, Tuples, Sets

S ETS

Sets are used to store multiple items in a single variable.


A set is a collection which is unordered, unchangeable, and unindexed.
Sets are written with curly brackets.
Set items do not allow duplicate values.

PYTHON - An Introduction 46 / 63
Lists, Tuples, Sets

S ETS

Sets are used to store multiple items in a single variable.


A set is a collection which is unordered, unchangeable, and unindexed.
Sets are written with curly brackets.
Set items do not allow duplicate values.
Once a set is created, one cannot change its items, but can add new items.

PYTHON - An Introduction 46 / 63
Lists, Tuples, Sets

S ETS

Sets are used to store multiple items in a single variable.


A set is a collection which is unordered, unchangeable, and unindexed.
Sets are written with curly brackets.
Set items do not allow duplicate values.
Once a set is created, one cannot change its items, but can add new items.
A set can contain different data types

PYTHON - An Introduction 46 / 63
Lists, Tuples, Sets

S ETS

Sets are used to store multiple items in a single variable.


A set is a collection which is unordered, unchangeable, and unindexed.
Sets are written with curly brackets.
Set items do not allow duplicate values.
Once a set is created, one cannot change its items, but can add new items.
A set can contain different data types
set() constructor can be used to make a set.

PYTHON - An Introduction 46 / 63
Lists, Tuples, Sets

E XERCISE :

Type the following statements and observe the output:


a=[23 ,46 ,56 ,21 ,13]
a
len ( a )
a[0] ,a[1] ,a[5]
a[6]
a[ −1]
a[ −4]
a . append ( 2 3 )
a
b=[78 ,58 ,74]

PYTHON - An Introduction 47 / 63
Lists, Tuples, Sets

E XERCISE :

Type the following statements and observe the output:


a . append ( b )
a
a . append ( b [ 2 ] )
a
a . i n s e r t (4 ,34)
a
a . i n s e r t (0 ,87)
a
a . pop ( )
a

PYTHON - An Introduction 48 / 63
Lists, Tuples, Sets

E XERCISE :

Type the following statements and observe the output:


a . pop ( 2 )
a
a . pop ( −1)
a
a . remove ( 3 4 )
a
a . remove ( a [ 2 ] )
a
a=[12 ,36 ,9 ,23 ,8 ,5 ,3]
a
a. sort ()
a

PYTHON - An Introduction 49 / 63
Lists, Tuples, Sets

E XERCISE :

Type the following statements and observe the output:


b=(12 ,36 ,9 ,23 ,8 ,5 ,3)
b
len ( b )
c= l i s t ( b )
c
c . sort ()
c
c . s o r t ( r e v e r s e =True )
c
b= t u p l e ( c )
c . clear ()

PYTHON - An Introduction 50 / 63
Lists, Tuples, Sets

E XERCISE :

Type the following statements and observe the output:


a={3 ,12 ,36 ,9 ,23 ,8 ,5 ,3}
a
b= s e t ( [ 1 2 , 1 6 , 1 9 , 2 5 , 3 5 , 3 6 , 3 9 ] )
b
c=a . union ( b )
c
d=a . i n t e r s e c t i o n ( b )
d
e=a . s y m m e t r i c _ d i f f e r e n c e ( b )
e
e . clear ()
e

PYTHON - An Introduction 51 / 63
Lists, Tuples, Sets

E XERCISE :

Type the following statements and observe the output:


a={3 ,12 ,36 ,9 ,23 ,8 ,5 ,3}
c=a . copy ( )
b={3 ,56 ,23}
d=a . d i f f e r e n c e ( b )
c=a . union ( b )
a. isdisjoint (b)
a . issubset (b)
a . remove ( 3 )
a

PYTHON - An Introduction 52 / 63
Input and output statements

I NPUT STATEMENT

The input() function allows user input.


S YNTAX :
input(prompt)

prompt– A String, representing a default message before the input.

For example:
x = i n p u t ( ’ E n t e r your name : ’ )
p r i n t ( ’ H e l l o , ’ +x )
p r i n t ( ’ Hello , ’ , x )
p r i n t ( type ( x ) )

PYTHON - An Introduction 53 / 63
Input and output statements

I NPUT STATEMENT

To read integer then the following to be used.


x = i n t ( i n p u t ( ’ E n t e r an i n t e g e r : ’ ) )
p r i n t ( type ( x ) )

PYTHON - An Introduction 54 / 63
Input and output statements

I NPUT STATEMENT

To read integer then the following to be used.


x = i n t ( i n p u t ( ’ E n t e r an i n t e g e r : ’ ) )
p r i n t ( type ( x ) )

To read float then the following to be used.


x = f l o a t ( i n p u t ( ’ E n t e r an f l o a t : ’ ) )
p r i n t ( type ( x ) )

PYTHON - An Introduction 54 / 63
Input and output statements

O UTPUT STATEMENTS

Python provides the print() function to display output to the standard output devices.
S YNTAX :
print(value(s),sep= ’ ’, end = ‘\n’, file=file, flush=flush)

value(s) – Any value, and as many as you like. Will be converted to string before printed
sep=‘separator’ – (Optional) Specify how to separate the objects, if there is more than
one. Default :’ ’
end=‘end’ – (Optional) Specify what to print at the end.Default : ‘\n’
file – (Optional) An object with a write method. Default :sys.stdout
flush – (Optional) A Boolean, specifying if the output is flushed (True) or buffered (False).
Default: False

PYTHON - An Introduction 55 / 63
Input and output statements

E XAMPLES FOR PRINT ()

Type the following commands and note the difference in output:


C ODES :

print ( " REVA " )


print ( ’ R’ , ’ E ’ , ’ V ’ , ’ A ’ )
print ( " REVA" , end = "@" )
print ( ’ R’ , ’ E ’ , ’ V ’ , ’ A ’ , sep = " # " )

PYTHON - An Introduction 56 / 63
Input and output statements

F ORMATTING OUTPUT

C ODE 1:

name = "REVA"
p r i n t ( f ’ Welcome t o { name } ! ’ )

PYTHON - An Introduction 57 / 63
Input and output statements

C ODE 2:

a = 20
b = 10
# addition
sum = a + b
# subtraction
sub = a− b
# Output
p r i n t ( ’ The v a l u e o f a i s { } and b i s { } ’ . f o r m a t ( a , b ) )
p r i n t ( ’ { 2 } i s t h e sum o f { 0 } and { 1 } ’ . f o r m a t ( a , b , sum ) )
p r i n t ( ’ { sub_value } i s t h e s u b t r a c t i o n o f { value_a } and { value_b } ’
. f o r m a t ( value_a = a , value_b = b , sub_value =sub ) )

PYTHON - An Introduction 58 / 63
Input and output statements

U SING % O PERATOR

We can use ‘%’ operator. % values are replaced with zero or more value of elements. The
formatting using % is similar to that of ‘printf’ in the C programming language.
%d – integer
%f – float
%s – string
%x – hexadecimal
%o – octal

PYTHON - An Introduction 59 / 63
Input and output statements

E XAMPLE

C ODE

# Taking i n p u t from t h e user


num = i n t ( i n p u t ( " E n t e r a v a l u e : " ) )
add = num + 5
# Output
p r i n t ( " The sum i s %d " %add )

PYTHON - An Introduction 60 / 63
Input and output statements

E XAMPLE

C ODE

# Taking i n p u t from t h e user


num = i n t ( i n p u t ( " E n t e r a v a l u e : " ) )
add = num + 5
# Output
p r i n t ( " The sum i s %d " %add )

C ODE :

x = 12.3456789
p r i n t ( ’ The v a l u e o f x i s %3.2 f ’ %x )
p r i n t ( ’ The v a l u e o f x i s %3.4 f ’ %x )

PYTHON - An Introduction 60 / 63
Input and output statements

O UTPUT STATEMENTS

display() command can be used to get the output printed on the screen. For example
x = 12.3456789
d i s p l a y ( ’ The v a l u e o f x i s %3.2 f ’ %x )
d i s p l a y ( ’ The v a l u e o f x i s ’ , x )

PYTHON - An Introduction 61 / 63
Input and output statements

O UTPUT STATEMENTS

display() command can be used to get the output printed on the screen. For example
x = 12.3456789
d i s p l a y ( ’ The v a l u e o f x i s %3.2 f ’ %x )
d i s p l a y ( ’ The v a l u e o f x i s ’ , x )

display() works similar to print().

PYTHON - An Introduction 61 / 63
Input and output statements

E XERCISES :

Read a list of elements using input function


Read a set from user input
Print the type of variable which were read in previous two commands

PYTHON - An Introduction 62 / 63
Input and output statements

Thank You

PYTHON - An Introduction 63 / 63

You might also like