SlideShare a Scribd company logo
Faculty of Engineering Science & Technology(FEST)
Hamdard Institute of Engineering Technology(HIET)
HAMDARD UNIVERSITY
Instructor
ABDUL HASEEB
HANDS-ON WORKSHOP ON PYTHON
PROGRAMMING LANGUAGE
Faculty Development Program (Session-10)
DAY-2
The Python Programming Language
Day 2 Workshop Contents
• range function
• Indentation in Python programming
• Loops
• for loop
• while loop
• Conditional statement
• if statement
• elif statement
• Modules of Math, Time and Random variable
Indentation
• Python uses whitespace
indentation to delimit
blocks – rather than curly
braces { code } or keywords
begin code end. An increase
in indentation comes after
certain statements followed
by colon( : ); a decrease in
indentation signifies the end
of the current block. This
feature is also sometimes
termed the off-side rule.
The range() function
The built-in function range() is the right
function to iterate over a sequence of
numbers. It generates an iterate of arithmetic
progressions
• range(stop) -> list of integers
>>> list(range(10))
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
• range(start, stop) -> list of integers
>>> list(range(2,10))
[2, 3, 4, 5, 6, 7, 8, 9]
• range(start, stop, step) -> list of integers
>>> list(range(2,10,3))
[2, 5, 8]
The range() function
for loop
For loop is use to repeat a part of code
to the defined number of time
Syntax:
for iterating_variable in sequence:
. . . . . . . . . . .statements(s)
The iterating_variable may be int or
string and automatically update to the
type of sequence
Program
for i in range(10):
print('Hamdard University')
Output
Hamdard University
Hamdard University
Hamdard University
Hamdard University
Hamdard University
Hamdard University
Hamdard University
Hamdard University
Hamdard University
Hamdard University
for loop example
For loop with integer sequence
a=10
for i in range(a):
print(i)
0
1
2
3
4
5
6
7
8
9
a=10
for i in range(a):
print(a)
10
10
10
10
10
10
10
10
10
10
For loop with different range()
function sequence
for loop starting from 0 and
run until define limit
reaches
Declaring for loop starting
value and run in define limit
Declaring loop starting, final
and difference value
for i in range(10):
print('The value is ',i)
for i in range(3,10):
print('The value is ',i)
for i in range(3,10,2):
print('The value is ',i)
The value is 0
The value is 1
The value is 2
The value is 3
The value is 4
The value is 5
The value is 6
The value is 7
The value is 8
The value is 9
The value is 3
The value is 4
The value is 5
The value is 6
The value is 7
The value is 8
The value is 9
The value is 3
The value is 5
The value is 7
The value is 9
for Loop Exercise 1
Generate Table of the user enter number
Output will look like this:
Enter a Number = 2
2 x 1 = 2
2 x 2 = 4
. . . . . .
2 x 10 = 20
for Loop Exercise 1 Solution
num = input("Enter a number = ")
for i in range(1,11):
print(num,' x ',i,' = ',int(num)*i)
Enter a number = 6
6 x 1 = 6
6 x 2 = 12
6 x 3 = 18
6 x 4 = 24
6 x 5 = 30
6 x 6 = 36
6 x 7 = 42
6 x 8 = 48
6 x 9 = 54
6 x 10 = 60
for Loop Exercise 2
Write a program which generates values of
Sinθ from 0ᴼ to 360ᴼ with the gap 10ᴼ
Output will look like this:
Sin(0) = 0
Sin(10) = 0.17364
Sin(20) =0.34202
. . . . . . .
Sin(360) = 0
import math
from math import sin
for i in range(0,361,10):
print('sin(',i,')',math.sin(math.radians(i)))
sin( 0 ) 0.0
sin( 10 ) 0.17364817766693033
sin( 20 ) 0.3420201433256687
sin( 30 ) 0.49999999999999994
sin( 40 ) 0.6427876096865393
sin( 50 ) 0.766044443118978
sin( 60 ) 0.8660254037844386
sin( 70 ) 0.9396926207859083
sin( 80 ) 0.984807753012208
sin( 90 ) 1.0
. . . . . .
For Loop Exercise 2 Solution
Decision Making statement
• Decision making check the
conditions occurring while
execution of the program
and take actions according to
the given conditions.
• Decision structures evaluate
multiple expressions which
produce TRUE or FALSE as
outcome. If the condition is
TRUE then condition code
will run and if it FALSE then
the conditional code will by-
pass it.
Where to use if and where elif(else if)
if(condition)
condition code
if(condition)
condition code
if(condition)
condition code
if(condition)
condition code
if(condition)
condition code
elif(condition)
condition code
elif(condition)
condition code
elif(condition)
condition code
Multiple outcomes Only Single outcomes
If else condition exercise solution
num = int(input("Enter a number = "))
if num%2==0:
print('The number',num,'is divisible by 2')
if num%3==0:
print('The number',num,'is divisible by 3')
if num%5==0:
print('The number',num,'is divisible by 5')
else:
print("the num you entered is prime")
Enter a number = 6
The number 6 is divisible by 2
The number 6 is divisible by 3
If else condition exercise solution
obtain_marks= int(input("Enter obtain marks = "))
total_marks = int(input("Enter total marks = "))
percentage = (obtain_marks/total_marks)*100
print("The percentage is = ", percentage)
if percentage>90:
print('The Grade is A+')
elif percentage>80:
print('The Grade is A')
elif percentage>70:
print('The Grade is B')
elif percentage>60:
print('The Grade is C')
elif percentage>50:
print('The Grade is D')
else:
print('Fail')
Enter obtain marks = 65
Enter total marks = 100
The percentage is = 65.0
The Grade is C
While loop
• While loop is a conditional
loop
• The code run until the
condition remain True and
escape if when condition
become False
Simply:
while loop = for loop + if else
write for loop using while loop
a=0
while(a<10):
print('Karachi',a)
a += 1 # a = a + 1
Karachi 0
Karachi 1
Karachi 2
Karachi 3
Karachi 4
Karachi 5
Karachi 6
Karachi 7
Karachi 8
Karachi 9
Example While loop
The following program take a character from user and show its ASCII
number. The program continue to run until user press TAB Key
a=0
while(a!=‘t’):
a = input("Enter a key, press Tab key to exit = ")
print('ASCII of',a,'is ',ord(a))
Enter a key, press Tab key to exit = f
ASCII of f is 102
Enter a key, press Tab key to exit = g
ASCII of g is 103
Enter a key, press Tab key to exit =
ASCII of is 9
Infinite Loop
Infinite loop run continuous and did not exit. The infinite
loop can be made by while loop with condition set to
‘True’ which mean the loop never become false and
never exit from while code block.
Syntax:
while True:
……….execute this code continuously to infinite time
Note this is infinite loop which never end to break the
infinite loop press Ctrl + c
While loop Exercise
• Write a program which print your name to
infinite time with delay of 1 sec.
HAMDARD UNIVERSITY
delay(1sec)
HAMDARD UNIVERSITY
delay(1sec)
HAMDARD UNIVERSITY
delay(1sec)
HAMDARD UNIVERSITY
delay(1sec)
. . . . . .
while loop Exercise Solution
import time
from time import sleep
while(True):
print('HAMDARD UNIVERSITY')
sleep(1)
HAMDARD UNIVERSITY
HAMDARD UNIVERSITY
HAMDARD UNIVERSITY
HAMDARD UNIVERSITY
. . . . . .
Logical operator in Python
Operator Description Example
and Logical
AND
If both the operands are
true then condition
becomes true.
(a and b) is true.
or Logical
OR
If any of the two operands
are non-zero then
condition becomes true.
(a or b) is true.
not Logical
NOT
Used to reverse the logical
state of its operand.
Not(a and b) is false.
For loop – if else example
msg = input("Enter a string = ")
for i in msg.lower():
if(i == 'a' or i =='e' or i == 'i'or i == 'o'or i == 'u'):
print(i , 'is vovel' )
else:
print(i , 'is not vovel' )
Enter a string = Hamdard
h is not vovel
a is vovel
m is not vovel
d is not vovel
a is vovel
r is not vovel
d is not vovel
Comparing Python with C/C++
Programming Parameter Python Language C/C++ Language
Programming type Interpreter Compiler
Programming Language High level Language Middle level Language
Header file import #include
Code block Whitespace Indentation
Code within curly bracket
{ code}
Single line statement
termination
Nothing Semicolon ;
Control flow (Multi line)
statement termination
Colon : Nothing
Single line comments # comments //comments
Multi-line comments
‘’’
Comments lines
‘’’
/*
Comments lines
*/
If error code found Partial run code until find error
Did not run until error is
present
Program length Take fewer lines Take relatively more lines
Python Programming code comparison with C
programming code
Python program C/C++ program
Multi-line comments
Including library
Declaring variable
Multi-line statement
Code Block
Single-line statement
Single line comments
‘’’ The Python Language
Example code ’’’
Import time
a=10 #integer value
name = 'karachi' #String
for i in range (a):
print("Hamdard")
print('University')
print(name)
#program end
/* The C/C++ language
Example code */
#include <iostream>
using namespace std;
int main()
{
int a = 10;
char name[10] = "Karachi";
for(int i =0 ; i <a ; i ++ )
{
cout<<"Hamdard ";
cout<<"University"<<endl;
}
cout<<name;
return 0;
}
//program end
THANK YOU

