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

Python Basics

The document provides an introduction to Python programming, covering its history, features, installation, debugging, and the use of different types of brackets. It explains Python's characteristics such as being a high-level, open-source, and user-friendly language, as well as its extensibility and support for object-oriented programming. Additionally, it discusses variables, keywords, type conversion, and various operators in Python.

Uploaded by

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

Python Basics

The document provides an introduction to Python programming, covering its history, features, installation, debugging, and the use of different types of brackets. It explains Python's characteristics such as being a high-level, open-source, and user-friendly language, as well as its extensibility and support for object-oriented programming. Additionally, it discusses variables, keywords, type conversion, and various operators in Python.

Uploaded by

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

Introduction to

Python Programming
V2V AKSHAY GAIKWAD
PROFESSOR
CLASSES V2V CLASSES
Python Capsule
HISTORY OF PYTHON

FEATURES OF PYTHOBN

INSTALLING PYTHON AND RUNNING


A PROGRAM

DEBUGGING THE PROGRAM

DIFFERENCE BETWEEN BRACES,


BRACKETS AND PARENTHESES

www.v2vclass.com Akshay A. Gaikwad


1.
HISTORY OF PYTHON
PROGRAMMING

www.v2vclass.com Akshay A. Gaikwad


3
History of Python Programming
▷ Python is a high level programming language.
▷ Python was developed by Guido Van Rossum.
▷ Python language first version was released in 1991
▷ Python was named as python after the BBC show
Monty Pythons Flying Circus
▷ Now a day’s python is getting more popular and it is
having a strong community
▷ In 1994, Python 1.0 was released with new features
▷ And then python 2.0 and after that python 3.0 was
released to rectify the flaw in the language
4
www.v2vclass.com Akshay A. Gaikwad
2.
FEATURES OF PYTHON
PROGRAMMING

www.v2vclass.com Akshay A. Gaikwad


5
Features of Python programming
 High language means , this is separate from the low processor operations
High Level Programming  High level language is closer to Human Language than machine language
Language

Python is a free and  Python is free, there is no need to pay money for installing python and one can
easily download it from the website
open source language  Python is an open source language, it makes the python popular

 In python there is no need of declaring the variables and in a single statement


Python is simple the users can express the complex operations
 It uses indentation for statement grouping instead of curly braces
 Python offers more structure and support for lengthy or big programs than
batch files or shell script

 Python offers more effective an error checking than C. Python is having built in
Error checking and level data types for example dictionaries and flexible arrays.
high level data type  If we compare python with AWK and Perl, Python has more general data types
and because of this python is applicable to bigger problem domain.
www.v2vclass.com Akshay A. Gaikwad
Features of Python programming
 Python can run on any operating systems like Windows, Linux, Unix , DOS, etc . Python is
Platform Independent interpreted Language , it saves time as it does not need compilation and linking.
 The interpreter can be utilized intelligently, which makes it easy to try different things with
Language elements of the language , to write disposed of programs , or to test function in the midst of
bottom up program development. It resembles a convenient work area calculator for clients

 Python is having the features of abstraction and hiding of the details which
Python is User makes it more user friendly. In python we can divide the programs into
Friendly modules that can be used in other programs.
 .python provides interface to the GUI kit such as TK

 If you have good knowledge of C programming then it is easy to add new built in
Python is function or method or module to the interpreter.
 If you are writing application in C and you are linking the python interpreter into
Extensible an application then you can use it is an extension or command language for that
application.

 Python supports the object oriented programming concept. But OOP is optional
Python is object in python
oriented  But whenever the user feels like using the OOPS concept user can use

www.v2vclass.com Akshay A. Gaikwad


3.
INSTALLING PYTHON AND
RUNNING THE PROGRAM

www.v2vclass.com Akshay A. Gaikwad


8
 Go to website
http://www.python.org
 Click on Downloads.
