Python Programming Practice-Optional arguments
There are two optional arguments to the print function. They are not overly important at this stage of the game, so you can safely skip over this section, but they are useful for making your output look nice.
sep Python will insert a space between each of the arguments of the print function. There is an optional argument called sep , short for separator, that you can use to change that space to some- thing else. For example, using sep= ‘ : ‘ would separate the arguments by a colon and sep= ‘ ## ‘ would separate the arguments by two pound signs.
One particularly useful possibility is to have nothing inside the quotes, as in sep= ” . This says to put no separation between the arguments. Here is an example where sep is useful for getting the output to look nice:
print ( ' The value of 3+4 is ' , 3+4, ' . ' ) print ( ' The value of 3+4 is ' , 3+4, ' . ' , sep= '' )
The value of 3+4 is 7 . The value of 3+4 is 7.
end The print function will automatically advance to the next line. For instance, the following will print on two lines:
print ( ' On the first line ' ) print ( ' On the second line ' )
On the first line On the second line
There is an optional argument called end that you can use to keep the print function from advancing to the next line. Here is an example:
print ( ' On the first line ' , end= '' ) print ( ' On the second line ' )
On the first line On the second line
Of course, this could be accomplished better with a single print, but we will see later that there are interesting uses for the end argument.
Variables
Looking back at our first program, we see the use of a variable called temp :
temp = eval ( input ( ' Enter a temperature in Celsius: ' )) print ( ' In Fahrenheit, that is ' , 9/5*temp+32)
One of the major purposes of a variable is to remember a value from one part of a program so that it can be used in another part of the program. In the case above, the variable temp stores the value that the user enters so that we can do a calculation with it in the next line.
In the example below, we perform a calculation and need to use the result of the calculation in several places in the program. If we save the result of the calculation in a variable, then we only need to do the calculation once. This also helps to make the program more readable.
temp = eval ( input ( ' Enter a temperature in Celsius: ' )) f_temp = 9/5*temp+32 print ( ' In Fahrenheit, that is ' , f_temp) if f_temp > 212: print ( ' That temperature is above the boiling point. ' ) if f_temp < 32: print ( ' That temperature is below the freezing point. ' )
We haven’t discussed if statements yet, but they do exactly what you think they do.
A second example Here is another example with variables. Before reading on, try to figure out what the values of x and y will be after the code is executed.
x=3 y=4
z=x+y z=z+1 x=y y=5
After these four lines of code are executed, x is 4, y is 5 and z is 8. One way to understand something like this is to take it one line at a time. This is an especially useful technique for trying to understand more complicated chunks of code. Here is a description of what happens in the code above:
1. x starts with the value 3 and y starts with the value 4. 2. In line 3, a variable z is created to equal x+y , which is 7.
3. Then the value of z is changed to equal one more than it currently equals, changing it from 7
to 8.
4. Next, x is changed to the current value of y , which is 4.
5. Finally, y is changed to 5. Note that this does not affect x .
6. So at the end, x is 4, y is 5, and z is 8.
Variable names
There are just a couple of rules to follow when naming your variables.
• Variable names can contain letters, numbers, and underscore.
• Variable names cannot contain spaces.
• Variable names cannot start with a number.
• Case matters—for instance, temp and Temp are different.