SlideShare ist ein Scribd-Unternehmen logo
1 von 34
Downloaden Sie, um offline zu lesen
Guide to Programming with
Python
Chapter One
Getting Started: The Game Over Program
What is programming
• Computer Science- The study of theoretical foundations of
information and computation and their implementation and
application in computer systems. It is mainly about
computational problem solving.
• The process of learning to program is an excellent
opportunity to practice problem-solving skills.
• Program: A computation expressed in a programming
language as a set of instructions to be carried out by a
computer.
• The computation might be something mathematical, such
as solving a system of equations or finding the roots of a
polynomial, but it can also be a symbolic computation, such
as searching and replacing text in a document or (strangely
enough) compiling a program!
• A program basically contains following instructions
• The details look different in different languages, but these
few basic instructions appear in just about every language
-input: Get data from the keyboard, a file, or some other
device.
-output: Display data on the screen or send data to a file or
other device.
-Calculations: Perform basic mathematical operations like
addition and multiplication.
-Conditional execution: (Boolean tests), Check for certain
conditions and execute the appropriate code.
Repetition: Perform some action repeatedly, usually with
some variation.
• Program execution: The act of carrying out the
instructions contained in a program.
• Programming language: Is an artificial language, a
systematic set of rules used to describe computations in a
format that is editable by humans.
– This textbook teaches programming in a language Python
Objectives
•
•
•
•
•

Introduce Python
Demonstrate how to install Python
Explain how to print text to the screen
Describe comments and how to use them
Demonstrate Python’s development environment,
IDLE, using it to write, edit, run, and save programs

Guide to Programming with Python

5
Examining the Game Over Program

Figure 1.1: Game Over Program Output
The all-too familiar words from a computer game
Guide to Programming with Python

6
Examining the Game Over Program
(continued)
• “Hello World” program: By tradition, prints "Hello,
world!”
– Often used as first program

• Console window: Provides a text-based interface
to operating system

Guide to Programming with Python

7
Introducing Python
•
•
•
•
•

Powerful yet easy to use programming language
Developed by Guido van Rossum
First released in 1991
Named after comedy troupe Monty Python
An alarming number of references to spam, eggs,
and the number 42 in documentation

Guide to Programming with Python

8
Python Is Easy to Use
• High-level language: Separate from the low-level
processor operations; closer to human language
than machine language
• "Programming at the speed of thought"
• Increases productivity
– Python programs three to five times shorter than
Java
– Python programs five to ten times shorter than C++

Guide to Programming with Python

9
Python Is Easy to Use (continued)
• Python Program
print "Game Over!"

• C++ Program
#include <iostream>
int main()
{
std::cout << "Game Over!" << std::endl;
return 0;
}

Guide to Programming with Python

10
Python Is Powerful
• Used by large organizations
– NASA
– Google
– Microsoft

• Used in published games
– Battlefield 2
– Civilization IV
– Disney’s Toontown Online

Guide to Programming with Python

11
Python Is Object-Oriented
• Object-oriented programming (OOP):
Methodology that defines problems in terms of
objects that send messages to each other
– 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

12
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++ might offer

Guide to Programming with Python

13
Python Runs Everywhere
• Platform independent: Independent of the
specific computer operating system
• Python runs on
–
–
–
–
–

Windows
DOS
Mac OS
Linux
Many more

Guide to Programming with Python

14
Python Has a Strong Community
• As an approachable language, has approachable
community
• Python Tutor mailing list
– http://mail.python.org/mailman/listinfo/tutor
– Perfect for beginners
– No actual "tutors" or "students"

Guide to Programming with Python

15
Python Is Free and Open Source
• Open source: Publicly available; open source
software typically programmed by volunteers;
anyone can use source code without fee
• Can modify or even resell Python
• Embracing open-source ideals is part of what
makes Python successful

Guide to Programming with Python

16
Setting up Python on Windows
•

Using the CD-ROM that came with the book
1. Find the Python Windows Installer, Python2.3.5.exe, under the Software section
2. Click on the Install Python 2.3.5 from this CDROM link and run the installer
3. Accept the default configuration
4. Version 2.3.5 installed in the C:Python23 folder

