Another Simple Program
# File: Camus.py
# Author: Mary Eberlein
# A simple first Python program
# print a Camus quote
print "Camus said:" # print text and then go to
# The escape sequence \n does a carriage return.
# If we want to include double quotes in the text we are
displaying, we can
# enclose the text in single quotes.
print '"Some people talk in
their sleep. \nLecturers talk while other people sleep." '
in their sleep.
while other people sleep."
In Our Program?
- Programs are saved in files that end in .py.
- The print function is used to
display data to the screen. There are several different versions:
# print a blank line
- print num1 # print num1 and skip
to next line
- print num1, # print num1 and then skip a
space, but don't goto next line
- print num1, num2 # print num1 and num2,
separated by a blank space, and go to next line
- print num1, num2, num3, # print 3 values and
then a blank, but don't go to next line
- Comments explain your program to other
- A function consists of a group of
- Form of a function definition:
- def functionName(parameter1, parameter2):
included in the function are indented.
- We call a function (execute the statements that are
part of the function) by typing its name:
- while the function is being executed, 5 will be
assigned to parameter1
and 7 will be assigned to parameter2.
sum = num1+num2
print "sum = ", sum
# call the
sum = 12
sum = 11
Bolts for Python Programs
Printing made nicer:
Escape Sequences - Two
character sequences that represent other
Note: If we want to
print text that appears on multiple lines, we can surround the text by
- \n : the new line escape sequence
- \t : the tab escape sequence
- \" : double quote
- \' : single quote
- \\ : backslash
- Identifiers - words used when
program, e.g. main, def, for
or reserved words - special identifiers that are
reserved for a
special purpose in Python, such as def, in, if, for,
- Identifiers must be composed of letters,
digits, _ (the
- They cannot begin with a digit
- Examples: total
- Choose meaningful identifier names:
- Choose max instead
- Choose currentItem
instead of c
- A variable is a name associated with a location in
memory where data is
- Example variable assignment: myNum =
- This statement stores the integer value
13 in memory, and associates the name myNum with
that memory location.
We can refer to the
value stored at this memory location through the variable name, myNum.
abs() absolute value
15 + 2 is 17
3 - 1 is 2
15/4 is 3 <----- If you divide one integer by another, the
result is an integer (the remainder is lost).
11.0/4 is 2.75
15%2 is 1
25%5 is 0
21% 6 is 3
2**3 is 8
Note: Not all floating point
numbers can be stored exactly, as you see in the above example.
Example: Write a program that
assigns values to 2 variables that
represent the length and width of a rectangle, and then prints the
Example: Write a program that
assigns integer values to 3 variables
that represent test scores, and then computes and prints the average
score to the screen.
Numbers which are integers, like 150 or 2, are of type int.
Numbers with fractional parts, like 3.75 or 2.111, are of type float.
For binary operations on numeric values of different types:
The operand of type int is
converted to float.
Otherwise, the result of a binary operation has the same type as the
2. * / %
3. + -
Given multiple operators of the same precedence, those operators are
executed in order from left to right.
We can always change this precedence using parentheses.
>>>5.0 + 6/2