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

Python Course

Uploaded by

kumaruxd.cw
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views

Python Course

Uploaded by

kumaruxd.cw
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 142

PYTHON

PROGRAMMING
HELLO!
I am Roheeth
I am here to share my knowledge on Python

2
1
DEMO
Day - 1

3
What You’ll Learn Today

¤ Introduction to Software Programming


¤ Language Processors
¤ Introduction to Python
¤ Why Python?

4
By the end of this session

¤ You would be able to understand


▻ Software programming and its types
▻ Communication with computers
▻ Why Python is better than other programming
languages

5
Introduction to Software Programming

¤ What is Software Programming?


¤ Types of Software
¤ Programming Language
¤ Programming Language vs Scripting Language
¤ Object Oriented vs Procedure Oriented

6
Language Processors

¤ Communication with computer


¤ Evolution of Programming Language
¤ Language to communicate
ø Low Level Language
ø Mid Level Language
ø High Level Language

7
Language Processors

¤ Language Processors or Translators

8
Language Processors

¤ Compiler

9
Language Processors

¤ Assembler

10
Language Processors

¤ Interpreter

11
Introduction to Python

¤ Evolution of Python

12
Introduction to Python

¤ Implementations of Python

13
Why Python?

¤ Features of Python

14
Why Python?

¤ Versatility of
Python

15
2
DEMO
Day - 2

16
What You’ll Learn Today

¤ Course Curriculum overview


¤ FAQ Session

17
Course Curriculum overview

¤ Object and Data Structure Basics


ø Arithmetic
ø Strings
ø Lists
ø Dictionaries
ø Tuples
ø Sets
ø Booleans
18
Course Curriculum overview

¤ Operators
ø Basic Operators
ø Chained comparison operators
¤ Python Statements
ø Conditional statements
ø Loops
ø Comprehensions

19
Course Curriculum overview

¤ Methods and Functions


ø Methods
ø Functions
ø Lambda Expressions
ø Nested Statements
ø Scope

20
Course Curriculum overview

¤ Object Oriented Programming


ø Objects
ø Classes
ø Methods
ø Inheritance
ø Special Methods

21
Course Curriculum overview

¤ Errors and Exception Handling


ø Errors
ø Exceptions
ø try
ø except
ø finally

22
Course Curriculum overview

¤ Libraries, Packages and Modules


ø Installing modules
ø Exploring Python Eco-system
¤ Decorators
¤ Generators
¤ Collections
¤ Regular Expressions
¤ Files

23
At the end of course

You’ll be able to,

¤ Perform server side programming for web


applications
¤ Develop business logics for,
ø Desktop applications
ø Game development
ø Enterprise applications

24
INR 6,00,000 PA
Average salary for a python programmer

200K
Live job oppurtunities

25
3
DEMO
Day - 3

26
What You’ll Learn Today

¤ Python Installation
¤ Interactive mode vs Script mode
¤ Introduction to IDE
¤ Anaconda Distribution

27
By the end of this session

¤ You would be able to run python on your computer


¤ Command line basics
¤ Your first python program
¤ Understanding of IDE
¤ Advantages of using Anaconda distribution

28
Python Installation

¤ Python can be downloaded from the below


mentioned site
https://www.python.org/downloads/
¤ Run the executable to install python

29
Interactive mode vs Script mode

¤ In Interactive mode, the command is executed at the


same time within the shell
¤ In Script mode, the complete code is saved in a .py
file and then is executed

30
Introduction to IDE

¤ IDE is an integrated development environment that


helps us keep all our code at one place and manage
the control better.
¤ Popular IDE used for programming with python are,
ø PyCharm
ø Spyder
ø Visual Studio Code

31
Introduction to Anaconda

¤ Anaconda is a free and open-source distribution of


the programming languages Python and .
¤ The distribution comes with the Python interpreter
and various packages related to machine learning and
data science.

32
Introduction to Anaconda

¤ Anaconda is a free and open-source distribution of


the programming languages Python and .
¤ The distribution comes with the Python interpreter
and various packages related to machine learning and
data science.

33
4
Demo
Day - 4

34
What You’ll Learn Today

¤ Keywords
¤ Variables & Identifiers
¤ Indentation and Line continuation
¤ Comments in Python

35
By the end of this session

¤ You would be able to recognise the keywords in


python
¤ Difference between an identifier and a variable
¤ Understand the assignment operation in python
¤ Multi line assignment
¤ Usage of comments
¤ Standard coding rules

36
Keywords