Guide to Programming with Python

17
Setting up Python on Windows
(continued)

Figure 1.2: Python Installation Dialogue under Windows
Your computer is soon to be home to Python.
Guide to Programming with Python

18
Setting up Python on Other Operating
Systems
• Download appropriate version from Python home
page at www.python.org
• Linux
– Python may already be installed
– Test: try running python at command prompt
– If not installed, go to Python home page to download

• Mac OS
– After Python page, visit MacPython page at
http://homepages.cwi.nl/~jack/macpython/index.html

Guide to Programming with Python

19
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

Guide to Programming with Python

20
Programming in Interactive Mode

Figure 1.4: Python in interactive mode
Python awaits your command.
Guide to Programming with Python

21
Programming in Interactive Mode
(continued)
• 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 2.3, IDLE (Python GUI)

Guide to Programming with Python

22
Programming in Interactive Mode
(continued)
• At command prompt (>>>), type: print
• Python responds with: Game Over

Guide to Programming with Python

"Game Over"

23
Programming in Interactive Mode
(continued)
Statement can display a string
• String: Sequence of characters
• Statement: Single unit in programming language
that performs some action
• print

– print "Game Over"

• 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

24
Programming in Interactive Mode
(continued)
• 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
– Bug: Error in programming code
Guide to Programming with Python

25
Programming in Script Mode

Figure 1.5: Python in script mode
Your blank canvas awaits.
Guide to Programming with Python

26
Programming in Script Mode
(continued)
• 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

27
Programming in Script Mode
(Continued)
• Write program
– In script window, type print "Game Over"

• Save program
– Select File, Save As, name game_over.py
– Always save before running

• Run Program
– Select Run, Run Module
– Results displayed in interactive window

Guide to Programming with Python

28
Programming in Script Mode
(continued)

Figure 1.6: Python after a script has been run
The results of running the Game Over program
Guide to Programming with Python

29
The Game Over Program
# Game Over
# Demonstrates the print command
print "Game Over"
raw_input("nnPress the enter key to exit.")

Guide to Programming with Python

30
The Game Over Program (continued)
• 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

31
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

32
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

33
Summary (continued)
• A syntax error is a violation of the grammar of a
programming language; often caused by a typo 
• 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 
Guide to Programming with Python

34

Weitere ähnliche Inhalte

Was ist angesagt?

Programming Languages / Translators
Programming Languages / TranslatorsProgramming Languages / Translators
Programming Languages / TranslatorsProject Student
 
Embedded systems tools & peripherals
Embedded systems   tools & peripheralsEmbedded systems   tools & peripherals
Embedded systems tools & peripheralsimtiazalijoono
 
11 Unit1 Chapter 1 Getting Started With Python
11   Unit1 Chapter 1 Getting Started With Python11   Unit1 Chapter 1 Getting Started With Python
11 Unit1 Chapter 1 Getting Started With PythonPraveen M Jigajinni
 
Btech i pic u-1 introduction to c language
Btech i pic u-1 introduction to c languageBtech i pic u-1 introduction to c language
Btech i pic u-1 introduction to c languageRai University
 
Intro To Programming Concepts
Intro To Programming ConceptsIntro To Programming Concepts
Intro To Programming ConceptsJussi Pohjolainen
 
Introduction to Python Programming - I
Introduction to Python Programming  - IIntroduction to Python Programming  - I
Introduction to Python Programming - IArnab Chakraborty
 
Embedded System Tools ppt
Embedded System Tools  pptEmbedded System Tools  ppt
Embedded System Tools pptHalai Hansika
 
Python programming
Python programmingPython programming
Python programmingMegha V
 
CSO Laboratory Manual
CSO Laboratory ManualCSO Laboratory Manual
CSO Laboratory ManualDwight Sabio
 
An Introduction To Python - Python, Print()
An Introduction To Python - Python, Print()An Introduction To Python - Python, Print()
An Introduction To Python - Python, Print()Blue Elephant Consulting
 
