A statement is a technical term that describes various parts of the Python language. There are different kinds of statements in Python. We will be discussing some of the basic but important ones today.
CONDITIONAL STATEMENTS (if-else STATEMENTS):
Conditional statements are blocks of codes that make decisions by
analyzing the expressions. Sounds cool right?? Finally, you are going to create a
program that is going to make decisions on its own. We are now getting into the
“cool” side of programming so stay tuned for more.
We can use the arithmetic,
comparison and logical operators (if you haven’t learnt about them yet, you can
do so by reading these posts <link> <link>) to write conditional
statements that performs different actions based on how and expression
evaluates.
Let’s take a look at the pseudo code of conditional
statements. A pseudo code is a notation
resembling code used to explain an example. So.... the pseudo code for
condition statements is:
If(expression):
code_area_1
else:
code_area_2
Don’t worry I don’t expect you to understand the concept just
by looking at the pseudo code. Let me break it down for you:
In this code, the ‘if’ and the ‘else’ statements are the
conditional statements. In the ‘if’ statement, following the ‘if’ keyword you
will specify and expression which contain the condition to be satisfied for the
program to move forward. For example, if you have a variable ‘x’ and you use
the condition ‘if x==2’ then the program will only move ahead if the value of
‘x’ is ‘2’. If the value of ‘x’ is not ‘2’ then the program terminates or moves
on the next line of code completely ignoring the ‘if’ statement.
If the value of ‘x’ is ‘2’, the program will enter the ‘if’
statement and execute any command that you have entered in there. For example,
if the value of ‘x’ is ‘2’ and you have the condition:
if x==2:
print(x)
Then, the program will print ‘x’ because the condition is
satisfied. If ‘x’ is not equals to ‘2’, the program will terminate and not
return anything.
So, in conclusion the ‘if’ statement specifies the condition
that needs to be satisfied by the program to move ahead. ‘code_area_1’ is code
that you want the program to execute once the condition is satisfied. Hope you
have understood the ‘if’ portion of this code. If not, feel free to ask me in
the comments.
OK, let’s try to understand what the ‘else’ statement is
about. The ‘else statement is completely optional, if you don’t have an ‘else’
statement in your code the program will simply terminate without returning
anything. You use an ‘else’ statement to
tell the program what to do IF the ‘if’ condition is not satisfied. So
let’s take the above example to understand this as well. Suppose you have the
following code:
x=5
if x==2:
print(“x is
equal to 2”)
else:
print(“x is
not equal to 2”)
Here, the value of ‘x’ is ‘5’ so the ‘if’ condition is not
satisfied for obvious reasons (because the ‘if’ condition wants the value of
‘x’ to be ‘2’ to be satisfied) , so now you are basically telling Python what
to do if the value of ‘x’ is not ‘2’ with the help of the ‘else’ statement. In
the above program, the program will print “x is not equal to 2” because the
value of ‘x’ is ‘5’ and not ‘2’.
elif STATEMENTS:
These statements
basically specify another ‘if’ condition if the original ‘if’ condition is not
satisfied.
When using the if-else
statements you can only have 1 ‘if’ statement and 1 ‘else’ statement.
So the elif statements basically tackles this problem by
allowing you to have infinite number of conditions. For example,
if x%2==0:
print(“x is
even”)
elif x==0:
print(“x is
equal to 0”)
else:
print(“x is
odd”)
This program finds out if x is an even number, an odd number
or zero.The ‘if statement check if ‘x’ is an even number by dividing it by ‘2’
and if the remainder is ‘0’, it prints “x is even” if the remainder is not
equal to ‘0’ it moves on to the ‘elif’ statement. The ‘elif’ statement checks
if ‘x’ is equal to ‘0’ , if yes, it print “x is equal to 0”, if not it moves on
to the ‘else’ statement.
CONGRATULATIONS !!! You have learnt how to create a program
that can make its own decisions.You have now unlocked a whole new level of
programming because you will need this knowledge no matter how advanced you
get. Have fun with it !!! Create some cool programs that can decide on their
own. Create programs like I have shown you as an example above that checks if a
number is even, odd or zero. Good luck with it.
I hope this article answered all of your questions and even helped you in becoming a better programmer. IF IT DID, leave a like. IF IT DIDN'T feel free to ask any further queries in the comment section below.
0 Comments
Welcome to the comments section, this is where you can contact me personally for any doubts or feedback