More Related Content

PPTX
Python programming workshop session 3
Abdul Haseeb
 
PPTX
Python workshop session 6
Abdul Haseeb
 
PPTX
Python programming workshop session 4
Abdul Haseeb
 
PPTX
Python programming workshop session 1
Abdul Haseeb
 
PPTX
10. Recursion
Intro C# Book
 
PPTX
12. Exception Handling
Intro C# Book
 
PPTX
09. Methods
Intro C# Book
 
PPTX
02. Primitive Data Types and Variables
Intro C# Book
 
Python programming workshop session 3
Abdul Haseeb
 
Python workshop session 6
Abdul Haseeb
 
Python programming workshop session 4
Abdul Haseeb
 
Python programming workshop session 1
Abdul Haseeb
 
10. Recursion
Intro C# Book
 
12. Exception Handling
Intro C# Book
 
09. Methods
Intro C# Book
 
02. Primitive Data Types and Variables
Intro C# Book
 

What's hot (20)

PPTX
03. Operators Expressions and statements
Intro C# Book
 
PPT
C++ Language
Syed Zaid Irshad
 
PPTX
04. Console Input Output
Intro C# Book
 
PPTX
Chapter 22. Lambda Expressions and LINQ
Intro C# Book
 
PPTX
USER DEFINE FUNCTIONS IN PYTHON
vikram mahendra
 