Diploma ii cfpc u-1 introduction to c language
Diploma ii  cfpc u-1 introduction to c languageDiploma ii  cfpc u-1 introduction to c language
Diploma ii cfpc u-1 introduction to c languageRai University
 
Mca i pic u-1 introduction to c language
Mca i pic u-1 introduction to c languageMca i pic u-1 introduction to c language
Mca i pic u-1 introduction to c languageRai University
 
Compilers programmingembedded
Compilers programmingembeddedCompilers programmingembedded
Compilers programmingembeddedManish Pandey
 
Computer programming - turbo c environment
Computer programming - turbo c environmentComputer programming - turbo c environment
Computer programming - turbo c environmentJohn Paul Espino
 
What is turbo c and how it works
What is turbo c and how it worksWhat is turbo c and how it works
What is turbo c and how it worksMark John Lado, MIT
 

Was ist angesagt? (19)

Introduction python
Introduction pythonIntroduction python
Introduction python
 
Programming Languages / Translators
Programming Languages / TranslatorsProgramming Languages / Translators
Programming Languages / Translators
 
Python Lecture 1
Python Lecture 1Python Lecture 1
Python Lecture 1
 
Computer programming
Computer programmingComputer programming
Computer programming
 
Embedded systems tools & peripherals
Embedded systems   tools & peripheralsEmbedded systems   tools & peripherals
Embedded systems tools & peripherals
 
11 Unit1 Chapter 1 Getting Started With Python
11   Unit1 Chapter 1 Getting Started With Python11   Unit1 Chapter 1 Getting Started With Python
11 Unit1 Chapter 1 Getting Started With Python
 
Btech i pic u-1 introduction to c language
Btech i pic u-1 introduction to c languageBtech i pic u-1 introduction to c language
Btech i pic u-1 introduction to c language
 
Intro To Programming Concepts
Intro To Programming ConceptsIntro To Programming Concepts
Intro To Programming Concepts
 
Introduction to Python Programming - I
Introduction to Python Programming  - IIntroduction to Python Programming  - I
Introduction to Python Programming - I
 
Embedded System Tools ppt
Embedded System Tools  pptEmbedded System Tools  ppt
Embedded System Tools ppt
 
Python programming
Python programmingPython programming
Python programming
 
CSO Laboratory Manual
CSO Laboratory ManualCSO Laboratory Manual
CSO Laboratory Manual
 
An Introduction To Python - Python, Print()
An Introduction To Python - Python, Print()An Introduction To Python - Python, Print()
An Introduction To Python - Python, Print()
 
Diploma ii cfpc u-1 introduction to c language
Diploma ii  cfpc u-1 introduction to c languageDiploma ii  cfpc u-1 introduction to c language
Diploma ii cfpc u-1 introduction to c language
 
Handson Python
Handson PythonHandson Python
Handson Python
 
Mca i pic u-1 introduction to c language
Mca i pic u-1 introduction to c languageMca i pic u-1 introduction to c language
Mca i pic u-1 introduction to c language
 
Compilers programmingembedded
Compilers programmingembeddedCompilers programmingembedded
Compilers programmingembedded
 
Computer programming - turbo c environment
Computer programming - turbo c environmentComputer programming - turbo c environment
Computer programming - turbo c environment
 
What is turbo c and how it works
What is turbo c and how it worksWhat is turbo c and how it works
What is turbo c and how it works
 

Andere mochten auch

轻量级Flash服务器开发框架(刘恒)
轻量级Flash服务器开发框架(刘恒)轻量级Flash服务器开发框架(刘恒)
轻量级Flash服务器开发框架(刘恒)FLASH开发者交流会
 
Mp24: Python in gaming industry
Mp24: Python in gaming industryMp24: Python in gaming industry
Mp24: Python in gaming industryMontreal Python
 
FluidDB NYC Python presentation
FluidDB NYC Python presentationFluidDB NYC Python presentation
FluidDB NYC Python presentationTerry Jones
 
