<?xml version="1.0"?>
<?xml-stylesheet type="text/css" href="https://script.spoken-tutorial.org/skins/common/feed.css?303"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
		<id>https://script.spoken-tutorial.org/index.php?action=history&amp;feed=atom&amp;title=Python%2FC3%2FConditionals%2FEnglish</id>
		<title>Python/C3/Conditionals/English - Revision history</title>
		<link rel="self" type="application/atom+xml" href="https://script.spoken-tutorial.org/index.php?action=history&amp;feed=atom&amp;title=Python%2FC3%2FConditionals%2FEnglish"/>
		<link rel="alternate" type="text/html" href="https://script.spoken-tutorial.org/index.php?title=Python/C3/Conditionals/English&amp;action=history"/>
		<updated>2026-05-14T23:05:07Z</updated>
		<subtitle>Revision history for this page on the wiki</subtitle>
		<generator>MediaWiki 1.23.17</generator>

	<entry>
		<id>https://script.spoken-tutorial.org/index.php?title=Python/C3/Conditionals/English&amp;diff=495&amp;oldid=prev</id>
		<title>Chandrika: Created page with '{| border=1 !Visual Cue !Narration |- | Show Slide 1   Containing title, name of the production team along with the logo of MHRD  | Hello friends and Welcome to the tutorial on '…'</title>
		<link rel="alternate" type="text/html" href="https://script.spoken-tutorial.org/index.php?title=Python/C3/Conditionals/English&amp;diff=495&amp;oldid=prev"/>
				<updated>2012-11-29T06:15:36Z</updated>
		
		<summary type="html">&lt;p&gt;Created page with &amp;#039;{| border=1 !Visual Cue !Narration |- | Show Slide 1   Containing title, name of the production team along with the logo of MHRD  | Hello friends and Welcome to the tutorial on &amp;#039;…&amp;#039;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;{| border=1&lt;br /&gt;
!Visual Cue&lt;br /&gt;
!Narration&lt;br /&gt;
|-&lt;br /&gt;
| Show Slide 1 &lt;br /&gt;
&lt;br /&gt;
Containing title, name of the production team along with the logo of MHRD &lt;br /&gt;
| Hello friends and Welcome to the tutorial on 'Conditionals'.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| Show Slide 2 &lt;br /&gt;
&lt;br /&gt;
Learning objectives &lt;br /&gt;
| At the end of this tutorial, you will be able to,&lt;br /&gt;
&lt;br /&gt;
# Use if/else blocks.&lt;br /&gt;
# Use if/elif/else blocks.&lt;br /&gt;
# Use the Ternary conditional statement - C if X else Y.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| Shift to terminal and start ipython &lt;br /&gt;
&lt;br /&gt;
 ipython&lt;br /&gt;
| To begin with let us start ipython,&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
|  a = 5&lt;br /&gt;
| Whenever we have two possible states that can occur depending on a a certain condition, we can use if/else construct in Python.&lt;br /&gt;
&lt;br /&gt;
For example, say, we have a variable &amp;lt;tt&amp;gt;a&amp;lt;/tt&amp;gt; which stores integers and we are required to find out whether &amp;lt;tt&amp;gt;a&amp;lt;/tt&amp;gt; is even or odd. Let's say the value of &amp;lt;tt&amp;gt;a&amp;lt;/tt&amp;gt; is 5.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
|  if a % 2 == 0:&lt;br /&gt;
     print &amp;quot;Even&amp;quot;&lt;br /&gt;
 else:&lt;br /&gt;
     print &amp;quot;Odd&amp;quot;&lt;br /&gt;
| In such a case we can write the if/else block as&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| &lt;br /&gt;
| If &amp;lt;tt&amp;gt;a&amp;lt;/tt&amp;gt; is divisible by 2, i.e., the result of &amp;quot;a modulo 2&amp;quot; is 0, it prints &amp;quot;Even&amp;quot;, otherwise it prints &amp;quot;Odd&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
Note that in such a case, only one of the two blocks gets executed depending on whether the condition is &amp;lt;tt&amp;gt;True&amp;lt;/tt&amp;gt; or &amp;lt;tt&amp;gt;False&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
There is a very important syntactic element to understand here. Every code block begins with a line that ends with a &amp;lt;tt&amp;gt;:&amp;lt;/tt&amp;gt;, in this example the &amp;lt;tt&amp;gt;if&amp;lt;/tt&amp;gt; and the &amp;lt;tt&amp;gt;else&amp;lt;/tt&amp;gt; lines. Also, all the statements inside a code block are intended by 4 spaces. Hitting enter twice, ends the code block.&lt;br /&gt;
&lt;br /&gt;
The if/else blocks work for a condition, which can take one of two states. But what do we do for conditions, which can take more than two states?&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
|  a = -3&lt;br /&gt;
 &lt;br /&gt;
 if a &amp;gt; 0:&lt;br /&gt;
     print &amp;quot;positive&amp;quot;&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;elif a &amp;lt; 0:&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
     print &amp;quot;negative&amp;quot;&lt;br /&gt;
 else:&lt;br /&gt;
     print &amp;quot;zero&amp;quot;&lt;br /&gt;