¤ Keywords are predefined, reserved words used in


programming that have special meanings to the
compiler. Keywords are part of the syntax and they
cannot be used as an identifier.

37
Keywords

Keywords Usage
True, False Boolean values
None Null object
and, or, not, in, is Logical and identity
operators
if, elif, else Conditional statements
for, while, break, continue Loops
def, class, with, as, pass, Structures
lambda
return, yield Return output
import, from, as Libraries
try, except, raise, finally, assert Exception Handling
38
async, await Asynchronous functions
Identifiers vs Variables

¤ A Python identifier is a name used to identify a


variable, function, class, module or other object
¤ Variables are the names you give to computer
memory locations which are used to store values in a
computer program.

39
Assignment Operator

¤ Assignment is done with a single equals sign (=)


¤ Python also allows chained assignment
¤ As python is a dynamically typed language, the
variable can be assigned any value
ø Statically typed vs Dynamically typed

40
Assignment Operator

¤ A Python variable is a symbolic name that is a


reference or pointer to an object. Once an object is
assigned to a variable, you can refer to the object by
that name. But the data itself is still contained within
the object.

Code Snippet for variable


Variable reference in memory
assignment

41
Assignment Operator

¤ Unlike other programming languages, python


supports the concept of shared reference

Multiple reference to a single


Code Snippet for shared reference
object

Change in reference for multiple


Code Snippet for a new object 42
objects
Assignment Operator

¤ Python supports automatic memory management for


allocation and deallocation of the memory

Code Snippet for a new object Orphaned Object

43
Indentation and Line Continuation

¤ Unlike other programming languages, python uses


indentation to identify a block of code
¤ Line continuation can be obtained explicitly using
backslash character “\”
¤ Line continuation is implied inside,
ø Brackets – [ ]
ø Parenthesis – ( )
ø Braces – { }

44
Comments

¤ A comment is a programmer-readable explanation in the


source code of a computer program
¤ They are added with the purpose of making the source code
easier to understand, and are ignored by language processors.
¤ Comments in Python,
ø Single line – #This is a single line comment
ø Multi line – “””This is can be used
for multiline comment“””
45
Standard Coding Rules

¤ Below mentioned the standard coding practices in python,


ø Use four spaces for indentation (preferred over tab)
ø Limit the code to 78 characters per line
ø For naming classes, use PascalCase
ø For naming functions and variables, use snake_case
ø All imports should be at the top
ø Keep all function definitions at one place
ø Use two blank lines between function definitions
ø Give comments when and where applicable
46
5
Python Course
Day - 5

47
What You’ll Learn Today

¤ Object Oriented vs Procedure Oriented Programming


¤ Objects and Data Structures in Python
¤ Type Casting

48
By the end of this session

¤ You would be able to understand the difference


between procedure oriented and object oriented
programming
¤ You would be able to understand all the in-built data
types in python
¤ Understand the concept of indexing
¤ Understand the concept of mutability
¤ Taking input from user
¤ Type-casting in python
49
Procedure Oriented vs Object Oriented
Programming

¤ Procedural Programming is a programming paradigm


based on the concept of calling procedure.
Procedures, also known as routines, subroutines or
functions, simply consist of a series of computational
steps to be carried out.
¤ Object-oriented programming (OOP) is a
programming paradigm based on the concept of
"objects", which can contain data and code: data in
the form of fields (often known as attributes or
properties), and code, in the form of procedures
(often known as methods). 50
Objects and Data Structures

¤ Everything is in Python treated as an object,


including variable, function, data structures etc.
¤ Data structures are the fundamental constructs
around which you build your programs. Each data
structure contains information about the data values,
relationships between the data and methods that can
be applied to the data.

51
Objects and Data Structures

Data Structures

Arithmetic Files

None
Integral Real

Integer Boolean Float


Mappings

Imaginary Mutable

Dictionaries
Complex

Non-
Sequences
Sequential

Mutable Immutable
Mutable Immutable

Lists Bytearrays Strings Tuples

Frozen Sets User-


Sets Built-In Bytes
defined

Non-Linear
Lists Linear Lists
Lists

52
Stacks Queues Graphs Trees
Type Casting

¤ Python defines type conversion functions to directly


convert one data type to another which is useful in
day to day and competitive programming.
¤ There are two types of Type Conversion in Python:
ø Implicit type casting
ø Explicit type casting

53
Explicit Type Casting

ascii() Returns a string containing a printable representation of an object


