Intermediate Lessons

If Statements

An if statement in Python is a conditional statement, if proved true, performs a function or displays information

>>> n=5
>>> if n <=5:
...print("Hello World!")
...else:print("Python is easy!")
Hello World!

This if statment will print "Hello World!" if the condition of n is less than or equal to 5 is met. If this condiiton is not met it will print "Python is easy!"

Let's try assigning n = 6, can you guess what the console will print?

>>> n=6
>>> if n <=5:
...print("Hello World!")
...else:print("Python is easy!")
Python is easy!

Range Function

The range() function helps us iterate through a range of numbers

>>> n = 3
>>> for i in range(n):
...print(i)
0
1
2

We just iterated through range of [0, n]

Now let's choose our start point to be 3 in range 6 [3, 6]

>>> n, m = 3, 6
>>> for i in range(n, m):
...print(i)
3
4
5

Video credits to Amuls Academy


Time to test what you have learned!


What is the start point of this range() function?
>>> n = 10
>>> for i in range(n):
 0
 10
n

What does the following if statment print?
>>> n=7
>>> if n <=9:
...print("Hello World!")
...else:print("Python is easy!")
 Hello World!
 Python is easy!

What does the following if statement print?
>>> n ,m = 5, 8
>>> if n <=7:
...for i in range(n, m):
    ...print(i)
else:print("Wrong")
 Wrong
[0-5]
[5-8]
[0-8]