Download any Python version
from the given list (python
2.7,3.2, 3.6 are the
recommended versions) for
windows
 Install the program
 Double clicking a python
script lauches a command-
line window (cmd) where it
run
 You can also open IDLE
(Python GUI) for script writing

www.v2vclass.com Akshay A. Gaikwad


 Firstly open the IDLE, go to file
and create new file and then
write the program and then save
the program
 Then either go on run in the
menu bar or else press F5 to run
the program. Then it will run the
program in the shell
 If we don’t save the program
then a dialog box opens asking to
save the file and then on clicking
OK it saves the program and
then runs the program
 If you want to do small tricks
then use python shell to execute
www.v2vclass.com Akshay A. Gaikwad
10
4.
DEBUGGING THE PROGRAM
DEBUGGING THE PROGRAM

 In programming various errors occurred they are called


as bugs
 The process of tracking the bugs are called as Debugging
the program
 There are 4 types of errors:-
1. Syntax Errors
2. Runtime Errors
3. Semantic Errors
4. Experimental Debugging

12
www.v2vclass.com Akshay A. Gaikwad
 Syntax means the structure of the program. If the syntax is not correct then it
result into syntax errors
SYNTAX ERRORS  E.g:- (2+2) this wont give an error but (2+2 then it will give an error because
the parentheses is not closed hence it will give syntax errors.

 Runtime errors are known as runtime errors because the error doesn’t
RUNTIME appear until after the program has started running runtime errors are
ERRORS also known exceptions which indicates something bad has happened
 Runtime errors occurs very rarely

 In case of semantic errors your program will execute properly without


showing any error message but it will not give the correct result you
SEMANTIC can also say hat there is a logical error
ERRORS  Tracking this error is tricky, since it requires to look for the output and
find out how the code is working

 Debugging is a challenging work. You have to check and infer the


processes and events that led to the results you see. Debugging is also
EXPERIMENTAL known as experimental science
DEBUGGING  If hypothesis is correct then your program will work or else you have to
come up with new hypothesis
13
www.v2vclass.com Akshay A. Gaikwad
5.
BRACKETS, CURLY
BRACKETS AND
PARANTHESES

www.v2vclass.com Akshay A. Gaikwad


BRACKETS
 Brackets are used for defining the list in python.
 The list of elements are put inside the brackets.
 For example:-
list1 = [a,b,c,d,e]
list2 = [1,2,3,4,5,6]

www.v2vclass.com Akshay A. Gaikwad


CURLY BRACKETS
 Curly Brackets{} are used to define the dictionaries in the python.
 The elements of the dictionaries are enclosed in the curly bracket
 For example:-
Mydict= {‘name’:’v2v class’, ‘city’:’Kalyan’}

www.v2vclass.com Akshay A. Gaikwad


PARENTHESES
 Tuples are enclosed in the parentheses()
 The elements of the tuple are written inside the parentheses
 For example:
a=(1,2,3,4,5)
 The parentheses are also used for the order of operation or evaluation
of operation.
For example: (a+b)*(c+d)

www.v2vclass.com Akshay A. Gaikwad


Python Capsule
HISTORY OF PYTHON

FEATURES OF PYTHOBN

INSTALLING PYTHON AND RUNNING


A PROGRAM

DEBUGGING THE PROGRAM

DIFFERENCE BETWEEN BRACES,


BRACKETS AND PARENTHESES

www.v2vclass.com Akshay A. Gaikwad


Prof. Akshay Gaikwad

FOLLOW US ON:
v2vclass

www.v2vclass.com

V2VClasses

www.v2vclass.com Akshay A. Gaikwad


Variables , Keywords
and Type conversion
V2V AKSHAY GAIKWAD
PROFESSOR
CLASSES V2V CLASSES

www.v2vclass.com Akshay A. Gaikwad


Python Capsule
Value and Data Type

Variables and variable name

How to assign value to the variable

Key words in Python

Type conversion in Python


www.v2vclass.com Akshay A. Gaikwad
1.
VALUE AND DATA TYPE

www.v2vclass.com Akshay A. Gaikwad


Value and Data Types

 Value is nothing but the numbers or letters. For e.g. 1,2,”hello”, 1.3222
 These value belongs to different data types
 In the above example 1 and 2 are integers
 hello is a string data type
 Any value written inside the quotation marks are considered as strings
 While 1.3222 is a float data type

www.v2vclass.com Akshay A. Gaikwad


 If you want to know the data
type of the value then we can
use type()
 We can see that ‘6’ is an
integer or int
 1.667 is a float datatype
 Hello is a string datatype
 Since value of d is in quotation
marks hence in output the
datatype is string.

www.v2vclass.com Akshay A. Gaikwad


2.
VARIABLE AND VARIABLE
NAME

www.v2vclass.com Akshay A. Gaikwad


VARIABLE

 Variables are reserved memory location for storing the value, when we
create variable it reserves some spaces
 Depending upon the data type interpreter reserves some memory and
decides what value can be stored in the allocated memory. We can
store integers, floats and strings in the variables
 In python variable declaration is not required. Python determines the
types of variable i.e. integers,floats,etc.

www.v2vclass.com Akshay A. Gaikwad


VARIABLE NAMES

 There are few rules for assigning the names to variales


1. Variable name should begin with the letter(a-z, A-Z) or
underscore(_)
2. Other characters in variable can be letters, numbers or underscore
3. Variable name can be of any length
4. Keywords cannot be used as variable names

www.v2vclass.com Akshay A. Gaikwad


3.
HOW TO ASSIGN VALUES TO
VARIABLES

www.v2vclass.com Akshay A. Gaikwad


ASSIGNING VALUE TO VARIABLES

 To assign a value to variable we use equal sign .


 Operand is on the left side of the equal sign(=) and operator is the value which is stored in
the variable
 For example:- a= 6 (in this a is the operand and 6 is the operator
 Multiple Assignments:- You can assign same value to more than one variable
 You can assign different values to different variables
 For example:-
- The variable name can be reused by assigning the new value to them.
- You can swap the values in python in a single line

www.v2vclass.com Akshay A. Gaikwad


4.
KEYWORDS IN PYTHON

www.v2vclass.com Akshay A. Gaikwad


KEYWORDS IN PYTHON True class finally is return

False continue for lambda try


 Keywords are reserved
words and cannot be used None def from nonlocal while
as constant or variable or
and del global not with
any other identifier names
 Python keywords are as elif if or yield
always in lower case
Assert else import pass
letters except for 3 wrods
 Different keywords of break except In raise
python are as follows:-
www.v2vclass.com Akshay A. Gaikwad
5.
TYPE CONVERSION IN
PYTHON

www.v2vclass.com Akshay A. Gaikwad


 Type conversion converts one data type to another data type.
.python defines type conversion features
 int(a,base) :- It converts any data type to integer.
 float() :- It converts any data type to floating point number
 ord() :- It converts a character to string
 hex() :- It converts integer to hexadecimal string
 oct():- It converts integer to octal string
 tuple():- It converts any list of items into tuples
 set() :- It return the type after converting to set
 list() :- It converts any data type to a list type
 dist():- It converts a tuple of order(key,value) into a dictionary
 str() :- It converts integer into string
 complex(real,img) :- It converts real numbers to complex(real,img) number

www.v2vclass.com Akshay A. Gaikwad


Example:- The code converts integers into float, hexadecimal ,
octal and string

www.v2vclass.com Akshay A. Gaikwad


Python Capsule
Value and Data Type

Variables and variable name

How to assign value to the variable

Key words in Python

Type conversion in Python


www.v2vclass.com Akshay A. Gaikwad
Prof. Akshay Gaikwad

FOLLOW US ON:
v2vclass

www.v2vclass.com

V2VClasses

www.v2vclass.com Akshay A. Gaikwad


Operators in
Python
V2V AKSHAY GAIKWAD
PROFESSOR
CLASSES V2V CLASSES

www.v2vclass.com Akshay A. Gaikwad


Python Capsule
WHAT IS OPERATOR
DIFFERENT TYPES OF
OPERATOR

ORDER OF OPERATIONS

EXPRESSIONS WITH
EXAMPLE

www.v2vclass.com Akshay A. Gaikwad


1.
WHAT ARE THE OPERATORS
IN PYTHON

www.v2vclass.com Akshay A. Gaikwad


OPERATORS IN PYTHON
 Operators are special symbols that represents different
operations
 There are different types of operators in Python:-
1. Arithmetic Operators
2. Comparison(Relational) Operator
3. Assignment Operator
4. Logical Operator
5. Bitwise Operator
6. Membership Operator
7. Identity Operator
www.v2vclass.com Akshay A. Gaikwad
2.
DIFFERENT TYPES OF
OPERATORS

www.v2vclass.com Akshay A. Gaikwad


1. ARITHMETIC OPERATOR
Operator Description
+ It adds value on either side of the operator

- It subtracts right hand operand from left hand operand

* It multiples values on either side of the operator

/ It divides the left hand operand by right hand operand and returns quotient

% It divides the left hand operand by right hand operand and returns remainder

** It performs exponential(powers) calculation on operators

// Floor Division – The division of operands where the result is the quotient in
which the digits after the decimal points are removed But if one of the
operands is negative , the result is floored , i.e., rounded away from zero
www.v2vclass.com Akshay A. Gaikwad
2. COMPARISON OPERATOR
Operator Description
== If the value of the operands are equal, then the condition becomes True

!= If value of the two operands are not equal, then the condition becomes True

<> If value of the two operands are not equal, then the condition becomes True

> If value of left hand operand is greater than right hand operator, then the
condition becomes True
< If value of right hand operand is greater than the left hand operand, then the
condition becomes True
>= If value of left hand operand is greater than or equal right hand operator, then
the condition becomes True
<= If value of right hand operand is greater than or equal to the left hand operand,
then the condition becomes True
www.v2vclass.com Akshay A. Gaikwad
3 ASSIGNMENT OPERATOR
Operator Description
= It is used to assign the value of right side operands to left side operand
+= It is used to add right operand to the left operand and result is assigned
to the left operand.
-+ The right operand gets subtracted from the left operand and the result is
assigned to the left operand
*= The right hand operand multiplied with left operand and tesult is assigned to left
operand
/= Left operand is divided by right operand and result is assigned to left operand

%= It performs modulus on tow operands and the result stored to left operand
**= It performs power calculation on operators and value is assigned to the left
operand
//= It performs floor division on operators and assigns value to the left operand
www.v2vclass.com Akshay A. Gaikwad
4 BITWISE OPERATOR
Operator Description
& It is Binary AND operator. This Operator is used to copy a bit to the result if it
exists in both operands
| It is Binary OR operator .it copies the bit if it is set in one operand but not both

^ It is Binary XOR operator .it copies the bit if it is set in one operand but not
both.

~ It is Binary Ones Complement operators. It is unary and has the effect of ‘


Flipping’

<< It is Binary Left Shift operator .it shifts the left operands value to left by the
number of bits given by the right operand.
>> It is Binary Right shift operator. It shifts the left operands value to right by the
number of bits given by the operand
www.v2vclass.com Akshay A. Gaikwad
5. LOGICAL OPERATOR

Operator Description

AND Logical AND– If both the operands are true then the condition becomes True

OR Logical OR:- If any of the two operands are non-zero then the condition becomes
True
NOT Logical NOT :- Used to reverse the logical state of its operand.

www.v2vclass.com Akshay A. Gaikwad


6. MEMBERSHIP OPERATOR
Operator Description

in Evaluate to true if it finds a variable in the specified sequence and false


otherwise.
not in Evaluates to true if it does not finds a variable in the specified sequence and
false otherwise.

7. IDENTIFY OPERATOR
Operator Description

is Evaluate to true if the variables on either side of the operator point to the
same object and false otherwise
not is Evaluates too false if the variables on either side of the operators point to
the same object and true otherwise.
www.v2vclass.com Akshay A. Gaikwad
3.
ORDER OF OPERATIONS

www.v2vclass.com Akshay A. Gaikwad


- The order of operation means the operator precedence to evaluate an
expression. The operator precedence and which are having lowest while
evaluating the expression.
- The operator precedence is shown in table it is descending order

▷ Parentheses have the highest precedence and Logical OR has lowest precedence

www.v2vclass.com Akshay A. Gaikwad


Operator Description
() Parentheses
** Exponent
+x,-x, ~x Unary plus, Unary minus, Bitwise NOT
*, /, //, % Multiplication, Division, Floor Division, Modulus
+, - Addition, Subtraction
<< , >> Bitwise Shift Operators
& Bitwise AND
^ Bitwise XOR
| Bitwise OR
==,!=,>,>=,<,<=,is, not is, in, Comparisons, Identity, Membership operators
not in
Not Logical NOT
And Logical AND
Or Logical OR

www.v2vclass.com Akshay A. Gaikwad


4.
EXPRESSIONS WITH
EXAMPLE

www.v2vclass.com Akshay A. Gaikwad


What is expression?
 An expressions is nothing but a
combination of values, operators, and
variables
 Evaluation of expression gives a value.
 Expressions always come at the right
hand side of the assignement
statements.
 Value itself is an expression and also a
variable is an expression
 In the first example, string includes
quotation marks and the print statement
print the value inside the quotation
marks
 In the second example the value that is
printed is the expression value.

www.v2vclass.com Akshay A. Gaikwad


Python Capsule
WHAT IS OPERATOR
DIFFERENT TYPES OF
OPERATOR

ORDER OF OPERATIONS

EXPRESSIONS WITH
EXAMPLE

www.v2vclass.com Akshay A. Gaikwad


Prof. Akshay Gaikwad

FOLLOW US ON:
v2vclass

www.v2vclass.com

V2VClasses

www.v2vclass.com Akshay A. Gaikwad


Python IDLE
V2V AKSHAY GAIKWAD
PROFESSOR
CLASSES V2V CLASSES

www.v2vclass.com Akshay A. Gaikwad


Python Capsule
WHAT IS IDLE IN PYTHON

INTERACTIVE MODE

SCRIPT MODE

www.v2vclass.com Akshay A. Gaikwad


1.
WHAT IS IDLE

www.v2vclass.com Akshay A. Gaikwad


 Python comes with a Graphical User Interface(GUI) Integrated
Development Environment called IDLE
 The IDLE helps the user to write the program. It enables the user to
save and edit their work.
 There are Two modes of working in IDLE:-
 The TWO modes of working are:-
1. Interactive Mode
2. Script Mode

www.v2vclass.com Akshay A. Gaikwad


2.
INTERACTIVE MODE

www.v2vclass.com Akshay A. Gaikwad


 In interactive mode, user will tell the
python what to do and python will execute
the code immediately
 To open the python in interactive mode:-
a) Click on start menu
b) Click on Python version
c) Click on IDLE (python GUI)
 This will open the python in interactive