bin() Converts an integer to a binary string
bool() Converts an argument to a Boolean value
chr() Returns string representation of character given by integer argument
complex() Returns a complex number constructed from arguments
float() Returns a floating-point object constructed from a number or string
hex() Converts an integer to a hexadecimal string
int() Returns an integer object constructed from a number or string
oct() Converts an integer to an octal string
ord() Returns integer representation of a character
repr() Returns a string containing a printable representation of an object
str() Returns a string version of an object
type() Returns the type of an object or creates a new type object
tuple() Converts an iterable object into tuple
list() Converts an iterable object into list 54
set() Converts an iterable object into set
6
Python Course
Day - 6

55
What You’ll Learn Today

¤ Containers in Python
¤ Sequenced Containers
¤ Non-Sequenced Containers
¤ Strings in python

56
By the end of this session

¤ Literals vs Variables
¤ Concept of containers
¤ Containers available in Python
¤ Concept of strings

57
Containers in Python

¤ A container is a class, a data structure, or an abstract


data type (ADT) whose instances are collections of
other objects. In other words, they store objects in an
organized way that follows specific access rules.
¤ One of the reasons why Python is so highly regarded
as a rapid development language is because of it’s
advanced data containers.

58
Sequenced Containers

¤ Built-In Sequenced Containers


ø Strings
ø Lists
ø Tuples
ø Bytes
ø Bytearrays

59
Non-Sequenced Containers

¤ Built-In Non-Sequenced Containers


ø Sets
ø Dictionaries
ø Frozen sets

60
Sequenced Containers

¤ Strings
ø A string in Python is a sequence of characters. It
is a derived data type.
ø Strings are immutable. This means that once
defined, they cannot be changed.
ø It is of the <<class str>>

61
Sequenced Containers

¤ Strings - Indexing

Indexing in a string

62
Sequenced Containers

¤ Inbuilt Functions and Method for strings

enumerate() len() lower() upper() split() isidentifier( rsplit() rindex()


)
capitalize() find() format() isalnum() isalpha() rfind() splitlines() startswith()

replace() join() isdecimal() isdigit() islower() partition() translate() zfill()

casefold() center() count() encode() endswith() maketrans() strip() title()

expandtabs() format_map( istitle() ljust() rjust() lstrip() swapcase() reversed()


)
isnumeric() isprintable() isspace() isupper() rstrip() rpartition() Index() map()

63
String Formatting

¤ The format() method that is available with the string


object is very versatile and powerful in formatting
strings. Format strings contain curly braces {} as
placeholders or replacement fields which get
replaced.
¤ Formatted String Literal (f-string), can be used for is
variable interpolation

64
String Formatting

\newline Backslash and newline ignored \b ASCII Backspace

\\ Backslash \f ASCII Form feed

\' Single quote \n ASCII Linefeed

ASCII Carriage
\" Double quote \r
Return

ASCII Horizontal
\a ASCII Bell \t
Tab
Character with hexadecimal value
\xHH \v ASCII Vertical Tab
HH
\ooo Character with octal value ooo

Escape Sequences in Python 65


Sequenced Containers

¤ Basic operations using strings


ø Slicing a string
ø Concatenating a string
ø Usage of Escape sequences
ø Using f-strings
ø String membership test
ø Type conversion to string using str() function
66
7
Python Course
Day - 7

67
What You’ll Learn Today

¤ Escape Sequences
¤ Functions and Methods for strings

68
By the end of this session

¤ Working with escape sequences


¤ Usage of various string methods

69
Escape Sequences

\newline Backslash and newline ignored \b ASCII Backspace

\\ Backslash \f ASCII Form feed

\' Single quote \n ASCII Linefeed

ASCII Carriage
\" Double quote \r
Return

ASCII Horizontal
\a ASCII Bell \t
Tab
Character with hexadecimal value
\xHH \v ASCII Vertical Tab
HH
\ooo Character with octal value ooo

https://www.sciencebuddies.org/science-fair-projects
70
/references/ascii-table#asciitable
Functions and Methods for strings

enumerate() Used to generate the ordered pair of the index and value

len() Used to get the length of the string

reversed() Used to reverse the sequences

capitalize() Used to capitalize the string

replace() Used to replace a substring with another substring

casefold() Used for caseless comparison for any Unicode text

index() Used to find the lowest index of a substring


rindex() Used to find the highest index of a substring

join() Used to join the values of a string


71
Functions and Methods for strings

lower() Used for converting the ASCII characters to lower case

upper() Used for converting the ASCII characters to upper case

expandtabs() Used for custom tab size

