Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

GUIDE TO PROGRAMMING WITH PYTHON

818 views

Published on

  • Be the first to comment

  • Be the first to like this

GUIDE TO PROGRAMMING WITH PYTHON

  1. 1. Guide to Programming with Python Chapter One Getting Started: The Game Over Program
  2. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 21. Programming in Interactive Mode Figure 1.4: Python in interactive mode Python awaits your command. Guide to Programming with Python 21
  22. 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. 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. 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. 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. 26. Programming in Script Mode Figure 1.5: Python in script mode Your blank canvas awaits. Guide to Programming with Python 26
  27. 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. 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. 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. 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. 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. 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. 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. 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

×