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

What's hot (20)

03. Operators Expressions and statements
03. Operators Expressions and statements03. Operators Expressions and statements
03. Operators Expressions and statements
Intro C# Book
 
C++ Language
C++ LanguageC++ Language
C++ Language
Syed Zaid Irshad
 
04. Console Input Output
04. Console Input Output 04. Console Input Output
04. Console Input Output
Intro C# Book
 
Chapter 22. Lambda Expressions and LINQ
Chapter 22. Lambda Expressions and LINQChapter 22. Lambda Expressions and LINQ
Chapter 22. Lambda Expressions and LINQ
Intro C# Book
 
USER DEFINE FUNCTIONS IN PYTHON
USER DEFINE FUNCTIONS IN PYTHONUSER DEFINE FUNCTIONS IN PYTHON
USER DEFINE FUNCTIONS IN PYTHON
vikram mahendra
 
INTRODUCTION TO FUNCTIONS IN PYTHON
INTRODUCTION TO FUNCTIONS IN PYTHONINTRODUCTION TO FUNCTIONS IN PYTHON
INTRODUCTION TO FUNCTIONS IN PYTHON
vikram mahendra
 
Introduction to cpp
Introduction to cppIntroduction to cpp
Introduction to cpp
Nilesh Dalvi
 
USER DEFINE FUNCTIONS IN PYTHON[WITH PARAMETERS]
USER DEFINE FUNCTIONS IN PYTHON[WITH PARAMETERS]USER DEFINE FUNCTIONS IN PYTHON[WITH PARAMETERS]
USER DEFINE FUNCTIONS IN PYTHON[WITH PARAMETERS]
vikram mahendra
 
Java Programming Workshop
Java Programming WorkshopJava Programming Workshop
Java Programming Workshop
neosphere
 
13 Strings and Text Processing
13 Strings and Text Processing13 Strings and Text Processing
13 Strings and Text Processing
Intro C# Book
 
Cbse marking scheme 2006 2011
Cbse marking scheme 2006  2011Cbse marking scheme 2006  2011
Cbse marking scheme 2006 2011
Praveen M Jigajinni
 
C++ Programming Language
C++ Programming Language C++ Programming Language
C++ Programming Language
Mohamed Loey
 
GE8151 Problem Solving and Python Programming
GE8151 Problem Solving and Python ProgrammingGE8151 Problem Solving and Python Programming
GE8151 Problem Solving and Python Programming
Muthu Vinayagam
 
Advanced C - Part 2
Advanced C - Part 2Advanced C - Part 2
Advanced C - Part 2
Emertxe Information Technologies Pvt Ltd
 
Intro to c++
Intro to c++Intro to c++
Intro to c++
temkin abdlkader
 
Develop Embedded Software Module-Session 3
Develop Embedded Software Module-Session 3Develop Embedded Software Module-Session 3
Develop Embedded Software Module-Session 3
Naveen Kumar
 
Introduction to c++
Introduction to c++Introduction to c++
Introduction to c++
Himanshu Kaushik
 
Chap 5 c++
Chap 5 c++Chap 5 c++
Chap 5 c++
Venkateswarlu Vuggam
 
OPERATOR IN PYTHON-PART2
OPERATOR IN PYTHON-PART2OPERATOR IN PYTHON-PART2
OPERATOR IN PYTHON-PART2
vikram mahendra
 
Programming Fundamentals Arrays and Strings
Programming Fundamentals   Arrays and Strings Programming Fundamentals   Arrays and Strings
Programming Fundamentals Arrays and Strings
imtiazalijoono
 
03. Operators Expressions and statements
03. Operators Expressions and statements03. Operators Expressions and statements
03. Operators Expressions and statements
Intro C# Book
 
04. Console Input Output
04. Console Input Output 04. Console Input Output
04. Console Input Output
Intro C# Book
 
