Docsity

learn to program with python, Notas de estudo de Engenharia Elétrica

Toggle sidebar
bg1
Learn to Program
with Python
—
Take your first steps in programming
and learn the powerful Python
programming language
—
Irv Kalb
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c
pf2d
pf2e
pf2f
pf30
pf31
pf32
pf33
pf34
pf35
pf36
pf37
pf38
pf39
pf3a
pf3b
pf3c
pf3d
pf3e
pf3f
pf40
pf41
pf42
pf43
pf44
pf45
pf46
pf47
pf48
pf49
pf4a
pf4b
pf4c
pf4d
pf4e
pf4f
pf50
pf51
pf52
pf53
pf54
pf55
pf56
pf57
pf58
pf59
pf5a
pf5b
pf5c
pf5d
pf5e
pf5f
pf60
pf61
pf62
pf63
pf64

Pré-visualização parcial do texto

Baixe learn to program with python e outras Notas de estudo em PDF para Engenharia Elétrica, somente na Docsity!

Learn to Program

with Python

Take your first steps in programming

and learn the powerful Python

programming language

Irv Kalb

Learn to Program

with Python

Irv Kalb

This book is dedicated to the memory of my mother, Lorraine Kalb. I started learning about programming when I was 16 years old, at Columbia High School in Maplewood, NJ. We were extremely fortunate to have a very early computer, an IBM 1130, that students could use. I remember learning the basics of the Fortran programming language and writing a simple program that would add two numbers together and print the result. I was thrilled when I finally got my program to work correctly. It was a rewarding feeling to be able to get this huge, complicated machine to do exactly what I wanted it to do. I clearly remember explaining to my mother that I wrote this program that got the computer to add 9 and 5 and come up with an answer of 14. She said that she didn’t need a computer to do that. I tried to explain to her that getting the answer of 14 was not the important part. What was important was that I had written a program that would add any two numbers and print the result. She still didn’t get it, but she was happy for me and very supportive. Hopefully, through my explanations in this book, you will get it.

vii

Contents

About the Author ............................................................................................... xvii

About the Technical Reviewer ............................................................................ xix

■ CONTENTS

■ CONTENTS

■ CONTENTS

■ CONTENTS

