SlideShare a Scribd company logo
Introduction to
Programming
Languages
Welcome Python Programming 1
Programming language
•A programming language is a system of
notation for writing computer programs
• Programming is used to automate, maintain, assemble,
measure, and interpret the processing of the data and
information
Welcome Python Programming 2
Programs
•Programs are written in programming languages
•Pieces of the same program can be written in
different programming languages
•A set of rules and symbols used to construct a
computer program
•A language used to interact with the computer
A Quick
Tour of
Python
4
The Programming Cycle for
Python
Jan-24 Programming 5
Write/Edit
Run
with some input
OK?
YES
More
Inputs?
YES
NO
NO
Jan-24 Programming 6
Jan-24 Programming 7
Filename, preferred extension is py
User Program
Python Shell is Interactive
Jan-24 Programming 8
IN[1]:
IN[2]:
IN[4]:
IN[3]:
Python Shell Prompt
User Commands
(Statements)
Outputs
( )
Elements of Python
• A Python program is a sequence of definitions and commands
(statements)
• Commands manipulate objects
• Each object is associated with a Type
• Type:
• A set of values
• A set of operations on these values
• Expressions: An operation (combination of objects and operators)
Jan-24 Programming 9
Types in Python
• int
• Bounded integers, e.g. 732 or -5
• float
• Real numbers, e.g. 3.14 or 2.0
• long
• Long integers with unlimited precision
• str
• Strings, e.g. ā€˜hello’ or ā€˜C’
• bool
• Boolean, true or false
Jan-24 Programming 10
Example of Types
Jan-24 Programming 11
Type Conversion (Type Cast)
• Conversion of value of one type to other
• We are used to int ↔ float conversion in Math
• Integer 3 is treated as float 3.0 when a real number is expected
• Float 3.6 is truncated as 3, or rounded off as 4 for integer contexts
• Type names are used as type converter functions
Jan-24 Programming 12
Type Conversion Examples
Jan-24 Programming 13
Note that float to int conversion
is truncation, not rounding off
Type Conversion and Input
Jan-24 Programming 14
Operators
• Arithmetic
• Comparison
Jan-24 Programming 15
+ - * // / % **
== != > < >= <=
Variables
• A name associated with an object
• Assignment used for binding
m = 64;
c = ā€˜Acads’;
f = 3.1416;
• Variables can change their bindings
f = 2.7183;
Jan-24 Programming 16
64
Acads
3.1416
2.7183
m
c
f
Assignment Statement
• A simple assignment statement
Variable = Expression;
• Computes the value (object) of the expression on the right hand side
expression (RHS)
• Associates the name (variable) on the left hand side (LHS) with the
RHS value
• = is known as the assignment operator.
Jan-24 Programming 17
Programming using Python
Operators and Expressions
1/22/2024 Programming 18
Binary Operations
1/22/2024 Programming 19
Op Meaning Example Remarks
+ Addition 9+2 is 11
9.1+2.0 is 11.1
- Subtraction 9-2 is 7
9.1-2.0 is 7.1
* Multiplication 9*2 is 18
9.1*2.0 is 18.2
/ Division 9/2 is 4.50 In Python3
9.1/2.0 is 4.55 Real div.
// Integer Division 9//2 is 4
% Remainder 9%2 is 1
The // operator
• Also referred to as ā€œinteger divisionā€
• Result is a whole integer (floor of real division)
• But the type need not be int
• the integral part of the real division
• rounded towards minus infinity (āˆ’āˆž)
• Examples
1/22/2024 Programming 20
9//4 is 2 (-1)//2 is -1 (-1)//(-2) is 0
1//2 is 0 1//(-2) is -1 9//4.5 is 2.0
The % operator
•The remainder operator % returns the
remainder of the result of dividing its
first operand by its second.
1/22/2024 Programming 21
9%4 is 1 (-1)%2 is 1 (-1)//(-2) is 0
9%4.5 is 0.0 1%(-2) is 1 1%0.6 is 0.4
Ideally: x == (x//y)*y + x %y
Conditional Statements
•In daily routine
•If it is very hot, I will skip exercise.
•If there is a quiz tomorrow, I will
first study and then sleep.
Otherwise I will sleep now.
•If I have to buy coffee, I will
go left. Else I will go
straight.
Jan-24 Programming 22
if-else statement
• Compare two integers and print the min.
Jan-24 Programming 23
1. Check if x is less
than y.
2. If so, print x
3. Otherwise, print y.
if x < y:
print (x)
else:
print (y)
print (ā€˜is the minimum’)
x,y = 6,10
if x < y:
print (x)
else:
print (y)
print (ā€˜is the min’)
x y
6 10
Run the program
Output
6
Indentation
•Indentation is important in Python
• grouping of statement (block of statements)
• no explicit brackets, e.g. { }, to group statements
Jan-24 Programming 24
if statement (no else!)
• General form of the if statement
• Execution of if statement
• First the expression is evaluated.
• If it evaluates to a true value, then S1 is executed and then control moves to
the S2.
• If expression evaluates to false, then control moves to the S2 directly.
Jan-24 Programming 25
if boolean-expr :
S1
S2
S1
S2
if-else statement
• General form of the if-else statement
• Execution of if-else statement
• First the expression is evaluated.
• If it evaluates to a true value, then S1 is executed and
then control moves to S3.
• If expression evaluates to false, then S2 is executed and
then control moves to S3.
• S1/S2 can be blocks of statements!
Jan-24 Programming 26
if boolean-expr :
S1
else:
S2
S3
S2
S1
S3
Nested if, if-else
Jan-24 Programming 27
if a <= b:
if a <= c:
…
else:
…
else:
if b <= c) :
…
else:
…
Elif
• A special kind of nesting is the chain of if-else-if-else-… statements
• Can be written elegantly using if-elif-..-else
Jan-24 Programming 28
if cond1:
s1
elif cond2:
s2
elif cond3:
s3
elif …
else
last-block-of-stmt
if cond1:
s1
else:
if cond2:
s2
else:
if cond3:
s3
else:
…
Summary of if, if-else
•if-else, nested if's, elif.
•Multiple ways to solve a problem
•issues of readability,
maintainability
•and efficiency
Jan-24 Programming 29
Class Quiz
• What is the value of expression:
a) Run time crash/error
b) I don’t know / I don’t care
c) False
d) True
Jan-24 Programming 30
(5<2) and (3/0 > 1)
The correct answer is
False
Programming using Python
Loops
Jan-24 Python Programming 31
Program…
Jan-24 Python Programming 32
n = int(input('Enter a number: '))
print (n, 'X', 1, '=', n*1)
print (n, 'X', 2, '=', n*2)
print (n, 'X', 3, '=', n*3)
print (n, 'X', 4, '=', n*4)
print (n, 'X', 5, '=', n*5)
print (n, 'X', 6, '=', n*6)
….
Too much
repetition!
Can I avoid
it?
While Statement
1. Evaluate expression
2. If TRUE then
a) execute statement1
b) goto step 1.
3. If FALSE then execute statement2.
Jan-24 Python Programming 33
while (expression):
S1
S2
FALSE
TRUE
S1
expression
S2
For loop in Python
• General form
Jan-24 Python Programming 34
for variable in sequence:
stmt
For Loop
Jan-24 Python Programming 35
The range() Function: To loop through a set
of code a specified number of times, we
can use the range() function
ļ‚¢ for x in range(6):
print(x)
For Loop
Jan-24 Python Programming 36
The range() function defaults to
0 as a starting value, it is
possible to specify the starting
value by adding a parameter:
range(2, 6), which means values
from 2 to 6 (but not including
6):
ļ‚¢ for x in range(2, 6):
print(x)
# print all odd numbers < 10
i = 1
while i < 10:
print(i)
i += 2
Quiz
Jan-24 Python Programming 37
Welcome Python Programming 38