Tickery, Pyjamas and FluidDB
Tickery, Pyjamas and FluidDBTickery, Pyjamas and FluidDB
Tickery, Pyjamas and FluidDBTerry Jones
 
Python games
Python gamesPython games
Python gamesmolw
 
Battlelog - Building scalable web sites with tight game integration
Battlelog - Building scalable web sites with tight game integrationBattlelog - Building scalable web sites with tight game integration
Battlelog - Building scalable web sites with tight game integrationElectronic Arts / DICE
 

Andere mochten auch (6)

轻量级Flash服务器开发框架(刘恒)
轻量级Flash服务器开发框架(刘恒)轻量级Flash服务器开发框架(刘恒)
轻量级Flash服务器开发框架(刘恒)
 
Mp24: Python in gaming industry
Mp24: Python in gaming industryMp24: Python in gaming industry
Mp24: Python in gaming industry
 
FluidDB NYC Python presentation
FluidDB NYC Python presentationFluidDB NYC Python presentation
FluidDB NYC Python presentation
 
Tickery, Pyjamas and FluidDB
Tickery, Pyjamas and FluidDBTickery, Pyjamas and FluidDB
Tickery, Pyjamas and FluidDB
 
Python games
Python gamesPython games
Python games
 
Battlelog - Building scalable web sites with tight game integration
Battlelog - Building scalable web sites with tight game integrationBattlelog - Building scalable web sites with tight game integration
Battlelog - Building scalable web sites with tight game integration
 

Ähnlich wie GUIDE TO PROGRAMMING WITH PYTHON

Python Book/Notes For Python Book/Notes For S.Y.B.Sc. I.T.
Python Book/Notes For Python Book/Notes For S.Y.B.Sc. I.T.Python Book/Notes For Python Book/Notes For S.Y.B.Sc. I.T.
Python Book/Notes For Python Book/Notes For S.Y.B.Sc. I.T.Niraj Bharambe
 
PE1 Module 1.ppt
PE1 Module 1.pptPE1 Module 1.ppt
PE1 Module 1.pptbalewayalew
 
Introduction to python for Beginners
Introduction to python for Beginners Introduction to python for Beginners
Introduction to python for Beginners Sujith Kumar
 
Python Tutorial | Python Programming Language
Python Tutorial | Python Programming LanguagePython Tutorial | Python Programming Language
Python Tutorial | Python Programming Languageanaveenkumar4
 
intro.pptx (1).pdf
intro.pptx (1).pdfintro.pptx (1).pdf
intro.pptx (1).pdfANIKULSAIKH
 
Beginning python programming
Beginning python programmingBeginning python programming
Beginning python programmingkanteshraj
 
ProgFund_Lecture_1_Introduction_to_Programming.pdf
ProgFund_Lecture_1_Introduction_to_Programming.pdfProgFund_Lecture_1_Introduction_to_Programming.pdf
ProgFund_Lecture_1_Introduction_to_Programming.pdflailoesakhan
 
python classes in thane
python classes in thanepython classes in thane
python classes in thanefaizrashid1995
 
Introduction_to_Programming.pptx
Introduction_to_Programming.pptxIntroduction_to_Programming.pptx
Introduction_to_Programming.pptxPmarkNorcio
 
The Python Book_ The ultimate guide to coding with Python ( PDFDrive ).pdf
The Python Book_ The ultimate guide to coding with Python ( PDFDrive ).pdfThe Python Book_ The ultimate guide to coding with Python ( PDFDrive ).pdf
The Python Book_ The ultimate guide to coding with Python ( PDFDrive ).pdfssuser8b3cdd
 
Phython Programming Language
Phython Programming LanguagePhython Programming Language
Phython Programming LanguageR.h. Himel
 
Python_Introduction_Good_PPT.pptx
Python_Introduction_Good_PPT.pptxPython_Introduction_Good_PPT.pptx
Python_Introduction_Good_PPT.pptxlemonchoos
 
