Conditional if Statements in Python | CBSE Class 11| Computer Science
Dear Class 11th STUDENTS, !
Welcome to this tutorial of Conditional if Statements in Python from your CBSE class 11 of Computer Science Syllabus .
In this tutorial, we shall be learning our chapter-7: Conditional if Statements in Python from Unit 2: Programming and Computational Thinking (PCT-1) as CBSE BOARD suggested to learn about computer system and its organisation to complete this section.
Unit 2: Programming and Computational Thinking (PCT-1)
chapter-7: Conditional statements in python:
- if,
- if-else,
- if-elif-else,
- flowcharts,
- simple programs:
- g.: absolute value, sort 3 numbers and divisibility of a number.
I advice you to check the latest syllabus given by CBSE Board at its Official website: www.cbseacademic.nic.in
Also, in this tutorial we will covers all necessary topics/concepts required to complete your exams preparations in CBSE classes 11th.
Also, you can Sign Up our free Computer Science Courses for classes 11th and 12th.
NOTE:
- We are also giving some important Questions & Answers for better understanding as well as preparation for your examinations.
- You may also download PDF file of this tutorial from our SHOP for free.
- For your ease and more understanding, we are also giving the video explanation class of each and every topic individually, so that you may clear your topics and get success in your examinations.
Introduction
The if statements are the conditional statements in Python and these implement selection constructs (decision constructs).
An if statement tests a particular condition; if the condition evaluates to true, a course-of-action is followed i.e, a statement or set-of-statements is executed. Otherwise (if the condition evaluates to false), the course-of-action is ignored.
Before we proceed, it is important that you recall what you learnt in Chapter 8 in relational and logical operators as all that will help you form conditionals in this chapter. Also, please note in all forms of if statement, we are using true to refer to Boolean value True as well as truth value true al collectively; similarly, false here will refer to Boolean value False and truth value false al collectively.
The If Statement
The simplest form of the if statement python tests a condition and check if the condition evaluates to true then it carries out some instructions and does nothing in case condition evaluates to false.
In general form its syntax is shown below figure.
Where a statement may consist of a single statement, a compound statement, or just the pass statement (in case of empty statement).
Carefully look at the if statement; it is also a compound statement having a header and a body containing indented statement. for instance, consider the following code fragment:
In an if statement, if the conditional expression evaluates to true, the statement in the body-of-if are exexecuted, otherwise ignored.
The above code will check whether the character variable Ch. Stores a space or not; if it does (i.e., the condition Ch== ‘‘evaluates to true), the number of spaces are incremented by 1 and number of character (stored in variable chars) are also incremented by value 1.
If, however, variable ch does not stores a space i.e., the condition Ch==’’ evaluates to false then nothing will happen; no statement from the body-of-if will be executed. consider another example illustrating the use of if statement:
Ch = input (“enter a single character:”)
If Ch >=‘0’ and Ch <=‘9’:
print (“you entered a digit.’’)
the above code after getting input in Ch, compares its value; if value of Ch falls between characters ‘0’ to ‘9’i.e., the condition evaluates to true, and thus it will be executing the statements in the if body; that is, it will print a massage saying you entered a digit.’
Now consider another example that use to if statements one after the other:
the above code example reads a single character in variable Ch. If the character input is a space, if flashes a massage specifying it. If the character input a digit, if flashes a massage specifying it.
The following example also make use of an if statement:
The above program has an if statement with two statements in its body; last print statement is not part of if statement. have a look at some more example of condition expression in if statements:
If grade == ‘A’: #comparison with a literal
Print (‘’you did well’’)
If a > b: #comparing two variables
Print (‘’A has more B has ‘’)
If x: #testing truth value of a variable
Print (‘’x has truth value as true ‘’)
Print (‘’hence you can see this massage.’’)
The if else statement
This form of if statement tests a condition and if the condition to true, it carries out statement indented below if and in case condition evaluates to false, it carries out statement indented below else. The syntax (general form) of the if else statement is as shown below:
If<conditional expression>:
Statement
[statements]
Else:
Statement
[statements]
For instances, consider the following code fragment:
If a > =0:
Print (a,’’ is zero or a positive number’’)
Else:
Print (a,’’ is a negative number’’)
For any value more than zero (say 7) of variable a, the above code will print a massage like:
7 is zero a positive number
And for a value less than zero (say-5) of variable a, the above code will print a massage like:
-5 is a negative number
Unlike previously discussed plain-if statement, which does nothing when the condition results into false, the if-else statement performs some action in both cases whether conditions true or false. consider one more example:
If sales>=10000:
Discount = sales *0.10
Else:
Discount = ales*0.05
The above statement calculates discount as 10% of sales amount if it 10000 or more otherwise it calculates discount as 5% of sales amount.
Following figure illustrates if and if-else construct of python.
The if – elif Statement
Sometimes, you need to check another condition in case the test-condition of if evaluates to false. That is, you want to check a condition when control else part, i.e., condition test in the form of else if. To understand this, consider this example:
if runs are more than 100
then it is a century
else if runs are more than 50
then it is a fifty
else
batsman has neither scored a century nor fifty
To serve condition as above i.e., in else if from (or if inside an else), Python provides if-elif and if- elif-else statements.The general form of these statements is:
if <conditional expression>:
Statement
[statements]
elif <conditional expression>:
Statement
[statements]
And if <conditional expression >:
Statement
[statements]
elif <conditional expression>:
statement
[statements]
else:
statement
[statements]
Now the above-mentioned example can be coded in Python as:
If runs >=100: Python will test this condition in case
Print (“Batsman scored a century”) previous condition (runs>=100) is false.
elif runs > = 50: This block will be executed when both the
Print (“Batsman scored a fifty”) if “s condition (i.e., runs > =100) and elif
elif: condition (i.e., runs>=50) are false.
Print (“Batsman has neither scored a century nor fifty”)
Let us have a look at some more code example:
if num < 0:
print (num, “is a negative number. “)
elif num = = 0:
print (num, “is equal to zero.”)
else:
print (num, “is a positive number.”)
You make out what the above code is doing? Hey, don’t gets angry. I was just kidding. you know that it is testing a number whether it is negative (< 0), or zero (= 0) or positive (> 0). Consider another code example:
if sales >= 30000:
discount = sales *0.18
elif sales > = 20000:
discount = sales * 0.15
elif sales > = 10000: There can be us many elif blocks as you
discount = sales * 0.10 need. Here, the code is having two elif
blocks
else: This block will be executed when none of
discount = sales * 0.05 above conditions are true.