Page 395 - Ai_417_V3.0_C9_Flipbook
P. 395
Task #Experiential Learning
#Coding & Computational Thinking
1. Type "import this" in your Python IDLE, and you will find a poem written by Tim Peters, a major
contributor to the Python community.
_______________________________________________________________________________________________________
2. Input a radius of a circle. Calculate the area and perimeter of circle.
_______________________________________________________________________________________________________
_______________________________________________________________________________________________________
_______________________________________________________________________________________________________
_______________________________________________________________________________________________________
_______________________________________________________________________________________________________
Selection Statements
In Python,
• Selection flow of control is achieved through conditional statements.
• In the conditional/selection statements, flow of control is changed based on a condition. We specify the
condition in the program, which evaluates to either 'True' or 'False'.
• If condition is 'True' then the block of statement written for 'True' will be executed, and if the condition is 'False'
then the block of statements for 'False' will be executed.
• Conditional statements are also called branching statements because a program decides which statement to
execute based on the result of the evaluated condition.
• A block is identified by using an indentation (minimum 1 space). Ensure that all statements in one block are
indented at the same level.
• Python mainly provides three types of conditional statements—if, if…else and if…elif…else statements.
Let us learn about them in detail.
The if Statement
We use the if statement to do conditional programming. Condition with the if statement can also be written
within the parenthesis ( ). However, it is optional to use parenthesis. It is mandatory to specify colon (:) after
writing a condition with the if statement. The number of statements (also called as a Block) written within if
statement can be of any size. A statement or block of statement will be executed only when the condition is
'True'. If the condition is 'False' then nothing happens.
The syntax of the if is as follows:
if (condition):
statements
Where,
• if is a keyword.
• condition can be any relational or logical expression.
• statements are always indented can be single or a block.
Introduction to Python 393