Introduction to Python Programming
Introduction to Python ProgrammingIntroduction to Python Programming
Introduction to Python ProgrammingAkhil Kaushik
 
python-160403194316.pdf
python-160403194316.pdfpython-160403194316.pdf
python-160403194316.pdfgmadhu8
 
Python Seminar PPT
Python Seminar PPTPython Seminar PPT
Python Seminar PPTShivam Gupta
 
Introduction to computers and programming languages
Introduction to computers and programming languages Introduction to computers and programming languages
Introduction to computers and programming languages binoysatheesh
 

Ähnlich wie GUIDE TO PROGRAMMING WITH PYTHON (20)

Python Programming
Python ProgrammingPython Programming
Python Programming
 
Python Book/Notes For Python Book/Notes For S.Y.B.Sc. I.T.
Python Book/Notes For Python Book/Notes For S.Y.B.Sc. I.T.Python Book/Notes For Python Book/Notes For S.Y.B.Sc. I.T.
Python Book/Notes For Python Book/Notes For S.Y.B.Sc. I.T.
 
PE1 Module 1.ppt
PE1 Module 1.pptPE1 Module 1.ppt
PE1 Module 1.ppt
 
Introduction to python for Beginners
Introduction to python for Beginners Introduction to python for Beginners
Introduction to python for Beginners
 
Python Tutorial | Python Programming Language
Python Tutorial | Python Programming LanguagePython Tutorial | Python Programming Language
Python Tutorial | Python Programming Language
 
intro.pptx (1).pdf
intro.pptx (1).pdfintro.pptx (1).pdf
intro.pptx (1).pdf
 
Beginning python programming
Beginning python programmingBeginning python programming
Beginning python programming
 
python into.pptx
python into.pptxpython into.pptx
python into.pptx
 
ProgFund_Lecture_1_Introduction_to_Programming.pdf
ProgFund_Lecture_1_Introduction_to_Programming.pdfProgFund_Lecture_1_Introduction_to_Programming.pdf
ProgFund_Lecture_1_Introduction_to_Programming.pdf
 
python classes in thane
python classes in thanepython classes in thane
python classes in thane
 
Introduction_to_Programming.pptx
Introduction_to_Programming.pptxIntroduction_to_Programming.pptx
Introduction_to_Programming.pptx
 
Python Programming Draft PPT.pptx
Python Programming Draft PPT.pptxPython Programming Draft PPT.pptx
Python Programming Draft PPT.pptx
 
The Python Book_ The ultimate guide to coding with Python ( PDFDrive ).pdf
The Python Book_ The ultimate guide to coding with Python ( PDFDrive ).pdfThe Python Book_ The ultimate guide to coding with Python ( PDFDrive ).pdf
The Python Book_ The ultimate guide to coding with Python ( PDFDrive ).pdf
 
Phython Programming Language
Phython Programming LanguagePhython Programming Language
Phython Programming Language
 
Python_Introduction_Good_PPT.pptx
Python_Introduction_Good_PPT.pptxPython_Introduction_Good_PPT.pptx
Python_Introduction_Good_PPT.pptx
 
Introduction to Python Programming
Introduction to Python ProgrammingIntroduction to Python Programming
Introduction to Python Programming
 
python-160403194316.pdf
python-160403194316.pdfpython-160403194316.pdf
python-160403194316.pdf
 
Python
PythonPython
Python
 
Python Seminar PPT
Python Seminar PPTPython Seminar PPT
Python Seminar PPT
 
Introduction to computers and programming languages
Introduction to computers and programming languages Introduction to computers and programming languages
Introduction to computers and programming languages
 

Mehr von Lebogang Modise

Partnership Accounting 2
Partnership Accounting 2Partnership Accounting 2
Partnership Accounting 2Lebogang Modise
 
Partnership accounts capital account
Partnership accounts capital accountPartnership accounts capital account
Partnership accounts capital accountLebogang Modise
 
Receipts and payments accounts 2011week
Receipts and payments accounts 2011weekReceipts and payments accounts 2011week
Receipts and payments accounts 2011weekLebogang Modise
 
