Chapter 1: Programming Basics, Python History and Program Components CS10061 – Introduction to Computer Programming Chapter 1: Getting Started
Why Write Computer Programs? To assist with a complex situation that requires a human touch Managing livestock Patient simulators Forensics Weather predictions Chapter 1: Getting Started
Why Write Computer Programs? To aid with creative thought and higher-level organization Virtual dancers Online academic classes Games Chapter 1: Getting Started
Why Write Computer Programs? To perform routine or repetitive tasks that follow a clear series of steps or work with electronic information Package deliveries Robotics or smart devices Banking transactions Chapter 1: Getting Started
Programming Basics A computer program contains instructions written by humans and understandable by computers (statements vs. instructions). A programming language is used to write a computer program. Chapter 1: Getting Started
Program Basics – Cont’d: Are all programming languages the same? No, there are many differences Machine-like vs. English-like Compiler (source code) vs. interpreted (dynamic) Commercial vs. open-source Platform dependent vs. platform independent Chapter 1: Getting Started
Program Basics – Cont’d: How do you get started with programs? Identify the task to solve or automate Determine the programming language to use Install the programming language Read the documentation and learn the jargon Navigate the working environment (IDLE) Chapter 1: Getting Started
Python’s History Developed by Guido van Rossum Released in 1991 Named after the English comedy troupe Monty Python (many unique references), but the current mascot is a little green snake Chapter 1: Getting Started
Why Use Python? Easy to learn and easy to use Powerful and efficient (no compilation) Open-source and free Platform independent Integrates or embeds easily with other programming languages Object-oriented ability is optional Chapter 1: Getting Started
Python IDLEs Python’s IDLEs are working environments for programmers. Statements are entered: At a command prompt (>>>) In a text editor There are two main IDLEs in Python Interactive (Shell) Mode (command prompt) Script Mode (text editor) Chapter 1: Getting Started
Python Interactive (Shell) Mode Utilizes the command prompt (>>>) Results are displayed to the screen immediately Not usable with programs that are already created and saved Automatically invoked for screen output when a saved program is run Useful for trying out correct syntax Chapter 1: Getting Started
Python Script Mode Environment is like a text editor (Notepad) Enables the writing, saving and editing of programs Invoke by File New Window from the interactive (shell) mode Save by File Save As Name the file and add the “.py” extension Run with Edit Run Script Results are displayed in a shell window Chapter 1: Getting Started
Program Components Statements do something Commands force action In English, they convey complete thoughts In Python, they give complete instructions Commands force action Denoted in orange Case sensitive, specifically lowercase Part of a statement Example: print “Hello, World” Command Chapter 1: Getting Started
Program Components Expressions are something with a value Denoted in green Part of a statement Values may be: Strings or series of characters Example: print “Hello, World” Numbers, a particular number Example: print 17 Evaluations, say 3 + 1 Example: print 3 + 1 String Number Evaluation Chapter 1: Getting Started
Program Components: Syntax Errors Syntax errors result when the interpreter cannot understand a statement Misspellings of commands Incorrect command usage Other typographical mistakes The error displayed generally indicates what is wrong with the statement Familiarize yourself with what syntax errors mean so errors may be fixed quickly Chapter 1: Getting Started
Program Components: Syntax Errors – Cont’d. Misspellings of commands Example: prnt “Hello, World” produces The cursor highlights the last character of the statement SyntaxError: invalid syntax Chapter 1: Getting Started
Program Components: Syntax Errors – Cont’d. Incorrect command usage Example: Hello produces A new command prompt is displayed Traceback (most recent call last): File "<pyshell#0>", line 1, in ? hello NameError: name 'hello' is not defined Chapter 1: Getting Started
Program Components: Syntax Errors – Cont’d. Other typographical mistakes Example: prnt “Hello, World produces The word “World” is highlighted to indicate the “token” SyntaxError: invalid token Chapter 1: Getting Started
Program Components Comments are used for program documentation Denoted in red Assists with understanding the code (original developer or subsequent programmers) Preceded by “#” which is ignored by the interpreter at program execution Good programming practice dictates the use, especially as the program “header” Chapter 1: Getting Started
Program Components Blank lines Prompts Used to separate blocks of related statements Aids in reading the source code Ignored by the interpreter at program execution Prompts Used to ask the user for input Forces the console window to remain open Example: raw_input(“\n\nPress the enter key to exit.”) Chapter 1: Getting Started