Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 5

2.

Explain the control statements if, else, elif with proper syntax and Examples:

Basic if Statement
In Python, if statements are a starting point to implement a condition. Let’s look at the
simplest example:
if <condition>:
<expression>
When <condition> is evaluated by Python, it’ll become either True or False (Booleans).
Thus, if the condition is True (i.e, it is met), the <expression> will be executed, but
if <condition> is False (i.e., it is not met), the <expression> won’t be executed.
We are pretty free to decide what conditions and expressions can be because Python is
very flexible.
Let’s look at a concrete example.

# Basic if statement
x = 3
y = 10

if x < y:
print("x is smaller than y.")
x is smaller than y.
First of all, we define two variables, x and y. Then we say that if variable x is smaller
than variable y, print out x is smaller than y). Indeed, if we execute this code, we’ll print
out this output because 3 is smaller than 10.

Output: x is smaller than y.

Let’s look at a more complex example.


# A slightly more complex example
x = 3
y = 10
z = None

if x < y:
z = 13
print(f"Variable z is now {z}.")
Variable z is now 13.
In this case, if the condition is met, then a value of 13 will be assigned to the variable z.
Then Variable z is now 13. will be printed out (note that the print statement can be used
both outside and inside the if statement).
As you can see, we aren't restrained in the choice of an expression to execute. You can
now practice more by writing more complex code.
Let’s know see what happens if we execute the following code:
# What happens here?
x = 3
y = 10

if x > y:
print("x is greater than y.")
Here we changed the direction of the comparison symbol (it was less than, and now
it’s greater than). Can you guess the output?
There will be no output! This happened because the condition hadn't been met. 3 is not
greater than 10, so the condition evaluated to False, and the expression wasn’t
executed. How do we solve this problem? With the else statement.

else Statement
What if we want to execute some code if the condition isn't met? We add
an else statement below the if statement. Let’s look at an example.
# else statement
x = 3
y = 10

if x > y:
print("x is greater than y.")
else:
print("x is smaller than y.")
x is smaller than y.
Output: x is smaller than y.

Here, Python first executes the if condition and checks if it’s True. Since 3 is not greater
than 10, the condition isn't met, so we don’t print out “x is greater than y.” Then we say
that in all other cases we should execute the code under the else statement: x is smaller
than y.

Let’s get back to our first example of a conditional statement:


If tomorrow it doesn't rain, I’ll go out with my friends in the park. Otherwise, I’ll stay
home with a cup of hot tea and watch TV.
Here the else statement is “Otherwise.”
What happens if the condition is met?
# What if the condition is met?
x = 3
y = 10

if x < y:
print("x is smaller than y.")
else:
print("x is greater than y.")
x is smaller than y.
In this case, Python just prints out the first sentence as before.
Output: x is smaller than y.

What if x is equal to y?
# x is equal to y
x = 3
y = 3

if x < y:
print("x is smaller than y.")
else:
print("x is greater than y.")
x is greater than y.
The output is clearly wrong because 3 is equal to 3! We have another condition outside
the greater or less than comparison symbols; thus, we have to use the elif statement.

elif Statement
Let’s rewrite the above example and add an elif statement.
# x is equal to y with elif statement
x = 3
y = 3

if x < y:
print("x is smaller than y.")
elif x == y:
print("x is equal to y.")
else:
print("x is greater than y.")
x is equal to y.
Output: x is equal to y.

Python first checks if the condition x < y is met. It isn't, so it goes on to the second
condition, which in Python, we write as elif, which is short for else if. If the first condition
isn't met, check the second condition, and if it’s met, execute the expression. Else, do
something else. The output is “x is equal to y.”
Let’s now get back to one of our first examples of conditional statements:
If tomorrow it isn't too hot, I’ll go to the sea, but if it is, I’ll have a walk in the forest.
However, if it rains, I’ll stay home.
Here, our first condition is that tomorrow it’s not too hot (if statement). If this condition
isn't met, then we go for a walk in the forest (elif statement). Finally, if neither condition
is met, we’ll stay home (else statement).
3.Python Variable Scope
In Python, we can declare variables in three different scopes: local scope, global, and nonlocal scope.

A variable scope specifies the region where we can access a variable. For example,

def add_numbers():
sum = 5 + 4

Here, the sum variable is created inside the function, so it can only be accessed within it (local scope). This type of
variable is called a local variable.
Based on the scope, we can classify Python variables into three types:

1. Local Variables

2. Global Variables

3. Nonlocal Variables

Python Local Variables


When we declare variables inside a function, these variables will have a local scope (within the function). We cannot
access them outside the function.

These types of variables are called local variables. For example,

def greet():

# local variable
message = 'Hello'

print('Local', message)

greet()

# try to access message variable


# outside greet() function
print(message)
Run Code

Output

Local Hello
NameError: name 'message' is not defined

Here, the message variable is local to the greet() function, so it can only be accessed within the function.
That's why we get an error when we try to access it outside the greet() function.
To fix this issue, we can make the variable named message global.
Python Global Variables
In Python, a variable declared outside of the function or in global scope is known as a global variable. This means
that a global variable can be accessed inside or outside of the function.

Let's see an example of how a global variable is created in Python.

# declare global variable


message = 'Hello'

def greet():
# declare local variable
print('Local', message)

greet()
print('Global', message)
Run Code

Output

Local Hello
Global Hello

This time we can access the message variable from outside of the greet() function. This is because we have created
the message variable as the global variable.

# declare global variable


message = 'Hello'

Now, message will be accessible from any scope (region) of the program.

You might also like