mode
 The open window is known as Python
shell also known as command prompt

www.v2vclass.com Akshay A. Gaikwad


 Understanding Jargons or special words/ expression
 print() function is known as statement
 Python is case sensitive, so all commands of python are in lowercase. For e.g. if we write print
function as PRINT or Print then python will generate an error message
 Errors Generation
 If computer does not understand any command which is typed incorrectly will generate the error
 E.g Print(‘v2v classes’)  In this you can see that the print command is in uppercase, it is not
understand by the computer, hence it generates the error
 Colour Coding
 Python uses the colour coding which will be helpful to understand what you have typed visually
categorizing it.
 For e.g.:- print("Welcome toV2V Classes")
 In this print is in pink colour and the message written inside the parentheses is in green colour
and the output is in blue colour

www.v2vclass.com Akshay A. Gaikwad


3.
SCRIPT MODE

www.v2vclass.com Akshay A. Gaikwad


 Second mode of IDLE is script mode.
 In this mode we can write, edit, load and
save your Python program like word
processor
 You can also cut, copy and paste
operations
 To open the window in script mode:
1. Click on file menu on the interactive
window
2. Click on New File
 After this a script window will get open
 If you will write print(“welcome to python
learning in v2v classes”) and press
ENTER key then nothing will happen
 Because we are not in interactive mode.
 We have to save the program first and
