Difference between revisions of "Python/C3/Conditionals/English-timed"

From Script | Spoken-Tutorial
Jump to: navigation, search
 
(3 intermediate revisions by 2 users not shown)
Line 1: Line 1:
 
{| border=1
 
{| border=1
!Visual Cue
+
!Time
 
!Narration
 
!Narration
 
|-
 
|-
| 0:01
+
| 00:01
 
| Hello friends and Welcome to the tutorial on 'Conditionals'.
 
| Hello friends and Welcome to the tutorial on 'Conditionals'.
  
 
|-
 
|-
| 0:05  
+
| 00:05  
 
| At the end of this tutorial, you will be able to,
 
| At the end of this tutorial, you will be able to,
 
+
Use if/else blocks.
# Use if/else blocks.
+
Use if/elif/else blocks.
# Use if/elif/else blocks.
+
Use the Ternary conditional statement - C if X else Y.
# Use the Ternary conditional statement - C if X else Y.
+
  
 
|-
 
|-
| 0:25
+
| 00:25
 
| To begin with let us start ipython,
 
| To begin with let us start ipython,
  
 
|-
 
|-
|0:29
+
|00:29
 
|So type ipython in the terminal
 
|So type ipython in the terminal
  
 
|-
 
|-
| 0:36
+
| 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.
 
| 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
+
| 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.  
 
| 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
+
| 00:52
 
| Let's say the value of a is 5.
 
| Let's say the value of a is 5.
  
 
|-
 
|-
|0:55
+
|00:55
 
|So type a=5
 
|So type a=5
  
 
|-
 
|-
| 0:59
+
| 00:59
 
| In such a case we can write the if/else block as
 
| In such a case we can write the if/else block as
  
 
|-
 
|-
|1:04
+
|01:04
 
|In command line if a percentage 2 == 0 colon
 
|In command line if a percentage 2 == 0 colon
 
+
print in double quotes Even
    print in double quotes Even
+
else colon
  else colon
+
print in double quotes Odd
    print in double quotes Odd
+
  
 
|-
 
|-
| 1:37
+
| 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".
 
| 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
+
|01:51
 
| Note that in such a case, only one of the two blocks gets executed depending on whether the condition is  True  or <tt> False.
 
| Note that in such a case, only one of the two blocks gets executed depending on whether the condition is  True  or <tt> False.
  
 
|-
 
|-
|1:58
+
|01:58
 
| There is a very important syntactic element to understand here.  
 
| There is a very important syntactic element to understand here.  
  
 
|-
 
|-
|2:03
+
|02:03
 
| Every code block begins with a line that ends with a  colon, in this example the if  and the  else  lines.  
 
| Every code block begins with a line that ends with a  colon, in this example the if  and the  else  lines.  
  
 
|-
 
|-
|2:15
+
|02:15
 
| Also, all the statements inside a code block are intended by 4 spaces.  
 
| Also, all the statements inside a code block are intended by 4 spaces.  
  
 
|-
 
|-
|2:21
+
|02:21
 
| Hitting enter twice, ends the code block.
 
| Hitting enter twice, ends the code block.
  
 
|-
 
|-
|2:25
+
|02:25
 
| The if/else blocks work for a condition, which can take one of two states.  
 
| The if/else blocks work for a condition, which can take one of two states.  
  
 
|-
 
|-
|2:31
+
|02:31
 
| But what do we do for conditions, which can take more than two states?
 
| But what do we do for conditions, which can take more than two states?
  
 
|-
 
|-
2:38
+
02:38
 
| Python provides if/elif/else blocks, for such conditions.  
 
| Python provides if/elif/else blocks, for such conditions.  
  
 
|-
 
|-
| 2:47
+
| 02:47
 
| For example.  
 
| For example.  
  
 
|-
 
|-
| 2:49
+
| 02:49
 
| We have a variable  a  which holds integer values.  
 
| We have a variable  a  which holds integer values.  
  
 
|-
 
|-
|2:52
+
|02:52
 
| We need to print "positive" if  a  is positive, "negative" if it is negative or "zero" if it is 0.
 
| We need to print "positive" if  a  is positive, "negative" if it is negative or "zero" if it is 0.
  
 
|-
 
|-
|3:04
+
|03:04
 
| Let us use if/elif/else ladder for it.  
 
| Let us use if/elif/else ladder for it.  
  
 
|-
 
|-
|3:09
+
|03:09
 
| For the purposes of testing our code let us assume that the value of a is -3
 
| For the purposes of testing our code let us assume that the value of a is -3
  
 
|-
 
|-
|3:16
+
|03:16
 
|Type a = -3
 
|Type a = -3
 
 
if a greater than 0 colon
 
if a greater than 0 colon
print within double quotes positive
+
print within double quotes positive
 
elif a less than 0 colon
 
elif a less than 0 colon
print in double quotes negative
+
print in double quotes negative
else colon
+
else colon
    print within double quotes zero
+
print within double quotes zero
  
 
|-
 
|-
| 4:17
+
| 04:17
 
| All the syntax and rules as said for if/else statements hold the same.  
 
| All the syntax and rules as said for if/else statements hold the same.  
  
 
|-
 
|-
| 4:24
+
| 04:24
 
| The only addition here is the  elif  statement which can have another condition of its own.
 
| The only addition here is the  elif  statement which can have another condition of its own.
  
 
|-
 
|-
| 4:30
+
| 04:30
 
| Here too, exactly one block of code is executed -- the block of code which first evaluates to  True .  
 
| Here too, exactly one block of code is executed -- the block of code which first evaluates to  True .  
  
 
|-
 
|-
| 4:41
+
| 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.  
 
| 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
+
| 04:51
 
| Consequently, the else block gets executed if and only if all the conditions evaluate to False.
 
| Consequently, the else block gets executed if and only if all the conditions evaluate to False.
  
 
|-
 
|-
| 4:59
+
| 04:59
 
| Also, the else  block in both if/else statement and if/elif/else is optional.  
 
| Also, the else  block in both if/else statement and if/elif/else is optional.  
  
 
|-
 
|-
| 5:08
+
| 05:08
 
| We can have a single if statement or just if/elif statements without having else block at all.  
 
| We can have a single if statement or just if/elif statements without having else block at all.  
  
 
|-
 
|-
| 5:17
+
| 05:17
 
| Also, there can be any number of elif's within an if/elif/else ladder.  
 
| Also, there can be any number of elif's within an if/elif/else ladder.  
  
 
|-
 
|-
| 5:26
+
| 05:26
 
| For example
 
| For example
  
 
|-
 
|-
|5:28
+
|05:28
 
|Type if user == in single quotes admin colon
 
|Type if user == in single quotes admin colon
elif user == in single quotes moderator colon
+
elif user == in single quotes moderator colon
elif user == in single quotes client colon  
+
elif user == in single quotes client colon  
  
 
|-
 
|-
| 5:47
+
| 05:47
 
| Note that there are multiple elif blocks and there is no else block.
 
| Note that there are multiple elif blocks and there is no else block.
  
 
|-
 
|-
| 5:53
+
| 05:53
 
| Pause the video here, try out the following exercise and resume the video.
 
| Pause the video here, try out the following exercise and resume the video.
  
 
|-
 
|-
5:57
+
05:57
 
| '''Given a number, num. Write an if else block to print num, as is,'''
 
| '''Given a number, num. Write an if else block to print num, as is,'''
  
 
|-
 
|-
| 6:05
+
|06:05
 
| if it is divisible by 10, else print 10 into num.
 
| if it is divisible by 10, else print 10 into num.
  
 
|-
 
|-
| 6:12
+
| 06:12
 
| The solution is on your screen.
 
| The solution is on your screen.
  
 
|-
 
|-
|6:14
+
|06:14
 
|if num modulo 10 == 0 colon
 
|if num modulo 10 == 0 colon
print num
+
print num
      else colon
+
else colon
print 10 star num  
+
print 10 star num  
  
 
|-
 
|-
| 6:29
+
| 06:29
 
| In addition to these conditional statements, Python provides a very convenient ternary conditional operator.  
 
| In addition to these conditional statements, Python provides a very convenient ternary conditional operator.  
  
 
|-
 
|-
| 6:35
+
| 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.  
 
| 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
+
| 06:43
 
| The marks can be in the range of 0 to 100 or 'AA' if the student is absent.  
 
| The marks can be in the range of 0 to 100 or 'AA' if the student is absent.  
  
 
|-
 
|-
| 6:51
+
| 06:51
 
| In such a case, to obtain the marks as an integer, we can use the ternary conditional operator.  
 
| In such a case, to obtain the marks as an integer, we can use the ternary conditional operator.  
  
 
|-
 
|-
| 6:58
+
| 06:58
 
| Let us say the string score is stored in score underscore str variable
 
| Let us say the string score is stored in score underscore str variable
  
 
|-
 
|-
|7:05
+
|07:05
 
|So type score underscore str = in single quotes AA  
 
|So type score underscore str = in single quotes AA  
  
 
|-
 
|-
| 7:13
+
| 07:13
 
| Now let us use the ternary conditional operator
 
| Now let us use the ternary conditional operator
  
 
|-
 
|-
|7:19
+
|07:19
 
|Type score = int within bracket score underscore str if score underscore str exclamation = in single quotes AA else 0  
 
|Type score = int within bracket score underscore str if score underscore str exclamation = in single quotes AA else 0  
  
 
|-
 
|-
| 7:54
+
| 07:54
 
| '''Given a number, num. Write a ternary operator to print num, as is,'''
 
| '''Given a number, num. Write a ternary operator to print num, as is,'''
  
 
|-
 
|-
| 8:01
+
| 08:01
 
| if it is divisible by 10, else print 10 star num.
 
| if it is divisible by 10, else print 10 star num.
  
 
|-
 
|-
| 8:08
+
| 08:08
 
| The solution is on your screen.
 
| The solution is on your screen.
  
 
|-
 
|-
| 8:11
+
| 08:11
 
| print num if num modulo 10 == 0 else 10 star num
 
| print num if num modulo 10 == 0 else 10 star num
  
 
|-
 
|-
| 8:19
+
| 08:19
 
| Moving on, there are certain situations where we will have no operations or statements within a block of code.  
 
| Moving on, there are certain situations where we will have no operations or statements within a block of code.  
  
 
|-
 
|-
| 8:27
+
| 08:27
 
| For example, we have a code where we are waiting for the keyboard input.  
 
| For example, we have a code where we are waiting for the keyboard input.  
  
 
|-
 
|-
| 8:32
+
| 08:32
 
| If the user enters "c", "d" or "x" as the input, we would perform some operation; nothing otherwise.  
 
| If the user enters "c", "d" or "x" as the input, we would perform some operation; nothing otherwise.  
  
 
|-
 
|-
| 8:42
+
| 08:42
 
| In such cases "pass" statement comes very handy
 
| In such cases "pass" statement comes very handy
  
 
|-
 
|-
| 8:49
+
| 08:49
 
| '''a = raw underscore input Enter 'c' to calculate and exit, 'd' to display the existing'''
 
| '''a = raw underscore input Enter 'c' to calculate and exit, 'd' to display the existing'''
  
 
|-
 
|-
| 9:04
+
| 09:04
 
| results exit and 'x' to exit and any other key to continue  
 
| results exit and 'x' to exit and any other key to continue  
 
|-
 
|-
| 9:12
+
| 09:12
 
| '''if a == in single quote c colon'''
 
| '''if a == in single quote c colon'''
  
 
|-
 
|-
| 9:18
+
| 09:18
 
| '''elif a == in single quote d colon'''
 
| '''elif a == in single quote d colon'''
  
 
|-
 
|-
| 9:23
+
| 09:23
 
| '''elif a == in single quote x colon'''
 
| '''elif a == in single quote x colon'''
  
 
|-
 
|-
| 9:29
+
| 09:29
 
| '''else colon'''
 
| '''else colon'''
  
 
|-
 
|-
| 9:32
+
| 09:32
 
| pass
 
| pass
  
 
|-
 
|-
| 9:35
+
| 09:35
 
| In this case "pass" statement acts as a place holder for the block of code.  
 
| In this case "pass" statement acts as a place holder for the block of code.  
  
 
|-
 
|-
| 9:41
+
| 09:41
 
| It is equivalent to a null operation.  
 
| It is equivalent to a null operation.  
  
 
|-
 
|-
| 9:45
+
| 09:45
| It literally does nothing.  
+
| 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.
 
+
|-
+
| 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
+
| 09:56
 
| This brings us to the end of the tutorial.  
 
| This brings us to the end of the tutorial.  
  
 
|-
 
|-
| 9:59
+
| 09:59
| In this tutorial, we have learnt to, 1. Understand the conditional statements in Python.
+
| In this tutorial, we have learnt to, Understand the conditional statements in Python.
  
 
|-
 
|-
 
| 10:04
 
| 10:04
| 2. Use if/else statement.
+
|Use if/else statement.
  
 
|-
 
|-
 
| 10:06
 
| 10:06
| 3. Use if/elif/else statement.
+
| Use if/elif/else statement.
  
 
|-
 
|-
 
| 10:09
 
| 10:09
| 4. Apply the ternary conditional statement - C if X else Y.
+
| Apply the ternary conditional statement - C if X else Y.
  
 
|-
 
|-
 
| 10:14
 
| 10:14
| 5. Use "pass" statement.
+
| Use "pass" statement.
  
 
|-
 
|-
Line 323: Line 316:
 
|-
 
|-
 
| 10:21
 
| 10:21
| 1. Use conditional statements for the following.  
+
| Use conditional statements for the following.  
  
 
|-
 
|-
Line 331: Line 324:
 
|-
 
|-
 
| 10:30
 
| 10:30
| 2. Convert the if else ladder below into a ternary conditional statement.
+
| Convert the if else ladder below into a ternary conditional statement.
  
 
|-
 
|-
 
| 10:39
 
| 10:39
 
| x = 20
 
| x = 20
  if x greater than 10 colon
+
if x greater than 10 colon
  print x multiply by 100
+
print x multiply by 100
  else colon
+
else colon
  print x
+
print x
  
 
|-
 
|-
Line 347: Line 340:
 
|-
 
|-
 
| 10:52
 
| 10:52
| 1. We can use the if/else statements as
+
| We can use the if/else statements as
  if time less than 12 colon  
+
if time less than 12 colon  
  print in double quotes Good Morning
+
print in double quotes Good Morning
  else colon
+
else colon
  print in double quotes Hello
+
print in double quotes Hello
  
 
|-
 
|-
 
| 11:06
 
| 11:06
| 1. The if else ladder can be converted to a ternary conditional statement as
+
|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
  print x multiply by 100 if x greater than 10 else x
+
  
 
|-
 
|-

Latest revision as of 12:20, 27 March 2017

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