Python/C3/Conditionals/English-timed

From Script | Spoken-Tutorial
Jump to: navigation, search
Time Narration
00:01 Hello friends and Welcome to the tutorial on 'Conditionals'.
00:05 At the end of this tutorial, you will be able to,

Use if/else blocks. Use if/elif/else blocks. Use the Ternary conditional statement - C if X else Y.

00:25 To begin with let us start ipython,
00:29 So type ipython in the terminal
00:36 Whenever we have two possible states that can occur depending on a a certain condition, we can use if/else construct in Python.
00: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.
00:52 Let's say the value of a is 5.
00:55 So type a=5
00:59 In such a case we can write the if/else block as
01:04 In command line if a percentage 2 == 0 colon

print in double quotes Even else colon print in double quotes Odd

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

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

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

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

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

print num else colon print 10 star num

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