"If" is the simplest conditional statement. It simply checks if an evaluation condition is True and if it is then it executes a certain block of code. The checking is usually done by a logical operator such as --> more than( > ), less than( < ), equal to( == ), not equal to ( != or not), etc
if <condition>:
execute statement(s)
1. Greater than ( > )
2. Less than ( < )
3. Equal to ( == )
1. ! or not
Let us look at some examples :
In [ ]:
# Greater than ( > )
if 1 > 0:
print("One is more than zero")
else:
print("BITS Pilani Goa Campus is better than IIT Kanpur")
# Less than ( < )
if 12 < 42:
print("Yes, 12 is less than 42")
else:
print("Everyone registered in CTE Python will pass with distinction (90%+ marks)")
# Equal to ( == )
if 2 + 2 == 4:
print("Two plus Two equals Four")
else:
print("Lite......")
# Not equal to ( != )
if 1 != 0:
print("Sachin")
else:
print("Kohli")
# Inversion operator
if not 2 + 2 == 4:
print("Lite....")
else:
print("CTE Python....")
In [ ]:
if 1 < 0:
print(1, end='')
elif 2 == 3:
print(2, end='')
elif not 3 == 4:
print(3, end='')
print(".....Lite")
In [ ]:
a = [1, 2, 3]
if isinstance(a, list):
print(a, "is a list !")
if 2 in a:
print("Yes, 2 is in ", a)
if 5 not in a:
print("No, 5 is not in ", a)
In [3]:
list(range(0, 10))
Out[3]:
In [4]:
list(range(-4, 10))
Out[4]:
In [5]:
list(range(0, 10, 3))
Out[5]:
In [7]:
list(range(0, 5, -6))
Out[7]:
In [8]:
list(range(-10, 10, -5))
Out[8]:
In [9]:
list(range(10, 2, 3))
Out[9]:
In [10]:
list(range(10, 1, -2))
Out[10]:
Usage : enumerate(<list>, starting_index=1)
This library function returns an iterable object contains tuples of structure (index, list value at index).
Extra optional argument provides a different starting index. Default starting index is 0.
In that case the tuples are of the structure (index, list value at <index - starting_index>)
In [11]:
list(enumerate(range(0,5)))
Out[11]:
In [13]:
list(enumerate(range(0,5), 10))
Out[13]:
In [32]:
a = [1, 2, 3, 4, 5]
for value in a:
print(value, end=' ')
In [31]:
b = [[1, 2, 3],
[4, 5, 6],
[7, 8, 9]]
for sublist in b:
for value in sublist:
print(value, end=' ')
In [4]:
a = (2, 3, 4)
print(a)
In [5]:
a[1] = 10
print(a)
In [38]:
a = ["One", "Two", "Three"]
for i, value in enumerate(a):
print("Value at index <", i, "> of list<a> is : ", value)
In [43]:
for i in range(0, 10):
for j in range(0, i):
print("*", end='')
print("")
In [42]:
for i in range(0, 10):
for j in range(10, i, -1):
print(" ", end='')
for k in range(0, i):
print("*", end='')
print("")
Write a program to print the following pattern.
**********
****--****
***----***
**------**
*--------*
**------**
***----***
****--****
**********
First line --> 10 stars
Second line --> 4 stars ,2 dashes, 4 stars
Third line --> 3 stars, 4 dashes, 3 stars
.
.
.
Eigth line --> 4 stars ,2 dashes, 4 stars
Ninth line --> 10 stars again.
In [57]:
for i in range(0, 5):
for j in range(0, 5 - i):
print("*", end='')
for k in range(0, 2*i):
print("-", end='')
for j in range(0, 5 - i):
print("*", end='')
print("")
for i in range(3, -1, -1):
for j in range(0, 5 - i):
print("*", end='')
for k in range(0, 2*i):
print("-", end='')
for j in range(0, 5 - i):
print("*", end='')
print("")
In [ ]: