august 29, 2005icp: chapter 1: introduction to python programming 1 introduction to computer...

14
August 29, 2005 ICP: Chapter 1: Introduct ion to Python Programming 1 Introduction to Computer Programming Chapter 1: Introduction to Python Programming Michael Scherger Department of Computer Science Kent State University

Upload: nicholas-norton

Post on 31-Dec-2015

224 views

Category:

Documents


8 download

TRANSCRIPT

Page 1: August 29, 2005ICP: Chapter 1: Introduction to Python Programming 1 Introduction to Computer Programming Chapter 1: Introduction to Python Programming

August 29, 2005 ICP: Chapter 1: Introduction to Python Programming

1

Introduction to Computer Programming

Chapter 1: Introduction to Python Programming

Michael Scherger

Department of Computer Science

Kent State University

Page 2: August 29, 2005ICP: Chapter 1: Introduction to Python Programming 1 Introduction to Computer Programming Chapter 1: Introduction to Python Programming

August 29, 2005 ICP: Chapter 1: Introduction to Python Programming

2

The Game Over Program

Page 3: August 29, 2005ICP: Chapter 1: Introduction to Python Programming 1 Introduction to Computer Programming Chapter 1: Introduction to Python Programming

August 29, 2005 ICP: Chapter 1: Introduction to Python Programming

3

History of Python

• Created in 1989 by Guido van Rossum– Created as a scripting language for administrative

tasks– Based on All Basic Code (ABC) and Modula-3

• Added extensibility

– Named after comic troupe Monty Python

• Released publicly in 1991– Growing community of Python developers– Evolved into well-supported programming language

Page 4: August 29, 2005ICP: Chapter 1: Introduction to Python Programming 1 Introduction to Computer Programming Chapter 1: Introduction to Python Programming

August 29, 2005 ICP: Chapter 1: Introduction to Python Programming

4

History of Python

• Modules– Reusable pieces of software– Can be written by any Python developer– Extend Python’s capabilities

• Python Web site at www.python.org– Primary distribution center for Python source

code, modules and documentation

Page 5: August 29, 2005ICP: Chapter 1: Introduction to Python Programming 1 Introduction to Computer Programming Chapter 1: Introduction to Python Programming

August 29, 2005 ICP: Chapter 1: Introduction to Python Programming

5

History of Python

• Python– Designed to be portable and extensible

• Originally implemented on UNIX• Programs often can be ported from one operating

system to another without any change

– Syntax and design promote good programming practices and surprisingly rapid development times

• Simple enough to be used by beginning programmers

• Powerful enough to attract professionals

Page 6: August 29, 2005ICP: Chapter 1: Introduction to Python Programming 1 Introduction to Computer Programming Chapter 1: Introduction to Python Programming

August 29, 2005 ICP: Chapter 1: Introduction to Python Programming

6

Setting Up Python on Windows

• Go to http://www.python.org and get the latest distribution (2.4.1)– Online tutorials– Python related websites

• Use the distribution on the CD ROM supplied with the textbook– Examples from the book

• Use all the defaults when installing

Page 7: August 29, 2005ICP: Chapter 1: Introduction to Python Programming 1 Introduction to Computer Programming Chapter 1: Introduction to Python Programming

August 29, 2005 ICP: Chapter 1: Introduction to Python Programming

7

Python IDLE

Page 8: August 29, 2005ICP: Chapter 1: Introduction to Python Programming 1 Introduction to Computer Programming Chapter 1: Introduction to Python Programming

August 29, 2005 ICP: Chapter 1: Introduction to Python Programming

8

Your First Python Program

• At the prompt (>>>) type:

print “Game Over”– Press [Enter]

• Very straightforward– You could have guessed what this does

without knowing Python!

Page 9: August 29, 2005ICP: Chapter 1: Introduction to Python Programming 1 Introduction to Computer Programming Chapter 1: Introduction to Python Programming

August 29, 2005 ICP: Chapter 1: Introduction to Python Programming

9

Your First Python Program

• Python is “case-sensitive”– print “Game Over”– Print “Game Over”– PRINT “Game Over”

Page 10: August 29, 2005ICP: Chapter 1: Introduction to Python Programming 1 Introduction to Computer Programming Chapter 1: Introduction to Python Programming

August 29, 2005 ICP: Chapter 1: Introduction to Python Programming

10

Your First Python Program

• In Python, this computer instruction is called a “statement”– Command (like a verb) (print)– Expression (like a value) (“Game Over”)

• More specifically, “Game Over” is called a string expression– A series of characters between “ “

Page 11: August 29, 2005ICP: Chapter 1: Introduction to Python Programming 1 Introduction to Computer Programming Chapter 1: Introduction to Python Programming

August 29, 2005 ICP: Chapter 1: Introduction to Python Programming

11

Syntax Errors

• When the computer does not recognize the statement to be executed, a syntax error is generated

• Analogous to a misspelled word in a programming language– Bug

>>> primt “Game Over”SyntaxError: invalid syntax

Page 12: August 29, 2005ICP: Chapter 1: Introduction to Python Programming 1 Introduction to Computer Programming Chapter 1: Introduction to Python Programming

August 29, 2005 ICP: Chapter 1: Introduction to Python Programming

12

Programming in Script Mode

• Interactive mode gives you immediate feedback

• Not designed to create programs to save and run later

• Script Mode– Write, edit, save, and run (later)

• Word processor for your code

• Save your file using the “.py” extension

Page 13: August 29, 2005ICP: Chapter 1: Introduction to Python Programming 1 Introduction to Computer Programming Chapter 1: Introduction to Python Programming

August 29, 2005 ICP: Chapter 1: Introduction to Python Programming

13

Program Documentation

• Comment lines provide documentation about your program– Anything after the “#” symbol is a comment– Ignored by the computer

# Ima P Programmer

# First Python Program

# September 1, 2005

Page 14: August 29, 2005ICP: Chapter 1: Introduction to Python Programming 1 Introduction to Computer Programming Chapter 1: Introduction to Python Programming

August 29, 2005 ICP: Chapter 1: Introduction to Python Programming

14

Text Examples

• Game Over Example