format() Used for text formatting

split() Used for splitting strings into list of broken strings from left to
right
rsplit() Used for splitting strings into list of broken strings from right to
left
splitlines() Used for splitting strings into list of broken strings from line
break
count() Used for returning the count of a substring

swapcase() Used to swap case from lower to upper and vice versa 72
8
Python Course
Day - 8

73
What You’ll Learn Today

¤ Functions and Methods for strings

74
By the end of this session

¤ Usage of various string methods

75
Functions and Methods for strings

find() Used to find the lowest index of a substring

rfind() Used to find the highest index of a substring

startswith() Used to check if a string starts with substring

endswith() Used to check if a string ends with substring

strip() Used to remove the characters from a string

lstrip() Used to remove the leading characters from a


string
rstrip() Used to remove the trailing characters from a
string
Center() Used to pad the string

ljust() Used for right padding 76


Functions and Methods for strings

partition() Used to split the string based on separator from left to


right
rpartition() Used to split the string based on separator from right to
left
zfill() Used to left pad the string with zero’s for required length

title() Used to convert to Pascal Case

maketrans() Used to construct the transition table

translate() Used to translate using maketrans

encode() Used to encode strings with the specified encoding


scheme

77
Functions and Methods for strings

isidentifier() To check if a valid identifier

isalnum() To check If a valid alphanumeric


element
isalpha() To check If a valid alphabet element

isdecimal() To check if a valid decimal number

isdigit() To check if a valid digit

islower() To check if a string is in lower case

istitle() To check if a string is in pascal case

isnumeric() To check if all the characters are


numeric
isprintable() To check if all characters are printable

isupper() To check if a string is in upper case 78


isspace() To check for whitespace characters
Basic Operations

¤ Reverse a string
¤ Remove a character from a string
¤ Check if a substring is present in string
¤ Form a new word using even and odd index
¤ Replace the occurrences of a substring in string
¤ Make a string of first and last two characters of a
string
¤ Find the duplicates in a string

79
9
Python Course
Day - 9

80
What You’ll Learn Today

¤ Lists in python
¤ Functions and Methods of Lists

81
By the end of this session

¤ Indexing of lists
¤ Mutability in lists
¤ Slicing of lists
¤ Working with lists

82
Lists

¤ List is a ordered mutable heterogenous container in


python
¤ It is declared using brackets [ ]
¤ It is of the <<class list>>

83
Lists

¤ Slicing in Lists
¤ Slicing can be done using the slicing operator “ :

¤ Usage – List[Start_Index:End_Index:Step]
¤ Mutability in Lists
¤ Items are mutated using the index

84
Lists

¤ Indexing in lists

85
Functions and Methods in Lists

enumerate() Used to generate the ordered pair of the index and value

len() Used to get the length of the string

reversed() Used to reverse the sequences

max() Used to return the maximum element in a list

min() Used to return the minimum element in a list

list() Used to convert the iterable into list

sum() Used to return the sum of objects in a list


range() Used to return the range of numbers in a list

sorted() Used to sort the list

clear() Used to clear the list 86


Functions and Methods in Lists

append() Used add an element at the end of a list

copy() Used to shallow copy a list

count() Used to count the required elements in a list

extend() Used to concatenate the list

index() Used to return the index of the specified element

insert() Used to insert an element at a specified index


pop() Used to remove an element at a specified index

remove() Used to remove an element at a specified element

reverse() Used to reverse a list and update

sort() Used to sort a list and update 87


10
Python Course
Day - 10

88
What You’ll Learn Today

¤ Tuples in python
¤ Functions and Methods of Lists

89
By the end of this session

¤ Working with tuples


¤ Tuples vs Lists

90
Tuples

¤ Tuple is a ordered immutable heterogenous container


in python
¤ It is declared using brackets ( )
¤ It is of the <<class tuple>>

91
Tuples

¤ Slicing in Tuples
¤ Slicing can be done using the slicing operator “ :

¤ Usage – Tuple[Start_Index:End_Index:Step]
¤ Mutability in Tuples
¤ Items cannot be mutated

92
Tuples

¤ Indexing in tuples

93
Functions and Methods in Tuples

enumerate() Used to generate the ordered pair of the index and value

len() Used to get the length of the string

reversed() Used to reverse the sequences

max() Used to return the maximum element in a tuple

min() Used to return the minimum element in a tuple

tuple() Used to convert the iterable into tuple

sum() Used to return the sum of objects in a tuple

94
Functions and Methods in Tuples