More Related Content

PPTX
Python details for beginners and for students
ssuser083eed1
Ā 
PPTX
python ppt
EmmanuelMMathew
Ā 
PPTX
Introduction to vrevr rv4 rvr r r r u r a Python.pptx
sahilurrahemankhan
Ā 
PPTX
ESCM303 Introduction to Python Programming.pptx
AvijitChaudhuri3
Ā 
PPTX
Python4HPC.pptx
Sonam Mittal
Ā 
PPTX
Python4HPC.pptx
priyam737974
Ā 
PPTX
Python4HPC.pptx
Prashanth Reddy
Ā 
PPTX
Python.pptx
AKANSHAMITTAL2K21AFI
Ā 
Python details for beginners and for students
ssuser083eed1
Ā 
python ppt
EmmanuelMMathew
Ā 
Introduction to vrevr rv4 rvr r r r u r a Python.pptx
sahilurrahemankhan
Ā 
ESCM303 Introduction to Python Programming.pptx
AvijitChaudhuri3
Ā 
Python4HPC.pptx
Sonam Mittal
Ā 
Python4HPC.pptx
priyam737974
Ā 
Python4HPC.pptx
Prashanth Reddy
Ā 
Python.pptx
AKANSHAMITTAL2K21AFI
Ā 

Similar to Updatedpython.pptxUpdatedpython.pptxUpdatedpython.pptx (20)