PPTX
INTRODUCTION TO FUNCTIONS IN PYTHON
vikram mahendra
 
PDF
Introduction to cpp
Nilesh Dalvi
 
PPTX
USER DEFINE FUNCTIONS IN PYTHON[WITH PARAMETERS]
vikram mahendra
 
PDF
Java Programming Workshop
neosphere
 
PPTX
13 Strings and Text Processing
Intro C# Book
 
PDF
Cbse marking scheme 2006 2011
Praveen M Jigajinni
 
PPSX
C++ Programming Language
Mohamed Loey
 
PPTX
GE8151 Problem Solving and Python Programming
Muthu Vinayagam
 
PPTX
Intro to c++
temkin abdlkader
 
PDF
Develop Embedded Software Module-Session 3
Naveen Kumar
 
PPTX
Introduction to c++
Himanshu Kaushik
 
PPT
Chap 5 c++
Venkateswarlu Vuggam
 
PPTX
OPERATOR IN PYTHON-PART2
vikram mahendra
 
PDF
Programming Fundamentals Arrays and Strings
imtiazalijoono
 
03. Operators Expressions and statements
Intro C# Book
 
C++ Language
Syed Zaid Irshad
 
04. Console Input Output
Intro C# Book
 
Chapter 22. Lambda Expressions and LINQ
Intro C# Book
 
USER DEFINE FUNCTIONS IN PYTHON
vikram mahendra
 
INTRODUCTION TO FUNCTIONS IN PYTHON
vikram mahendra
 
Introduction to cpp
Nilesh Dalvi
 
USER DEFINE FUNCTIONS IN PYTHON[WITH PARAMETERS]
vikram mahendra
 
