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

06 - Intro to Python Part 3 with Exercise

This document introduces Python programming concepts, focusing on conditional structures (if, if-else, if-elif-else) and looping structures (for and while loops). It provides syntax examples and exercises to illustrate how these constructs work in practice. Additionally, it covers control statements like break and continue within loops.

Uploaded by

Rabbani Radzi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

06 - Intro to Python Part 3 with Exercise

This document introduces Python programming concepts, focusing on conditional structures (if, if-else, if-elif-else) and looping structures (for and while loops). It provides syntax examples and exercises to illustrate how these constructs work in practice. Additionally, it covers control statements like break and continue within loops.

Uploaded by

Rabbani Radzi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 28

INTRODUCTION TO

Python
Part 3
At the end of this lecture, you should learn:

◼ Conditional programming structure


◼ Looping programming stucture
Conditional - if

◼ The if statement of Python is similar to that of


other programming languages.
◼ The if statement contains a logical expression
using which data is compared, and a decision is
made based on the result of the comparison.

3
Conditional - if

◼ The If statement in Python Programming has


simple structure:
if expression:
statement(s)

Note: In Python, all the statements indented by the


same number of character spaces after a
programming construct are considered to be part of
a single block of code. Python uses indentation as
its method of grouping statements.
4
Conditional - if

5
Conditional – if (exercise)

number = input(" Please Enter any integer


Value: ")
if int(number) >= 1:
print(" You Have Entered Positive Integer ")

What is the
output if user
What is the
insert 29?
output if user
insert -17?
6
Conditional – if (exercise)

number = int(input(" Please Enter any integer Value: "))


remainder = number % 2
if remainder == 1:
print(" You Have Entered an Odd number ")
print(" This Message is not coming from PYTHON IF
STATEMENT")

What is the What is the


output if user output if user
insert 29? insert 100?
7
Conditional – if-else

◼ An else statement can be combined with an if


statement.
◼ An else statement contains the block of code that
executes if the conditional expression in the if
statement resolves to 0 or a false value.
◼ The else statement is an optional statement and
there could be at most only one else statement
following an if .

8
Conditional – if-else
◼ The syntax of If Else Statement in Python is as follows:

if (Test condition):
# If TRUE then these statements will be executed
True statements
else :
# If FALSE then these statements will be executed
False statements

9
Conditional – if - else

10
Conditional – if - else (exercise)

marks = int(input(”Enter Your Subject Marks: "))


if marks >= 50:
print(" Congratulations ")
print(" You cleared the subject ")
else:
print(" You Failed")
print(" Better Luck Next Time")
What is the
output if user
insert 29?

11
Conditional – if – elif - else

◼ The elif statement allows you to check multiple


expressions for truth value and execute a block of
code as soon as one of the conditions evaluates to
true.
◼ Like the else, the elif statement is optional.
◼ However, unlike else, for which there can be at
most one statement, there can be an arbitrary
number of elif statements following an if.

12
Conditional – if – elif - else
if (condition 1):
statements 1
elif (condition 2):
statements 2
elif (condition 3):
statements 3
...........
elif (condition n):
statements n
else:
default 13statements
Conditional – if – elif - else

14
Conditional – if – elif - else (exercise)

age = input("How old are you? : ”) What is the


if int(age)<18: output if user
print ("Discount rate 10%”) insert 29?
elif int(age)>65:
print ("Discount rate 15%”)
else:
print ("Adult rate - No discount”)
print ("Good bye") What is the
output if user
insert 17?
15
Loop
◼ The two distinctive loops we have in Python 3 logic
are the "for loop" and the "while loop."
◼ Both of them achieve very similar results, and can
almost always be used interchangeably towards a
goal. Many times it comes down to programmer
preference, or is reliant on efficiency.
◼ Generally, the for loop can be more efficient than the
while loop, but not always.
◼ The idea of the While loop is:
 While something is the case, do the following block
of code.
While loop
◼ The while loop is one of the looping constructs
available in Python. The while loop continues until the
expression becomes false. The expression has to be
a logical expression and must return either a true or a
false value
◼ The syntax of the while loop is:

While (Condition or Expression):


statement 1
statement 2
………….
# This is the statement Outside the While Loop
While loop

While loop - example
total
◼ =0
count = 0
number = int(input("Enter number less than 10: "))
while (count < 10):
number = int(input("Enter number less than 10:
"))
total = total + number
count = count + 1
print("Total sum of all 10 number is: ", total)
count = 0
while (count < 9):
print ('The count is:', count)
count = count + 1
print ("Good bye!")
For Loop

◼ The for loop in Python has the ability to iterate over


the items of any sequence, such as a list or a string.
Iterating over a sequence is called traversal.
◼ The syntax of the for loop is:

for iterating_var in sequence:


statements(s)

21
For Loop

22
For Loop

◼ The Python For Loop is used to repeat a block of


statements until there is no items in Object may
be String, List, Tuple or any other object in
python.
◼ For loop is one of the mostly used loop in any
programming language.

23
For Loop example (string)

for letter in 'Python': # First Example


print 'Current Letter :', letter
print "Good bye!”
This will produce following
output:
Current Letter: P
Current Letter: y
Current Letter: t
Current Letter: h
Current Letter: o
Current Letter: n
Good bye!
24
For Loop example (string)
fruits = ['banana', 'apple', 'mango']
for fruit in fruits: # Second Example
print 'Current fruit :', fruit
print "Good bye!”

This will produce following


output:
Current fruit : banana
Current fruit : apple
Current fruit : mango
Good bye!

25
For loop example (list)
# List of numbers
numbers = [6, 5, 3, 8, 4, 2, 5, 4, 11]
sum = 0

# iterate over the list


for val in numbers:
sum = sum+val

# Output sum
print("The sum is", sum)
26
For loop example (range)
◼ You can use range with for loop
◼ range function syntax as follows
range(start, stop, step)

#This code will loop 3 times ( start with 1,


stop before 6 with 2 increament
This will produce
x = range(1,6,2) following output:
for n in x: 1
print(n) 3
5
Break
◼ The break Statement:
◼ The break statement in Python terminates the current
loop and resumes execution at the next statement
◼ The break statement can be used in both while and for
loops.
# Break Example
for letter in 'Python': This will produce
if letter == 'h': following output:
Current Letter : P
break
Current Letter : y
print ('Current Letter :', letter) Current Letter : t
print("Good bye!") Good bye!
Continue
◼ The continue statement in Python returns the control
to the beginning of the while loop. The continue
statement rejects all the remaining statements in the
current iteration of the loop and moves the control
back to the top of the loop.
◼ The continue statement can be used in both while and
for loops.
for nama in "JOHOR":
if nama=='R':
continue
print ('Huruf :', nama)
print ("Tamat!")

You might also like