PPTX
Python4HPC.pptx
TusharAlande
Ā 
PPTX
Introduction to Python Part-1
Devashish Kumar
Ā 
PPTX
made it easy: python quick reference for beginners
SumanMadan4
Ā 
PPTX
lecture 2.pptx
Anonymous9etQKwW
Ā 
PPTX
ā€œPythonā€ or ā€œCPythonā€ is written in C/C+
Mukeshpanigrahy1
Ā 
PPTX
Python programing
hamzagame
Ā 
PDF
python notes.pdf
RohitSindhu10
Ā 
PDF
pythonQuick.pdf
PhanMinhLinhAnxM0190
Ā 
PDF
python 34šŸ’­.pdf
AkashdeepBhattacharj1
Ā 
PPTX
1664611760basics-of-python-for begainer1 (3).pptx
krsonupandey92
Ā 
PPT
python operators.ppt
ErnieAcuna
Ā 
PDF
Introduction To Programming with Python
Sushant Mane
Ā 
PPTX
Bikalpa_Thapa_Python_Programming_(Basics).pptx
Bikalpa Thapa
Ā 
PDF
cos 102 - getting into programming with python.pdf
GeraldineAdukeh
Ā 
PPTX
Presgfdjfwwwwwwwwwwqwqeeqentation11.pptx
aarohanpics
Ā 
PPTX
python BY ME-2021python anylssis(1).pptx
rekhaaarohan
Ā 
PPT
introduction to python in english presentation file
RujanTimsina1
Ā 
PPTX
PYTHON PROGRAMMING
indupps
Ā 
PPTX
Chapter 2-Python and control flow statement.pptx
atharvdeshpande20
Ā 
PPTX
Unit - 2 CAP.pptx
malekaanjum1
Ā 
Python4HPC.pptx
TusharAlande
Ā 
Introduction to Python Part-1
Devashish Kumar
Ā 
made it easy: python quick reference for beginners
SumanMadan4
Ā 
lecture 2.pptx
Anonymous9etQKwW
Ā 
ā€œPythonā€ or ā€œCPythonā€ is written in C/C+
Mukeshpanigrahy1
Ā 
Python programing
hamzagame
Ā 
python notes.pdf
RohitSindhu10
Ā 
pythonQuick.pdf
PhanMinhLinhAnxM0190
Ā 
python 34šŸ’­.pdf
AkashdeepBhattacharj1
Ā 
1664611760basics-of-python-for begainer1 (3).pptx
krsonupandey92
Ā 
python operators.ppt
ErnieAcuna
Ā 
Introduction To Programming with Python
Sushant Mane
Ā 
Bikalpa_Thapa_Python_Programming_(Basics).pptx
Bikalpa Thapa
Ā 
cos 102 - getting into programming with python.pdf
GeraldineAdukeh
Ā 
Presgfdjfwwwwwwwwwwqwqeeqentation11.pptx
aarohanpics
Ā 
python BY ME-2021python anylssis(1).pptx
rekhaaarohan
Ā 
introduction to python in english presentation file
RujanTimsina1
Ā 
PYTHON PROGRAMMING
indupps
Ā 
Chapter 2-Python and control flow statement.pptx
atharvdeshpande20
Ā 
Unit - 2 CAP.pptx
malekaanjum1
Ā 