then run it

www.v2vclass.com Akshay A. Gaikwad


Python Capsule
WHAT IS IDLE IN PYTHON

INTERACTIVE MODE

SCRIPT MODE

www.v2vclass.com Akshay A. Gaikwad


Prof. Akshay Gaikwad

FOLLOW US ON:
v2vclass

www.v2vclass.com

V2VClasses

www.v2vclass.com Akshay A. Gaikwad


Conditional Statements
in Python
V2V AKSHAY GAIKWAD
PROFESSOR
CLASSES V2V CLASSES

www.v2vclass.com Akshay A. Gaikwad


Python Capsule
What is conditional statement and its
types

If else statement

Nested if else

Elif ladder
www.v2vclass.com Akshay A. Gaikwad
1.
WHAT IS CONDITIONAL STATEMENTS
AND ITS TYPES IN PYTHON

www.v2vclass.com Akshay A. Gaikwad


68
How does this
Conditional
statement
 In ‘Python’ program is executed sequentially. works
This happens when there is no conditions.
 If you put some condition for a block of statements the flow of execution might change based
on the result evaluated by the condition. This process is referred to as decision making in
‘Python.
 Conditional statements help you to make a decision based on certain conditions.
 These conditions are specified by a set of conditional statements having Boolean expressions
which are evaluated to a Boolean value true or false.

