Download Chapter 1: Programming Basics, Python History and Program

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Programming language wikipedia , lookup

Falcon (programming language) wikipedia , lookup

Compiler wikipedia , lookup

Object-oriented programming wikipedia , lookup

Go (programming language) wikipedia , lookup

Reactive programming wikipedia , lookup

Python (programming language) wikipedia , lookup

C Sharp (programming language) wikipedia , lookup

Control flow wikipedia , lookup

One-pass compiler wikipedia , lookup

Interpreter (computing) wikipedia , lookup

Structured programming wikipedia , lookup

Transcript
Why Write Computer Programs?
Chapter 1: Programming
Basics, Python History
and Program Components
CS10061 – Introduction to Computer
Programming
Chapter 1: Getting Started
1
 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?
Why Write Computer Programs?
 To aid with creative thought and higher-level
 To perform routine or repetitive tasks that
organization
follow a clear series of steps or work with
electronic information

Virtual dancers

Online academic classes

Package deliveries

Games

Robotics or smart devices

Banking transactions
Chapter 1: Getting Started
2
3
Chapter 1: Getting Started
4
1
Programming Basics
Program Basics – Cont’d:
 A computer program contains instructions
 Are all programming languages the same?
written by humans and understandable by

computers (statements vs. instructions).
 A programming language is used to write a
computer program.
Chapter 1: Getting Started
5
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:
Python’s History
 How do you get started with programs?
 Developed by Guido van Rossum

Identify the task to solve or automate
 Released in 1991

Determine the programming language to use
 Named after the English comedy troupe

Install the programming language
Monty Python (many unique references), but

Read the documentation and learn the jargon
the current mascot is a little green snake

Navigate the working environment (IDLE)
Chapter 1: Getting Started
7
Chapter 1: Getting Started
6
8
2
Why Use Python?
Python IDLEs
 Easy to learn and easy to use
 Python’s IDLEs are working environments for
 Powerful and efficient (no compilation)
programmers.
 Statements are entered:
 Open-source and free
 Platform independent

 Integrates or embeds easily with other

At a command prompt (>>>)
In a text editor
 There are two main IDLEs in Python
 Interactive (Shell) Mode (command prompt)
 Script Mode (text editor)
programming languages
 Object-oriented ability is optional
Chapter 1: Getting Started
9
Chapter 1: Getting Started
Python Interactive (Shell) Mode
Python Script Mode
 Utilizes the command prompt (>>>)
 Environment is like a text editor (Notepad)
 Results are displayed to the screen
 Enables the writing, saving and editing of
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
programs
 Invoke by File  New Window from the
interactive (shell) mode
 Save by File  Save As

10
Name the file and add the “.py” extension
 Run with Edit  Run Script
 Results are displayed in a shell window
Chapter 1: Getting Started
11
Chapter 1: Getting Started
12
3
Program Components
Program Components
 Statements do something
 In English, they convey complete thoughts
 In Python, they give complete instructions
 Expressions are something with a value
 Denoted in green
 Part of a statement
 Values may be:
 Commands force action
 Denoted in orange
 Case sensitive, specifically lowercase
 Part of a statement

Strings or series of characters

Numbers, a particular number

Evaluations, say 3 + 1
 Example: print “Hello, World”
 Example: print 17
Example: print “Hello, World”
 Example: print 3 + 1
Command
Chapter 1: Getting Started

13
 Syntax errors result when the interpreter
 Misspellings of commands


Example: prnt “Hello, World” produces
SyntaxError: invalid syntax

 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
Evaluation
14
Program Components:
Syntax Errors – Cont’d.
Misspellings of commands
 Incorrect command usage
 Other typographical mistakes
Number
Chapter 1: Getting Started
Program Components:
Syntax Errors
cannot understand a statement
String
15
The cursor highlights the last character of the
statement
Chapter 1: Getting Started
16
4
Program Components:
Syntax Errors – Cont’d.
Program Components:
Syntax Errors – Cont’d.
 Incorrect command usage
 Other typographical mistakes

Example: Hello produces

SyntaxError: invalid token
Traceback (most recent call last):
File "<pyshell#0>", line 1, in ?
hello
NameError: name 'hello' is not defined

Example: prnt “Hello, World produces

The word “World” is highlighted to indicate the
“token”
A new command prompt is displayed
Chapter 1: Getting Started
17
Chapter 1: Getting Started
18
Program Components
Program Components
 Comments are used for program
 Blank lines
 Used to separate blocks of related statements
 Aids in reading the source code
 Ignored by the interpreter at program
execution
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
 Prompts
 Used to ask the user for input
 Forces the console window to remain open

19
Example: raw_input(“\n\nPress the enter key to exit.”)
Chapter 1: Getting Started
20
5