Lecture 2

Harold Nelson

1/18/2018

Functions

Function definitions begin with the keyword “def” followed by the name of the function with a comma- separated argument list in parentheses. A colon ends this line. The body of the function is demarcated by indentation(automatic). The value returned by the function follows the key-word command “return”.

Example:

def circle_area(radius):
    a = 3.14159 * radius**2
    return a
# usage

ca = circle_area(5.)
print(ca)
## 78.53975

Functions may have multiple arguments and may make use of other functions in their code.

Example

def circle_area(radius):
    a = 3.14159 * radius**2
    return a

def cyl_vol(radius, height):
    base_area = circle_area(radius)
    vol = base_area * height
    return vol
    
# run it
cv = cyl_vol(5.,10.)
print(cv)
## 785.3975

Exercise 1

Create a function triangle_area which accepts the base and height of a triangle and returns the area.

Answer 1

def triangle_area(base,height):
    triangle_area = .5 * base * height
    return triangle_area
    
# Test

ta = triangle_area(2.5,5.0)
print(ta)
## 6.25

The range function

range looks and acts like a function built into python. It is technically the “constructor method the class range.” We’ll talk about those concepts later. It may be called with 1, 2 or 3 arguments. Look at docs https://docs.python.org/3/library/stdtypes.html?highlight=range#range

You can think of the three arguments as * start: included * stope: excluded * step ## The for loop

At this point we will restrict ourselve to for loops defined by ranges.
We will use ranges to create for loops. * Begin with key-word for * Next comes an integer variable which will take on the values in the range. * Next comes the key-word “in”. * Next comes “range()” with an appropriate list of arguments. * End with a colon.

The body of the loop is an indented set of statements which will be executed for each value of the for loop.

Examples

for i in range(4):
    print(i)
 
## 0
## 1
## 2
## 3
for i in range(0,4):
   print(i)
## 0
## 1
## 2
## 3
for i in range(1,4):
   print(i)
## 1
## 2
## 3
for i in range(1,5):
   print(i)
## 1
## 2
## 3
## 4

Reversed ranges

We can make start larger than stop. Try This

for i in range(4,0):
    print(i)

Nothing was printed because the default value of step is -1.

Better.

for i in range(4,0,-1):
    print(i)
## 4
## 3
## 2
## 1

Exercises 2

Use a for loop to print all of the integers from 1 to 8 inclusive.

Use a for loop to print the numbers 2,4,6,8.

Answers 2

for i in range(1,9):
    print(i)
## 1
## 2
## 3
## 4
## 5
## 6
## 7
## 8
for i in range(2,10,2):
    print(i)
## 2
## 4
## 6
## 8

Turtle Graphics

Review text page 24

Run the following commands in the Python shell.

import turtle
Joe = turtle.Turtle()
print(Joe.position())
print(Joe.heading())
Joe.forward(100)
print(Joe.position())
print(Joe.heading())
Joe.right(90)
print(Joe.position())
print(Joe.heading())
## (0.00,0.00)
## 0.0
## (100.00,0.00)
## 0.0
## (100.00,0.00)
## 270.0

Exercise 3

Use a turtle to create a square divided into four equal squares.