range() Used to return the range of numbers in a tuple

sorted() Used to sort the tuple

any() and all() Used to check if any null elements on a tuple

index() Used to return the index of a specific element

count() Used to return the count of a specific element

95
11
Python Course
Day - 11

96
What You’ll Learn Today

¤ Bytes and Bytearrays in python


¤ Functions and Methods of Bytes and Bytearrays
¤ Exercise on strings, lists, tuples

97
By the end of this session

¤ Working with Bytes and Bytearrays


¤ Exercise on strings, lists, tuples

98
Bytes and Bytearrays

¤ Bytes function returns a sequence bytes which are


immutable
¤ Bytearray function returns a sequence of bytes which
are mutable

99
Bytes and Bytearrays

String Objects vs Byte Objects


100
Bytes and Bytearrays

String Objects vs Byte Objects


101
Functions for bytes and bytearray

enumerate() Used to generate the ordered pair of the index and value

len() Used to get the length of the byte string

reversed() Used to reverse the sequences

max() Used to return the maximum element

min() Used to return the minimum element

list() Used to convert the iterable into a list

sum() Used to return the sum of objects


range() Used to return the range of numbers

sorted() Used to sort


102
Methods for bytes and bytearray

find() Used to find the lowest index of a byte substring

rfind() Used to find the highest index of a byte substring

startswith() Used to check if a string starts with byte substring

endswith() Used to check if a string ends with byte substring

strip() Used to remove the characters from a byte string

lstrip() Used to remove the leading characters from a byte


string
rstrip() Used to remove the trailing characters from a byte
string
Center() Used to pad the byte string

ljust() Used for right padding 103


Methods for bytes and bytearray

partition() Used to split the byte string based on separator from left to
right
rpartition() Used to split the byte string based on separator from right to
left
zfill() Used to left pad the byte string with zero’s for required length

title() Used to convert to Pascal Case

maketrans() Used to construct the transition table

translate() Used to translate using maketrans

decode() Used to encode strings with the specified encoding scheme

104
Methods in bytearray

append() Used add an element at the end of a list


copy() Used to shallow copy a list
count() Used to count the required elements in a list
extend() Used to concatenate the list
index() Used to return the index of the specified element
insert() Used to insert an element at a specified index
pop() Used to remove an element at a specified index
remove() Used to remove an element at a specified element
reverse() Used to reverse a list and update
Clear() Used to clear the bytearray
105
12
Python Course
Day - 12

106
What You’ll Learn Today

¤ Non Sequenced containers


¤ Sets and frozen sets in python
¤ Functions and Methods of Sets

107
By the end of this session

¤ Hashing
¤ Working of sets in python
¤ Working with sets in python

108
Concept of hash table

¤ Hashing data structure is designed to use a special


function called the Hash function which is used to
map a given value with a particular key for faster
access of elements.
¤ The efficiency of mapping depends of the efficiency
of the hash function used.

109
Concept of hash table

¤ The mapping between an item and the slot where that item
belongs in the hash table is called the hash function. The hash
function will take any item in the collection and return an integer
in the range of slot names, between 0 and m-1

Hash Table 110


Concept of hash table

¤ A collision occurs when the algorithm generates the same hash for
more than one value.
¤ The systematic method for placing the second item in the hash
table is called collision resolution.
¤ Open addressing
¤ Separate Chaining

111
Sets

¤ A Set is an unique collection of heterogenous


unordered elements which are iterable, mutable.
¤ A Frozen Set is an unique collection of heterogenous
unordered elements which are iterable, immutable.

112
Sets

enumerate() Used to generate the ordered pair of the index and value

len() Used to get the length of the set

max() Used to return the maximum element

min() Used to return the minimum element

list() Used to convert the iterable into a list

sum() Used to return the sum of objects


range() Used to return the range of numbers

sorted() Used to sort

113
Sets

add() Adds an element to the set

clear() Removes all the elements from the set

copy() Returns a shallow copy of the set

difference() Returns the unique items in first set

difference_update() Removes the unique items in first set

discard() Remove the specified item

intersection() Returns the non unique items in first set


intersection_update( Removes the unique items in first set
)
isdisjoint() Returns whether two sets have a intersection or not
114
Sets

issubset() Returns whether another set contains this set or not

issuperset() Returns whether this set contains another set or not

pop() Removes an element from the set

remove() Removes the specified element

symmetric_difference() Returns a set with the symmetric differences of two


sets
symmetric_difference_update Inserts the symmetric differences from both sets
()
union() Return a set containing the union of sets