More from ZahouAmel1 (18)

PPTX
2- lec_2.pptxDesigning with Type, SpacingDesigning with Type, SpacingDesignin...
ZahouAmel1
Ā 
PPTX
1-Lect_1.pptxLecture 5 array in PHP.pptx
ZahouAmel1
Ā 
PPTX
Lecture 8 PHP and MYSQL part 2.ppType Classificationtx
ZahouAmel1
Ā 
PPTX
Lecture 9 CSS part 1.pptxType Classification
ZahouAmel1
Ā 
PPTX
Lecture 5 array in PHP.pptxLecture 10 CSS part 2.pptxvvvvvvvvvvvvvv
ZahouAmel1
Ā 
PPTX
Lecture 2 HTML part 1.pptxLecture 10 CSS part 2.pptxvvvvvvvvvvvvvv
ZahouAmel1
Ā 
PPTX
Lecture 10 CSS part 2.pptxvvvvvvvvvvvvvv
ZahouAmel1
Ā 
PPTX
Lec 1 Introduction to Computer and Information Technology #1.pptx
ZahouAmel1
Ā 
PPTX
DB-Lec1.pptxUpdatedpython.pptxUpdatedpython.pptx
ZahouAmel1
Ā 
PPTX
DB- lec2.pptxUpdatedpython.pptxUpdatedpy
ZahouAmel1
Ā 
PPTX
4-Lect_4-2.pptx4-Lect_4-2.pptx4-Lect_4-2.pptx
ZahouAmel1
Ā 
PPTX
5-LEC- 5.pptxTransport Layer. Transport Layer Protocols
ZahouAmel1
Ā 
PPTX
6-LEC- 6.pptx Network Layer. Addressing Subnetting Mask (default and subnet) ...
ZahouAmel1
Ā 
PPTX
7-Lect_7 .pptxNetwork Layer. Addressing Subnetting Mask (default and subnet) ...
ZahouAmel1
Ā 
PPTX
7-Lect_7 .pptxNetwork LayerNetwork Layer
ZahouAmel1
Ā 
PPTX
8-Lect_8 Addressing the Network.tcp.pptx
ZahouAmel1
Ā 
PPTX
9-Lect_9-1.pptx9-Lect_9-1.pptx9-Lect_9-1.pptx
ZahouAmel1
Ā 
PPTX
9-Lect_9-2.pptx DataLink Layer DataLink Layer
ZahouAmel1
Ā 
2- lec_2.pptxDesigning with Type, SpacingDesigning with Type, SpacingDesignin...
ZahouAmel1
Ā 
1-Lect_1.pptxLecture 5 array in PHP.pptx
ZahouAmel1
Ā 
Lecture 8 PHP and MYSQL part 2.ppType Classificationtx
ZahouAmel1
Ā 
Lecture 9 CSS part 1.pptxType Classification
ZahouAmel1
Ā 
Lecture 5 array in PHP.pptxLecture 10 CSS part 2.pptxvvvvvvvvvvvvvv
ZahouAmel1
Ā 
Lecture 2 HTML part 1.pptxLecture 10 CSS part 2.pptxvvvvvvvvvvvvvv
ZahouAmel1
Ā 
Lecture 10 CSS part 2.pptxvvvvvvvvvvvvvv
ZahouAmel1
Ā 
Lec 1 Introduction to Computer and Information Technology #1.pptx
ZahouAmel1
Ā 
DB-Lec1.pptxUpdatedpython.pptxUpdatedpython.pptx
ZahouAmel1
Ā 
DB- lec2.pptxUpdatedpython.pptxUpdatedpy
ZahouAmel1
Ā 
4-Lect_4-2.pptx4-Lect_4-2.pptx4-Lect_4-2.pptx
ZahouAmel1
Ā 
5-LEC- 5.pptxTransport Layer. Transport Layer Protocols
ZahouAmel1
Ā 
6-LEC- 6.pptx Network Layer. Addressing Subnetting Mask (default and subnet) ...
ZahouAmel1
Ā 
7-Lect_7 .pptxNetwork Layer. Addressing Subnetting Mask (default and subnet) ...
ZahouAmel1
Ā 
7-Lect_7 .pptxNetwork LayerNetwork Layer
ZahouAmel1
Ā 
8-Lect_8 Addressing the Network.tcp.pptx
ZahouAmel1
Ā 
9-Lect_9-1.pptx9-Lect_9-1.pptx9-Lect_9-1.pptx
ZahouAmel1
Ā 
9-Lect_9-2.pptx DataLink Layer DataLink Layer
ZahouAmel1
Ā 

