Time for some if-else conditions!
In Python, if else code is very simple in syntax .
if condition1:
Do stuff
elif condition2:
Do different stuff
else:
Do even different stuff
Notice –
- The colon positions
- And as soon as you hit enter after putting the colons like that there will a tab in next line.
Without the tab (indented block) – the ‘do stuff’ part won’t work
- Type and run:
song = “Purbo digonte”
if song ==’Jala Jala Jala’:
print(“This is a ‘band er gaan’! “)
elif song==’Purbo digonte’:
print(“This is a ‘deshattobodhok gaan’!”)
else:
print(“I don’t know!”)
2. Type and run
bag =[]
if bag:
print(“Bag has something!”)
else:
print(“Empty bag :/ “)
3. Put a number or a few numbers in the ‘bag’ list above and run the code again.
4. Type and run
age = 21
if age>8:
print(“You are NOT allowed to call me Aunty. Call me Apu!”)
else:
print(“Ok, you can call me Aunty, you cute little munchkin!”)
5. Type and run:
list1 = [ ‘Hawkgirl’, ‘Alucard’, ‘Peter Pan’, ‘Marry Poppins’]
list2 = [‘Hulk’, ‘Starfire’, ‘Hercules’, ‘Goku’ ]
name = ‘Peter Pan’
if name in list1:
print(“You can fly!”)
else:
print(‘You are strong!’)
6. Type and run
list3 = [‘Alucard’,’Goku’]
if list1[2] in list3:
print(“%s is an anime character!” %list1[2])
7. Fix ONLY the index numbers in Problem 6 (change in two places only) , so that an output is printed.