Docsity

Introduction to Python Interpreter and Programming Basics - Prof. Leona Mcmillan, Study notes of Computer Science

Partial preview of the text

Download Introduction to Python Interpreter and Programming Basics - Prof. Leona Mcmillan and more Study notes Computer Science in PDF only on Docsity!

You Need an Interpreter!

Closing the GAP

  • Thus far, we’ve been struggling to speak to computers in “their” language, maybe its time we spoke to them in ours
  • How “high” can we rasie the level of discourse?
  • We’ll enlist the aid of an interpreter
  • An “interpreted” language uses the computer to do all the hard work of figuring out what is “meant” by a statement. It figures out what instructions to execute, where variables are stored, and how best to accomplish the goals of the program given a high-level specification

Getting Started

  • Type the following commands at the “>>>” prompt
  • After entering a couple of commands, try using the “up-arrow” key to bring back an old command

Feeling Loopy?

  • Let’s write a simple loop to repeat a command several times Enter a “tab” to set off the “body” of the loop, which is the part that gets repeated. Type a “return” at the end of the body.

What’s in a “Range”

  • A “Range” generates a “list” of values to set a variable to.
  • Three versions range(whileLessThan) # starts at ‘ 0 ’ range(first, whileLessThan) range(first, whileLessThan, step)

Or Make your own List

  • A “for” loop will replace the variable with the items from any user-specified list
  • Lists can hold numbers, “strings”, or variables

Loops with Strings

  • You’ve seen “range()”
  • Another built in function is len()

A Function

  • Let’s try some simple user-defined functions
  • Functions can call other functions Two levels of indention. One for the body of the function, another for the subbody of the “for” loop

More Lists

  • Lists are very flexible variable types
  • Can be “indexed” and “sliced” like strings
  • Also, sorted, modified, shortened, an enlarged

Conditionals

  • “if/then/else” allow statements to be executed conditionally
  • Can use “and” and “or” to combine conditions

A Simple Text Editor

  • Python provides a simple text editor called “Idle”
  • Operating system also provides a simple editor (Windows XP: Notepad, Mac OSX: TextEdit)
  • Idle
    • Python Aware, and thus tries to help you with your program
    • It does not conform to user-interface standards

Creating a File

  1. On the “File” Menu Choose “New Window”
  2. Type in the simple function and the following for loop Notice how Idle automatically highlights keywords
  3. When finished save the file as “HexDigits.py”

Getting User Input

  • We used a new built in function at the end of this program called raw_input()
  • raw_input(“prompt string”) prints the prompt and then reads an input string from the user
  • In our example we ignored the input string, but we could have saved it in a variable. Ex. x = raw_input(“What is your name?”)
  • This will allow us to write more interesting programs that interact with us

A Simple Game

  • Let’s use what we’ve learned so far to write a simple number guessing game
  • Save as “YouGuess.py”
  • Run using F