Saturday, February 17, 2018

If Statements

Python 's if statement allows you to examine the current state of a program and respond appropriately to that state.

Let's say we played a lottery number and we want to check if we win or not. We haven't covered yet the input section but just for testing purposes let's use it.

First we create a list with the following winning numbers:
winningNumbers = [10, 20, 25, 29]

And we ask the user to enter the number he/she played:
myNumber = int(input('Enter the number you played'))

winningNumbers = ['10', '20', '0', '59']
myNumber = input('Enter the number you played\n')
for i in winningNumbers:
        if myNumber == i:


Ignore Case:
favoriteTeam = 'Real Madrid'
favoriteTeam.lower() =='real Madrid'


Checking for Inequality could be possible too (!=):
winningNumbers = ['10''20''0''59']
myNumber = input('Enter the number you played\n')
for in winningNumbers:
        if myNumber != i:



Checking Multiple Conditions(AND/OR):
yourAge = int(input('How old are you?'))
myFriend = int(input('How old your friend is?'))
       if yourAge>= 18 and myFriend<= 21:
                  print('You can enter but your friend cannot!')

if we enter for our age a value greater than 18 and for our friend a value less than 21 then the condition will be meet(test the values for the condition True, other values were not considered in the code) 

On the other hand, if we change our code and replace and for or, if any either of any condition is meet then the whole statement is true. 
yourAge = int(input('How old are you?'))
myFriend = int(input('How old your friend is?'))
       if yourAge>= 18 or myFriend>= 21:
                  print('You can enter with your friend!')

Checking if a value is in the list:
myTeams = ['Real Madrid', 'Miami Heat', ;'LA Galaxy','Arsenal']

'Real Madrid' in myTeams will return True

Checking if a value is not in a list:
myTeams = ['Real Madrid', 'Miami Heat', 'LA Galaxy','Arsenal']
myFav = 'Clippers'
if myFav not in myTeams:
        print('Your team is not in the list')

        print('Your team is in the list')

Your team is not in the list

If-else Statements:
if age >= 18:
print("You can drive a car alone!")
print("Sorry, but you need supervision to drive!")

If-elif-else Statements:
income= 35000

if income < 10000:
      print("This year you don't have to declare any income.")
elif income < 25000:
      print("This year you will receive a bonus based on your annual income.")
       print("This year you won't receive any bonus but still have to declare your income.")

Our program will test if the first statement is True, if this is meet it will skip the rest of the program and print "This year you don't have to declare any income". In case this condition is false our program will test the second condition, which only will execute if our if is false. Then our output will be "This year you will receive a bonus based on your annual income". If both conditions are false the our else statement will execute.

No comments:

Post a Comment