Download Why learn programming? - CS

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
no text concepts found
Transcript
Overview/Questions
CS101 Lecture 12
12:
Python: Introduction
±
±
±
±
±
What is programming about?
Why should we learn programming?
What is Python, and how do we run it?
How do we create a reusable program?
What are the elements that make up a program?
1
What is programming?
2
Why learn programming?
Programming is the process of encoding our ideas
into the form (syntax
(syntax)) and meaning (semantics
(semantics)) the
computer expects.
± In machine language or assembly language, this
means using opcodes to specify processor
instructions and operands to specify immediate
values or memory addresses.
± In a highhigh-level languages, this means following
the formatting,
formatting, punctuation,
punctuation, and naming
requirements of the language, as well as building
statements suitable for the compiler or interpreter.
± Programming is a challenging and
intellectually engaging experience.
± Programming is a part of computer
science.
± Computers have become commonplace in
our society, and understanding their
strengths and limitations requires and
understanding of programming.
3
4
1
Why learn programming?
About learning to program
± Programming is an important part of diverse and
interesting disciplines:
ƒ
ƒ
ƒ
ƒ
ƒ
ƒ
PrePre-requisites
This is a first course in computer science. There are
no formal prepre-requisites. Some informal prepre-req¶s:
req¶s:
± browsing files in your file system
± copying and pasting text
± some highhigh-school level algebra
Biology/bioinformatics: sequencing genes
Business: financial calculations, marketing research
Chemistry: drug discovery
Economics: data analysis, models/simulations for forecasting
Film: modeling and rendering for motion pictures
Weather: modeling storm patterns
Time
Anyone can be successful at learning programming.
Decent results, however, take a lot of time.
± Programming is a skill that can pay the bills.
Very well.
5
6
Introducing Python
Python Interactive Mode
We will experiment with the Python programming
language. What is Python?
± A very high level interpreted language.
language.
± Very clear and simple syntax.
± Runs an almost any computing device
The Python interactive mode will run commands for us.
When we start it up, we¶ll get a prompt:
prompt:
ƒ Windows, Linux/Unix, Mac OS X, OS/2, Amiga, Palm
Handhelds, Nokia mobile phones, and any device which
supports Java.
± Two modes:
ƒ interactive mode (a.k.a. command line) and
program).
ƒ interpreted (a.k.a. saved program).
7
8
2
Experimenting with Python
Experimenting with Python
Experiment by typing some commands at the prompt:
Standard Output, Variables, Assignment:
Try:
>>> print ³Hello, world´
Hello, World
>>> print 2 + 3
5
>>> print ³2 + 3 =´, 2 + 3
2 + 3 = 5
print "hello, world´
print 'hello, world'
a = "hello, world³
a
print a
print a, 'said I'
„
Some simple output statements, and a hint of arithmetic
„
How to get it to print quotes?
Figure out when it prints a variable versus literal.
9
10
Python Interpreted Mode
Software Development Example
Expressions and instructions typed in
Python¶s interactive mode go away when
we close the interpreter.
Problem Description
Suppose you¶re going to visit Mexico.
You want to go shopping, but the prices
are in ³pesos´, and you need to know if the
prices are good deals before you buy.
Instead, we can type source code into an
editor window,
window, save it as its own file,
file, and
then open and run it whenever we want.
What information do you need to solve this
problem?
11
12
3
3
Software Development Example
Software Development Example
Design Algorithm
Refine Algorithm
This relationship 100 P = 9 D,
can be rewritten as
Suppose you observe that a currency
exchange offers you 100 pesos for 9
American dollars.
P = 9 / 100 D
We can express this relationship as:
100 P = 9 D
or
D = 100 / 9 P
13
Input, Process, Output
3
14
3
Input, Process, Output
Completed algorithm:
± input the price in pesos
± calculate dollars as pesos * 100 / 9
± output dollars
Design Pattern
A reusable model for a program.
Input, Process, Output pattern
± prompt the user for some input
± apply algorithm to the input
± display output back to the user
15
16
4
The Completed Program
Elements of Programs
Comments
Lines which are ignored by the interpreter.
Programmers use comments to document their
programs, and to remove troublesome lines
during testing.
Any part of a line after the # symbol is a comment
Example:
# currencyconversion.py
# A program to convert a price in pesos to dollars.
17
Elements of Programs
18
Elements of Programs
Keywords
Special words reserved by the language.
Keywords are used to perform basic computation
functions, and are one of the building blocks of
programs.
Identifiers
Programmers use names to identify modules
(e.g. currencyconversion)
currencyconversion) and variables
(e.g. pesos) within programs.
Example: print
Variables
Give names to values within programs
# The word µprint¶ is a Python keyword
print ³Hello, world!´
19
20
5
Elements of Programs
Elements of Programs
Identifiers in Python follow these rules:
Valid Identifier Examples:
x
pesos
dollars
dollarsAndCents
dollars_and_cents
main
± Must begin with a letter or underscore _
± May contain letters, numbers, underscores
± May not contain spaces, punctuation, etc.
± May not be Python keywords
± Are case sensitive
Invalid Identifier Examples:
9x
print
21
Elements of Programs
22
Output Statements
The simplest form of output from a program is a
print statement, which prints text to the
console window.
print takes a variable number of arguments,
separated by commas:
print
print <expr
>
<expr>
print <expr
>,<expr
expr>,
>, ... , <expr
>
<expr>,<
<expr>
print <expr
>,<expr
expr>,
>, ... , <expr
>,
<expr>,<
<expr>,
Expressions
Any statement which produces a value.
Values can be numeric, text, or other types.
Variables, too are expressions.
Examples (each line is a valid Python statement):
input("Enter a price in pesos: ")
9.0 / 100.0 * pesos
dollars
23
24
6
Assignment Statements
Assignment Statements
A variable can be assigned many times during a
Python program or interactive session (called
reassignment).
reassignment).
It always holds the most recent value.
The process of giving a value to a variable.
General form: <variable> = <expr
>
<expr>
Examples (each line is an assignment statement):
x = 5
dollars = 100.0 / 9.0 * pesos
word = ³hello´
Examples:
x = 5
print x
x = x + 1
print x
25
Input Statements
26
Input Statements
When Python encounters an input statement,
the interpreter pauses, and waits for the user to
input an expression (value), terminated by the
<Enter> key.
An input statement uses the special builtbuilt-in
expression called input. input takes the
general form:
<variable> = input(<prompt>)
Examples:
Only after this input does processing continue.
pesos = input(³Enter the price in pesos: ´)
name = raw_input(³What is your name? ´)
NOTE:
input evaluates the input as an expression, whereas raw_input does not.
For text input, you will want to use raw_input instead.
27
28
7
Summary
Student To Dos
± Programming is the process of encoding our
ideas into the form (syntax) and meaning
(semantics) the computer expects.
± Design patterns are reusable models for
programs.
± The main elements of Python programs are
keywords, comments, identifiers, expressions,
input and output.
29
± If you want to use Python on your computer, go to
www.python.org and click on ³DOWNLOADS´.
ƒ Python 2.5.1 Windows installer
ƒ Python 2.5.1 for Macintosh OS X
This download includes the Python interpreter and
the IDLE editor.
30
Variables
A variable is a spot in memory which can
hold a value.
± The value is stored using a name we chose.
± We can recall it when needed to use the
value, or change it to hold something else.
x=
42
31
8