Chapter 22. Lambda Expressions and LINQ
Chapter 22. Lambda Expressions and LINQChapter 22. Lambda Expressions and LINQ
Chapter 22. Lambda Expressions and LINQ
Intro C# Book
 
USER DEFINE FUNCTIONS IN PYTHON
USER DEFINE FUNCTIONS IN PYTHONUSER DEFINE FUNCTIONS IN PYTHON
USER DEFINE FUNCTIONS IN PYTHON
vikram mahendra
 
INTRODUCTION TO FUNCTIONS IN PYTHON
INTRODUCTION TO FUNCTIONS IN PYTHONINTRODUCTION TO FUNCTIONS IN PYTHON
INTRODUCTION TO FUNCTIONS IN PYTHON
vikram mahendra
 
Introduction to cpp
Introduction to cppIntroduction to cpp
Introduction to cpp
Nilesh Dalvi
 
USER DEFINE FUNCTIONS IN PYTHON[WITH PARAMETERS]
USER DEFINE FUNCTIONS IN PYTHON[WITH PARAMETERS]USER DEFINE FUNCTIONS IN PYTHON[WITH PARAMETERS]
USER DEFINE FUNCTIONS IN PYTHON[WITH PARAMETERS]
vikram mahendra
 
Java Programming Workshop
Java Programming WorkshopJava Programming Workshop
Java Programming Workshop
neosphere
 
13 Strings and Text Processing
13 Strings and Text Processing13 Strings and Text Processing
13 Strings and Text Processing
Intro C# Book
 
C++ Programming Language
C++ Programming Language C++ Programming Language
C++ Programming Language
Mohamed Loey
 
GE8151 Problem Solving and Python Programming
GE8151 Problem Solving and Python ProgrammingGE8151 Problem Solving and Python Programming
GE8151 Problem Solving and Python Programming
Muthu Vinayagam
 
Develop Embedded Software Module-Session 3
Develop Embedded Software Module-Session 3Develop Embedded Software Module-Session 3
Develop Embedded Software Module-Session 3
Naveen Kumar
 
OPERATOR IN PYTHON-PART2
OPERATOR IN PYTHON-PART2OPERATOR IN PYTHON-PART2
OPERATOR IN PYTHON-PART2
vikram mahendra
 
Programming Fundamentals Arrays and Strings
Programming Fundamentals   Arrays and Strings Programming Fundamentals   Arrays and Strings
Programming Fundamentals Arrays and Strings
imtiazalijoono
 

Similar to Python programming workshop session 2 (20)

1. control structures in the python.pptx
1. control structures in the python.pptx1. control structures in the python.pptx
1. control structures in the python.pptx
DURAIMURUGANM2
 
2 Python Basics II meeting 2 tunghai university pdf
2 Python Basics II meeting 2 tunghai university pdf2 Python Basics II meeting 2 tunghai university pdf
2 Python Basics II meeting 2 tunghai university pdf
Anggi Andriyadi
 
Python Decision Making And Loops.pdf
Python Decision Making And Loops.pdfPython Decision Making And Loops.pdf
Python Decision Making And Loops.pdf
NehaSpillai1
 
Control structures pyhton
Control structures  pyhtonControl structures  pyhton
Control structures pyhton
Prakash Jayaraman
 
Python if_else_loop_Control_Flow_Statement
Python if_else_loop_Control_Flow_StatementPython if_else_loop_Control_Flow_Statement
Python if_else_loop_Control_Flow_Statement
AbhishekGupta692777
 
Chapter 9 Conditional and Iterative Statements.pptx
Chapter 9 Conditional and Iterative Statements.pptxChapter 9 Conditional and Iterative Statements.pptx
Chapter 9 Conditional and Iterative Statements.pptx
XhelalSpahiu
 
Loops in Python
Loops in PythonLoops in Python
Loops in Python
Arockia Abins
 