Java Programming Workshop
neosphere
 
13 Strings and Text Processing
Intro C# Book
 
Cbse marking scheme 2006 2011
Praveen M Jigajinni
 
C++ Programming Language
Mohamed Loey
 
GE8151 Problem Solving and Python Programming
Muthu Vinayagam
 
Intro to c++
temkin abdlkader
 
Develop Embedded Software Module-Session 3
Naveen Kumar
 
Introduction to c++
Himanshu Kaushik
 
OPERATOR IN PYTHON-PART2
vikram mahendra
 
Programming Fundamentals Arrays and Strings
imtiazalijoono
 
Ad

Similar to Python programming workshop session 2 (20)

PPTX
1. control structures in the python.pptx
DURAIMURUGANM2
 
PDF
2 Python Basics II meeting 2 tunghai university pdf
Anggi Andriyadi
 
PDF
Python Decision Making And Loops.pdf
NehaSpillai1
 
PPT
Control structures pyhton
Prakash Jayaraman
 
PPTX
loops _
SwatiHans10
 
PPTX
Python if_else_loop_Control_Flow_Statement
AbhishekGupta692777
 
PPTX
Chapter 9 Conditional and Iterative Statements.pptx
XhelalSpahiu
 
PPTX
Module_2_1_Building Python Programs_Final.pptx
nikhithavarghese77
 
PPTX
Loops in Python
Arockia Abins
 
PDF
011 LOOP.pdf cs project important chapters
AryanRajak
 
PPTX
Week 4.pptx computational thinking and programming
cricketfundavlogs
 
PDF
Slide 6_Control Structures.pdf
NuthalapatiSasidhar
 
PDF
Python Programming
Sreedhar Chowdam
 
PPTX
Python.pptx
AKANSHAMITTAL2K21AFI
 
PPTX
ForLoops.pptx
RabiyaZhexembayeva
 
PPTX
While_for_loop presententationin first year students
SIHIGOPAL
 
PDF
While-For-loop in python used in college
ssuser7a7cd61
 
PPTX
python ppt.pptx
ssuserd10678
 
PDF
Python_Module_2.pdf
R.K.College of engg & Tech
 
PPTX
Fifth session
AliMohammad155
 
1. control structures in the python.pptx
DURAIMURUGANM2
 
2 Python Basics II meeting 2 tunghai university pdf
Anggi Andriyadi
 
Python Decision Making And Loops.pdf
NehaSpillai1
 
Control structures pyhton
Prakash Jayaraman
 
loops _
SwatiHans10
 
Python if_else_loop_Control_Flow_Statement
AbhishekGupta692777
 
Chapter 9 Conditional and Iterative Statements.pptx
XhelalSpahiu
 
Module_2_1_Building Python Programs_Final.pptx
nikhithavarghese77
 
Loops in Python
Arockia Abins
 
011 LOOP.pdf cs project important chapters
AryanRajak
 
Week 4.pptx computational thinking and programming
cricketfundavlogs
 
Slide 6_Control Structures.pdf
NuthalapatiSasidhar
 
Python Programming
Sreedhar Chowdam
 
ForLoops.pptx
RabiyaZhexembayeva
 
While_for_loop presententationin first year students
SIHIGOPAL
 
While-For-loop in python used in college
ssuser7a7cd61
 
python ppt.pptx
ssuserd10678
 
Python_Module_2.pdf
R.K.College of engg & Tech
 
Fifth session
AliMohammad155
 
Ad

Recently uploaded (20)

PPTX
Introduction to pediatric nursing in 5th Sem..pptx
AneetaSharma15
 
PDF
RA 12028_ARAL_Orientation_Day-2-Sessions_v2.pdf
Seven De Los Reyes
 
PPTX
Five Point Someone – Chetan Bhagat | Book Summary & Analysis by Bhupesh Kushwaha
Bhupesh Kushwaha
 
PPTX
Measures_of_location_-_Averages_and__percentiles_by_DR SURYA K.pptx
Surya Ganesh
 
PPTX
Kanban Cards _ Mass Action in Odoo 18.2 - Odoo Slides
Celine George
 
PDF
Presentation of the MIPLM subject matter expert Erdem Kaya
MIPLM
 
