Variables, Expressions, and
Statements
Python Programming
1 Cristan Josh S. Nuguid
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 Programming
Data types
Python has multiple data types
Built-in
User defined
Numeric types:
int, float, long, complex
string
Boolean
3 Python Programming
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 Programming
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 Programming
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 Programming
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 Programming
Keywords
You cannot use any of the
keywords for variable names
and del for is raise
assert elif from lambda return
break else global not try
class except if or while
continue exec import pass yield
def finally in print
8 Python Programming
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 Programming
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 Programming
String operators
String concatenation
Use + sign
print ‘this’ + ‘is a string’
String repetition
Use * sign
lunch = ‘Spam’ * 4
print lunch
11 Python Programming
Comparison operators
Perform logical comparison and
return Boolean value
Equality ==
Inequality <> or !=
Less than < Greater than >
(or equal to) <= >=
is
12 Python Programming
Comments
The # symbol indicates the rest
of the line is a comment
Good programming practice:
Use comments liberally
13 Python Programming
docstrings
The built-in PyDoc module can
automatically generate
documentation if the
programmer uses docstrings
Ex: [Link]
14 Python Programming