Recently uploaded (20)

PDF
Test Bank, Solutions for Java How to Program, An Objects-Natural Approach, 12...
famaw19526
Ā 
PPTX
Smart Infrastructure and Automation through IoT Sensors
Rejig Digital
Ā 
PDF
DevOps & Developer Experience Summer BBQ
AUGNYC
Ā 
PDF
Security features in Dell, HP, and Lenovo PC systems: A research-based compar...
Principled Technologies
Ā 
PDF
CIFDAQ'S Market Insight: BTC to ETH money in motion
CIFDAQ
Ā 
PDF
Software Development Methodologies in 2025
KodekX
Ā 
DOCX
Top AI API Alternatives to OpenAI: A Side-by-Side Breakdown
vilush
Ā 
PDF
Cloud-Migration-Best-Practices-A-Practical-Guide-to-AWS-Azure-and-Google-Clou...
Artjoker Software Development Company
Ā 
PDF
AI Unleashed - Shaping the Future -Starting Today - AIOUG Yatra 2025 - For Co...
Sandesh Rao
Ā 
PPTX
ChatGPT's Deck on The Enduring Legacy of Fax Machines
Greg Swan
Ā 
PDF
The Evolution of KM Roles (Presented at Knowledge Summit Dublin 2025)
Enterprise Knowledge
Ā 
PDF
Event Presentation Google Cloud Next Extended 2025
minhtrietgect
Ā 
PDF
How Onsite IT Support Drives Business Efficiency, Security, and Growth.pdf
Captain IT
Ā 
PDF
Shreyas_Phanse_Resume: Experienced Backend Engineer | Java • Spring Boot • Ka...
SHREYAS PHANSE
Ā 
PDF
Using Anchore and DefectDojo to Stand Up Your DevSecOps Function
Anchore
Ā 
PDF
Google’s NotebookLM Unveils Video Overviews
SOFTTECHHUB
Ā 
PPTX
How to Build a Scalable Micro-Investing Platform in 2025 - A Founder’s Guide ...
Third Rock Techkno
Ā 
PPTX
PA Analog/Digital System: The Backbone of Modern Surveillance and Communication
AVTRON Technologies LLC
Ā 
PDF
How-Cloud-Computing-Impacts-Businesses-in-2025-and-Beyond.pdf
Artjoker Software Development Company
Ā 
PDF
A Day in the Life of Location Data - Turning Where into How.pdf
Precisely
Ā 
Test Bank, Solutions for Java How to Program, An Objects-Natural Approach, 12...
famaw19526
Ā 
Smart Infrastructure and Automation through IoT Sensors
Rejig Digital
Ā 
DevOps & Developer Experience Summer BBQ
AUGNYC
Ā 
Security features in Dell, HP, and Lenovo PC systems: A research-based compar...
Principled Technologies
Ā 
CIFDAQ'S Market Insight: BTC to ETH money in motion
CIFDAQ
Ā 
Software Development Methodologies in 2025
KodekX
Ā 
Top AI API Alternatives to OpenAI: A Side-by-Side Breakdown
vilush
Ā 
Cloud-Migration-Best-Practices-A-Practical-Guide-to-AWS-Azure-and-Google-Clou...
Artjoker Software Development Company
Ā 
AI Unleashed - Shaping the Future -Starting Today - AIOUG Yatra 2025 - For Co...
Sandesh Rao
Ā 
ChatGPT's Deck on The Enduring Legacy of Fax Machines
Greg Swan
Ā 
The Evolution of KM Roles (Presented at Knowledge Summit Dublin 2025)
Enterprise Knowledge
Ā 
Event Presentation Google Cloud Next Extended 2025
minhtrietgect
Ā 
How Onsite IT Support Drives Business Efficiency, Security, and Growth.pdf
Captain IT
Ā 
Shreyas_Phanse_Resume: Experienced Backend Engineer | Java • Spring Boot • Ka...
SHREYAS PHANSE
Ā 
Using Anchore and DefectDojo to Stand Up Your DevSecOps Function
Anchore
Ā 
Google’s NotebookLM Unveils Video Overviews
SOFTTECHHUB
Ā 
How to Build a Scalable Micro-Investing Platform in 2025 - A Founder’s Guide ...
Third Rock Techkno
Ā 
PA Analog/Digital System: The Backbone of Modern Surveillance and Communication
AVTRON Technologies LLC
Ā 
How-Cloud-Computing-Impacts-Businesses-in-2025-and-Beyond.pdf
Artjoker Software Development Company
Ā 
A Day in the Life of Location Data - Turning Where into How.pdf
Precisely
Ā 

