Docsity

the python workbook, Notas de estudo de Engenharia Elétrica

Pré-visualização parcial do texto

Baixe the python workbook e outras Notas de estudo em PDF para Engenharia Elétrica, somente na Docsity!

The Python

Workbook

Ben Stephenson

A Brief Introduction with Exercises

and Solutions

The Python Workbook

Ben Stephenson University of Calgary Calgary, AB Canada

ISBN 978-3-319-14239-5 ISBN 978-3-319-14240-1 (eBook) DOI 10.1007/978-3-319-14240-

Library of Congress Control Number: 2014957402

Springer Cham Heidelberg New York Dordrecht London © Springer International Publishing Switzerland 2014 This work is subject to copyright. All rights are reserved by the Publisher, whether the whole or part of the material is concerned, specifically the rights of translation, reprinting, reuse of illustrations, recitation, broadcasting, reproduction on microfilms or in any other physical way, and transmission or information storage and retrieval, electronic adaptation, computer software, or by similar or dissimilar methodology now known or hereafter developed. The use of general descriptive names, registered names, trademarks, service marks, etc. in this publication does not imply, even in the absence of a specific statement, that such names are exempt from the relevant protective laws and regulations and therefore free for general use. The publisher, the authors and the editors are safe to assume that the advice and information in this book are believed to be true and accurate at the date of publication. Neither the publisher nor the authors or the editors give a warranty, express or implied, with respect to the material contained herein or for any errors or omissions that may have been made.

Printed on acid-free paper

Springer International Publishing AG Switzerland is part of Springer Science+Business Media (www.springer.com)

To my son, Jonathan, who surprised us all by

arriving before this book was completed, and

to my wife Flora, for 12 fantastic years of

marriage, and many more to come.

solution length exactly, I hope that providing this information will prevent you from going too far astray before seeking assistance. This book can be used in a variety of ways. It can supplement another textbook that has a limited selection of exercises, or it can be used as the sole source of exercises when an instructor has decided not to use another textbook. A motivated individual could also learn Python programming by carefully studying each of the included exercises and solutions, though there are probably easier ways to learn the language. No matter what other resources you use with this book, completing the exercises and studying the provided solutions will enhance your programming ability.

Calgary, Canada, November 2014 Ben Stephenson

viii Preface

Contents

Part I

Exercises

1

Introduction to Programming

Exercises

The exercises in this chapter are designed to help you develop your analysis skills by providing you with the opportunity to practice breaking small problems down into sequences of steps. In addition, completing these exercises will help you become familiar with Python’s syntax. To complete each exercise you should expect to use some or all of these Python features:

  • Generate output with print statements
  • Read input, including casting that input to the appropriate type
  • Perform calculations involving integers and floating point numbers using Python operators like +, - , *, /, //, %, and **
  • Call functions residing in the math module
  • Control how output is displayed using format specifiers
Exercise 1: Mailing Address

( Solved—9 Lines ) Create a program that displays your name and complete mailing address formatted in the manner that you would usually see it on the outside of an envelope. Your program does not need to read any input from the user.

Exercise 2: Hello

( 9 Lines ) Write a program that asks the user to enter his or her name. The program should respond with a message that says hello to the user, using his or her name.

© Springer International Publishing Switzerland 2014 B. Stephenson, The Python Workbook , DOI 10.1007/978-3-319-14240-1_

3

Exercise 7: Sum of the First n Positive Integers 5

Exercise 7: Sum of the First n Positive Integers

( Solved—12 Lines ) Write a program that reads a positive integer, n , from the user and then displays the sum of all of the integers from 1 to n. The sum of the first n positive integers can be computed using the formula:

sum =

( n )( n + 1 ) 2

Exercise 8: Widgets and Gizmos

( 15 Lines ) An online retailer sells two products: widgets and gizmos. Each widget weighs 75 grams. Each gizmo weighs 112 grams. Write a program that reads the number of widgets and the number of gizmos in an order from the user. Then your program should compute and display the total weight of the order.

Exercise 9: Compound Interest

( 19 Lines ) Pretend that you have just opened a new savings account that earns 4 percent interest per year. The interest that you earn is paid at the end of the year, and is added to the balance of the savings account. Write a program that begins by reading the amount of money deposited into the account from the user. Then your program should compute and display the amount in the savings account after 1, 2, and 3 years. Display each amount so that it is rounded to 2 decimal places.

Exercise 10: Arithmetic

( Solved—20 Lines ) Create a program that reads two integers, a and b , from the user. Your program should compute and display:

  • The sum of a and b
  • The difference when b is subtracted from a
  • The product of a and b

6 1 Introduction to Programming Exercises

  • The quotient when a is divided by b
  • The remainder when a is divided by b
  • The result of log 10 a
  • The result of a b

Hint: You will probably find the log10 function in the math module helpful for computing the second last item in the list.

Exercise 11: Fuel Efficiency

( 13 Lines ) In the United States, fuel efficiency for vehicles is normally expressed in miles-per- gallon (MPG). In Canada, fuel efficiency is normally expressed in liters-per-hundred kilometers (L/100 km). Use your research skills to determine how to convert from MPG to L/100 km. Then create a program that reads a value from the user in American units and displays the equivalent fuel efficiency in Canadian units.

Exercise 12: Distance Between Two Points on Earth

( 27 Lines ) The surface of the Earth is curved, and the distance between degrees of longitude varies with latitude. As a result, finding the distance between two points on the surface of the Earth is more complicated than simply using the Pythagorean theorem. Let ( t 1 , g 1 ) and ( t 2 , g 2 ) be the latitude and longitude of two points on the Earth’s surface. The distance between these points, following the surface of the Earth, in kilometers is:

distance = 6371. 01 × arccos(sin( t 1 ) × sin( t 2 ) + cos( t 1 ) × cos( t 2 ) × cos( g 1 − g 2 ))

The value 6371.01 in the previous equation wasn’t selected at random. It is the average radius of the Earth in kilometers.

Create a program that allows the user to enter the latitude and longitude of two points on the Earth in degrees. Your program should display the distance between the points, following the surface of the earth, in kilometers.