73
28
Chapter 1
print('You will be ' + str(int(myAge) + 1) + ' in a year.')
print('You will be ' + str(int( ) + 1) + ' in a year.')
print('You will be ' + str( ) + ' in a year.')
print('You will be ' + str( ) + ' in a year.')
print('You will be ' + + ' in a year.')
'4'
'5'
print('You will be 5' + ' in a year.')
print('You will be 5 in a year.')
5
4 + 1
Figure 1-4: The evaluation steps, if 4 was stored in myAge
Summary
You can compute expressions with a calculator or type string concatena-
tions with a word processor. You can even do string replication easily by
copying and pasting text. But expressions, and their component values—
operators, variables, and function calls—are the basic building blocks
that make programs. Once you know how to handle these elements, you
will be able to instruct Python to operate on large amounts of data for you.
It is good to remember the different types of operators (
+
,
-
,
*
,
/
,
//
,
%
,
and
**
for math operations, and
+
and
*
for string operations) and the three
data types (integers, floating-point numbers, and strings) introduced in this
chapter.
A few different functions were introduced as well. The
print()
and
input()
functions handle simple text output (to the screen) and input (from the key-
board). The
len()
function takes a string and evaluates to an int of the num-
ber of characters in the string. The
str()
,
int()
, and
float()
functions will
evaluate to the string, integer, or floating-point number form of the value
they are passed.
In the next chapter, you will learn how to tell Python to make intelli-
gent decisions about what code to run, what code to skip, and what code to
repeat based on the values it has. This is known as flow control, and it allows
you to write programs that make intelligent decisions.
Practice Questions
1. Which of the following are operators, and which are values?
*
'hello'
-88.8
-
/
+
5