2
Introduction to Python
Programming
Objectives
In this chapter, you’ll:
Use arithmetic operators and
comparison operators, and
understand their precedence.
Use single-, double- and triple-
quoted strings.
whether to execute a
statement or group of
statements.
Learn about objects and
Python’s dynamic typing.
PyCDS_02_IntroToPython.fm Page 1 Tuesday, May 21, 2019 11:16 AM
2Introduction to Python Programming
Note: Throughout the Instructor Solutions Manual, solutions are not provided for project,
research and challenge exercises—many of which are substantial and appropriate for term
projects, directed-study projects, capstone-course projects and thesis topics. Before assign-
ing a particular exercise for homework, instructors should check the IRC to be sure the
Exercises
Unless specified otherwise, use IPython sessions for each exercise.
2.1 (What does this code do?) Create the variables x = 2 and y = 3, then determine what
each of the following statements displays:
a) print(‘x =’, x)
Answer: x = 2
2.2 (What’s wrong with this code?) The following code should read an integer into the
variable rating:
rating = input(‘Enter an integer rating between 1 and 10′)
2.3 (Fill in the missing code) Replace *** in the following code with a statement that
will print a message like ‘Congratulations! Your grade of 91 earns you an A in this
course’. Your statement should print the value stored in the variable grade:
if grade >= 90:
***
PyCDS_02_IntroToPython.fm Page 2 Tuesday, May 21, 2019 11:16 AM