Difference between revisions of "Python-3.4.3/C3/Conditional-Statements/English"

From Script | Spoken-Tutorial
Jump to: navigation, search
(Created page with " {| style="border-spacing:0;" | style="background-color:#ffffff;border-top:0.5pt solid #000001;border-bottom:0.5pt solid #000001;border-left:0.5pt solid #000001;border-right:n...")
 
Line 21: Line 21:
 
* '''Ternary''' '''conditional''' '''statement '''and
 
* '''Ternary''' '''conditional''' '''statement '''and
 
* '''Pass statement'''
 
* '''Pass statement'''
 
 
  
 
|-
 
|-
Line 29: Line 27:
 
System Specifications
 
System Specifications
 
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.092cm;padding-right:0.191cm;"| To record this tutorial, I am using
 
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.092cm;padding-right:0.191cm;"| To record this tutorial, I am using
 
  
 
* '''Ubuntu Linux 16.04''' operating system
 
* '''Ubuntu Linux 16.04''' operating system
 
* '''Python 3.4.3'''
 
* '''Python 3.4.3'''
 
* '''IPython 5.1.0'''
 
* '''IPython 5.1.0'''
 
 
  
 
|-
 
|-
Line 45: Line 40:
  
 
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.092cm;padding-right:0.191cm;"| To practice this tutorial, you should know how to use
 
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.092cm;padding-right:0.191cm;"| To practice this tutorial, you should know how to use
 
  
 
* basic''' data types '''and '''operators '''
 
* basic''' data types '''and '''operators '''
 
* '''strings '''and '''Input/Output statements'''
 
* '''strings '''and '''Input/Output statements'''
  
If not, see the relevant Python tutorials on this website.
+
If not, see the relevant '''Python''' tutorials on this website.
  
 
|-
 
|-
Line 63: Line 57:
  
  
'''if condition '''is used to decide whether to execute the statements in the '''if block '''or not.
+
'''if condition '''is used to decide whether to execute the '''statements''' in the '''if block '''or not.
  
  
Line 89: Line 83:
  
  
Note that, '''if''' and '''else''' statements end with a colon.
+
Note that, '''if''' and '''else statements''' end with a '''colon'''.
  
  
Line 132: Line 126:
  
  
Highlight the output
 
  
 
HIghlight the spaces
 
 
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.092cm;padding-right:0.191cm;"| Type, '''if num '''''percentage''''' 2 '''''equal to equal to '''''0 '''''colon''
 
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.092cm;padding-right:0.191cm;"| Type, '''if num '''''percentage''''' 2 '''''equal to equal to '''''0 '''''colon''
  
Line 145: Line 136:
 
'''print '''''inside brackets inside single quotes '''''Odd'''
 
'''print '''''inside brackets inside single quotes '''''Odd'''
  
 +
|-
 +
| style="background-color:#ffffff;border-top:0.5pt solid #000001;border-bottom:0.5pt solid #000001;border-left:0.5pt solid #000001;border-right:none;padding-top:0cm;padding-bottom:0cm;padding-left:0.092cm;padding-right:0.191cm;"| Highlight the output
  
Press''' Enter '''twice to get the output.
 
  
 +
Highlight the spaces
 +
 +
 +
Press''' Enter '''twice to get the output.
  
 +
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.092cm;padding-right:0.191cm;"|
 
As expected, it displays '''Even''' as output.
 
As expected, it displays '''Even''' as output.
  
Line 196: Line 193:
 
|-
 
|-
 
| style="background-color:#ffffff;border-top:0.5pt solid #000001;border-bottom:0.5pt solid #000001;border-left:0.5pt solid #000001;border-right:none;padding-top:0cm;padding-bottom:0cm;padding-left:0.092cm;padding-right:0.191cm;"| Type '''a = -3'''
 
| style="background-color:#ffffff;border-top:0.5pt solid #000001;border-bottom:0.5pt solid #000001;border-left:0.5pt solid #000001;border-right:none;padding-top:0cm;padding-bottom:0cm;padding-left:0.092cm;padding-right:0.191cm;"| Type '''a = -3'''
 +
 +
 +
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.092cm;padding-right:0.191cm;"| Let us understand '''if/elif/else ladder '''with an example.
 +
 +
 +
Type, '''a '''''is equal to minus''''' 3'''
 +
 +
 +
We have a variable''' a''' which holds '''integer''' value as -3.
 +
 +
|-
 +
| style="background-color:#ffffff;border-top:0.5pt solid #000001;border-bottom:0.5pt solid #000001;border-left:0.5pt solid #000001;border-right:none;padding-top:0cm;padding-bottom:0cm;padding-left:0.092cm;padding-right:0.191cm;"| Type
  
 
'''if a > 0:'''
 
'''if a > 0:'''
Line 215: Line 224:
  
 
Highlight '''zero'''
 
Highlight '''zero'''
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.092cm;padding-right:0.191cm;"| Let us understand '''if/elif/else ladder '''with an example.
+
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.092cm;padding-right:0.191cm;"|Type as shown.
 
+
 
+
Type, '''a '''''is equal to minus''''' 3'''
+
 
+
 
+
We have a variable''' a''' which holds '''integer''' value as -3.
+
 
+
 
+
Type as shown.
+
  
  
Line 233: Line 233:
 
* '''"zero"''' if it is''' 0'''.
 
* '''"zero"''' if it is''' 0'''.
  
Press '''Enter''' twice to see the output.
+
|-
 +
| style="background-color:#ffffff;border-top:0.5pt solid #000001;border-bottom:0.5pt solid #000001;border-left:0.5pt solid #000001;border-right:none;padding-top:0cm;padding-bottom:0cm;padding-left:0.092cm;padding-right:0.191cm;"| Press Enter twice
 +
 
 +
Point to the output
 +
 
 +
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.092cm;padding-right:0.191cm;"|Press '''Enter''' twice to see the output.
  
 
|-
 
|-
Line 281: Line 286:
  
  
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.092cm;padding-right:0.191cm;"| Next let us learn about '''ternary''' operator.
+
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.092cm;padding-right:0.191cm;"| Next let us learn about '''ternary operator'''.
  
  
Ternary operator allows to test a condition in a single line replacing the multiline''' if-else'''.  
+
'''Ternary operator''' allows to test a condition in a single line replacing the multiline''' if-else'''.  
  
  
Line 346: Line 351:
 
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.092cm;padding-right:0.191cm;"| Next we will learn about '''pass statement'''.
 
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.092cm;padding-right:0.191cm;"| Next we will learn about '''pass statement'''.
  
The '''pass '''statement acts as a '''''null ''operation''' (i.e) nothing happens when it executes.
+
The '''pass statement''' acts as a '''''null ''operation''' (i.e) nothing happens when it executes.
  
  
Line 356: Line 361:
 
|-
 
|-
 
| style="background-color:#ffffff;border-top:0.5pt solid #000001;border-bottom:0.5pt solid #000001;border-left:0.5pt solid #000001;border-right:none;padding-top:0cm;padding-bottom:0cm;padding-left:0.092cm;padding-right:0.191cm;"| Type '''a = 11'''
 
| style="background-color:#ffffff;border-top:0.5pt solid #000001;border-bottom:0.5pt solid #000001;border-left:0.5pt solid #000001;border-right:none;padding-top:0cm;padding-bottom:0cm;padding-left:0.092cm;padding-right:0.191cm;"| Type '''a = 11'''
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.092cm;padding-right:0.191cm;"| Let us understand the '''pass''' statement with an example.
+
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.092cm;padding-right:0.191cm;"| Let us understand the '''pass statement''' with an example.
  
  
Line 372: Line 377:
 
Else no operation is performed.
 
Else no operation is performed.
  
 
+
|-
Press '''Enter''' key twice to get the output.
+
| style="background-color:#ffffff;border-top:0.5pt solid #000001;border-bottom:0.5pt solid #000001;border-left:0.5pt solid #000001;border-right:none;padding-top:0cm;padding-bottom:0cm;padding-left:0.092cm;padding-right:0.191cm;"| Press '''Enter''' twice
 +
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.092cm;padding-right:0.191cm;"|Press '''Enter''' key twice to get the output.
  
  
Line 428: Line 434:
 
# The following code gives the required output.
 
# The following code gives the required output.
 
# Use the following statement to convert the '''if else''' code into a '''ternary''' statement
 
# Use the following statement to convert the '''if else''' code into a '''ternary''' statement
 
 
  
 
|-
 
|-

Revision as of 07:22, 8 June 2018

Visual Cue
Narration
Show Slide title Welcome to the spoken tutorial on Conditional statements.
Show Slide

Objectives


In this tutorial, we will learn to use
  • if/else blocks
  • if/elif/else blocks
  • Ternary conditional statement and
  • Pass statement
Show Slide

System Specifications

To record this tutorial, I am using
  • Ubuntu Linux 16.04 operating system
  • Python 3.4.3
  • IPython 5.1.0
Show Slide

Pre-requisite


To practice this tutorial, you should know how to use
  • basic data types and operators
  • strings and Input/Output statements

If not, see the relevant Python tutorials on this website.

Show Slide


if condition


First let us learn about if condition.


if condition is used to decide whether to execute the statements in the if block or not.


Here is the syntax.

Show Slide

if else condition

Box line by line


Highlight :


Highlight 4 spaces

Next we will see about if else condition.


When the test condition is true, it will execute the body of if.


If the condition is False, body of the else is executed.


Note that, if and else statements end with a colon.


This denotes the beginning of the code block of True or False condition.


All the statements inside a code block are indented by 4 spaces.

Open the terminal Let us start ipython.


Open the terminal.

Type ipython3

press Enter

Type ipython3 and press Enter.


From here onwards, remember to press the Enter key after typing every command on the terminal.

Type: num = int(input())


Enter number 4 and press Enter

Now let us check if an entered number is even or not.


Type, num is equal to int inside brackets input open and close brackets


Enter 4 and press Enter

Type

if num % 2 == 0: print ('Even')else: print ('Odd')


Type, if num percentage 2 equal to equal to 0 colon

print inside brackets inside single quotes Even


Press backspace four times and type, else colon

print inside brackets inside single quotes Odd

Highlight the output


Highlight the spaces


Press Enter twice to get the output.

As expected, it displays Even as output.


Note that, the statements inside a code block are indented by 4 spaces automatically.


<<PAUSE>>

Show Slide


elif statement


Next we will see another conditional statement called elif.


All the syntax and rules for elif are same as if/else statements.


In if/else, only one condition is checked.


But in elif statement multiple conditions can be checked.

Show Slide


if/elif Example


Box line by line

For example,

The first block of code is executed as soon as one of the conditions evaluates to True.


All the subsequent conditions in elif ladder are not checked.


The else block gets executed if all the conditions in elif ladder are evaluated to False.

Type a = -3


Let us understand if/elif/else ladder with an example.


Type, a is equal to minus 3


We have a variable a which holds integer value as -3.

Type

if a > 0:

print ("positive")

elif a < 0:

print ("negative")

else:

print ("zero")


Highlight positive

Highlight negative

Highlight zero

Type as shown.


This program will print

  • "positive" if a is positive,
  • "negative" if it is negative or
  • "zero" if it is 0.
Press Enter twice

Point to the output

Press Enter twice to see the output.
Show Slide if/elif example

Highlight elif

We can also have any number of elif conditions within an if/elif/else statements.


For example, based on the type of user the corresponding operations are performed using elif.


Note that else block is optional in both if/else and if/elif/else statements.

Pause the video.


Try this exercise and then resume the video.

Show Slide

Assignment 1

Given a number, num as input.


Write an if else block to print num, if it is divisible by 10, else print 10 asterisk num.

Show Slide Solution 1 The solution is on your screen.


<<PAUSE>>

Slide:

Ternary operator

syntax:

[A] if <condition> else [B]


Next let us learn about ternary operator.


Ternary operator allows to test a condition in a single line replacing the multiline if-else.


It can reduce the code size and increase the readability of the code.


Here is the syntax.

Type, a = 4 Let us write a ternary conditional statement to find the largest of two numbers.


Type, a is equal to 4

Type, b = 2 b is equal to 2
Type, large_num = a if a > b else b large underscore num is equal to a if a greater than b else b
Type, print(large_num) print inside brackets large underscore num


We got the required output.

Pause the video.


Try this exercise and then resume the video.

Show Slide

Assignment 2

Given a number, n as input.


Write a ternary operator to print n, if it is divisible by 10, else print 10 asterisk n.

Show Slide Solution 2 The solution is on your screen.


<<PAUSE>>

Show Slide

Pass statement


Next we will learn about pass statement.

The pass statement acts as a null operation (i.e) nothing happens when it executes.


It works as a placeholder for a block of code.


It is used in a code block where actual code implementation is not known yet.

Type a = 11 Let us understand the pass statement with an example.


Type a is equal to 11

Type,

if a%2 == 0: print(‘even’)else: pass

Then type as shown.


If a divided by 2 equals to zero, if block is executed.

Else no operation is performed.

Press Enter twice Press Enter key twice to get the output.


Here a divided by 2 is not 0.


So no operation is performed and nothing is displayed as output.

Show Slide

Summary


This brings us to the end of the tutorial.


Let us summarize.


In this tutorial, we have learnt the conditional statements such as,

  • if/else statement
  • if/elif/else statement
  • Ternary conditional statement and
  • Pass statement

<<PAUSE>>

Show Slide

Evaluation


Here are some self assessment questions for you to solve.


1. Given a variable t.

Print Good Morning if it is less than 12, otherwise print Hello. Use if else statement


2.Convert the below if else code into ternary conditional statement.

Show Slide

Solutions

And the answers,
  1. The following code gives the required output.
  2. Use the following statement to convert the if else code into a ternary statement
Show Slide Forum Please post your timed queries in this forum.
Show Slide Fossee Forum Please post your general queries on Python in this forum.
Show Slide Textbook Companion FOSSEE team coordinates the TBC project.
Show Slide

Acknowledgment

http://spoken-tutorial.org

Spoken Tutorial Project is funded by NMEICT, MHRD, Govt. of India.

For more details, visit this website.

Previous slide This is Priya from IIT Bombay signing off. Thanks for watching.

Contributors and Content Editors

Nancyvarkey, Priyacst