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

Python programming workshop session 2

  • 1.
    Faculty of EngineeringScience & 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 ProgrammingLanguage 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 useswhitespace 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 Thebuilt-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 loopis 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 inrange(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 withinteger 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 withdifferent 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 Exercise1 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 Exercise1 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 Exercise2 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 mathimport 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 useif 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 conditionexercise 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 conditionexercise 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 • Whileloop 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 loopusing 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 Thefollowing 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 looprun 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 ExerciseSolution import time from time import sleep while(True): print('HAMDARD UNIVERSITY') sleep(1) HAMDARD UNIVERSITY HAMDARD UNIVERSITY HAMDARD UNIVERSITY HAMDARD UNIVERSITY . . . . . .
  • 24.
    Logical operator inPython 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 withC/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 codecomparison 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
  • 28.