Introductory Python
Introductory Python
1
Last Time
2
Reminders
¤ Lab Today
¤ My OH are today
3
Questions
4
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
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
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
9
A Short Introduction to Python
10
Programming Languages
11
A programming “language” is a
formal notation
12
Not a natural language
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
15
Python
$ python3
or
$ python3 –i filename.py
16
Command Line Interfaces
Be aware of the difference between
“talking to the shell” and “talking to Python”
$ ssh [email protected]
[email protected]'s password:
…
[annpenny@unix2 ~]$ pwd
Talking to /afs/andrew.cmu.edu/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”
18
Data Types
19
Data Types
¤ Integers 4 15110 -53 0
4.0 0.80333333333
¤ Floating Point Numbers 7.34e+014
¤ 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:
() ** * / % + -
22
Integer Division
In Python3:
¤ 7 / 2 equals 3.5
¤ 7 // 2 equals 3
¤ -7 // 2 equals -4
¤ beware! // rounds down to smaller number,
not towards zero
23
Expressions and Statements
Know the difference!
24
Variables
25
Variables
¤ A variable is not an “unknown” as in algebra.
>> a = 5
>> a a: 5
=> 5
26
Variables
¤ A variable is not an “unknown” as in algebra.
>> a = 5
>> a a: 5
=> 5
27
Variables
¤ A variable is not an “unknown” as in algebra.
>> 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
¤ The remainder of the variable name (if any) can consist of any combination of:
¤ uppercase letters
¤ lowercase letters
¤ digits and underscores (_).
31
Syntax and Semantics
32
Syntax vs. Semantics
Syntax Semantic
¤ Rules, structure ¤ Meaning
33
Colorless green ideas sleep furiously
34
Functions
35
Function Syntax
def functionname(parameterlist):
☐☐☐☐instructions
36
Function Syntax
def is a reserved word and
cannot be used as a variable name
def functionname(parameterlist):
☐☐☐☐instructions
37
Function Syntax
def is a reserved word and
cannot be used as a variable name
def functionname(parameterlist):
☐☐☐☐instructions
38
Function Syntax
The parameter list can contain 1 or more
def is a reserved word and
variables that represent data to be used
cannot be used as a variable name
in the function’s computation
def functionname(parameterlist):
☐☐☐☐instructions
39
Function Syntax
The parameter list can contain 1 or more
def is a reserved word and
variables that represent data to be used
cannot be used as a variable name
in the function’s computation
def functionname(parameterlist):
☐☐☐☐instructions
40
Function Syntax
The parameter list can contain 1 or more
def is a reserved word and
variables that represent data to be used
cannot be used as a variable name
in the function’s computation
def functionname(parameterlist):
☐☐☐☐instructions
def functionname(parameterlist):
☐☐☐☐instructions
print("Hello World!\n”)
def multiply(v1,v2,v3) :
return v1*v2*v3
43
Functions are general
¤ Function that takes 1 parameter: ¤ A function can also have no
parameters – but now it can only do
def multiply_5(val): one thing!
print("Hello World!\n”)
44
Example: area of a countertop
?
4/2=2
4/2=2
45
countertop.py
def compute_area():
empty parameter list
square_area = 4 * 4
triangle_area = 0.5 * (4 / 2) * (4 / 2)
total_area = square_area – triangle_area
return total_area
python3 –i countertop.py
>>> compute_area() empty argument
14.0 list
46
Generalizing the problem
X
?
X/2
X/2
47
countertop.py
parameter
def compute_area(side):
square_area = side * side
triangle_area = 0.5 * (side / 2 * side / 2)
total_area = square_area – triangle_area
return total_area
python3 –i countertop.py
argument
>>> compute_area(109) (run function with side = 109)
48
Function Outputs
49
Function Outputs
Return value Return None
¤ Function does some action and ¤ Function does some action and (by
returns a value: default) returns None:
50
Function Outputs
¤ >>> three_x(12)
36 value returned
>>> print(three_x(12))
36
value returned and printed
¤ >>> hello_world()
Hello World! value printed by method
>>> print(hello_world())
Hello World! valueprinted by method
None
value returned and printed
51
Function Outputs
¤ A function can return either one answer or no answer (None) to its “caller”.
¤ The hello_world function does not return anything to its caller. It simply
prints something on the screen.
¤ The three_x function does return its result to its caller so it can use the
value in another computation:
three_x(12) + three_x(16)
52
Function Outputs
¤ Suppose we write compute_area this way:
def compute_area(side):
print(total_area)
53
Built-in Functions (Methods)
54
Built-In Functions (Methods)
import math
r = 5 + math.sqrt(2)
alpha = math.sin(math.pi/3)
55
Using predefined modules
¤ math is a predefined module of functions (also called methods) that we can use
without writing their implementations.
math.sqrt(16)
math.pi
math.sin(math.pi / 2)
56
What Could Possibly Go Wrong?
alpha = 5
2 + alhpa
3 / 0
import math
math.sqrt(-1)
math.sqrt(2, 3)
57
Try
58
Remember
¤ Tonight:
¤ Lab 2
¤ OLI Iteration Module
59
A SS
C L !
N O W !
R RO
M O
TO
60
Talking to the Shell (not Python!!)
Some useful Unix commands
61
Useful Unix Commands (Part 1)
All commands must be typed in lower case.
pwd --> print working directory, prints where you currently are
ls --> list, lists all the files and folders in the directory
62
Useful Unix Commands (Part 2)
mkdir lab1 --> make directory lab1 aka makes a folder called lab1
"tab" - autocompletes what you're typing based on the files in the current folder
"up" - cycles through the commands you've typed. Similarly for the opposite effect,
press "down"
64
Useful Unix Commands (Part 4)
python3 -i test.py --> load test.py in python3, and
you can call the functions in test.py.
gedit lb1.txt & --> opens up lb1.txt on gedit and & allows you to
run your terminal at the same time
( else your terminal pauses until you close gedit)
And lastly, you can always do man <command> to find out more about a
particular command you're interested about (eg. man cp, man ls)
65