Python Programming Fundamentals
Computer programming is a skill that can bring great enjoyment from the creativity involved in designing and implementing a solution to a problem. This classroom-tested and easy-to-follow textbook teaches the reader how to program using Python, an accessi
- PDF / 516,828 Bytes
- 24 Pages / 439.37 x 666.142 pts Page_size
- 105 Downloads / 239 Views
2
In this chapter we explore how to make choices in our programs. Decision making is valuable when something we want to do depends on some user input or some other value that is not known when we write our program. This is quite often the case and Python, along with all interesting programming languages, has the ability to compare values and then take one action or another depending on that outcome. For instance, you might write a program that reads data from a file and takes one action or another based on the data it read. Or, a program might get some input from a user and then take one of several actions based on that input. To make a choice in Python you write an if statement. An if statement takes one of two forms. It may be just an if statement. In this case, if the condition evaluates to true then it will evaluate the then statements. If the condition is not true the computer will skip to the statements after the if statement.
if :
Figure 2.1 depicts this graphically. An if statement evaluates the conditional expression and then goes to one of two places depending on the outcome. Notice the indentation in the if statement above. The indentation indicates the then statements are part of the if statement. In- Fig. 2.1 If statement dentation is very important in Python. Indentation determines the control flow of the program. Figure 2.1 graphically depicts this as well. K.D. Lee, Python Programming Fundamentals, Undergraduate Topics in Computer Science, DOI 10.1007/978-1-84996-537-8_2, © Springer-Verlag London Limited 2011
41
42
2
Fig. 2.2 Relational operators
2 Operator < >
Decision Making
Condition Less Than Greater Than
=
Greater Than or Equal to
==
Equal to
!=
Not Equal to
Fig. 2.3 Stepping into and over
If the condition evaluates to true, a detour is taken to execute the then statements before continuing on after the if statement. Generally, we want to know if some value in our program is equal to, greater, or less than another value. The comparison operators, or relational operators, in Python allow us to compare two values. Any value in your program, usually a variable, can be compared with another value to see how the two values relate to each other. Figure 2.2 lists the operators you can use to compare two values. Each of these operators is written between the two values or variables you want to compare. They evaluate to either true or false depending on the two values. When the condition evaluates to true, the then statements are executed. Otherwise, the then statements are skipped.
2
Decision Making
43
Example 2.1 An if statement is best described by giving an example. Assume we want to see if a number entered by a user is divisible by 7. We can write the program pictured in Fig. 2.3 to decide this. The program gets some input from the user. Remember that input reads a string from the user. The int converts the string to an integer. Then, the num variable is checked to see if it is divisible by 7. The % is called the modulo or just the mod operator. It gives us the remainder a
Data Loading...