think python chapter 1

Upload: joe-meyer

Post on 02-Jun-2018

237 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/10/2019 Think Python Chapter 1

    1/15

    Think Python, Chapter 1

    CS 1400: Fundamentals of Computing Prof. Cindy Marling 1

    Think Python:How to Think Like a Computer Scientist

    Allen B. Downey

    http://www.greenteapress.com/thinkpython/thinkpython.pdf

  • 8/10/2019 Think Python Chapter 1

    2/15

    Chapter 1: The Way of the Program

    This book aims to teach you to think like a computer scientist These thought processes draw from mathematics, engineering, and

    natural sciences

    Problem solving is the most important skill

    Computer scientists must formulate problems, find creativesolutions to problems, and express these solutions clearly

    Writing computer programs requires problem solving Programming is a useful practical skill

    Programming helps people become better problem solvers

    CS 1400: Fundamentals of Computing Prof. Cindy Marling 2

  • 8/10/2019 Think Python Chapter 1

    3/15

    The Python Programming Language

    Python is a high-level programming language Most computer programs are written in high-level

    programming languages and then converted into low-level forms that computers can execute

    This conversion is accomplished by other computerprograms, interpretersand compilers

    Python is an interpreted language An interpreter reads a Python program and does

    what the program says

    It processes the program a little at a time,executing the lines of the program as it readsthem

    CS 1400: Fundamentals of Computing Prof. Cindy Marling 3

  • 8/10/2019 Think Python Chapter 1

    4/15

    More on Python

    Languages can be interpreted, like Python, or compiled A compiler reads and converts a whole program before it starts running

    Interpreters allow more flexibility, while compilers optimize speed

    Python can be used in interactive mode or in script mode

    In interactive mode, you type Python programs directly into theinterpreter, and the interpreter shows the results

    >>> 1 + 1

    2 The interpreter displays a prompt, >>>, to show that it is ready to

    process what you type

    CS 1400: Fundamentals of Computing Prof. Cindy Marling 4

  • 8/10/2019 Think Python Chapter 1

    5/15

    Pythons Script Mode

    When programs grow longer than a few lines, it is handy to keep themin files, called scripts

    You can open a script in the interpreter to use it without typing it all in

    Our Python environment, IDLE, has a Filedrop-down menu, whichallows you to Opena Python script, much as you would open a file inMicrosoft Word (or the software package of your choice)

    It has a Rundrop-down menu, with a Run Module option, that tells theinterpreter to execute the script that you opened

    This will become Extremely Clear shortly!

    CS 1400: Fundamentals of Computing Prof. Cindy Marling 5

  • 8/10/2019 Think Python Chapter 1

    6/15

    What is a Program?

    Aprogramis a sequence of instructions that specifies how to perform acomputation

    In other words, when you write a program, you tell the computerwhat to do

    The computer can do mathematical computations, computationsthat create and display presentation slides (like these),

    computations that play music, plot maps, or whatever else you canfigure out how to tell it to do!

    CS 1400: Fundamentals of Computing Prof. Cindy Marling 6

  • 8/10/2019 Think Python Chapter 1

    7/15

    Basic Instructions in Computer Programs

    Different programming languages have different details, but the basicinstructions are similar in all of them. These are:

    Input: Get data from a computer keyboard, file, or other device

    Output: Display data on a screen, or send it to a file or device

    Math: Perform operations like addition and subtraction

    Conditional Execution: Check for certain conditions and execute the

    code that applies to the current condition Example: If severe weather is expected, display an alert on a cell

    phone. (But dont display alerts on bright, sunny days.)

    Repetition: Perform some instruction repeatedly, usually with somevariation

    Example: Send a welcome message to every new freshman, but

    vary the email address for each message

    CS 1400: Fundamentals of Computing Prof. Cindy Marling 7

  • 8/10/2019 Think Python Chapter 1

    8/15

    What is Debugging?

    Debuggingis the process of finding and fixing errors in programs It would be great if we all wrote error-free code, but even the bestprogrammers run up against bugs!

    The first computer bug was an actual moth, found in a US Navycomputer by Admiral Grace Hoppers team, in 1947. This moth is in theSmithsonian Institutions National Museum of American History.

    CS 1400: Fundamentals of Computing Prof. Cindy Marling 8

  • 8/10/2019 Think Python Chapter 1

    9/15

    Three Kinds of Bugs

    Syntax Errors are mistakes in the form of the program

    These are like spelling or punctuation mistakes in essays

    The interpreter will display an error message instead of runningyour program

    Runtime Errors are exceptions that occur after the program starts

    running The syntax was OK, but perhaps the program cant access a

    resource it needs, like a file or a database

    The program will start running, but it will be unable to finish andwill crash

    Semantic Errors occur when the program gives the wrong answer or

    does something it shouldnt do The computer does what you say, not what you mean

    The program will run to completion, but you will not get theresults you want

    CS 1400: Fundamentals of Computing Prof. Cindy Marling 9

  • 8/10/2019 Think Python Chapter 1

    10/15

    The Debugging Process

    Debugging is something like detective work Look for clues to figure out why the program doesnt work

    Debugging is something like experimental science

    Develop a hypothesis about what went wrong, modify the program,and try again

    If the program still doesnt work, come up with a new hypothesis,

    and start again Debugging may seem very different from other intellectual pursuits

    It can bring out strong emotions

    It is natural for people get frustrated, angry, sad, or embarrassed

    The computer may seem like an opponent, rather than a tool

    When this happens: Try to stay calm (Take a break, if you have to)

    Practice your problem-solving skills

    Experience satisfaction once bugs are found and fixed

    CS 1400: Fundamentals of Computing Prof. Cindy Marling 10

  • 8/10/2019 Think Python Chapter 1

    11/15

    Formal Languages vs. Natural Languages

    Natural languages are the ones people naturally speak, like English andChinese

    Formal languages are designed by people for specific purposes, likenotations for expressing mathematical formulas or representations ofthe chemical structure of molecules

    Programming languages are formal languages designed to express

    computations Formal languages have strict rules about syntax

    3 + 3 = 6 and H20 are syntactically valid

    3 + = @ 6 and 2HO are not

    Syntaxincludes tokens(basic elements) and structure(arrangement)

    3, +, =, and 6 are valid mathematical tokens, but @ is not

    2HO has invalid structure, because subscripts must come after, notbefore, the names of elements in chemical formulas

    Figuring out the structure of a statement, in a formal language, or asentence, in a natural language, is calledparsing

    CS 1400: Fundamentals of Computing Prof. Cindy Marling 11

  • 8/10/2019 Think Python Chapter 1

    12/15

    More on Language Differences

    People who grew up speaking natural languages (all of us!) may havetrouble adjusting to formal languages

    Here are some differences to keep in mind

    Ambiguity: Natural language has ambiguity, which people deal withthrough context. Example: Ill get it. Ill understand it? Ill obtain it?Ill answer it? In a formal language, a statement must have only one

    meaning. Redundancy: To make up for ambiguity, natural languages mayemploy redundancy, or be verbose. Formal languages are moreconcise. It takes longer to read and understand a program than thesame amount of text in a natural language.

    Literalness: Natural languages use idiom and metaphor. Examples:Raining cats and dogs; Costs an arm and a leg. Formal languagesmean exactly what they say.

    CS 1400: Fundamentals of Computing Prof. Cindy Marling 12

  • 8/10/2019 Think Python Chapter 1

    13/15

  • 8/10/2019 Think Python Chapter 1

    14/15

    The First Program

    Traditionally, the first program you write in any language is the Hello,World! program

    This program writes the words Hello, World! to the screen

    Here it is in Python:

    >>> print 'Hello, World!'

    Hello, World!

    CS 1400: Fundamentals of Computing Prof. Cindy Marling 14

  • 8/10/2019 Think Python Chapter 1

    15/15

    About the Glossary

    There is a glossary of important vocabulary terms at the end of eachchapter in Think Python

    Be sure to review the glossary!

    A common vocabulary helps us to communicate more effectively

    Terms from the glossary will appear on quizzes and exams

    CS 1400: Fundamentals of Computing Prof. Cindy Marling 15