Python Class 8 - Python Control Flow Statements Part 2

preview_player
Показать описание

Python Conditional Statements, Decision making in Python programming, Python if statement, Python else statement, and Python elif statement.

We have three types of Control Flow Statements in Python:
1. Conditional Statements
2. Loop Statements

Python Language Loops
Programming languages provide various control structures that allow for more complicated execution paths.

A loop statement allows us to execute a statement or group of statements multiple times.

Python programming language provides two loop structures,

1. while loop
2. for loop to handle loop requirements…

Python Complete Tutorial

Python Videos PlayList

Python Programming Syllabus

Python Programming Quiz

Python Interview Questions for Fresher
--------------------------------------------------
1. Introduction to Python Programming Language

2. Download and Install Python

Python Environment Setup (Using PyCharm IDE)

3. Python Language Syntax

4. Python Keywords and Identifiers

5. Comments in Python

6. Python Variables

7. Python Data Types

8. Python Operators

9. Python Conditional Statements

10. Python Loops

11. Python Branching Statements

12. Python Numbers

13. String Handling in Python

14. Python Data Structures - Lists

15. Python Data Structures - Sets

16. Python Data Structures - Tuples

17. Python Data Structures - Dictionaries

18. Python User Defined Functions

19. Python Built-in Functions

20. Python Modules

21. Python User Input

22. File Handling in Python

23. Python Date and Time

24. Python Exception Handling

25. Python Regular Expressions

26. Python Object Oriented Programming

27. Inheritance in Python

28. Polymorphism in Python

29. Abstraction in Python
--------------------------------------------------
Рекомендации по теме
Комментарии
Автор

Python Complete Tutorial

Python Videos PlayList

Python Programming Syllabus

Python Programming Quiz

Python Interview Questions for Fresher

1. Introduction to Python Programming Language

2. Download and Install Python

Python Environment Setup (Using PyCharm IDE)

3. Python Language Syntax

4. Python Keywords and Identifiers

5. Comments in Python

6. Python Variables

7. Python Data Types

8. Python Operators

9. Python Conditional Statements

10. Python Loops

11. Python Branching Statements

12. Python Numbers

13. String Handling in Python

14. Python Data Structures - Lists

15. Python Data Structures - Sets

16. Python Data Structures - Tuples

17. Python Data Structures - Dictionaries

18. Python User Defined Functions

19. Python Built-in Functions

20. Python Modules

21. Python User Input

22. File Handling in Python

23. Python Date and Time

24. Python Exception Handling

25. Python Regular Expressions

26. Python Object Oriented Programming

27. Inheritance in Python

28. Polymorphism in Python

29. Abstraction in Python

gcreddy
Автор

Class Notes:
Python Class 8: Python Control Flow Statements Part-2

Control Flow - Decision making/Conditional Statements
1. Run a block of statements when a condition is true
2. Run a block of statements when a condition is true, otherwise run another block of statements
3. Run a block of statements when a compound condition is true
4. Run a block of statements when a compound condition is true, otherwise run another block of statements

5. Decide among several alternates (elif)

Syntax:

if (condition1):
Statement/s
elif (condition2):
Statement/s
elif (condition3):
Statement/s
elif (condition4):
Statement/s
else:
Statement/s

Problem:

Initialize an Integer Variable and verify the range

If the number is in between 1 and 100 then display "The Number is Small Number"
If the number is in between 101 and 1000 then display "The Number is Medium Number"
If the number is in between 1001 and 10000 then display "The Number is Big Number"
If the number is more than 10000 then display "The Number is High Number"
Otherwise, display "The Number is either Zero or Negative Number"

Solution:

num=100

if ((num>=1) and (num<=100)):
print ("The Number is Small Number")
elif ((num>100) and (num<=1000)):
print ("The Number is Medium Number")
elif ((num>1000) and (num<=10000)):
print ("The Number is Big Number")
elif (num>10000):
print ("The Number is High Number")
else:
print("The Number is either Zero or Negative Number")

6. Run a block of statements when more than one condition is true (Nested if)

Syntax:

if (condition1):
if (condition2):
if (condition3):
Statement/s
else:
Statements

Example:

a, b, c, d = 100, 90, 60, 700

if (a>b):
if (a>c):
if (a>d):
print ("A is Big Number")
else:
print ("A is Not Big Number")

Compound Condition:

a, b, c, d = 100, 90, 60, 700

if ((a>b) and (a>c) and (a>d)):
print ("A is Big Number")
else:
print ("A is Not Big Number")

Nested Condition:

a, b, c, d = 100, 900, 600, 700

if (a>b):
if (a>c):
if (a>d):
print ("A is Big Number")
else:
print ("A is Not Big Number - 3rd Condition is False")
else:
print ("A is Not Big Number - 2nd Condition is False")
else:
print ("A is Not Big Number - 1st Condition is False")

Note: Nested if Condition vs. Compound Condition

In Nested, if condition we can write multiple else parts, whereas in the compound condition we can write single else part only.

Problem: Find the Biggest Number among four numbers

Solution:

Hint: Use Compound conditions and elif structure,

a, b, c, d = 700, 90, 700, 70

if ((a>b) and (a>c) and (a>d)):
print ("A is Big Number")
elif ((b>a) and (b>c) and (b>d)):
print ("B is Big Number")
elif ((c>a) and (c>b) and (c>d)):
print ("C is Big Number")
elif ((d>a) and (d>b) and (d>c)):
print ("D is Big Number")
else:
print ("Two are more Numbers are Big Numbers")

II. Control Flow - Loop Statements

1. while loop
2. for loop

1. while loop

Syntax:

Initialization
while(condition):
statement/s
.
increment/decrement


Example 1: Print 1 to 10 Numbers using while loop

i=1
while (i<=10):
print (i)
i=i+1

not

==
!=
>
>=
<
<=


Example 2: Print 1 to 5 Numbers except 3rd Number using while loop

i=1
while (i<=5):
if (i!=3):
print (i)
i=i+1

gcreddy