update() Update the set with the union of this set and others

115
13
Python Course
Day - 13

116
What You’ll Learn Today

¤ Non Sequenced containers


¤ Dictionaries in python
¤ Functions and Methods of Dictionaries

117
By the end of this session

¤ Hash Map
¤ Working with dictionaries in python

118
Dictionaries

¤ A Dictionary is an unordered collection of key-value


pairs. It supports mutation.
¤ It uses the concept of HashMap

119
Concept of hash map

¤ Hash map data structure is designed to use a special function


called the Hash function which is used to map a given value
with a particular key for faster access to its corresponding
values.
¤ The efficiency of mapping depends of the efficiency of the
hash function used.

120
Concept of hash map

¤ HashMap works on the principal of hashing.


¤ The mapping between an item and the slot where that item
belongs in the hash table is called the hash function. The hash
function will take any item in the collection and return an integer
in the range of slot names, between 0 and m-1
¤ The returned integer will be used as the key for its corresponding
value mapping.

121
Dictionaries

enumerate() Used to generate the ordered pair of the index and value

len() Used to get the length of the dictionary

max() Used to return the maximum element

min() Used to return the minimum element

list() Used to convert the iterable into a list

sum() Used to return the sum of objects


range() Used to return the range of numbers

sorted() Used to sort the keys

reversed() Used to reverse the keys


122
Dictionaries

clear() Removes all the elements from the dictionary

copy() Returns a shallow copy of the dictionary

fromkeys() Returns a dictionary with the specified keys and value

get() Returns the value of the specified key

items() Returns a list containing a tuple for each key value pair

keys() Returns a list containing the dictionary's keys

pop() Removes the element with the specified key

popitem() Removes the last inserted key-value pair

setdefault() Returns the value of the specified key

update() Updates the dictionary with the specified key-value pairs

values() Returns a list of all the values in the dictionary 123


14
Python Course
Day - 14

124
What You’ll Learn Today

¤ Operators in Python

125
By the end of this session

¤ Working with operators in python

126
Operators

¤ Arithmetic Operators
¤ Comparison Operators
¤ Logical Operators
¤ Bitwise Operators
¤ Assignment Operators
¤ Identity Operators
¤ Membership Operators
¤ Augmented Assignment Operators

127
15
Python Course
Day - 15

128
What You’ll Learn Today

¤ Python Statements
¤ Conditional Statements
¤ Iteration Statements

129
By the end of this session

¤ Control Structures
¤ Working with control structures in python

130
Control Structures

¤ A control structure directs the order of execution of


the statements in a program.
¤ Sequential
¤ Selection
¤ Repetition
¤ Function Calls

131
Control Structures

¤ By default, all the statements in python follow


sequential control structure.
¤ In selection control structure, we use the conditional
statements to control the flow of execution of code.
¤ In repetition control structure, we use the loop
statements to repeat a block of code.
¤ We can use the functional calls to call the user
defined functions.

132
Conditional Statements

¤ Condition statements are used for decision making.


They use the Boolean values in controlling the flow.
¤ Syntax –
if condition1:
statement1
elif condition2:
statement2
else:
statement3

133
Iteration Statements

¤ Repetitive execution of the same block of code over


and over is referred to as iteration.
¤ Types,
¤ Definite Iteration
¤ In-Definite Iteration

134
Definite Iteration Statements

¤ Definite iteration, in which the number of repetitions


is specified explicitly in advance
¤ In python, definite iteration is done using “for” loop
¤ Syntax,
for element in <collection>:
Statements
else:
Statements

135
In-Definite Iteration Statements

¤ Indefinite iteration, in which the code block executes


until some condition is met
¤ In Python, the indefinite iteration is performed using
“while” loop
¤ Syntax,
while condition:
Statements
else:
Statements

136
Iteration Statements

¤ The break statement in


Python terminates the
current loop and
resumes execution at
the next statement
¤ The continue
statement in Python
returns the control to
the beginning of the
loop
137
16
Python Course
Day - 16

138
What You’ll Learn Today

¤ Comprehensions
¤ Sample Exercises using selection and repetitive
control structures

139
By the end of this session

¤ Working with comprehensions in python

140
Comprehensions

¤ Comprehensions in Python provide us with a short


and concise way to construct new sequences
¤ List Comprehensions
¤ Dictionary Comprehensions
¤ Set Comprehensions
¤ Generator Comprehensions

141
THANKS!
Any questions?
You can find me at
[email protected]

142

You might also like