THERE ARE 3 DIFFERENT TYPES OF CONDITIONAL STATEMENTS:-


 IF ELSE STATEMENT
 NESTED IF ELSE STATEMENT
 IF- ELSE-IF LADDER STATEMENT

www.v2vclass.com Akshay A. Gaikwad69


CODE
 Firstly, the main function is executed and
during execution the compiler encounters the if
loop
 Then it checks the condition for the Boolean
CONDITION value like true and false.

IF
 Consider the value is true, then the compiler
IF
CONDITION will first compile the conditional code
CONDITION
IS TRUE
IS FALSE  Then it again execute the main function
CONDITIONAL  Now if we consider the Boolean value is False
CODE  then it will not execute the conditional code.
 It will directly execute the main function after
the if-else block

STOP

www.v2vclass.com Akshay A. Gaikwad70


2.
IF ELSE STATEMENTS

www.v2vclass.com Akshay A. Gaikwad


 IF statement is the most powerful conditional statement.
 In this statement we use relational operator i.e. ==,>=,<=,!=,etc.
 If the condition in IF block becomes False then the ELSE block is executed

 For example:
we know that if an angle is 90⁰ then the triangle is right angle
Now we will see it in IF ELSE form
if(angle==90)
triangle is right angle
else
triangle is not right angle
 NOTE:- If else loop is even possible without an else block

