Difference between revisions of "Python-3.4.3/C3/Conditional-Statements/English"
Nancyvarkey (Talk | contribs) |
|||
Line 21: | Line 21: | ||
* '''Ternary''' '''conditional''' '''statement '''and | * '''Ternary''' '''conditional''' '''statement '''and | ||
* '''Pass statement''' | * '''Pass statement''' | ||
+ | |||
+ | |||
|- | |- | ||
Line 27: | Line 29: | ||
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 40: | Line 45: | ||
| 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 | + | If not, see the relevant Python tutorials on this website. |
|- | |- | ||
Line 57: | Line 63: | ||
− | '''if condition '''is used to decide whether to execute the | + | '''if condition '''is used to decide whether to execute the statements in the '''if block '''or not. |
Line 83: | Line 89: | ||
− | Note that, '''if''' and '''else | + | Note that, '''if''' and '''else''' statements end with a colon. |
Line 126: | Line 132: | ||
+ | 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 136: | Line 145: | ||
'''print '''''inside brackets inside single quotes '''''Odd''' | '''print '''''inside brackets inside single quotes '''''Odd''' | ||
− | |||
− | |||
+ | Press''' Enter '''twice to get the output. | ||
− | |||
− | |||
− | |||
− | |||
− | |||
As expected, it displays '''Even''' as output. | As expected, it displays '''Even''' as output. | ||
Line 183: | Line 186: | ||
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.092cm;padding-right:0.191cm;"| For example, | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.092cm;padding-right:0.191cm;"| For example, | ||
− | The first block of code is executed as soon as one | + | |
+ | The first block of code is executed as soon as condition one evaluates to '''True'''. | ||
Line 189: | Line 193: | ||
− | The '''else''' '''block''' gets executed if all the '''conditions''' in '''elif '''ladder are evaluated to '''False'''. | + | The '''else''' '''block''' gets executed if all the '''conditions''' in if and '''elif '''ladder are evaluated to '''False'''. |
|- | |- | ||
| 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''' | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
'''if a > 0:''' | '''if a > 0:''' | ||
Line 224: | Line 216: | ||
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;"|Type as shown. | + | | 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. | ||
+ | |||
+ | |||
+ | Type as shown. | ||
Line 231: | Line 232: | ||
* '''"positive"''' if '''a''' is '''positive''', | * '''"positive"''' if '''a''' is '''positive''', | ||
* "'''negative'''" if it is '''negative''' or | * "'''negative'''" if it is '''negative''' or | ||
− | * '''"zero"''' | + | * '''"zero"''' the above conditions are evaluated to '''False ''' |
− | + | Press '''Enter''' twice to see the output. | |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
|- | |- | ||
Line 248: | Line 244: | ||
For example, based on the type of user the corresponding operations are performed using '''elif'''. | For example, based on the type of user the corresponding operations are performed using '''elif'''. | ||
− | |||
− | |||
− | |||
|- | |- | ||
Line 286: | Line 279: | ||
− | | 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 | + | | 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'''. | |
Line 302: | Line 295: | ||
− | ''' | + | Type,''' a '''''is equal to''''' 4''' |
|- | |- | ||
Line 351: | Line 344: | ||
| 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 | + | The '''pass '''statement acts as a '''''null ''operation''' (i.e) nothing happens when it executes. |
Line 361: | Line 354: | ||
|- | |- | ||
| 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 | + | | 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 377: | Line 370: | ||
Else no operation is performed. | Else no operation is performed. | ||
− | + | ||
− | + | Press '''Enter''' key twice to get the output. | |
− | + | ||
Line 421: | Line 413: | ||
1. Given a '''variable t. ''' | 1. Given a '''variable t. ''' | ||
− | Print''' Good Morning '''if it is less than 12, otherwise print''' Hello'''. Use '''if else''' statement | + | Print''' Good Morning '''if it is less than 12, otherwise print''' Hello'''. Use '''if else''' statement. |
Line 434: | Line 426: | ||
# 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 | ||
+ | |||
+ | |||
|- | |- |
Latest revision as of 17:02, 6 August 2018
|
|
Show Slide title | Welcome to the spoken tutorial on Conditional statements. |
Show Slide
Objectives
|
In this tutorial, we will learn to use
|
Show Slide
System Specifications |
To record this tutorial, I am using
|
Show Slide
Pre-requisite
|
To practice this tutorial, you should know how to use
If not, see the relevant Python tutorials on this website. |
Show Slide
|
First let us learn about if condition.
|
Show Slide
if else condition Box line by line
|
Next we will see about if else condition.
|
Open the terminal | Let us start ipython.
|
Type ipython3
press Enter |
Type ipython3 and press Enter.
|
Type: num = int(input())
|
Now let us check if an entered number is even or not.
|
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
print inside brackets inside single quotes Odd
|
Show Slide
|
Next we will see another conditional statement called elif.
|
Show Slide
|
For example,
|
Type a = -3
if a > 0: print ("positive") elif a < 0: print ("negative") else: print ("zero")
Highlight negative Highlight zero |
Let us understand if/elif/else ladder with an example.
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.
|
Pause the video.
| |
Show Slide
Assignment 1 |
Given a number, num as input.
|
Show Slide Solution 1 | The solution is on your screen.
|
Slide:
Ternary operator syntax: [A] if <condition> else [B]
|
Next let us learn about ternary operator.
|
Type, a = 4 | Let us write a ternary conditional statement to find the largest of two numbers.
|
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
|
Pause the video.
| |
Show Slide
Assignment 2 |
Given a number, n as input.
|
Show Slide Solution 2 | The solution is on your screen.
|
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.
|
Type a = 11 | Let us understand the pass statement with an example.
|
Type,
if a%2 == 0: print(‘even’)else: pass |
Then type as shown.
Else no operation is performed.
|
Show Slide
Summary
|
This brings us to the end of the tutorial.
<<PAUSE>> |
Show Slide
Evaluation
|
Here are some self assessment questions for you to solve.
Print Good Morning if it is less than 12, otherwise print Hello. Use if else statement.
|
Show Slide
Solutions |
And the answers,
|
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 |
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. |