Starting up with Python – Part8

I am now at part 8 of my one of my objective to refresh myself with the basics of Python. This part will be dedicated to Nesting and Comparison statements in Python. Here are the topics discussed earlier on this blog about python:

python_logo_by_bluex_pl

   

18.Nesting statements

Let's see this simple if statement which will output "We are mauritius hackers group"

#! /usr/bin/env python
hackers = "mauritius"
mauritius = "country"
if hackers == "mauritius":
    print 'we are mauritius hackers group'
else:
    print 'Country is not in our record'

At this point we are going to create a nested if statement. Its first going to test the first if statement and since hackers is equal to "toto" , the main or first if statement when run will redirect to the last else statement by giving the output "country is not in our record"

#! /usr/bin/env python
hackers = "toto"
mauritius = "country"
if hackers == "mauritius":
     if mauritius =="country":
          print 'mauritius is a country'
     else:
          print 'wrong country name'
else:
     print 'Country is not in our record'

19. Comparisons Operators

In python, by default it output the "True" or "False" statement if a certain values are entered. For examples

>> 9 >2
True
>>> 3 >10
False
>>> 9!=0
True
>>> 9!=9
False

You can also create an array of numbers. For example

>> toto = [22,23,24]
>>> titi = [25,26,27]
>>> toto == titi
False
>>> toto != titi
True

You can also asked for a certain numbers within the array :

>> toto is 22
True
>>> toto is 29
False

When there are same values in both array, an if else statement together with loops can be used to check which is true or false.