PPTX
An introduction to Dialogue writing.pptx
drsiddhantnagine
 
PDF
The Minister of Tourism, Culture and Creative Arts, Abla Dzifa Gomashie has e...
nservice241
 
PDF
Virat Kohli- the Pride of Indian cricket
kushpar147
 
PPTX
How to Manage Leads in Odoo 18 CRM - Odoo Slides
Celine George
 
PPTX
Odoo 18 Sales_ Managing Quotation Validity
Celine George
 
PPTX
PPTs-The Rise of Empiresghhhhhhhh (1).pptx
academysrusti114
 
PPT
Python Programming Unit II Control Statements.ppt
CUO VEERANAN VEERANAN
 
PDF
Health-The-Ultimate-Treasure (1).pdf/8th class science curiosity /samyans edu...
Sandeep Swamy
 
PPTX
Software Engineering BSC DS UNIT 1 .pptx
Dr. Pallawi Bulakh
 
PDF
UTS Health Student Promotional Representative_Position Description.pdf
Faculty of Health, University of Technology Sydney
 
PDF
Phylum Arthropoda: Characteristics and Classification, Entomology Lecture
Miraj Khan
 
PDF
Sunset Boulevard Student Revision Booklet
jpinnuck
 
PPTX
HISTORY COLLECTION FOR PSYCHIATRIC PATIENTS.pptx
PoojaSen20
 
PPTX
CONCEPT OF CHILD CARE. pptx
AneetaSharma15
 
Introduction to pediatric nursing in 5th Sem..pptx
AneetaSharma15
 
RA 12028_ARAL_Orientation_Day-2-Sessions_v2.pdf
Seven De Los Reyes
 
Five Point Someone – Chetan Bhagat | Book Summary & Analysis by Bhupesh Kushwaha
Bhupesh Kushwaha
 
Measures_of_location_-_Averages_and__percentiles_by_DR SURYA K.pptx
Surya Ganesh
 
Kanban Cards _ Mass Action in Odoo 18.2 - Odoo Slides
Celine George
 
Presentation of the MIPLM subject matter expert Erdem Kaya
MIPLM
 
An introduction to Dialogue writing.pptx
drsiddhantnagine
 
The Minister of Tourism, Culture and Creative Arts, Abla Dzifa Gomashie has e...
nservice241
 
Virat Kohli- the Pride of Indian cricket
kushpar147
 
How to Manage Leads in Odoo 18 CRM - Odoo Slides
Celine George
 
Odoo 18 Sales_ Managing Quotation Validity
Celine George
 
PPTs-The Rise of Empiresghhhhhhhh (1).pptx
academysrusti114
 
Python Programming Unit II Control Statements.ppt
CUO VEERANAN VEERANAN
 
Health-The-Ultimate-Treasure (1).pdf/8th class science curiosity /samyans edu...
Sandeep Swamy
 
Software Engineering BSC DS UNIT 1 .pptx
Dr. Pallawi Bulakh
 
UTS Health Student Promotional Representative_Position Description.pdf
Faculty of Health, University of Technology Sydney
 
Phylum Arthropoda: Characteristics and Classification, Entomology Lecture
Miraj Khan
 
Sunset Boulevard Student Revision Booklet
jpinnuck
 
HISTORY COLLECTION FOR PSYCHIATRIC PATIENTS.pptx
PoojaSen20
 
CONCEPT OF CHILD CARE. pptx
AneetaSharma15
 

