Chapter 1 Programming Basics Python History and Program

Chapter 1: Programming Basics, Python History and Program Components CS 10061 – Introduction to Computer Programming Chapter 1: Getting Started 1

Why Write Computer Programs? n To assist with a complex situation that requires a human touch n Managing livestock n Patient simulators n Forensics n Weather predictions Chapter 1: Getting Started 2

Why Write Computer Programs? n To aid with creative thought and higher-level organization n Virtual dancers n Online academic classes n Games Chapter 1: Getting Started 3

Why Write Computer Programs? n To perform routine or repetitive tasks that follow a clear series of steps or work with electronic information n Package deliveries n Robotics or smart devices n Banking transactions Chapter 1: Getting Started 4

Programming Basics n A computer program contains instructions written by humans and understandable by computers (statements vs. instructions). n A programming language is used to write a computer program. Chapter 1: Getting Started 5

Program Basics – Cont’d: n Are all programming languages the same? n No, there are many differences n Machine-like vs. English-like n Compiler (source code) vs. interpreted (dynamic) n Commercial vs. open-source n Platform dependent vs. platform independent Chapter 1: Getting Started 6

Program Basics – Cont’d: n How do you get started with programs? n Identify the task to solve or automate n Determine the programming language to use n Install the programming language n Read the documentation and learn the jargon n Navigate the working environment (IDLE) Chapter 1: Getting Started 7

Python’s History n Developed by Guido van Rossum n Released in 1991 n Named after the English comedy troupe Monty Python (many unique references), but the current mascot is a little green snake Chapter 1: Getting Started 8

Why Use Python? n Easy to learn and easy to use n Powerful and efficient (no compilation) n Open-source and free n Platform independent n Integrates or embeds easily with other programming languages n Object-oriented ability is optional Chapter 1: Getting Started 9

Python IDLEs n Python’s IDLEs are working environments for programmers. n Statements are entered: At a command prompt (>>>) n In a text editor n n There are two main IDLEs in Python n Interactive (Shell) Mode (command prompt) n Script Mode (text editor) Chapter 1: Getting Started 10

Python Interactive (Shell) Mode n Utilizes the command prompt (>>>) n Results are displayed to the screen immediately n Not usable with programs that are already created and saved n Automatically invoked for screen output when a saved program is run n Useful for trying out correct syntax Chapter 1: Getting Started 11

Python Script Mode n Environment is like a text editor (Notepad) n Enables the writing, saving and editing of programs n Invoke by File New Window from the interactive (shell) mode n Save by File Save As n Name the file and add the “. py” extension n Run with Edit Run Script n Results are displayed in a shell window Chapter 1: Getting Started 12

Program Components n Statements do something n In English, they convey complete thoughts n In Python, they give complete instructions n Commands force action n Denoted in orange n Case sensitive, specifically lowercase n Part of a statement n Example: print “Hello, World” Command Chapter 1: Getting Started 13

Program Components n Expressions are something with a value n Denoted in green n Part of a statement n Values may be: n Strings or series of characters § Example: print “Hello, World” n Numbers, a particular number § Example: print 17 n String Number Evaluations, say 3 + 1 § Example: print 3 + 1 Chapter 1: Getting Started Evaluation 14

Program Components: Syntax Errors n Syntax errors result when the interpreter cannot understand a statement Misspellings of commands n Incorrect command usage n Other typographical mistakes n n The error displayed generally indicates what is wrong with the statement n Familiarize yourself with what syntax errors mean so errors may be fixed quickly Chapter 1: Getting Started 15

Program Components: Syntax Errors – Cont’d. n Misspellings of commands n Example: prnt “Hello, World” produces Syntax. Error: invalid syntax n The cursor highlights the last character of the statement Chapter 1: Getting Started 16

Program Components: Syntax Errors – Cont’d. n Incorrect command usage n Example: Hello produces Traceback (most recent call last): File "<pyshell#0>", line 1, in ? hello Name. Error: name 'hello' is not defined n A new command prompt is displayed Chapter 1: Getting Started 17

Program Components: Syntax Errors – Cont’d. n Other typographical mistakes n Example: prnt “Hello, World produces Syntax. Error: invalid token n The word “World” is highlighted to indicate the “token” Chapter 1: Getting Started 18

Program Components n Comments are used for program documentation Denoted in red n Assists with understanding the code (original developer or subsequent programmers) n Preceded by “#” which is ignored by the interpreter at program execution n Good programming practice dictates the use, especially as the program “header” n Chapter 1: Getting Started 19

Program Components n Blank lines n Used to separate blocks of related statements n Aids in reading the source code n Ignored by the interpreter at program execution n Prompts n Used to ask the user for input n Forces the console window to remain open n Example: raw_input(“nn. Press the enter key to exit. ”) Chapter 1: Getting Started 20
- Slides: 20