Python/C3/Conditionals/English

From Script | Spoken-Tutorial
Revision as of 11:45, 29 November 2012 by Chandrika (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search
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 'Conditionals'.
Show Slide 2

Learning objectives

At the end of this tutorial, you will be able to,
  1. Use if/else blocks.
  2. Use if/elif/else blocks.
  3. Use the Ternary conditional statement - C if X else Y.


Shift to terminal and start ipython
ipython
To begin with let us start ipython,
a = 5 Whenever we have two possible states that can occur depending on a a certain condition, we can use if/else construct in Python.

For example, say, we have a variable a which stores integers and we are required to find out whether a is even or odd. Let's say the value of a is 5.

if a % 2 == 0:
    print "Even"
else:
    print "Odd"
In such a case we can write the if/else block as
If a is divisible by 2, i.e., the result of "a modulo 2" is 0, it prints "Even", otherwise it prints "Odd".

Note that in such a case, only one of the two blocks gets executed depending on whether the condition is True or False.

There is a very important syntactic element to understand here. Every code block begins with a line that ends with a :, in this example the if and the else lines. Also, all the statements inside a code block are intended by 4 spaces. Hitting enter twice, ends the code block.

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?

a = -3
if a > 0:
    print "positive"
elif a < 0:
    print "negative"
else:
    print "zero"
Python provides if/elif/else blocks, for such conditions. For example. We have a variable a which holds integer values. We need to print "positive" if a is positive, "negative" if it is negative or "zero" if it is 0.

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

All the syntax and rules as said for if/else statements hold the same. The only addition here is the elif statement which can have another condition of its own.

Here too, exactly one block of code is executed -- the block of code which first evaluates to True. 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.

Show Slide 3

~if/elif~ ladder

if user == 'admin':

# Do admin operations

elif user == 'moderator':

# Do moderator operations

elif user == 'client':

# Do customer operations

Also, the else 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
Note that there are multiple elif blocks and there is no else block.

Pause the video here, try out the following exercise and resume the video.

Show Slide 4

Assignment 1

Given a number, num. Write an if else block to print num, as is,

if it is divisible by 10, else print 10 * num.

Show Slide 5

Solution 1

if num%10 == 0:

print num

else:

print 10*num

The solution is on your screen.
score_str = 'AA' 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
score = int(score_str) if score_str != 'AA' else 0 Now let us use the ternary conditional operator
Show Slide 6

Assignment 2

Given a number, num. Write a ternary operator to print num, as is,

if it is divisible by 10, else print 10 * num.

Show Slide 7

Solution 2

The solution is on your screen.

print num if num%10 == 0 else 10*num

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 "c", "d" or "x" as the input, we would perform some operation; nothing otherwise. In such cases "pass" statement comes very handy
Show Slide 8

Pass statement

a = raw_input("Enter 'c' to calculate and exit, 'd' to display the existing

results exit and 'x' to exit and any other key to continue: ")

if a == 'c':

# Calculate the marks and exit

elif a == 'd':

# Display the results and exit

elif a == 'x':

# Exit the program

else:

pass

In this case "pass" 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.
Show Slide 9

Summary slide

This brings us to the end of the tutorial. In this tutorial, we have learnt to,
  1. Understand the conditional statements in Python.
  2. Use if/else statement.
  3. Use if/elif/else statement.
  4. Apply the ternary conditional statement - C if X else Y.
  5. Use "pass" statement.


Show Slide 10

Self assessment questions slide

Here are some self assessment questions for you to solve
  1. Use conditional statements for the following. Given a variable time, print Good Morning if it is less than 12, otherwise print Hello.
  2. Convert the if else ladder below into a ternary conditional statement.

Enumerated list ends without a blank line; unexpected unindent.

x = 20

if x > 10:
    print x * 100
else:
    print x
Show Slide 11

Solution of self assessment questions on slide

And the answers,

1. We can use the if/else statements as

if time < 12:
    print "Good Morning"
else:
    print "Hello"
  1. The if else ladder can be converted to a ternary conditional statement as

Enumerated list ends without a blank line; unexpected unindent.

print x * 100 if x > 10 else x
Show Slide 12

Acknowledgment slide

Hope you have enjoyed this tutorial and found it useful. Thank you!

Contributors and Content Editors

Chandrika