Python programming workshop session 2

  • 1. Faculty of Engineering Science & Technology(FEST) Hamdard Institute of Engineering Technology(HIET) HAMDARD UNIVERSITY Instructor ABDUL HASEEB HANDS-ON WORKSHOP ON PYTHON PROGRAMMING LANGUAGE Faculty Development Program (Session-10) DAY-2
  • 2. The Python Programming Language Day 2 Workshop Contents • range function • Indentation in Python programming • Loops • for loop • while loop • Conditional statement • if statement • elif statement • Modules of Math, Time and Random variable
  • 3. Indentation • Python uses whitespace indentation to delimit blocks – rather than curly braces { code } or keywords begin code end. An increase in indentation comes after certain statements followed by colon( : ); a decrease in indentation signifies the end of the current block. This feature is also sometimes termed the off-side rule.
  • 4. The range() function The built-in function range() is the right function to iterate over a sequence of numbers. It generates an iterate of arithmetic progressions
  • 5. • range(stop) -> list of integers >>> list(range(10)) [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] • range(start, stop) -> list of integers >>> list(range(2,10)) [2, 3, 4, 5, 6, 7, 8, 9] • range(start, stop, step) -> list of integers >>> list(range(2,10,3)) [2, 5, 8] The range() function
  • 6. for loop For loop is use to repeat a part of code to the defined number of time Syntax: for iterating_variable in sequence: . . . . . . . . . . .statements(s) The iterating_variable may be int or string and automatically update to the type of sequence
  • 7. Program for i in range(10): print('Hamdard University') Output Hamdard University Hamdard University Hamdard University Hamdard University Hamdard University Hamdard University Hamdard University Hamdard University Hamdard University Hamdard University for loop example
  • 8. For loop with integer sequence a=10 for i in range(a): print(i) 0 1 2 3 4 5 6 7 8 9 a=10 for i in range(a): print(a) 10 10 10 10 10 10 10 10 10 10
  • 9. For loop with different range() function sequence for loop starting from 0 and run until define limit reaches Declaring for loop starting value and run in define limit Declaring loop starting, final and difference value for i in range(10): print('The value is ',i) for i in range(3,10): print('The value is ',i) for i in range(3,10,2): print('The value is ',i) The value is 0 The value is 1 The value is 2 The value is 3 The value is 4 The value is 5 The value is 6 The value is 7 The value is 8 The value is 9 The value is 3 The value is 4 The value is 5 The value is 6 The value is 7 The value is 8 The value is 9 The value is 3 The value is 5 The value is 7 The value is 9
  • 10. for Loop Exercise 1 Generate Table of the user enter number Output will look like this: Enter a Number = 2 2 x 1 = 2 2 x 2 = 4 . . . . . . 2 x 10 = 20
  • 11. for Loop Exercise 1 Solution num = input("Enter a number = ") for i in range(1,11): print(num,' x ',i,' = ',int(num)*i) Enter a number = 6 6 x 1 = 6 6 x 2 = 12 6 x 3 = 18 6 x 4 = 24 6 x 5 = 30 6 x 6 = 36 6 x 7 = 42 6 x 8 = 48 6 x 9 = 54 6 x 10 = 60
  • 12. for Loop Exercise 2 Write a program which generates values of Sinθ from 0ᴼ to 360ᴼ with the gap 10ᴼ Output will look like this: Sin(0) = 0 Sin(10) = 0.17364 Sin(20) =0.34202 . . . . . . . Sin(360) = 0
  • 13. import math from math import sin for i in range(0,361,10): print('sin(',i,')',math.sin(math.radians(i))) sin( 0 ) 0.0 sin( 10 ) 0.17364817766693033 sin( 20 ) 0.3420201433256687 sin( 30 ) 0.49999999999999994 sin( 40 ) 0.6427876096865393 sin( 50 ) 0.766044443118978 sin( 60 ) 0.8660254037844386 sin( 70 ) 0.9396926207859083 sin( 80 ) 0.984807753012208 sin( 90 ) 1.0 . . . . . . For Loop Exercise 2 Solution
  • 14. Decision Making statement • Decision making check the conditions occurring while execution of the program and take actions according to the given conditions. • Decision structures evaluate multiple expressions which produce TRUE or FALSE as outcome. If the condition is TRUE then condition code will run and if it FALSE then the conditional code will by- pass it.
  • 15. Where to use if and where elif(else if) if(condition) condition code if(condition) condition code if(condition) condition code if(condition) condition code if(condition) condition code elif(condition) condition code elif(condition) condition code elif(condition) condition code Multiple outcomes Only Single outcomes
  • 16. If else condition exercise solution num = int(input("Enter a number = ")) if num%2==0: print('The number',num,'is divisible by 2') if num%3==0: print('The number',num,'is divisible by 3') if num%5==0: print('The number',num,'is divisible by 5') else: print("the num you entered is prime") Enter a number = 6 The number 6 is divisible by 2 The number 6 is divisible by 3
  • 17. If else condition exercise solution obtain_marks= int(input("Enter obtain marks = ")) total_marks = int(input("Enter total marks = ")) percentage = (obtain_marks/total_marks)*100 print("The percentage is = ", percentage) if percentage>90: print('The Grade is A+') elif percentage>80: print('The Grade is A') elif percentage>70: print('The Grade is B') elif percentage>60: print('The Grade is C') elif percentage>50: print('The Grade is D') else: print('Fail') Enter obtain marks = 65 Enter total marks = 100 The percentage is = 65.0 The Grade is C
  • 18. While loop • While loop is a conditional loop • The code run until the condition remain True and escape if when condition become False Simply: while loop = for loop + if else
  • 19. write for loop using while loop a=0 while(a<10): print('Karachi',a) a += 1 # a = a + 1 Karachi 0 Karachi 1 Karachi 2 Karachi 3 Karachi 4 Karachi 5 Karachi 6 Karachi 7 Karachi 8 Karachi 9
  • 20. Example While loop The following program take a character from user and show its ASCII number. The program continue to run until user press TAB Key a=0 while(a!=‘t’): a = input("Enter a key, press Tab key to exit = ") print('ASCII of',a,'is ',ord(a)) Enter a key, press Tab key to exit = f ASCII of f is 102 Enter a key, press Tab key to exit = g ASCII of g is 103 Enter a key, press Tab key to exit = ASCII of is 9
  • 21. Infinite Loop Infinite loop run continuous and did not exit. The infinite loop can be made by while loop with condition set to ‘True’ which mean the loop never become false and never exit from while code block. Syntax: while True: ……….execute this code continuously to infinite time Note this is infinite loop which never end to break the infinite loop press Ctrl + c
  • 22. While loop Exercise • Write a program which print your name to infinite time with delay of 1 sec. HAMDARD UNIVERSITY delay(1sec) HAMDARD UNIVERSITY delay(1sec) HAMDARD UNIVERSITY delay(1sec) HAMDARD UNIVERSITY delay(1sec) . . . . . .
  • 23. while loop Exercise Solution import time from time import sleep while(True): print('HAMDARD UNIVERSITY') sleep(1) HAMDARD UNIVERSITY HAMDARD UNIVERSITY HAMDARD UNIVERSITY HAMDARD UNIVERSITY . . . . . .
  • 24. Logical operator in Python Operator Description Example and Logical AND If both the operands are true then condition becomes true. (a and b) is true. or Logical OR If any of the two operands are non-zero then condition becomes true. (a or b) is true. not Logical NOT Used to reverse the logical state of its operand. Not(a and b) is false.
  • 25. For loop – if else example msg = input("Enter a string = ") for i in msg.lower(): if(i == 'a' or i =='e' or i == 'i'or i == 'o'or i == 'u'): print(i , 'is vovel' ) else: print(i , 'is not vovel' ) Enter a string = Hamdard h is not vovel a is vovel m is not vovel d is not vovel a is vovel r is not vovel d is not vovel
  • 26. Comparing Python with C/C++ Programming Parameter Python Language C/C++ Language Programming type Interpreter Compiler Programming Language High level Language Middle level Language Header file import #include Code block Whitespace Indentation Code within curly bracket { code} Single line statement termination Nothing Semicolon ; Control flow (Multi line) statement termination Colon : Nothing Single line comments # comments //comments Multi-line comments ‘’’ Comments lines ‘’’ /* Comments lines */ If error code found Partial run code until find error Did not run until error is present Program length Take fewer lines Take relatively more lines
  • 27. Python Programming code comparison with C programming code Python program C/C++ program Multi-line comments Including library Declaring variable Multi-line statement Code Block Single-line statement Single line comments ‘’’ The Python Language Example code ’’’ Import time a=10 #integer value name = 'karachi' #String for i in range (a): print("Hamdard") print('University') print(name) #program end /* The C/C++ language Example code */ #include <iostream> using namespace std; int main() { int a = 10; char name[10] = "Karachi"; for(int i =0 ; i <a ; i ++ ) { cout<<"Hamdard "; cout<<"University"<<endl; } cout<<name; return 0; } //program end