011 LOOP.pdf cs project important chapters
011 LOOP.pdf cs project important chapters011 LOOP.pdf cs project important chapters
011 LOOP.pdf cs project important chapters
AryanRajak
 
Week 4.pptx computational thinking and programming
Week 4.pptx computational thinking and programmingWeek 4.pptx computational thinking and programming
Week 4.pptx computational thinking and programming
cricketfundavlogs
 
Slide 6_Control Structures.pdf
Slide 6_Control Structures.pdfSlide 6_Control Structures.pdf
Slide 6_Control Structures.pdf
NuthalapatiSasidhar
 
Python Programming
Python Programming Python Programming
Python Programming
Sreedhar Chowdam
 
Python.pptx
Python.pptxPython.pptx
Python.pptx
AKANSHAMITTAL2K21AFI
 
ForLoops.pptx
ForLoops.pptxForLoops.pptx
ForLoops.pptx
RabiyaZhexembayeva
 
While-For-loop in python used in college
While-For-loop in python used in collegeWhile-For-loop in python used in college
While-For-loop in python used in college
ssuser7a7cd61
 
While_for_loop presententationin first year students
While_for_loop presententationin first year studentsWhile_for_loop presententationin first year students
While_for_loop presententationin first year students
SIHIGOPAL
 
python ppt.pptx
python ppt.pptxpython ppt.pptx
python ppt.pptx
ssuserd10678
 
Python_Module_2.pdf
Python_Module_2.pdfPython_Module_2.pdf
Python_Module_2.pdf
R.K.College of engg & Tech
 
Fifth session
Fifth sessionFifth session
Fifth session
AliMohammad155
 
Looping Statements and Control Statements in Python
Looping Statements and Control Statements in PythonLooping Statements and Control Statements in Python
Looping Statements and Control Statements in Python
PriyankaC44
 
1. control structures in the python.pptx
1. control structures in the python.pptx1. control structures in the python.pptx
1. control structures in the python.pptx
DURAIMURUGANM2
 
2 Python Basics II meeting 2 tunghai university pdf
2 Python Basics II meeting 2 tunghai university pdf2 Python Basics II meeting 2 tunghai university pdf
2 Python Basics II meeting 2 tunghai university pdf
Anggi Andriyadi
 
Python Decision Making And Loops.pdf
Python Decision Making And Loops.pdfPython Decision Making And Loops.pdf
Python Decision Making And Loops.pdf
NehaSpillai1
 
Python if_else_loop_Control_Flow_Statement
Python if_else_loop_Control_Flow_StatementPython if_else_loop_Control_Flow_Statement
Python if_else_loop_Control_Flow_Statement
AbhishekGupta692777
 
Chapter 9 Conditional and Iterative Statements.pptx
Chapter 9 Conditional and Iterative Statements.pptxChapter 9 Conditional and Iterative Statements.pptx
Chapter 9 Conditional and Iterative Statements.pptx
XhelalSpahiu
 
011 LOOP.pdf cs project important chapters
011 LOOP.pdf cs project important chapters011 LOOP.pdf cs project important chapters
011 LOOP.pdf cs project important chapters
AryanRajak
 
Week 4.pptx computational thinking and programming
Week 4.pptx computational thinking and programmingWeek 4.pptx computational thinking and programming
Week 4.pptx computational thinking and programming
cricketfundavlogs
 
While-For-loop in python used in college
While-For-loop in python used in collegeWhile-For-loop in python used in college
While-For-loop in python used in college
ssuser7a7cd61
 
While_for_loop presententationin first year students
While_for_loop presententationin first year studentsWhile_for_loop presententationin first year students
While_for_loop presententationin first year students
SIHIGOPAL
 
Looping Statements and Control Statements in Python
Looping Statements and Control Statements in PythonLooping Statements and Control Statements in Python
Looping Statements and Control Statements in Python
PriyankaC44
 
Ad

Recently uploaded (20)

