if.txt Last updated: Monday 16th January 2017, 8:38 PT, AD The "if" selection statement in Python 3 ---------------------------------------- There are times in a computer program when we have to make a decision. For example, you may want to write program where the user of the program inputs their exam score, and the program will output a message saying "Pass" or "Fail" depending on the score entered. Let's say the pass mark is 50, if they enter a score of 50 or more, then the program outputs "Pass" otherwise the program outputs "Fail". The if statement ---------------- The if statement starts with the keyword if followed by a Boolean condition (an expression that evaluates to either True or False), followed by a colon (:). day = 'Monday' if day == 'Monday': print ('Today is Monday') else: print ('Today is not Monday') print("The program has now ended") Note: the relational operator == means "is equal to" The relation operators are explained in this document: http://www.annedawson.net/Python3_Processing_Selection.htm It's possible to have multiple statements in the True or False sections of an if statement... grade = 95 if grade >= 91: print ("Nice score!") print (" Grade A+ ") else: print ("Better luck next time!") print ("Good Luck!") print("The program has now ended") The condition (grade >= 91) can only have a value either True or False. If the condition is True, the first two print statements are executed and the second set is skipped. The program then continues with the first line after the second indented block of code, in this case: print("The program has now ended") If the condition is False, the second two print statements are executed and the first set is skipped. The program then continues with the first line after the second indented block of code, in this case: print("The program has now ended") Notice you can enclose strings (text) in either matching single or double quotes( ' or " ). When you write your own program that contains an if statement, beneath the if line, the statement (or set of statements) to be run if the condition is True are entered after pressing the Tab key or typing a few space characters. If you use the Tab key, always use the Tab key. If you use three spaces, always use three spaces. If you use four spaces, always use four spaces. Mixing Tabs and spaces is a syntax error, even if the program looks correct. You will get an "incorrect indent" error. You have been warned! Start Python. Open up an IDLE editor window (when the Python 3 shell window appears, select New File from the File menu), save the file as selection1.py, then type in the following code exactly as shown (or copy and paste the code into the editor window): # File: selection1.py # Programmer: (type your name here) # Date: (type today's date here) temperature = input("Please enter a temperature: ") temperature = float(temperature) if temperature >= 25.0: print("Hot") else: print("Cold") There are several example programs using the if statement contained in the following page: http://www.annedawson.net/Python3Programs.txt With that page displayed in a browser window, search for the text 04-10.py and read down to program 04-17.py. You can run these example programs by copying and pasting them into an IDLE editor window. Read more about Python Processing and Selection here: http://www.annedawson.net/Python3_Processing_Selection.htm If you need help using IDLE, read this page first: https://www2.cs.arizona.edu/people/mccann/usingidle The page above will explain how to use the IDLE editor window to write and run programs, how to use the shell window to experiment with Python statements, and how to install Python with IDLE if you haven't already done so.