Another Simple Program
#***************************************
# File: Camus.py
# Author: Mary Eberlein
#
# A simple first Python program
#***************************************
# print a Camus quote
def main():
print "Camus said:" # print text and then go to
next line
# 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." '
main()
Output
Camus said:
"Some people
talk
in their sleep.
Lecturers talk
while other people sleep."
What's
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
# 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
humans.
- A function consists of a group of
programming statements.
- Form of a function definition:
- def functionName(parameter1, parameter2):
- statements
included in the function are indented.
- We call a function (execute the statements that are
part of the function) by typing its name:
- functionName(5,
7)
- while the function is being executed, 5 will be
assigned to parameter1
and 7 will be assigned to parameter2.
Another
function example
def add(num1,
num2):
sum = num1+num2
print "sum = ", sum
# call the
add function
add(5, 7)
add(2, 9)
Output:
sum = 12
sum = 11
Nuts
and
Bolts for Python Programs
Printing made nicer:
Escape Sequences - Two
character sequences that represent other
characters
- \n : the new line escape sequence
- \t : the tab escape sequence
- \" : double quote
- \' : single quote
- \\ : backslash
Note: If we want to
print text that appears on multiple lines, we can surround the text by
triple quotes:
print """One
Two
Three"""
Ouput:
One
Two
Three
Identifiers and
Keywords
- Identifiers - words used when
writing a
program, e.g. main, def, for
- Keywords
or reserved words - special identifiers that are
reserved for a
special purpose in Python, such as def, in, if, for,
print
- Identifiers must be composed of letters,
digits, _ (the
underscore character)
- They cannot begin with a digit
- Examples: total
sum
min_length
_amount
- Choose meaningful identifier names:
- Choose max instead
of m
- Choose currentItem
or current_item
instead of c
Variables
- A variable is a name associated with a location in
memory where data is
stored.
- Example variable assignment: myNum =
13
- 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.
Operators
+ addition
- subtraction
* multiplication
/ division
** exponentiation
% remainder
abs() absolute value
Examples:
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
Example:
>>>abs(-3)
3
>>> abs(-2.3)
2.29999999999999998
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
rectangle's area.
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.
Conversions
between
Numeric Types
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.
Example:
>>>
type(3)
<type
'int'>
>>>
type(3.5)
<type
'float'>
>>>
type(3.0)
<type
'float'>
For binary operations on numeric values of different types:
The operand of type int is
converted to float.
Example:
>>>3+4
7
>>>3.0+4
7.0
Otherwise, the result of a binary operation has the same type as the
operands.
Operator
Precedence
1. **
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.
Example:
>>> 2*10%5
0
>>>5.0 + 6/2
8.0
Arithmetic Operator
Examples
- 15+2
- 3-1
- 15%2
- 25%5
- 2**3
- result
= 17%3*4