Artificial intelligence Presented by JM.
Artificial intelligence Presented by JM.Artificial intelligence Presented by JM.
Artificial intelligence Presented by JM.
jmansha170
 
FEBA Sofia Univercity final diplian v3 GSDG 5.2025.pdf
FEBA Sofia Univercity final diplian v3 GSDG 5.2025.pdfFEBA Sofia Univercity final diplian v3 GSDG 5.2025.pdf
FEBA Sofia Univercity final diplian v3 GSDG 5.2025.pdf
ChristinaFortunova
 
What are the benefits that dance brings?
What are the benefits that dance brings?What are the benefits that dance brings?
What are the benefits that dance brings?
memi27
 
Module 4 Presentation - Enhancing Competencies and Engagement Strategies in Y...
Module 4 Presentation - Enhancing Competencies and Engagement Strategies in Y...Module 4 Presentation - Enhancing Competencies and Engagement Strategies in Y...
Module 4 Presentation - Enhancing Competencies and Engagement Strategies in Y...
GeorgeDiamandis11
 
Respiratory System , Urinary System
Respiratory  System , Urinary SystemRespiratory  System , Urinary System
Respiratory System , Urinary System
RushiMandali
 
Energy Balances Of Oecd Countries 2011 Iea Statistics 1st Edition Oecd
Energy Balances Of Oecd Countries 2011 Iea Statistics 1st Edition OecdEnergy Balances Of Oecd Countries 2011 Iea Statistics 1st Edition Oecd
Energy Balances Of Oecd Countries 2011 Iea Statistics 1st Edition Oecd
razelitouali
 
Webcrawler_Mule_AIChain_MuleSoft_Meetup_Hyderabad
Webcrawler_Mule_AIChain_MuleSoft_Meetup_HyderabadWebcrawler_Mule_AIChain_MuleSoft_Meetup_Hyderabad
Webcrawler_Mule_AIChain_MuleSoft_Meetup_Hyderabad
Veera Pallapu
 
MATERI PPT TOPIK 4 LANDASAN FILOSOFIS PENDIDIKAN
MATERI PPT TOPIK 4 LANDASAN FILOSOFIS PENDIDIKANMATERI PPT TOPIK 4 LANDASAN FILOSOFIS PENDIDIKAN
MATERI PPT TOPIK 4 LANDASAN FILOSOFIS PENDIDIKAN
aditya23173
 
Analysis of Quantitative Data Parametric and non-parametric tests.pptx
Analysis of Quantitative Data Parametric and non-parametric tests.pptxAnalysis of Quantitative Data Parametric and non-parametric tests.pptx
Analysis of Quantitative Data Parametric and non-parametric tests.pptx
Shrutidhara2
 
Optimization technique in pharmaceutical product development.pptx
Optimization technique in pharmaceutical product development.pptxOptimization technique in pharmaceutical product development.pptx
Optimization technique in pharmaceutical product development.pptx
UrmiPrajapati3
 
Exploring Ocean Floor Features for Middle School
Exploring Ocean Floor Features for Middle SchoolExploring Ocean Floor Features for Middle School
Exploring Ocean Floor Features for Middle School
Marie
 
LDMMIA Free Reiki Yoga S9 Grad Level Intuition II
LDMMIA Free Reiki Yoga S9 Grad Level Intuition IILDMMIA Free Reiki Yoga S9 Grad Level Intuition II
LDMMIA Free Reiki Yoga S9 Grad Level Intuition II
LDM & Mia eStudios
 
Different pricelists for different shops in odoo Point of Sale in Odoo 17
Different pricelists for different shops in odoo Point of Sale in Odoo 17Different pricelists for different shops in odoo Point of Sale in Odoo 17
Different pricelists for different shops in odoo Point of Sale in Odoo 17
Celine George
 