Updatedpython.pptxUpdatedpython.pptxUpdatedpython.pptx

  • 2. Programming language •A programming language is a system of notation for writing computer programs • Programming is used to automate, maintain, assemble, measure, and interpret the processing of the data and information Welcome Python Programming 2
  • 3. Programs •Programs are written in programming languages •Pieces of the same program can be written in different programming languages •A set of rules and symbols used to construct a computer program •A language used to interact with the computer
  • 5. The Programming Cycle for Python Jan-24 Programming 5
  • 7. Jan-24 Programming 7 Filename, preferred extension is py User Program
  • 8. Python Shell is Interactive Jan-24 Programming 8 IN[1]: IN[2]: IN[4]: IN[3]: Python Shell Prompt User Commands (Statements) Outputs ( )
  • 9. Elements of Python • A Python program is a sequence of definitions and commands (statements) • Commands manipulate objects • Each object is associated with a Type • Type: • A set of values • A set of operations on these values • Expressions: An operation (combination of objects and operators) Jan-24 Programming 9
  • 10. Types in Python • int • Bounded integers, e.g. 732 or -5 • float • Real numbers, e.g. 3.14 or 2.0 • long • Long integers with unlimited precision • str • Strings, e.g. ā€˜hello’ or ā€˜C’ • bool • Boolean, true or false Jan-24 Programming 10
  • 11. Example of Types Jan-24 Programming 11
  • 12. Type Conversion (Type Cast) • Conversion of value of one type to other • We are used to int ↔ float conversion in Math • Integer 3 is treated as float 3.0 when a real number is expected • Float 3.6 is truncated as 3, or rounded off as 4 for integer contexts • Type names are used as type converter functions Jan-24 Programming 12
  • 13. Type Conversion Examples Jan-24 Programming 13 Note that float to int conversion is truncation, not rounding off
  • 14. Type Conversion and Input Jan-24 Programming 14
  • 15. Operators • Arithmetic • Comparison Jan-24 Programming 15 + - * // / % ** == != > < >= <=
  • 16. Variables • A name associated with an object • Assignment used for binding m = 64; c = ā€˜Acads’; f = 3.1416; • Variables can change their bindings f = 2.7183; Jan-24 Programming 16 64 Acads 3.1416 2.7183 m c f
  • 17. Assignment Statement • A simple assignment statement Variable = Expression; • Computes the value (object) of the expression on the right hand side expression (RHS) • Associates the name (variable) on the left hand side (LHS) with the RHS value • = is known as the assignment operator. Jan-24 Programming 17
  • 18. Programming using Python Operators and Expressions 1/22/2024 Programming 18
  • 19. Binary Operations 1/22/2024 Programming 19 Op Meaning Example Remarks + Addition 9+2 is 11 9.1+2.0 is 11.1 - Subtraction 9-2 is 7 9.1-2.0 is 7.1 * Multiplication 9*2 is 18 9.1*2.0 is 18.2 / Division 9/2 is 4.50 In Python3 9.1/2.0 is 4.55 Real div. // Integer Division 9//2 is 4 % Remainder 9%2 is 1
  • 20. The // operator • Also referred to as ā€œinteger divisionā€ • Result is a whole integer (floor of real division) • But the type need not be int • the integral part of the real division • rounded towards minus infinity (āˆ’āˆž) • Examples 1/22/2024 Programming 20 9//4 is 2 (-1)//2 is -1 (-1)//(-2) is 0 1//2 is 0 1//(-2) is -1 9//4.5 is 2.0
  • 21. The % operator •The remainder operator % returns the remainder of the result of dividing its first operand by its second. 1/22/2024 Programming 21 9%4 is 1 (-1)%2 is 1 (-1)//(-2) is 0 9%4.5 is 0.0 1%(-2) is 1 1%0.6 is 0.4 Ideally: x == (x//y)*y + x %y
  • 22. Conditional Statements •In daily routine •If it is very hot, I will skip exercise. •If there is a quiz tomorrow, I will first study and then sleep. Otherwise I will sleep now. •If I have to buy coffee, I will go left. Else I will go straight. Jan-24 Programming 22
  • 23. if-else statement • Compare two integers and print the min. Jan-24 Programming 23 1. Check if x is less than y. 2. If so, print x 3. Otherwise, print y. if x < y: print (x) else: print (y) print (ā€˜is the minimum’)
  • 24. x,y = 6,10 if x < y: print (x) else: print (y) print (ā€˜is the min’) x y 6 10 Run the program Output 6 Indentation •Indentation is important in Python • grouping of statement (block of statements) • no explicit brackets, e.g. { }, to group statements Jan-24 Programming 24
  • 25. if statement (no else!) • General form of the if statement • Execution of if statement • First the expression is evaluated. • If it evaluates to a true value, then S1 is executed and then control moves to the S2. • If expression evaluates to false, then control moves to the S2 directly. Jan-24 Programming 25 if boolean-expr : S1 S2 S1 S2
  • 26. if-else statement • General form of the if-else statement • Execution of if-else statement • First the expression is evaluated. • If it evaluates to a true value, then S1 is executed and then control moves to S3. • If expression evaluates to false, then S2 is executed and then control moves to S3. • S1/S2 can be blocks of statements! Jan-24 Programming 26 if boolean-expr : S1 else: S2 S3 S2 S1 S3
  • 27. Nested if, if-else Jan-24 Programming 27 if a <= b: if a <= c: … else: … else: if b <= c) : … else: …
  • 28. Elif • A special kind of nesting is the chain of if-else-if-else-… statements • Can be written elegantly using if-elif-..-else Jan-24 Programming 28 if cond1: s1 elif cond2: s2 elif cond3: s3 elif … else last-block-of-stmt if cond1: s1 else: if cond2: s2 else: if cond3: s3 else: …
  • 29. Summary of if, if-else •if-else, nested if's, elif. •Multiple ways to solve a problem •issues of readability, maintainability •and efficiency Jan-24 Programming 29
  • 30. Class Quiz • What is the value of expression: a) Run time crash/error b) I don’t know / I don’t care c) False d) True Jan-24 Programming 30 (5<2) and (3/0 > 1) The correct answer is False
  • 31. Programming using Python Loops Jan-24 Python Programming 31
  • 32. Program… Jan-24 Python Programming 32 n = int(input('Enter a number: ')) print (n, 'X', 1, '=', n*1) print (n, 'X', 2, '=', n*2) print (n, 'X', 3, '=', n*3) print (n, 'X', 4, '=', n*4) print (n, 'X', 5, '=', n*5) print (n, 'X', 6, '=', n*6) …. Too much repetition! Can I avoid it?
  • 33. While Statement 1. Evaluate expression 2. If TRUE then a) execute statement1 b) goto step 1. 3. If FALSE then execute statement2. Jan-24 Python Programming 33 while (expression): S1 S2 FALSE TRUE S1 expression S2
  • 34. For loop in Python • General form Jan-24 Python Programming 34 for variable in sequence: stmt
  • 35. For Loop Jan-24 Python Programming 35 The range() Function: To loop through a set of code a specified number of times, we can use the range() function ļ‚¢ for x in range(6): print(x)
  • 36. For Loop Jan-24 Python Programming 36 The range() function defaults to 0 as a starting value, it is possible to specify the starting value by adding a parameter: range(2, 6), which means values from 2 to 6 (but not including 6): ļ‚¢ for x in range(2, 6): print(x)
  • 37. # print all odd numbers < 10 i = 1 while i < 10: print(i) i += 2 Quiz Jan-24 Python Programming 37