■ CONTENTS

  • ■Chapter 1: Getting Started .................................................................................. Acknowledgments xxi
    • Welcome.......................................................................................................................
    • What Is Python?
    • Installing Python
    • IDLE and the Python Shell
    • Hello World
    • Creating, Saving, and Running a Python File
    • IDLE on Multiple Platforms
    • Summary
  • ■Chapter 2: Variables and Assignment Statements .............................................
    • A Sample Python Program
    • The Building Blocks of Programming
    • Four Types of Data
      • Integers............................................................................................................................................
      • Floats
      • Strings
      • Booleans
      • Examples of Data
      • Form with Underlying Data
      • Variables......................................................................................................................... viii
      • Assignment Statements
      • Variable Names
        • Naming Convention
        • Keywords
        • Case Sensitivity
        • More Complicated Assignment Statements..........................................................................................
      • Print Statements
      • Simple Math
      • Order of Operations
      • First Python Programs
      • Shorthand Naming Convention.......................................................................................
      • Adding Comments
        • Full-Line Comment
        • Add a Comment After a Line of Code
        • Multiline Comment
      • Whitespace.....................................................................................................................
      • Errors..............................................................................................................................
        • Syntax Error
        • Exception Error
        • Logic Error
      • Summary
  • ■Chapter 3: Built-in Functions...............................................................................
    • Overview of Built-in Functions
    • Function Call
    • Arguments
    • Results
    • Built-in type Function
      • Getting Input from the User ix
      • Conversion Functions
        • int Function...........................................................................................................................................
        • fl oat Function........................................................................................................................................
        • str Function
      • First Real Programs
      • Concatenation
      • Another Programming Exercise......................................................................................
      • Using Function Calls Inside Assignment Statements
      • Summary
  • ■Chapter 4: User-Defined Functions
    • A Recipe as an Analogy for Building Software - Ingredients............................................................................................................................................ - Directions
      • Defi nition of a Function
      • Building Our First Function
      • Calling a User-Defi ned Function
      • Receiving Data in a User-Defi ned Function: Parameters................................................
      • Building User-Defi ned Functions with Parameters.........................................................
      • Building a Simple Function that Does Addition
      • Building a Function to Calculate an Average
      • Returning a Value from a Function: The return Statement
      • Returning No Value: None...............................................................................................
      • Returning More Than One Value
      • Specifi c and General Variable Names in Calls and Functions
      • Temperature Conversion Functions
      • Placement of Functions in a Python File
      • Never Write Multiple Copies of the Same Code
      • Constants
      • Scope x
      • Global Variables and Local Variables with the Same Names..........................................
      • Finding Errors in Functions: Traceback
      • Summary
  • ■Chapter 5: if, else, and elif Statements................................................................
    • Flowcharting
    • The if Statement
    • Comparison Operators....................................................................................................
    • Examples of if Statements
    • Nested if Statement
    • The else Statement
    • Using if/else Inside a Function
    • The elif Statement
    • Using Many elif Statements
    • A Grading Program
    • A Small Sample Program: Absolute Value
    • Programming Challenges - Negative, Positive, Zero......................................................................................................................... - isSquare................................................................................................................................................ - isEven - isRectangle
      • Conditional Logic
      • The Logical not Operator
      • The Logical and Operator
      • The Logical or Operator
      • Precedence of Comparison and Logical Operators
      • Booleans in if Statements
      • Program to Calculate Shipping.......................................................................................
      • Summary
  • ■Chapter 6: Loops................................................................................................ xi
    • User’s View of the Game
    • Loops
    • The while Statement
    • First Loop in a Real Program
    • Increment and Decrement............................................................................................
    • Running a Program Multiple Times
    • Python Built-in Packages
    • Generating a Random Number
    • Simulation of Flipping a Coin
    • Other Examples of Using Random Numbers
    • Creating an Infi nite Loop
    • A New Style of Building a Loop: while True, and break
    • The continue Statement
    • Asking If the User Wants to Repeat: the Empty String
    • Pseudocode
    • Building the “Guess the Number” Program
    • Playing a Game Multiple Times
    • Error Checking with try/except
    • Building Error-Checking Utility Functions.....................................................................
    • Coding Challenge
    • Summary
  • ■Chapter 7: Lists
    • Collections of Data
    • Lists
    • Elements
    • Python Syntax for a List
    • Empty List.....................................................................................................................
      • Position of an Element in a List: Index xii
      • Accessing an Element in a List
      • Using a Variable or Expression as an Index in a List
      • Changing a Value in a List
      • Using Negative Indices
      • Building a Simple Mad Libs Game................................................................................
      • Adding a List to Our Mad Libs Game
      • Determining the Number of Elements in a List: The len Function
      • Programming Challenge
      • Using a List Argument with a Function
      • Accessing All Elements of a List: Iteration
      • for Statements and for Loops
      • Programming Challenge
      • Generating a Range of Numbers
      • Programming Challenge
      • Scientifi c Simulations...................................................................................................
      • List Manipulation
      • List Manipulation Example: An Inventory Example
      • Pizza Toppings Example
      • Summary
  • ■Chapter 8: Strings..............................................................................................
    • len Function Applied to Strings
    • Indexing Characters in a String
    • Accessing Characters in a String
    • Iterating Through Characters in a String
    • Creating a Substring: A Slice
    • Programming Challenge 1: Creating a Slice
      • Additional Slicing Syntax xiii
      • Slicing As Applied to a List
      • Strings Are Not Changeable
      • Programming Challenge 2: Searching a String
      • Built-in String Operations
      • Examples of String Operations
      • Programming Challenge 3: Directory Style
      • Summary
  • ■Chapter 9: File Input/Output
    • Saving Files on a Computer..........................................................................................
    • Defi ning a Path to a File
    • Reading from and Writing to a File
    • File Handle
    • The Python os Package
    • Building Reusable File I/O Functions
    • Example Using Our File I/O Functions
    • Importing Our Own Modules.........................................................................................
    • Saving Data to a File and Reading It Back
    • Building an Adding Game
    • Programming Challenge
    • Programming Challenge
    • Writing/Reading One Piece of Data to and from a File
    • Writing/Reading Multiple Pieces of Data to and from a File.........................................
    • The join Function
    • The split Function
    • Final Version of the Adding Game.................................................................................
    • Writing and Reading a Line at a Time with a File
      • Example: Multiple Choice Test xiv
      • A Compiled Version of a Module...................................................................................
      • Summary
  • ■Chapter 10: Internet Data
    • Request/Response Model.............................................................................................
    • Request with Values
    • Example URL: Getting a Stock Price
    • Pretending to Be a Browser
    • API
    • Example Program to Get Stock Price Information Using an API
    • Example Program to Get Exchange Rate Information
    • Example Program to Get Powerball Information
    • API Key
    • URL Encoding
    • Summary
  • ■Chapter 11: Data Structures
    • Tuple
    • Lists of Lists
    • Representing a Grid or a Spreadsheet
    • Representing the World of an Adventure Game............................................................
    • Reading a Comma-Separated Value (.csv) File
    • Dictionary
    • Using the in Operator on a Dictionary
    • Programming Challenge
    • A Python Dictionary to Represent a Programming Dictionary
    • Iterating Through a Dictionary
    • Combining Lists and Dictionaries
    • JSON: JavaScript Object Notation
      • Example Program to Get Weather Data xv
      • XML Data
      • Accessing Repeating Groupings in JSON and XML
      • Summary
  • ■Chapter 12: Where to Go from Here
    • Python Language Documentation
    • Python Standard Library...............................................................................................
    • Python External Packages
    • Python Development Environments
    • Places to Find Answers to Questions
    • Projects and Practice, Practice, Practice
    • Summary
  • Index

xix

About the Technical Reviewer

Michael Thomas has worked in software development for more than 20 years as an individual contributor, team lead, program manager, and vice president of engineering. Michael has more than 10 years of experience working with mobile devices. His current focus is in the medical sector, using mobile devices to accelerate information transfer between patients and health care providers.