Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
3 views

PYTHON-PROGRAMMING-topic-3

Uploaded by

mariyarovic
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

PYTHON-PROGRAMMING-topic-3

Uploaded by

mariyarovic
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 22

CONDITIONAL,

LOOPS &
FUNCTIONS
Python Conditions and If Statements
Python supports the usual logical conditions from
mathematics:
• Equals: a == b
• Not Equals: a != b
• Less than: a < b
• Less than or equal to: a <= b
• Greater than: a > b
• Greater than or equal to: a >= b
If Statement
 The if statement is used to check if a certain condition is
true, and if so, execute a specific block of code.

A few important things to note about if statements:

❑ The colon (:) is significant and required. It separates the header of the
compound statement from the body.
❑ The line after the colon must be indented. It is standard in Python to use four
spaces for indenting.
❑ All lines indented the same amount after the colon will be executed whenever
the BOOLEAN_EXPRESSION is true.
a = 40
b = 200
if b > a:
print("b is greater than a")
print();
print("\tYear Level Checker");
print();
year_level = int(input("\tWhat is your year level? "));
print();

if (year_level == 1) :
print("\tThe given year level is ",year_level,'.');
print();
print("\tYou belong to Freshmen.");

if (year_level == 2) :
print("\tThe given year level is ",year_level,'.');
print();
print("\tYou belong to Sophomore.");

if (year_level == 3) :
print("\tThe given year level is " ,year_level,'.');
print();
print("\tYou belong to Juniors.");

if (year_level == 4) :
print("\tThe given year level is " ,year_level,'.');
print();
print("\tYou belong to Seniors.");
print();
print("\tEND OF PROGRAM");
If else statement
 The if else statement is used to
execute one block of code if a
condition is true, and another
block of code if the condition is
false.
if elif else statement


print();
print("\tFind the Day of the Week ");
print();
week = int(input("\tEnter Week Number (1-7) : "));
print();
if (week == 1) :
print("\tThe day is MONDAY.")
elif (week == 2) :
print("\tThe day is TUESDAY.")
elif (week == 3) :
print("\tThe day is WEDNESDAY.")
elif (week == 4) :
print("\tThe day is THURSDAY.")
elif (week == 5) :
print("\tThe day is FRIDAY.")
elif (week == 6) :
print("\tThe day is SATURDAY.")
elif (week == 7) :
print("\tThe day is SUNDAY.")
else :
print();
print("\tInvalid Input! Please enter week in between 1-7.");
print();
print("\tEND OF PROGRAM");
Nested IF ELSE
Statement

WHILE LOOP Statement
 A while loop statement in Python is used to repeatedly execute a block
of code if a condition is true. It is typically used when you don’t know
how many times the loop will run in advance.
For Loop Statement
 A for-loop statement in Python is used to iterate over a
sequence (such as a list, tuple, or string) and perform a
certain action for each item in the sequence.
NESTED LOOPS
 Python allows us to nest loops
within each other. This means
that we can have a loop inside
another loop. Nested loops are
useful when dealing with
multidimensional data or when
we need to iterate over multiple
sequences simultaneously.
Loop Control Statements
Python provides loop control statements to modify the behavior
of loops. These statements include break, continue, and pass:
❑ break: Terminates the loop prematurely and transfers control
to the next statement after the loop.
❑ continue: Skips the current iteration of the loop and moves to
the next iteration.
❑ pass: Acts as a placeholder and does nothing. It is commonly
used when a statement is required syntactically but no action
is needed.
Problem 1: Write a program to ask the user to input a number and then the
program will compute the sum of odd numbers based on the given number
by the user and display the result on the screen. The program also asks the
user if the user what to continue using the program or not.
number = 1

print();
print("\tWhile Loop and Break Statement Demonstration");
print();
while(number<=10):
print("\tThe Number is %d." % number)
number = number + 1
if number == 6:
break
print()
print('\tOut of the loop starting with number {0}.'.format(number))
print()
print("\tEND OF PROGRAM");

You might also like