📖
Introduction to programming with Python
  • Introduction to programming with Python 3
  • Preface
    • Common misconceptions about learning to program
    • The Hard truth about learning to program
    • Personal qualities for success
  • 1 - The way of the program
    • Python Programming Language
    • The first program
    • What is debugging?
    • Glossary
    • Exercises
  • 2 - Variables, expressions and statements
    • Values, types and variables
    • Common Built-in data types
    • Expressions
    • Code Format
    • Debugging
    • Glossary
    • Exercises
  • 3 - Functions
    • Python "built-in" Functions
    • Math Functions
    • Composition
    • User Defined Functions
    • PEP 8 Recommendations
    • Debugging
    • Glossary
    • Exercises
  • 4 - Conditionals
    • Boolean expressions
    • Conditional statements
    • PEP 8 Recommendations
  • 5 - Iteration
    • The while statement
    • The for statement
    • break and continue statements
    • Nested Loops
    • PEP 8 Recommendations
    • Debugging
    • Glossary
    • Exercises
  • 6 - A short introduction to testing: Building Reliable Software
  • 7 - A deeper dive into Strings, Lists and Tuples
    • More on Strings
    • More on Lists
    • More on Tuples
    • Debugging
    • Glossary
    • Exercises
  • 8 - A deeper look at Functions in Python
    • Function Preconditions and Postconditions
    • Positional and Keywords arguments
    • Nested Functions
    • Scope of a variable
    • Recursion
    • Functions' side effects
    • Glossary
    • Exercises
  • 9 - Code Documentation
    • Basics of Commenting Code
    • Documenting Code via Python Docstring
  • 10 - Sets and dictionaries
    • Sets
    • Dictionaries
    • Which data structure should I use?
    • Debugging
    • Glossary
    • Exercises
  • 11 - File I/O
    • Read/Write to a file
    • File management
    • Debugging
    • Glossary
    • Exercises
  • 12 - Handling Errors and Exceptions
  • 13 - Writing modules
  • 14 - Classes
    • Classes and Objects
    • Classes and Functions
    • Classes and Methods
    • Pythonic Magic: Understanding and Implementing Dunder Methods
    • Glossary
    • Exercises
  • 15 - Python's Type Hinting
  • Acknowledgements
Powered by GitBook
On this page
  • Exercise 1
  • Exercise 2
Edit on GitHub
  1. 2 - Variables, expressions and statements

Exercises

Exercise 1

Assume that we execute the following assignment statements:

width = 17 
height = 12.0 
delimiter = '.' 

For each of the following expressions, write the value of the expression and the type (of the value returned by the expression).

  • width/2

  • width/2.0

  • height/3

  • 1 + 2 * 5

  • delimiter * 5

Use the Python interpreter to check your answers.

Exercise 2

Practice using the Python interpreter as a calculator:

  • The volume of a sphere with radius rrr is 43Ï€r3\frac{4}{3} \pi r^334​πr3. What is the volume of a sphere with radius 5? Hint: 392.6 is incorrect!

  • Suppose the cover price of a book is £24.95, but bookstores get a 40% discount. Shipping costs £3 for the first copy and 75 pence for each additional copy. What is the total wholesale cost for 60 copies?

  • If I leave my house at 6:52 am and run 1 mile at an easy pace (8'15" per mile), then 3 miles at tempo (7'12" per mile) and 1 mile at easy pace again, what time do I get home for breakfast?

PreviousGlossaryNext3 - Functions

Last updated 1 year ago