SYNTAX
If condition:
statement body1
else:
statement body2

www.v2vclass.com Akshay A. Gaikwad72


3.
NESTED IF ELSE
STATEMENTS

www.v2vclass.com Akshay A. Gaikwad


 If we require a series of decision, then we use nested IF-ELSE
 Use of one IF-ELSE statement inside an IF statement or ELSE
statement is called as Nested IF ELSE statement

if boolean_expression_1:
if nested_expression_1:
statement 1
else : NOTE: In nested if-else, we have to be careful with the indentation because multiple if-
statement 2 else constructs are involved in this process, so it becomes difficult to figure out
else: individual constructs. Proper indentation makes it easy to read the program.
if nested_expression_1:
statement 3
else:
statement 4

www.v2vclass.com Akshay A. Gaikwad


4.
ELIF STATEMENTS
 When a programming situation requires the choice of one case from many different cases successive
if statements can be tied together forming what is sometimes called as if-else-if statements
 An if condition is tested only when all previous if conditions in if-else ladder is false
 If any of the conditional expression evaluates to true, then it will execute the corresponding code
block and exits whole if-else ladder.

SYNTAX
 First of all condition1 is tested and if it TRUE then statement1 will be
If condition1 :
executed and control comes out of the whole if-else ladder
statement1
 If condition1 is FALSE then condition2 is tested.
