Think how often your mind goes like this:
"If it's sunny we're going to zipline. If it's raining we'll play Tombraider."
"If something is true then we'll do this, otherwise we'll do something else.
"If ... then ..."
Computer programs work the same way. Computers operate just like people, because it is people who created them in the first place!
So, how do we use it in Python?
Let's see what is true and what is false in computer's brain and how they can compare things.
First something that can look familiar (operators and their meaing):
== equal to
!= Not equal to
< Less than
> Greater than
<= Less or equal to
>= Greater or equal to
Notice, that to compare equality of two objects we use double equal sign (==). One equal sign (=) is assigning value to a variable, remember?
Now, enter python interpreter to have some fun with those.
pi@tron ~ $ python3 Python 3.4.3 (default, Oct 14 2015, 20:28:29) [GCC 4.8.4] on linux Type "help", "copyright", "credits" or "license" for more information. >>> >>&g 'python' == 'python' True >>
>'python' == 'Python' False >>
>8 < 100 True >>
>'car' != 'bike' True
This quality of comparing can be used in many programs. Try a simple one like shown below. Pay attention to colon at the end of if and else line (:) and indentation (4 spaces) that follow them in the next line. Write exactly this content into a leafpad and save it as if.py
print("What is your name?") name = input() if name == 'Natalie': print("Hello, Natalie! It's so good to see you again!") else: print("Nice to meet you", name, "!")
In Python, if statement ends with a colon (:). The next line is so called block of code that must be indented by a number of spaces (preferred 4 spaces). Try to write it yourself (don't copy that from the screen). Run it, and see what happens. Try your name first, then run it again and try to introduce yourself as Natalie.
You can follow me along. Look below:
pi@tron ~ $ python3 if.py What is your name? Jimmy Nice to meet you Jimmy ! pi@tron ~ $ pi@tron ~ $ python3 if.py What is your name? Natalie Hello, Natalie! It's so good to see you again! pi@tron ~ $
And now, try to figure out what this one is going to do. I named this file above club.py
Another example below:
i = 8; if i > 8: print("Variable i is greater than 8.") print("These two print statements won't be printed!") else: print("Variable i is not greater than 8.") print("Program ends!")
When you run the above code (if-statement.py), here's what will be printed:
pi@tron ~ $ python3 if-statement.py Variable i is not greater than 8. Program ends! pi@tron ~ $
Program checks the conditions:
i == 8 # true if i is equal to 8. i != 8 # true if i is not equal to 8. i > 8 # true if i is greater than 8. i > 8 # true if i is less than 8. i <= 8 # true if i is less or equal to 8. i >= 8 # true if i is greater or equal to 8. In a nutshell if the condition is true, the block of code will run Else We run different block of code.
Type it in you the leafpad and save like mine. Then run it few times giving different age (first 9, then 14, then 19) See what happens each time!
print("*******************************") print("*** Welcome to Python Club ***") print("*******************************") print("What is your name?") name = input() print("How old are you?") age = int(input()) if age < 9: print("We love young adventurers in our Python club!") elif age < 16: print("Welcome young apprentice. You're goint to love Python!") else: print("Python will be your second nature in no time!")
Write similar program about different ages allowed to see the movie in the cinema or similar to this.