How to Manage Upselling of Subscriptions in Odoo 18
How to Manage Upselling of Subscriptions in Odoo 18How to Manage Upselling of Subscriptions in Odoo 18
How to Manage Upselling of Subscriptions in Odoo 18
Celine George
 
Adam Grant: Transforming Work Culture Through Organizational Psychology
Adam Grant: Transforming Work Culture Through Organizational PsychologyAdam Grant: Transforming Work Culture Through Organizational Psychology
Adam Grant: Transforming Work Culture Through Organizational Psychology
Prachi Shah
 
Strengthened Senior High School - Landas Tool Kit.pptx
Strengthened Senior High School - Landas Tool Kit.pptxStrengthened Senior High School - Landas Tool Kit.pptx
Strengthened Senior High School - Landas Tool Kit.pptx
SteffMusniQuiballo
 
Gibson "Secrets to Changing Behaviour in Scholarly Communication: A 2025 NISO...
Gibson "Secrets to Changing Behaviour in Scholarly Communication: A 2025 NISO...Gibson "Secrets to Changing Behaviour in Scholarly Communication: A 2025 NISO...
Gibson "Secrets to Changing Behaviour in Scholarly Communication: A 2025 NISO...
National Information Standards Organization (NISO)
 
Diptera: The Two-Winged Wonders, The Fly Squad: Order Diptera.pptx
Diptera: The Two-Winged Wonders, The Fly Squad: Order Diptera.pptxDiptera: The Two-Winged Wonders, The Fly Squad: Order Diptera.pptx
Diptera: The Two-Winged Wonders, The Fly Squad: Order Diptera.pptx
Arshad Shaikh
 
THERAPEUTIC COMMUNICATION included definition, characteristics, nurse patient...
THERAPEUTIC COMMUNICATION included definition, characteristics, nurse patient...THERAPEUTIC COMMUNICATION included definition, characteristics, nurse patient...
THERAPEUTIC COMMUNICATION included definition, characteristics, nurse patient...
parmarjuli1412
 
How to Manage Maintenance Request in Odoo 18
How to Manage Maintenance Request in Odoo 18How to Manage Maintenance Request in Odoo 18
How to Manage Maintenance Request in Odoo 18
Celine George
 
Artificial intelligence Presented by JM.
Artificial intelligence Presented by JM.Artificial intelligence Presented by JM.
Artificial intelligence Presented by JM.
jmansha170
 
FEBA Sofia Univercity final diplian v3 GSDG 5.2025.pdf
FEBA Sofia Univercity final diplian v3 GSDG 5.2025.pdfFEBA Sofia Univercity final diplian v3 GSDG 5.2025.pdf
FEBA Sofia Univercity final diplian v3 GSDG 5.2025.pdf
ChristinaFortunova
 
What are the benefits that dance brings?
What are the benefits that dance brings?What are the benefits that dance brings?
What are the benefits that dance brings?
memi27
 
Module 4 Presentation - Enhancing Competencies and Engagement Strategies in Y...
Module 4 Presentation - Enhancing Competencies and Engagement Strategies in Y...Module 4 Presentation - Enhancing Competencies and Engagement Strategies in Y...
Module 4 Presentation - Enhancing Competencies and Engagement Strategies in Y...
GeorgeDiamandis11
 
Respiratory System , Urinary System
Respiratory  System , Urinary SystemRespiratory  System , Urinary System
Respiratory System , Urinary System
RushiMandali
 
Energy Balances Of Oecd Countries 2011 Iea Statistics 1st Edition Oecd
Energy Balances Of Oecd Countries 2011 Iea Statistics 1st Edition OecdEnergy Balances Of Oecd Countries 2011 Iea Statistics 1st Edition Oecd
Energy Balances Of Oecd Countries 2011 Iea Statistics 1st Edition Oecd
razelitouali
 
