Python-3.4.3/C3/Conditional-Statements/English-timed

From Script | Spoken-Tutorial
Revision as of 17:28, 3 June 2019 by PoojaMoolya (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search
Time
Narration
00:01 Welcome to the spoken tutorial on Conditional statements.
00:06 In this tutorial, we will learn to use

if/else blocks

00:13 if/elif/else blocks

Ternary conditional statement and Pass statement

00:22 To record this tutorial, I am using

Ubuntu Linux 16.04 operating system

00:30 Python 3.4.3

and IPython 5.1.0

00:37 To practice this tutorial, you should know how to use

basic data types and operators

00:46 strings and Input/Output statements
00:50 If not, see the relevant Python tutorials on this website.
00:55 First let us learn about if condition.
00:59 if condition is used to decide whether to execute the statements inside the if block or not.

Here is the syntax.

01:10 Next we will see about if else condition.
01:14 When the test condition is true, it will execute the body of if.
01:20 If the condition is False, body of the else is executed.
01:25 Note that, if and else statements end with a colon.
01:30 This denotes the beginning of the code block of True or False condition.
01:36 All the statements inside a code block are indented by 4 spaces.
01:42 Let us start ipython.

Open the terminal.

01:48 Type ipython3 and press Enter.
01:53 From here onwards, remember to press the Enter key after typing every command on the terminal.
02:00 Now let us check if an entered number is even or not.
02:05 Type, num is equal to int inside brackets input open and close brackets
02:14 Enter 4 and press Enter
02:18 Type, if num percentage 2 equal to equal to 0 colon
02:26 print inside brackets inside single quotes Even
02:32 Press backspace four times and type, else colon
0002:39 print inside brackets inside single quotes Odd
02:44 Press Enter twice to get the output.

As expected, it displays Even as output.

02:53 Note that, the statements inside a code block are indented by 4 spaces automatically.
03:00 Next we will see another conditional statement called elif.
03:05 All the syntax and rules for elif are same as if/else statements.
03:11 In if/else, only one condition is checked.
03:15 But in elif statement multiple conditions can be checked.
03:20 For example,

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

03:28 All the subsequent conditions in elif ladder are not checked.
03:33 The else block gets executed if all the conditions in if and elif ladder are evaluated to False.
03:42 Let us understand if/elif/else ladder with an example.
03:48 Type, a is equal to minus 3

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

03:58 Type as shown.

This program will print "positive" if a is positive,

04:06 "negative" if it is negative or "zero" if the above conditions are evaluated to False
04:15 Press Enter twice to see the output.
04:20 We can also have any number of elif conditions within an if/elif/else statements.
04:27 For example, based on the type of user the corresponding operations are performed using elif.
04:35 Pause the video.

Try this exercise and then resume the video.

04:41 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.

04:53 The solution is on your screen.
04:57 Next let us learn about ternary operator.
05:01 Ternary operator allows to test a condition in a single line replacing the multiline if-else.
05:08

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

Here is the syntax.

05:16 Let us write a ternary conditional statement to find the largest of two numbers.
05:23 Type, a is equal to 4
05:26 b is equal to 2
05:30 large underscore num is equal to a if a greater than b else b
05:39 print inside brackets large underscore num
05:44 We got the required output.
05:47 Pause the video.

Try this exercise and then resume the video.

05:53 Given a number, n as input.

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

06:05 The solution is on your screen.
06:09 Next we will learn about pass statement.
06:13 The pass statement acts as a null operation (i.e) nothing happens when it executes.
06:21 It works as a placeholder for a block of code.
06:25 It is used in a code block where actual code implementation is not known yet.
06:32 Let us understand the pass statement with an example.
06:37 Type a is equal to 11
06:41 Then type as shown.
06:44 If a divided by 2 equals to zero, if block is executed.

Else no operation is performed.

06:54 Press Enter key twice to get the output.

Here a divided by 2 is not 0.

07:02 So no operation is performed and nothing is displayed as output.
07:08 This brings us to the end of the tutorial.

Let us summarize.

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

if/else statement

07:22 if/elif/else statement
07:25 Ternary conditional statement and Pass statement
07:30 Here are some self assessment questions for you to solve.
07:35 First. Given a variable t.
07:38 Print Good Morning if it is less than 12, otherwise print Hello. Use if else statement.
07:47 Second. Convert the below if else code into ternary conditional statement.
07:56 And the answers,

First. The following code gives the required output.

08:03 Second. Use the following statement to convert the if else code into a ternary statement
08:11 Please post your timed queries in this forum.
08:15 Please post your general queries on Python in this forum.
08:20 FOSSEE team coordinates the TBC project.
08:24 Spoken Tutorial Project is funded by NMEICT, MHRD, Govt. of India.

For more details, visit this website.

08:34 This is Priya from IIT Bombay signing off. Thanks for watching.

Contributors and Content Editors

PoojaMoolya