Guide to Programming with Python
Chapter OneGetting Started: The Game Over Program
2
Fm: http://xkcd.com/353/
Guide to Programming with Python 3
Objectives Introduce Python
– History– High-level language (computer program &
algorithms)– Interpreted language
Get started with Python– Python installation – Statements (e.g., print text to the screen)– Comments & application
Demonstrate Python’s development environment, IDLE: iterative / script modes
Guide to Programming with Python 4
Hello World / Game Starts
“Hello World” program: By tradition, prints "Hello, world!” – Often used as first program– print “Hello world”
Console window: Provides a text-based interface to Windows operating system
Terminal application: Provides a text-based interface to Mac OS X and Linux operating systems
Guide to Programming with Python 5
Introducing Python Powerful yet easy to use programming language
(platform independent) Developed by Guido van Rossum First released in 1991 Named after comedy troupe Monty Python Getting more popular
– Python is free and open source– Has strong community
High-level programming Language Interpreted language Object oriented
(image: stock.xchg, No.655639)
Algorithms & Computer programs An algorithm is a set of well-defined instructions for
accomplishing a task (e.g., S’mores algorithm for making s'mores)
When we write computer program, we are generally implementing a method (an algorithm) devised previously to solve some problem.
A computer program is a sequence of instructions that are executed by a CPU
Computer programs can be written in high-level (e.g., Python, Perl, C, C++, Java), or primitive programming languages
Guide to Programming with Python 6
High-level Programming Language High-level programming languages separate from
the low-level processor operations; closer to human language than machine language (machine code or machine language, a system of instructions and data executed directly by a computer's central processing unit, CPU.)
This greater abstraction and hiding of details is generally intended to make the language user-friendly ("Programming at the speed of thought”), but maybe less efficient (but it is not a big problem in many cases with powerful personal computers)
Guide to Programming with Python 7
Guide to Programming with Python 8
Python Is a “Glue” Language
Can be integrated with other languages– C/C++– Java
Use existing code Leverage strengths of other languages
– Extra speed that C or C++ offers
Python is Interpreted Language A python program is just a text file; How can a computer
know what to do? Different models of execution for modern high-level languages
Interpreted – Interpreted languages are read and then executed
directly, with no compilation stage.– Examples: Python, Perl, Ruby, Java
Compiled – Compiled languages are transformed into an executable
form before running.– Examples: C (and C++)
Comparison of “Hello World” in Python, Perl, and C
Guide to Programming with Python 9
Guide to Programming with Python 10
Setting up Python Windows Linux
– Python probably already installed– Test: try running python at command prompt
Mac OS 10.5.x and up– Lion (10.7) Python 2.7.2 installed; IDLE 2.7.2 (
http://www.python.org/idle/), Tk version 8.5– Leopard (10.5.x) already has Python 2.5.1 installed, but you need
to install IDLE.app following instructions at http://wiki.python.org/moin/MacPython/Leopard
If not installed, go to http://www.python.org/download/
Remember: your computer needs a python “interpreter” to understand python codes
Guide to Programming with Python 11
Introducing IDLE
Integrated Development Environment (IDE): Application that helps software developers write programs– Like a word processor for your code
IDE that ships with Python Has two “modes”: Interactive and Script (see
demo)
But using which editing program (IDLE, or whatever) is really personal taste.
Interactive and Script Mode
Iterative mode: You tell Python what to do, and it will do it immediately
Script mode: You write, edit, load, and save python programs (just like you write a document in Word processor, and any other types of text processors). But definitely you do not want to write your Python codes with Word!
Guide to Programming with Python 12
Guide to Programming with Python 13
Programming in Interactive Mode Great for immediate feedback
– Test a simple idea– Remember how something works
Open Python in interactive mode– In Windows, from the Start menu, choose Programs, Python
<version>, IDLE (Python GUI) On STC Lab machines
– Windows: Will be in Start menu > All Programs > Departmentally Sponsored > Informatics
– Mac: Type python in /Applications/Utilities/Terminal.app or run IDLE.app from the Developer Tools folder in the Dock
Command prompt >>>
Guide to Programming with Python 14
Jargon Statement: Single unit in programming language
that performs some action– print "Game Over”
print Statement can display a string (actually, any expression)
String: Sequence of characters Expression: Something which has a value or that
can be evaluated to a single value– "Game Over"– 7 + 2
Code: Sequence of programming statements
Guide to Programming with Python 15
Jargon Syntax highlighting: Displaying programming
code in different colors or fonts, according to the category of each item
Errors– Computers take everything literally– primt "Game Over" produces an Error Message: SyntaxError: invalid syntax
– Syntax error: Error in the rules of usage; often a typo (versus logic error)
– Bug: Error in programming code
Guide to Programming with Python 16
Programming in Script Mode
Great for programs you want to run later– Write, edit, save, and load programs– Like word processor for your programs
• Find and replace
• Cut and paste
Open a script window– In interactive window, select File menu, New
Window
Guide to Programming with Python 17
The Game Over Program
Comment: Note in source code meant only for programmers; ignored by computer– Start comment with #– Use opening block of comments
Blank Lines– Also (generally) ignored by computer– Use for readability; keep related code together
Console Window– Final line keeps console window open
Guide to Programming with Python 18
Python Is Object-Oriented
Object-oriented programming (OOP): Methodology that defines problems in terms of objects that send messages to each other– dir(1) – In a game, a Missile object could send a Ship
object a message to Explode OOP not required, unlike Java and C#
Guide to Programming with Python 19
Summary
Python is a high-level, object-oriented programming language that’s powerful yet easy to use
Python can interface with other programming languages
IDLE is Python’s standard IDE IDLE has an interactive mode that offers immediate
response to Python code IDLE has a script mode that allows programmers to
write, edit, load, save, and run their programs
Guide to Programming with Python 20
Summary (continued)
A string is a sequence of characters A statement is a single unit of programming that
performs some action The print statement displays strings on the screen An expression is something which has a value or
that can be evaluated to a single value Syntax highlighting is displaying programming code
in different colors or fonts, according to the category of each item
Guide to Programming with Python 21
Summary (continued)
A syntax error is a violation of the grammar of a programming language; often caused by a typo (we will see logic error)
A bug is an error in programming code A comment is a note in source code meant only for
programmers; ignored by computer Comments start with # You should use an opening block of comments in
your programs to identify the programmer, the creation date, and the program’s purpose