| Python provides if/elif/else blocks, for such conditions. For example. We have a variable &amp;lt;tt&amp;gt;a&amp;lt;/tt&amp;gt; which holds integer values. We need to print &amp;quot;positive&amp;quot; if &amp;lt;tt&amp;gt;a&amp;lt;/tt&amp;gt; is positive, &amp;quot;negative&amp;quot; if it is negative or &amp;quot;zero&amp;quot; if it is 0.&lt;br /&gt;
&lt;br /&gt;
Let us use if/elif/else ladder for it. For the purposes of testing our code let us assume that the value of a is -3&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| &lt;br /&gt;
| All the syntax and rules as said for if/else statements hold the same. The only addition here is the &amp;lt;tt&amp;gt;elif&amp;lt;/tt&amp;gt; statement which can have another condition of its own.&lt;br /&gt;
&lt;br /&gt;
Here too, exactly one block of code is executed -- the block of code which first evaluates to &amp;lt;tt&amp;gt;True&amp;lt;/tt&amp;gt;. Even if there is a situation where multiple conditions evaluate to True, all the subsequent conditions other than the first one, which evaluates to True, are neglected. Consequently, the else block gets executed if and only if all the conditions evaluate to False.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| Show Slide 3 &lt;br /&gt;
&lt;br /&gt;
~if/elif~ ladder &lt;br /&gt;
&lt;br /&gt;
'''if user == 'admin':'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;nowiki&amp;gt;# Do admin operations&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''elif user == 'moderator':'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;nowiki&amp;gt;# Do moderator operations&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''elif user == 'client':'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;nowiki&amp;gt;# Do customer operations&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
| Also, the &amp;lt;tt&amp;gt;else&amp;lt;/tt&amp;gt; block in both if/else statement and if/elif/else is optional. We can have a single if statement or just if/elif statements without having else block at all. Also, there can be any number of elif's within an if/elif/else ladder. For example&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| &lt;br /&gt;
| Note that there are multiple elif blocks and there is no else block.&lt;br /&gt;
&lt;br /&gt;
Pause the video here, try out the following exercise and resume the video.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| Show Slide 4 &lt;br /&gt;
&lt;br /&gt;
Assignment 1 &lt;br /&gt;
| '''Given a number, num. Write an if else block to print num, as is,'''&lt;br /&gt;
&lt;br /&gt;
if it is divisible by 10, else print 10 * num.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| Show Slide 5 &lt;br /&gt;
&lt;br /&gt;
Solution 1 &lt;br /&gt;
&lt;br /&gt;
'''if num%10 == 0:'''&lt;br /&gt;
&lt;br /&gt;
print num&lt;br /&gt;
&lt;br /&gt;
'''else:'''&lt;br /&gt;
&lt;br /&gt;
print 10*num&lt;br /&gt;
| The solution is on your screen.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
|  score_str = 'AA'&lt;br /&gt;
| In addition to these conditional statements, Python provides a very convenient ternary conditional operator. Let us take the following example where we read the marks from a data file which is obtained as a string as we read a file. The marks can be in the range of 0 to 100 or 'AA' if the student is absent. In such a case, to obtain the marks as an integer, we can use the ternary conditional operator. Let us say the string score is stored in score_str variable&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
|  score = int(score_str) if score_str != 'AA' else 0&lt;br /&gt;
| Now let us use the ternary conditional operator&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| Show Slide 6 &lt;br /&gt;
&lt;br /&gt;
Assignment 2 &lt;br /&gt;
| '''Given a number, num. Write a ternary operator to print num, as is,'''&lt;br /&gt;
&lt;br /&gt;
if it is divisible by 10, else print 10 * num.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| Show Slide 7 &lt;br /&gt;
&lt;br /&gt;
Solution 2 &lt;br /&gt;
| The solution is on your screen.&lt;br /&gt;
&lt;br /&gt;
print num if num%10 == 0 else 10*num&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| &lt;br /&gt;
| Moving on, there are certain situations where we will have no operations or statements within a block of code. For example, we have a code where we are waiting for the keyboard input. If the user enters &amp;quot;c&amp;quot;, &amp;quot;d&amp;quot; or &amp;quot;x&amp;quot; as the input, we would perform some operation; nothing otherwise. In such cases &amp;quot;pass&amp;quot; statement comes very handy&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| Show Slide 8 &lt;br /&gt;
&lt;br /&gt;
Pass statement &lt;br /&gt;
| '''a = raw_input(&amp;quot;Enter 'c' to calculate and exit, 'd' to display the existing'''&lt;br /&gt;
&lt;br /&gt;
results exit and 'x' to exit and any other key to continue: &amp;quot;)&lt;br /&gt;
&lt;br /&gt;
'''if a == 'c':'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;nowiki&amp;gt;# Calculate the marks and exit&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''elif a == 'd':'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;nowiki&amp;gt;# Display the results and exit&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''elif a == 'x':'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;nowiki&amp;gt;# Exit the program&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''else:'''&lt;br /&gt;
&lt;br /&gt;
pass&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| &lt;br /&gt;
| In this case &amp;quot;pass&amp;quot; statement acts as a place holder for the block of code. It is equivalent to a null operation. It literally does nothing. It can used as a place holder when the actual code implementation for a particular block of code is not known yet but has to be filled up later.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| Show Slide 9 &lt;br /&gt;
&lt;br /&gt;
Summary slide &lt;br /&gt;
| This brings us to the end of the tutorial. In this tutorial, we have learnt to,&lt;br /&gt;
&lt;br /&gt;
# Understand the conditional statements in Python.&lt;br /&gt;
# Use if/else statement.&lt;br /&gt;
# Use if/elif/else statement.&lt;br /&gt;
# Apply the ternary conditional statement - C if X else Y.&lt;br /&gt;
# Use &amp;quot;pass&amp;quot; statement.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| Show Slide 10 &lt;br /&gt;
&lt;br /&gt;
Self assessment questions slide &lt;br /&gt;
| Here are some self assessment questions for you to solve&lt;br /&gt;
&lt;br /&gt;
# Use conditional statements for the following. Given a variable &amp;lt;tt&amp;gt;time&amp;lt;/tt&amp;gt;, print &amp;lt;tt&amp;gt;Good Morning&amp;lt;/tt&amp;gt; if it is less than 12, otherwise print &amp;lt;tt&amp;gt;Hello&amp;lt;/tt&amp;gt;.&lt;br /&gt;
# Convert the if else ladder below into a ternary conditional statement.&lt;br /&gt;
&lt;br /&gt;
Enumerated list ends without a blank line; unexpected unindent.&lt;br /&gt;
&lt;br /&gt;
 x = 20&lt;br /&gt;
 &lt;br /&gt;
 if x &amp;gt; 10:&lt;br /&gt;
     print x * 100&lt;br /&gt;
 else:&lt;br /&gt;
     print x&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| Show Slide 11&lt;br /&gt;
&lt;br /&gt;
Solution of self assessment questions on slide &lt;br /&gt;
| And the answers,&lt;br /&gt;
&lt;br /&gt;
1. We can use the if/else statements as&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;if time &amp;lt; 12:&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
     print &amp;quot;Good Morning&amp;quot;&lt;br /&gt;
 else:&lt;br /&gt;
     print &amp;quot;Hello&amp;quot;&lt;br /&gt;
&lt;br /&gt;
# The if else ladder can be converted to a ternary conditional statement as&lt;br /&gt;
&lt;br /&gt;
Enumerated list ends without a blank line; unexpected unindent.&lt;br /&gt;
&lt;br /&gt;
 print x * 100 if x &amp;gt; 10 else x&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| Show Slide 12&lt;br /&gt;
&lt;br /&gt;
Acknowledgment slide &lt;br /&gt;
| Hope you have enjoyed this tutorial and found it useful. Thank you!&lt;br /&gt;
&lt;br /&gt;
|}&lt;/div&gt;</summary>
		<author><name>Chandrika</name></author>	</entry>

	</feed>