elif condition2:
 Control will keep on flowing downward, if none of the conditional
statement2
statement is TRUE.
elif condition3:
 The last else is the default block of code which will gets executed if
statement3
none of the conditional statement is TRUE
else:
statement4

www.v2vclass.com Akshay A. Gaikwad


Python Capsule
What is conditional statement and its
types

If else statement

Nested if else

Elif ladder
www.v2vclass.com Akshay A. Gaikwad
Prof. Akshay Gaikwad

FOLLOW US ON:
v2vclass

www.v2vclass.com

V2VClasses

www.v2vclass.com Akshay A. Gaikwad


Loops in Python

V2V AKSHAY GAIKWAD


PROFESSOR
CLASSES V2V CLASSES

www.v2vclass.com Akshay A. Gaikwad


Python Capsule
What are loops and its types
For Loop in Python

While Loop

Nested Loops

Control Statements in Python


www.v2vclass.com Akshay A. Gaikwad
1.
WHAT ARE LOOPS IN
PYTHON AND ITS TYPES

www.v2vclass.com Akshay A. Gaikwad


81
 A Loop executes the sequence of statements many times until the stated condition becomes false.
 A loop consists of two parts, a body of a loop and a control statement. The control statement is a
combination of some conditions that direct the body of the loop to execute until the specified
condition becomes false.
 The purpose of the loop is to repeat the same code a number of times

There are 3 types of Loops in


Python:-
 FOR LOOP
WHILE LOOP
NESTED FOR LOOP

Akshay A. Gaikwad82
www.v2vclass.com
2.
FOR LOOP IN PYTHON

www.v2vclass.com Akshay A. Gaikwad


83
A for loop is a repetition control structure that allows you to efficiently
write a loop that needs to execute a specific number of times.
SYNTAX
for interating _va in sequence:
statements;
 If the Logical expression list contained by a sequence then it is evaluated first
.The first element in the sequence is assigned to the iterating variable i.e.
iterating _var After that the block of the statement is executed until the
complete sequence is done or finished
 The range () function is a built –in function. it iterates over a sequence of
numbers . It produces an iterator of arithmetic series.
 The range () function produce an iterator to progress integers begin with 0 upto
n-1. You can typecast the range () to list () to get a list object of the sequence.
After that you can iterate the list by using a for Statement.
 Another way of iterating through each item is by using index offset into the
sequence itself. The following example shows iterating through each item or
element using index using index offset
 Using else Statement with Loops:
 Python allows you to use conditional statement along with the loop statement
 You can use the else statement with the for loop
www.v2vclass.com Akshay A. Gaikwad
3.
WHILE IN PYTHON

www.v2vclass.com Akshay A. Gaikwad


85
A while loop in Python programming repeatedly executes a target
statement as long as a given condition is true.

SYNTAX
While condition:
statements

STEPS HOW THE WHILE LOOP EXECUTES


 In the syntax the statement can be single or multiple statements of
