Python/C3/Conditionals/English-timed

From Script | Spoken-Tutorial
Revision as of 17:40, 18 March 2013 by Sneha (Talk | contribs)

Jump to: navigation, search
Visual Cue Narration
0:01 Hello friends and Welcome to the tutorial on 'Conditionals'.
0:05 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.
0:25 To begin with let us start ipython,
0:29 So type ipython in the terminal
0:36 Whenever we have two possible states that can occur depending on a a certain condition, we can use if/else construct in Python.
0:45 For example, say, we have a variable a which stores integers and we are required to find out whether a is even or odd.
0:52 Let's say the value of a is 5.
0:55 So type a=5
0:59 In such a case we can write the if/else block as
1:04 In command line if a percentage 2 == 0 colon
   		print in double quotes Even

else colon

   		print in double quotes Odd
1:37 If a is divisible by 2, i.e., the result of "a modulo 2" is 0, it prints "Even", otherwise it prints "Odd".
1:51 Note that in such a case, only one of the two blocks gets executed depending on whether the condition is True or False.
1:58 There is a very important syntactic element to understand here.
2:03 Every code block begins with a line that ends with a colon, in this example the if and the else lines.
2:15 Also, all the statements inside a code block are intended by 4 spaces.
2:21 Hitting enter twice, ends the code block.
2:25 The if/else blocks work for a condition, which can take one of two states.
2:31 But what do we do for conditions, which can take more than two states?
2:38 Python provides if/elif/else blocks, for such conditions.
2:47 For example.
2:49 We have a variable a which holds integer values.
2:52 We need to print "positive" if a is positive, "negative" if it is negative or "zero" if it is 0.
3:04 Let us use if/elif/else ladder for it.
3:09 For the purposes of testing our code let us assume that the value of a is -3
3:16 Type a = -3

if a greater than 0 colon print in double quotes positive elif a less than 0 colon print in double quotes negative else colon print in double quotes zero

4:17 All the syntax and rules as said for if/else statements hold the same.
4:24 The only addition here is the elif statement which can have another condition of its own.
4:30 Here too, exactly one block of code is executed -- the block of code which first evaluates to True .
4:41 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.
4:51 Consequently, the else block gets executed if and only if all the conditions evaluate to False.
4:59 Also, the else block in both if/else statement and if/elif/else is optional.
5:08 We can have a single if statement or just if/elif statements without having else block at all.
5:17 Also, there can be any number of elif's within an if/elif/else ladder.
5:26 For example
5:28 Type if user == in single quotes admin colon

elif user == in single quotes moderator colon elif user == in single quotes client colon

5:47 Note that there are multiple elif blocks and there is no else block.
5:53 Pause the video here, try out the following exercise and resume the video.
5:57 Given a number, num. Write an if else block to print num, as is,
6:05 if it is divisible by 10, else print 10 into num.
6:12 The solution is on your screen.
6:14 if num modulo 10 == 0 colon

print num

     else colon

print 10 star num

6:29 In addition to these conditional statements, Python provides a very convenient ternary conditional operator.
6:35 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.
6:43 The marks can be in the range of 0 to 100 or 'AA' if the student is absent.
6:51 In such a case, to obtain the marks as an integer, we can use the ternary conditional operator.
6:58 Let us say the string score is stored in score underscore str variable
7:05 So type score underscore str = in single quotes AA
7:13 Now let us use the ternary conditional operator
7:19 Type score = int within bracket score underscore str if score underscore str exclamation = in single quotes AA else 0
7:54 Given a number, num. Write a ternary operator to print num, as is,
8:01 if it is divisible by 10, else print 10 star num.
8:08 The solution is on your screen.
8:11 print num if num modulo 10 == 0 else 10 star num
8:19 Moving on, there are certain situations where we will have no operations or statements within a block of code.
8:27 For example, we have a code where we are waiting for the keyboard input.
8:32 If the user enters "c", "d" or "x" as the input, we would perform some operation; nothing otherwise.
8:42 In such cases "pass" statement comes very handy
8:49 a = raw underscore input Enter 'c' to calculate and exit, 'd' to display the existing
9:04 results exit and 'x' to exit and any other key to continue
9:12 if a == in single quote c colon
9:18 elif a == in single quote d colon
9:23 elif a == in single quote x colon
9:29 else colon
9:32 pass
9:35 In this case "pass" statement acts as a place holder for the block of code.
9:41 It is equivalent to a null operation.
9:45 It literally does nothing.
9:46 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.
9:56 This brings us to the end of the tutorial.
9:59 In this tutorial, we have learnt to, 1. Understand the conditional statements in Python.
10:04 2. Use if/else statement.
10:06 3. Use if/elif/else statement.
10:09 4. Apply the ternary conditional statement - C if X else Y.
10:14 5. Use "pass" statement.
10:18 Here are some self assessment questions for you to solve
10:21 1. Use conditional statements for the following.
10:23 Given a variable time , print Good Morning if it is less than 12, otherwise print Hello .
10:30 2. Convert the if else ladder below into a ternary conditional statement.
10:39 x = 20
 if x greater than 10 colon
  print x multiply by 100
 else colon
  print x
10:48 And now look at the answers,
10:52 1. We can use the if/else statements as
  if time less than 12 colon 
  print in double quotes Good Morning
 else colon
  print in double quotes Hello
11:06 1. The if else ladder can be converted to a ternary conditional statement as
  print x multiply by 100 if x greater than 10 else x
11:17 Hope you have enjoyed this tutorial and found it useful.
11:20 Thank you!

Contributors and Content Editors

Gaurav, Minal, PoojaMoolya, Sneha