Webcrawler_Mule_AIChain_MuleSoft_Meetup_Hyderabad
Webcrawler_Mule_AIChain_MuleSoft_Meetup_HyderabadWebcrawler_Mule_AIChain_MuleSoft_Meetup_Hyderabad
Webcrawler_Mule_AIChain_MuleSoft_Meetup_Hyderabad
Veera Pallapu
 
MATERI PPT TOPIK 4 LANDASAN FILOSOFIS PENDIDIKAN
MATERI PPT TOPIK 4 LANDASAN FILOSOFIS PENDIDIKANMATERI PPT TOPIK 4 LANDASAN FILOSOFIS PENDIDIKAN
MATERI PPT TOPIK 4 LANDASAN FILOSOFIS PENDIDIKAN
aditya23173
 
Analysis of Quantitative Data Parametric and non-parametric tests.pptx
Analysis of Quantitative Data Parametric and non-parametric tests.pptxAnalysis of Quantitative Data Parametric and non-parametric tests.pptx
Analysis of Quantitative Data Parametric and non-parametric tests.pptx
Shrutidhara2
 
Optimization technique in pharmaceutical product development.pptx
Optimization technique in pharmaceutical product development.pptxOptimization technique in pharmaceutical product development.pptx
Optimization technique in pharmaceutical product development.pptx
UrmiPrajapati3
 
Exploring Ocean Floor Features for Middle School
Exploring Ocean Floor Features for Middle SchoolExploring Ocean Floor Features for Middle School
Exploring Ocean Floor Features for Middle School
Marie
 
LDMMIA Free Reiki Yoga S9 Grad Level Intuition II
LDMMIA Free Reiki Yoga S9 Grad Level Intuition IILDMMIA Free Reiki Yoga S9 Grad Level Intuition II
LDMMIA Free Reiki Yoga S9 Grad Level Intuition II
LDM & Mia eStudios
 
Different pricelists for different shops in odoo Point of Sale in Odoo 17
Different pricelists for different shops in odoo Point of Sale in Odoo 17Different pricelists for different shops in odoo Point of Sale in Odoo 17
Different pricelists for different shops in odoo Point of Sale in Odoo 17
Celine George
 
How to Manage Upselling of Subscriptions in Odoo 18
How to Manage Upselling of Subscriptions in Odoo 18How to Manage Upselling of Subscriptions in Odoo 18
How to Manage Upselling of Subscriptions in Odoo 18
Celine George
 
Adam Grant: Transforming Work Culture Through Organizational Psychology
Adam Grant: Transforming Work Culture Through Organizational PsychologyAdam Grant: Transforming Work Culture Through Organizational Psychology
Adam Grant: Transforming Work Culture Through Organizational Psychology
Prachi Shah
 
Strengthened Senior High School - Landas Tool Kit.pptx
Strengthened Senior High School - Landas Tool Kit.pptxStrengthened Senior High School - Landas Tool Kit.pptx
Strengthened Senior High School - Landas Tool Kit.pptx
SteffMusniQuiballo
 
Diptera: The Two-Winged Wonders, The Fly Squad: Order Diptera.pptx
Diptera: The Two-Winged Wonders, The Fly Squad: Order Diptera.pptxDiptera: The Two-Winged Wonders, The Fly Squad: Order Diptera.pptx
Diptera: The Two-Winged Wonders, The Fly Squad: Order Diptera.pptx
Arshad Shaikh
 
THERAPEUTIC COMMUNICATION included definition, characteristics, nurse patient...
THERAPEUTIC COMMUNICATION included definition, characteristics, nurse patient...THERAPEUTIC COMMUNICATION included definition, characteristics, nurse patient...
THERAPEUTIC COMMUNICATION included definition, characteristics, nurse patient...
parmarjuli1412
 
How to Manage Maintenance Request in Odoo 18
How to Manage Maintenance Request in Odoo 18How to Manage Maintenance Request in Odoo 18
How to Manage Maintenance Request in Odoo 18
Celine George
 
Ad

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