summaryrefslogtreecommitdiff
path: root/conditionals/script.rst
diff options
context:
space:
mode:
Diffstat (limited to 'conditionals/script.rst')
-rw-r--r--conditionals/script.rst18
1 files changed, 16 insertions, 2 deletions
diff --git a/conditionals/script.rst b/conditionals/script.rst
index c59f1f8..156102b 100644
--- a/conditionals/script.rst
+++ b/conditionals/script.rst
@@ -126,10 +126,19 @@ is no else block.
Following is an exercise that you must do.
-%% %%
+%% %% Given a number, num. Write an if else block to print num, as is,
+ if it is divisible by 10, else print 10 * num.
Please, pause the video here. Do the exercise and then continue.
+::
+
+ if num%10 == 0:
+ print num
+ else:
+ print 10*num
+
+
In addition to these conditional statements, Python provides a very
convenient ternary conditional operator. Let us take the following
example where we read the marks data from a data file which is
@@ -155,10 +164,15 @@ absent for the exam 0.
Following is an exercise that you must do.
-%% %%
+%% %% Given a number, num. Write a ternary operator to print num, as is,
+ if it is divisible by 10, else print 10 * num.
Please, pause the video here. Do the exercise and then continue.
+::
+
+ print num if num%10 == 0 else 10*num
+
Moving on, there are certain situations where we will have no
operations or statements within a block of code. For example, we have
a code where we are waiting for the keyboard input. If the user enters