[go: up one dir, main page]

0% found this document useful (0 votes)
4 views18 pages

Section 4

The document provides an overview of conditional statements and loops in Python, including if statements, elif, else, and various loop types such as while and for loops. It explains the use of break, continue, and pass statements, as well as the range() function for iteration. Additionally, it includes programming problems for practice, focusing on user input and conditional logic.

Uploaded by

mudy1980
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)
4 views18 pages

Section 4

The document provides an overview of conditional statements and loops in Python, including if statements, elif, else, and various loop types such as while and for loops. It explains the use of break, continue, and pass statements, as well as the range() function for iteration. Additionally, it includes programming problems for practice, focusing on user input and conditional logic.

Uploaded by

mudy1980
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/ 18

Conditions and

Loops
If Statement
• condition operators can be used in several ways, most
commonly in

"if statements" and loops.


• Remember!
Indentations define the scope of code
If statement, without indentation (will raise an error).

Short Hand If
• Used when you have only one statement to execute.
Elif
• if the previous conditions were not true, then try this
condition.
Else
• The else keyword catches anything which isn't caught by the
preceding conditions.
Short Hand If ... Else
If you have only one statement to execute, one for if, and
one for else, you can put it all on the same line.

pass Statement
if statements cannot be empty, but if you for some reason
have an if statement with no content, put in
the pass statement to avoid getting an error.
Loops
• python has two type of loop:
1. while loop
2. for loop

While Loop
break Statement
With the break statement we can stop the loop even if the
while condition is true.
continue Statement

With the continue statement we can stop the current


iteration, and continue with the next
else Statement

With the else statement we can run a block of code once


when the condition is false.
For loops
• A for loop is used for iterating over a sequence (that is either a
list, a tuple, a dictionary, a set, or a string).

• This is less like the for keyword in other programming


languages, and works more like an iterator method as found in
other object-orientated programming languages.

• With the for loop we can execute a set of statements, once for
each item in a list, tuple, set etc.
Nested Loops
• A nested loop is a loop inside a loop.

• The "inner loop" will be executed one time for each iteration
of the "outer loop"
Range() Function
• To loop through a set of code a specified number of times, we
can use the range() function,
• The range() function returns a sequence of numbers, starting
from 0 by default, and increments by 1 (by default), and ends
at a specified number.

• NOTE : in for loop we also use break , continue , else , and


pass.
• 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).

• it is possible to specify the increment value by adding a third


parameter.
Problems
 Create a program that asks the user for input 2 different
information for 2 person “name & age ” ,compare it and display
who is the oldest in an appropriate sentence.
(hint: you should indicate if they are the same age if it happen )
Ask the user for a number. Depending on whether the number is
even or odd, print out an appropriate message to the user. Hint: how
does an even / odd number react differently when divided by 2?

• Extras:

- If the number is a multiple of 4, print out a different message.

- Ask the user for two numbers: one number to check (call
it num) and one number to divide by (check). If check divides
evenly into num, tell that to the user. If not, print a different
appropriate message.
 Take a list, say for example this one:

a = [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]

and write a program that prints out all the elements of the
list that are less than 5.

 Create a program that asks the user for a number and then
prints out a list of all the divisors of that number. (If you don’t
know what a divisor is, it is a number that divides evenly into
another number. For example, 13 is a divisor of 26 because 26
/ 13 have no remainder.)
Any Question…?

You might also like