Today’s Lecture
¤ Introduction to Python
¤ Mechanics
¤ Some Specifics:
¤ Programming Languages
¤ Basic datatypes
¤ Variables
¤ Functions
5
Hardware versus Software
software
….. hardware
6
Execution of Python Programs
When you write a
program in Python,
Java etc. It does not
run directly on the OS.
Another program
called an
interpreter or virtual
machine takes it and
runs it for you
translating your
commands into the
…
language of the OS.
7
Execution of Python Programs
We will write Python
programs that are
executed by the
Python Interpreter.
A Python Interpreter
for your OS already
exists. You can use the
one on lab machines,
install one for your
laptop, or use one
remotely
…
8
Using a Python Interpreter
There are two ways to interact with a Python interpreter:
1. Tell it to execute a program that is saved in a file with a
.py extension
2. Interact with it in a program called a shell
You will interact with Python in
both ways.
9
A Short Introduction to Python
¤ Starting the Python interpreter either using a Unix Server
at CMU or on your own computer
¤ See the Resources page for specific instructions
¤ Creating .py files with a text editor
¤ Files with the .py extension can be created by any editor but
needs a Python interpreter to be read.
¤ We have chosen editor editor for the course but you
may use an editor of your own choice if you feel comfortable.
¤ Why IDE? Why Not?
10
Programming Languages
11
A programming “language” is a
formal notation
12
Not a natural language
Recipe Computer program
Toast and cereal for i in range(5):
Toast the bread. Butter it. Put pritn(whatever I want)
some cereal in a bowl. Add mikl.
¤ Interpreted by a person ¤ Interpreted by a machine
¤ Unclear? Can be figured out ¤ …for a human (“somebody wants to
(What kind of bread? Butter? What print something”)
kind of milk?)
¤ Unclear? Not a program
¤ Typos? Can be figured out (“whatever I want”????)
(“mikl” means “milk”)
¤ Typos? Program errors
(“pritn”???)
13
A programming “language” is a
formal notation
for generalized problem solving
14
Programs should be general
Recipe Program
def force(mass, accel) :
¤
return mass*accel
Specific: “output” is two cups of General: output is force for any
sauce. combination of mass and
acceleration.
15
Python
¤ Python is one of many programming languages.
¤ 2 widely used versions. We will use Python 3.
¤ Running Python on the command line:
$ python3
or
$ python3 –i [Link]
16
Command Line Interfaces
Be aware of the difference between
“talking to the shell” and “talking to Python”
$ ssh annpenny@[Link]
annpenny@[Link]'s password:
…
[annpenny@unix2 ~]$ pwd
Talking to /afs/[Link]/usr14/annpenny
the shell [annpenny@unix2 ~]$ python3
Python 3.3.2 (default, Aug 12 2013, 13:12:23)
[GCC 4.6.3] on linux
Type "help", "copyright", "credits" or
"license" for more information.
Talking to >>> quit()
Python
17
Command Line Interfaces
Be aware of the difference between
“talking to the shell” and “talking to Python”
Shell $ ssh annpenny@[Link]
prompt annpenny@[Link]'s password:
… User input
[annpenny@unix2 ~]$ pwd
Talking to /afs/[Link]/usr14/annpenny Ask shell to
the shell [annpenny@unix2 ~]$ python3 run Python
Python 3.3.2 (default, Aug 12 2013, 13:12:23)
Shell [GCC 4.6.3] on linux
response Type "help", "copyright", "credits" or
"license" for more information.
Talking to >>> quit() Input to python
Python
18
Data Types
19
Data Types
¤ Integers 4 15110 -53 0
4.0 0.80333333333
¤ Floating Point Numbers 7.34e+014
"hello” "A" " " ””
¤ Strings 'there' '"' '15110’
¤ Booleans True False
¤ Literal None None
20
Arithmetic Expressions
¤ Mathematical Operators
+ Addition
- Subtraction // Integer division
* Multiplication ** Exponentiation
/ Division % Modulo (remainder)
¤ Python is
like a calculator: type an expression and it tells
you the value.
>> 2 + 3 * 5
Þ17
21
Order of Evaluation
Order of operator precedence:
() ** * / % + -
Use parentheses to force alternate precedence
5*6+7 ≠ 5 * (6 + 7)
Left associativity except for **
2 + 3 + 4 = (2 + 3) + 4
2 ** 3 ** 4 = 2 **(3 ** 4)
22
Integer Division
In Python3:
¤ 7 / 2 equals 3.5
¤ 7 // 2 equals 3
¤ 7 // 2.0 equals 3.0
¤ 7.0 // 2 equals 3.0
¤ -7 // 2 equals -4
¤ beware! // rounds down to smaller number,
not towards zero
23
Expressions and Statements
Know the difference!
¤ Python evaluates an expression to get a result (number or
other value)
¤ Python executes a statement to perform an action that has
an effect (printing something, for example)
24
Variables
25
Variables
¤ A variable is not an unknown as in algebra.
¤ In computer programming, a variable is a place where you can store a value.
¤ In Python we store a value using an assignment statement
>> a = 5
>> a a: 5
=> 5
26
Variables
¤ A variable is not an unknown as in algebra.
¤ In computer programming, a variable is a place where you can store a value.
¤ In Python we store a value using an assignment statement
>> a = 5
>> a a: 5
=> 5
27
Variables
¤ A variable is not an unknown as in algebra.
¤ In computer programming, a variable is a place where you can store a value.
¤ In Python we store a value using an assignment statement
>> a = 5
>> a a: 5
=> 5
28
Variables
>> a
Þ5 a: 5
>> b = 2 * a
>> b b: 10
Þ10
29
Variables
>> a
Þ5 a:
>> b Woof
Þ10
>> a = Woof
>> a b: 10
Þ Woof
>> b Variable b does not “remember” that its
Þ10 value came from variable a.
30
Variable Names
¤ All variable names must start with a letter (lowercase recommended).
¤ The remainder of the variable name (if any) can consist of any combination of:
¤ uppercase letters
¤ lowercase letters
¤ digits and underscores (_).
¤ Identifiers in Python are case sensitive.
¤ Example: Value is not the same as value.
31
Syntax and Semantics
32
Syntax vs. Semantics
Syntax Semantic
¤ Rules, structure ¤ Meaning
¤ Errors result when code is ¤ Error results when
not well formed expression/statement can’t
be evaluated or executed
due to meaning
Colorless green ideas sleep furiously
33
Colorless green ideas sleep furiously
It can only be the thought of verdure to come, which
prompts us in the autumn to buy these dormant white lumps
of vegetable matter covered by a brown papery skin, and
lovingly to plant them and care for them. It is a marvel to
me that under this cover they are laboring unseen at such a
rate within to give us the sudden awesome beauty of spring
flowering bulbs. While winter reigns the earth reposes but
these colorless green ideas sleep furiously.
34