block of statements with uniform indent
 The condition may be any Logical expression.
 The condition is true if any non-zero value is given. Till the condition
become true the while loop iterates. If the condition returns true then the
statements inside the body of while loop are executed else control
comes out of the loop.
 If the condition becomes false then the program control passes to the
line immediately following the loop..
 Python uses indentation for grouping the statements. All the statement
are by same number of character spaces

www.v2vclass.com Akshay A. Gaikwad


INFINITE LOOP
 When the condition never become false then loop become the infinite loop.
 You have to take care when using the while loop, because there is possibility that the condition
will never resolves to a false value.
 The infinite loop means the result in the loop never ends.
 You can use the infinite loop in client server programming. In client server programming the
server needs to run continuously so that client programs can communicate with the server
whenever it required.
 As you can see the output program has gone into the infinite loop. To exit from the infinite loop
press CTRL +C Keys .
 Using else statement with loops
1. Python is having the feature to use else statement with the loop statement .
2. With the for loop if the else statement is used , the else statement is executed when the
loop has done in iterating the list .
3. With the while loop if the else statement is used , the else statement is executed if the
condition becomes false.
www.v2vclass.com Akshay A. Gaikwad
4.
NESTED LOOPS IN PYTHON

www.v2vclass.com Akshay A. Gaikwad


 Python allows you to use nested loops that is one inside the another loop.You can use
for loop inside the for loop .
 The for loop which is inside finishes first and then it passes the control to the outer for
loop statement.

SYNTAX
Following is the syntax of a nested for loop :-
for iterating_var1 in sequence1:
for iterating_var2 in sequence2:
statement(s)
statement(s)

Following is the syntax of a nested while loop:-


while expression1:
while expression2:
statement(s)
In loop nesting you can also put any type of loop inside any
other type of loop.
It means you can put a for loop inside a while loop or vice-
versa
www.v2vclass.com Akshay A. Gaikwad
5.
CONTROL STATEMENTS IN
PYTHON

www.v2vclass.com Akshay A. Gaikwad


WHAT IS CONTROL STATEMENTS IN PYTHON?

 Loop control statements change execution from its


normal sequence.
 When execution leaves a scope, all automatic objects that
were created in that scope are destroyed.
 Python supports the following control statements.:
1. Break
2. Continue
3. Pass

www.v2vclass.com Akshay A. Gaikwad


Break Statements
 Break statements is used to terminate the loop
and it transfer execution to the statement
immediately following the loop.
 If the loops are nested then the break would
only exit from the loop which contains it. It
means the break statement exit only from the
single loop
 The break statement’s most common use is
when any external condition triggered requires
a quick exit from the loop . you can use the
break statement in for and while loop.
 Following is the syntax of break statement :
Syntax:-
Break
www.v2vclass.com Akshay A. Gaikwad
Continue Statements
 The continue statement makes the loop to skip
the rest of its body and immediately reset its
condition before the re-iteration.
 The continue statement returns the control to the
start of the current loop.
 When the condition encountered, the loop do not
execute the remaining statements and starts the
next iteration
 The continue statement is used in both for while
loop.
 Following is the syntax of continue statement:
Syntax
Continue
www.v2vclass.com Akshay A. Gaikwad
Pass Statements
 If there is a need of syntactical statement yet
you don’t need any other or code to execute
then you can use the pass statement.
 When you execute then pass statement nothing
will happen so you can say the pass statement
is an invalid or null operation.
 The pass statement is valuable in place where
your code will in the end go , yet has not been
composed yet. For instance stubs.

www.v2vclass.com Akshay A. Gaikwad


Python Capsule
What are loops and its types
For Loop in Python

While Loop

Nested Loops

Control Statements in Python


www.v2vclass.com Akshay A. Gaikwad
Prof. Akshay Gaikwad

FOLLOW US ON:
v2vclass

www.v2vclass.com

V2VClasses

www.v2vclass.com Akshay A. Gaikwad

You might also like