Lesson 3 - University of Oklahoma

Download Report

Transcript Lesson 3 - University of Oklahoma

Day 1 – Lesson 3
Variables, Expressions, and
Statements
Python Mini-Course
University of Oklahoma
Department of Psychology
1
Python Mini-Course: Day 1 - Lesson 3
4/5/09
Lesson objectives
1. Identify the data types and
keywords in the Python language
2. Understand how variable
assignment works in Python
3. Identify and use math, string, and
comparison operators
4. Write in-code comments and
docstrings
2
Python Mini-Course: Day 1 - Lesson 3
4/5/09
Data types
 Python has multiple data types
 Built-in
 User defined
 Numeric types:
 int, float, long, complex
 string
 Boolean
3
Python Mini-Course: Day 1 - Lesson 3
4/5/09
Data types
 Use the type() function to find
the type for a value or variable
 Data can be converted using
cast commands
 Ex. a = str(17)
type(a)
4
Python Mini-Course: Day 1 - Lesson 3
4/5/09
Variables
 As in mathematics, much of
the power of computer
programs is in the use of
variables
 Python variables are identified
by name
5
Python Mini-Course: Day 1 - Lesson 3
4/5/09
Variable naming conventions
 Must start with a letter
 Can contain letters, numbers,
and underscore character (_)
 Can be any (reasonable) length
 Case sensitive
 big is NOT the same as BIG
6
Python Mini-Course: Day 1 - Lesson 3
4/5/09
Naming style guidelines
 Use descriptive names
 Use lowercase with underscores for
variable names
 Don’t use “I”, “l”, or “O” for single
letter names
 Be consistent
 Except when it makes code easier to
read
7
Python Mini-Course: Day 1 - Lesson 3
4/5/09
Keywords
 You cannot use any of the
keywords for variable names
and
assert
break
class
continue
def
8
del
elif
else
except
exec
finally
Python Mini-Course: Day 1 - Lesson 3
for
from
global
if
import
in
is
lambda
not
or
pass
print
raise
return
try
while
yield
4/5/09
Assignment and initialization
 Variables can be created at any
time (no need to declare them)
 Variable names refer to info in
memory
 The Python interpreter keeps track of
the namespace
 Two variables can refer to the
same memory location
9
Python Mini-Course: Day 1 - Lesson 3
4/5/09
Math operators
 Arithmetic
 +, -, *, and /
 Note: the / operator performs floor
division on int data types
 Try: minute = 59
minute/60
 Exponentiation **
 Order of operations (PEMDAS)
10
Python Mini-Course: Day 1 - Lesson 3
4/5/09
String operators
 String concatenation
 Use + sign
 print ‘this’ + ‘is a string’
 String repetition
 Use * sign
 lunch = ‘Spam’ * 4
print lunch
11
Python Mini-Course: Day 1 - Lesson 3
4/5/09
Comparison operators
 Perform logical comparison and
return Boolean value
 Equality
==
 Inequality <> or !=
 Less than <
Greater than >
(or equal to) <=
>=
 is
12
Python Mini-Course: Day 1 - Lesson 3
4/5/09
Comments
 The # symbol indicates the rest
of the line is a comment
 Good programming practice:
Use comments liberally
13
Python Mini-Course: Day 1 - Lesson 3
4/5/09
docstrings
 The built-in PyDoc module can
automatically generate
documentation if the
programmer uses docstrings
 Ex: doctest.py
14
Python Mini-Course: Day 1 - Lesson 3
4/5/09