Conditional Statement is a very important concept in computer programming as well as in Python.

Let’s check out some exercises that will help understand Conditional Statements (if-elif-else) better.

Exercise 7-a

Write an if statement that asks for the user's name via input() function. If the name is "Bond" make it print "Welcome on board 007." Otherwise make it print "Good morning NAME". (Replace Name with user's name)


input() function will return the input from the user.

name = input(“Please enter your name.”)

An if – else statement will achieve what you need.

if name == xx:
    statement1
else:
    statement2

Make sure to note logical operators can be slightly different than mathematical operators in Python (i.e.: “==” instead of “=”)

name = input("Please enter your name.")
if name == "Bond":
print("Welcome on board 007.")
else:
print("Good morning " + name)

 

Exercise 7-b

Do the same thing as exercise 7-a this time making sure if the name is bond with lower case b it still prints "Welcome on board 007."


input() function will return the input from the user.

name = input(“Please enter your name.”)

An if – else statement will achieve what you need.

if name == xx:
    statement1
else:
    statement2

Make sure to note logical operators can be slightly different than mathematical operators in Python (i.e.: “==” instead of “=”)

if you construct your logical statement with name.lower() it will work either way.

name = input("Please enter your name.")
if name.lower() == "bond":
print("Welcome on board 007")
else:
print("Good morning " + name)

 

Exercise 7-c

Write a function named "evens" which returns True if a number is even and otherwise returns False.


% is an operator that gives the remainder of a division.

def evens(i):
    if i % 2 == 0:
        return True
    else:
        return False

Exercise 7-d

Write a function named "thedecimal" which returns the decimal part of a number. If decimal part is zero function should return this string: "INTEGER".


You can use int() function to find the decimal part of a number.

def thedecimal(i):
    if i - int(i) != 0:
        return i-int(i)
    else:
        return "INTEGER"

Exercise 7-e

treepersqkm is a dictionary showing the tree number of countries per square kilometer for random countries with sizeable population numbers. Write a function named "moretrees" that returns a list of country names with more than 20.000 trees per square kilometer.


Make sure you create an empty list inside your function before appending items to it.

You can iterate through the list with a for loop. Inside the loop you can use an if statement to control the flow of execution.

If you use an iterator i to iterate through the dictionary. i will be the key and dict[i] will be the value of that key.

def moretrees(dict):
    lst = []
    for i in dict:
        if dict[i]>20000:
            lst.append(i)
        else:
            pass
    return lst
print(moretrees(treepersqkm))

Exercise 7-f

Write a function named "count_l" that counts the number of words that contain the letter: "l" in a given string.


You can use a for loop and a counter. Initiate your counter as:
c = 0
before the loop,

and increase it with 1 each time it encounters an “l” letter.

To iterate through a list of each word in a string, you can use split function:

for i in str.split():

Make sure your function returns something in the end.

def count_l(a):
    c = 0
    for i in a.split():
        if "l" in i:
            c = c+1
        else:
            pass

return c
print(count_l(str))

Exercise 7-g

Write a similar function to 7-e which returns the number of words that start with letter "A" in a string. (Make sure it counts lower case a's as well.).


You can use a for loop and a counter. Initiate your counter as:
c = 0
before the loop,

and increase it with 1 each time a word starts with a or A:

i[0].lower() == a

To iterate through a list of each word in a string, you can use split function:

for i in str.split():

Make sure your function returns something in the end.

def count_l(a):
    c = 0
    for i in a.split():
        if i[0].lower() == a:
            c = c+1
        else:
            pass

return c
print(count_l(str))