Lecture notes 1 manufacturing+accounts 2014
Lecture notes 1 manufacturing+accounts 2014Lecture notes 1 manufacturing+accounts 2014
Lecture notes 1 manufacturing+accounts 2014Lebogang Modise
 
Partnership 20 accounts-goodwill_202
Partnership 20 accounts-goodwill_202Partnership 20 accounts-goodwill_202
Partnership 20 accounts-goodwill_202Lebogang Modise
 

Mehr von Lebogang Modise (10)

Partnership Accounting 2
Partnership Accounting 2Partnership Accounting 2
Partnership Accounting 2
 
Partnership accounts capital account
Partnership accounts capital accountPartnership accounts capital account
Partnership accounts capital account
 
Partnership 1
Partnership 1Partnership 1
Partnership 1
 
Partnership 1
Partnership 1Partnership 1
Partnership 1
 
Nonp 1 slides
Nonp 1 slidesNonp 1 slides
Nonp 1 slides
 
Receipts and payments accounts 2011week
Receipts and payments accounts 2011weekReceipts and payments accounts 2011week
Receipts and payments accounts 2011week
 
Lecture notes 1 manufacturing+accounts 2014
Lecture notes 1 manufacturing+accounts 2014Lecture notes 1 manufacturing+accounts 2014
Lecture notes 1 manufacturing+accounts 2014
 
Incomplete records
Incomplete recordsIncomplete records
Incomplete records
 
Incomplete records (1)
Incomplete records (1)Incomplete records (1)
Incomplete records (1)
 
Partnership 20 accounts-goodwill_202
Partnership 20 accounts-goodwill_202Partnership 20 accounts-goodwill_202
Partnership 20 accounts-goodwill_202
 

GUIDE TO PROGRAMMING WITH PYTHON

  • 1. Guide to Programming with Python Chapter One Getting Started: The Game Over Program
  • 2. What is programming • Computer Science- The study of theoretical foundations of information and computation and their implementation and application in computer systems. It is mainly about computational problem solving. • The process of learning to program is an excellent opportunity to practice problem-solving skills. • Program: A computation expressed in a programming language as a set of instructions to be carried out by a computer. • The computation might be something mathematical, such as solving a system of equations or finding the roots of a polynomial, but it can also be a symbolic computation, such as searching and replacing text in a document or (strangely enough) compiling a program!
  • 3. • A program basically contains following instructions • The details look different in different languages, but these few basic instructions appear in just about every language -input: Get data from the keyboard, a file, or some other device. -output: Display data on the screen or send data to a file or other device. -Calculations: Perform basic mathematical operations like addition and multiplication. -Conditional execution: (Boolean tests), Check for certain conditions and execute the appropriate code. Repetition: Perform some action repeatedly, usually with some variation.
  • 4. • Program execution: The act of carrying out the instructions contained in a program. • Programming language: Is an artificial language, a systematic set of rules used to describe computations in a format that is editable by humans. – This textbook teaches programming in a language Python
  • 5. Objectives • • • • • Introduce Python Demonstrate how to install Python Explain how to print text to the screen Describe comments and how to use them Demonstrate Python’s development environment, IDLE, using it to write, edit, run, and save programs Guide to Programming with Python 5
  • 6. Examining the Game Over Program Figure 1.1: Game Over Program Output The all-too familiar words from a computer game Guide to Programming with Python 6
  • 7. Examining the Game Over Program (continued) • “Hello World” program: By tradition, prints "Hello, world!” – Often used as first program • Console window: Provides a text-based interface to operating system Guide to Programming with Python 7
  • 8. Introducing Python • • • • • Powerful yet easy to use programming language Developed by Guido van Rossum First released in 1991 Named after comedy troupe Monty Python An alarming number of references to spam, eggs, and the number 42 in documentation Guide to Programming with Python 8
  • 9. Python Is Easy to Use • High-level language: Separate from the low-level processor operations; closer to human language than machine language • "Programming at the speed of thought" • Increases productivity – Python programs three to five times shorter than Java – Python programs five to ten times shorter than C++ Guide to Programming with Python 9
  • 10. Python Is Easy to Use (continued) • Python Program print "Game Over!" • C++ Program #include <iostream> int main() { std::cout << "Game Over!" << std::endl; return 0; } Guide to Programming with Python 10
  • 11. Python Is Powerful • Used by large organizations – NASA – Google – Microsoft • Used in published games – Battlefield 2 – Civilization IV – Disney’s Toontown Online Guide to Programming with Python 11
  • 12. Python Is Object-Oriented • Object-oriented programming (OOP): Methodology that defines problems in terms of objects that send messages to each other – 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 12
  • 13. 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++ might offer Guide to Programming with Python 13
  • 14. Python Runs Everywhere • Platform independent: Independent of the specific computer operating system • Python runs on – – – – – Windows DOS Mac OS Linux Many more Guide to Programming with Python 14
  • 15. Python Has a Strong Community • As an approachable language, has approachable community • Python Tutor mailing list – http://mail.python.org/mailman/listinfo/tutor – Perfect for beginners – No actual "tutors" or "students" Guide to Programming with Python 15
  • 16. Python Is Free and Open Source • Open source: Publicly available; open source software typically programmed by volunteers; anyone can use source code without fee • Can modify or even resell Python • Embracing open-source ideals is part of what makes Python successful Guide to Programming with Python 16
  • 17. Setting up Python on Windows • Using the CD-ROM that came with the book 1. Find the Python Windows Installer, Python2.3.5.exe, under the Software section 2. Click on the Install Python 2.3.5 from this CDROM link and run the installer 3. Accept the default configuration 4. Version 2.3.5 installed in the C:Python23 folder Guide to Programming with Python 17
  • 18. Setting up Python on Windows (continued) Figure 1.2: Python Installation Dialogue under Windows Your computer is soon to be home to Python. Guide to Programming with Python 18
  • 19. Setting up Python on Other Operating Systems • Download appropriate version from Python home page at www.python.org • Linux – Python may already be installed – Test: try running python at command prompt – If not installed, go to Python home page to download • Mac OS – After Python page, visit MacPython page at http://homepages.cwi.nl/~jack/macpython/index.html Guide to Programming with Python 19
  • 20. 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 Guide to Programming with Python 20
  • 21. Programming in Interactive Mode Figure 1.4: Python in interactive mode Python awaits your command. Guide to Programming with Python 21
  • 22. Programming in Interactive Mode (continued) • 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 2.3, IDLE (Python GUI) Guide to Programming with Python 22
  • 23. Programming in Interactive Mode (continued) • At command prompt (>>>), type: print • Python responds with: Game Over Guide to Programming with Python "Game Over" 23
  • 24. Programming in Interactive Mode (continued) Statement can display a string • String: Sequence of characters • Statement: Single unit in programming language that performs some action • print – print "Game Over" • 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 24
  • 25. Programming in Interactive Mode (continued) • 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 – Bug: Error in programming code Guide to Programming with Python 25
  • 26. Programming in Script Mode Figure 1.5: Python in script mode Your blank canvas awaits. Guide to Programming with Python 26
  • 27. Programming in Script Mode (continued) • 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 27
  • 28. Programming in Script Mode (Continued) • Write program – In script window, type print "Game Over" • Save program – Select File, Save As, name game_over.py – Always save before running • Run Program – Select Run, Run Module – Results displayed in interactive window Guide to Programming with Python 28
  • 29. Programming in Script Mode (continued) Figure 1.6: Python after a script has been run The results of running the Game Over program Guide to Programming with Python 29
  • 30. The Game Over Program # Game Over # Demonstrates the print command print "Game Over" raw_input("nnPress the enter key to exit.") Guide to Programming with Python 30
  • 31. The Game Over Program (continued) • 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 31
  • 32. 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 32
  • 33. 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 33
  • 34. Summary (continued) • A syntax error is a violation of the grammar of a programming language; often caused by a typo  • 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  Guide to Programming with Python 34