python programming
ISTA 130: Lab 2
1 Turtle Review
Here are all of the turtle functions we have utilized so far in this course:
turtle.forward(distance) – Moves the turtle forward in the direction it is currently facing the distance entered
turtle.backward(distance) – Same as forward but it moves in the opposite direction the turtle is facing
turtle.right(degrees) – Roates the turtle to the right by the degrees enteres
turtle.left(degrees) – Same as right, but it rotates the turtle to the left
turtle.pensize(size) – Adjusts the size of the line left by the turtle to whatever value is entered for size
turtle.home() – Moves the turtle to the default location and faces it to the right
turtle.clear() – Clears all the lines that were left by the turtle in the window.
turtle.penup() – Causes the turtle to stop leaving lines (until pen is placed back down)
turtle.pendown() – Places the pen back down to the turtle can continue leaving lines when forward and backward are called.
turtle.pencolor(color string) – Changes the color of the lines left by the turtle to whatever color string entered (so long as Python recognizes it).
turtle.bgcolor(color string) – Changes the background color for the window that the turtle draws in.
turtle.speed(new speed) – Changes the speed at which the turtle moves to whatever newSpeed is.
turtle.clearscreen() – Deletes all drawings and turtles from the screen, leaving it in its initial state
Note that abbreviations also exist for many of these functions; for example:
� turtle.fd(distance)
� turtle.rt(degrees)
� turtle.pu()
1
2 Functions and Parameters
Here is the square function we looked at yesterday:
def square(side_length): ’’’ Draws a square given a numerical side_length ’’’ turtle.forward(side_length) turtle.right(90) turtle.forward(side_length) turtle.right(90) turtle.forward(side_length) turtle.right(90) turtle.forward(side_length) turtle.right(90) return
square(50) # This would give side_length the value of 50 square(100) # This would give side_length the value of 100
print side_length # This will give an error because side_length # only exists inside the function!
Try it out:
(1 pt.) Create a new file called lab02.py. In this file, create a simple function called rhombus. It will take one parameter, side length. Using this parameter, have your function create a rhombus using turtle graphics. Call your rhombus function in the script. What happens if you provide no arguments to the function? Two or three arguments?
Then, modify your rhombus function so it takes another argument for the angle inside the rhombus.
3 Data types
Python recognizes many different types of values when working with data. These can be numbers, strings of characters, or even user defined objects. For the time being, however, were only going to focus on three of the data types:
integer – These are whole numbers, both positive and negative. Examples are 5000, 0, and -25
float – These are numbers that are followed by a decimal point. Examples are 26.58, 0.0, and -1.23
string – These are a string of characters that include letters, punctuation, symbols, and “white- space” characters. They are always surrounded by double quotes (”) or single quotes (’). Examples are “Hello World!”, “!@#%#!” and “I like the number 47”.
2
It is important to know what type of values you are going to be working with when writing code in Python. Let’s take our square example from above. We know what happens if we call the square function using an integer, but what happens if we call it with a float or string?
Try it out:
(1 pt.) Copy this code for the square function above into your lab02.py. In the main function, try calling this function and passing in an integer. Next, try to pass in a float (just replace the integer value with a float, no need to call the function twice). Finally, try calling it by passing it a string value. Now that youve seen the results of passing it a string, what happens if you pass the square function the string “100”?
3.1 Operators and data types
In class we learned some useful operators: + addition - subtraction * multiplication ** power/exponent function / division // integer division
How do these operators apply to different data types?
Try it out:
In your lab02.py, try each of these 6 operators on every combination of int, float, and string, and write down in a comment whether the result is an integer, a float, a string, or an error. Also, take note if anything unexpected happens.
There should be six combinations for each operator (int & int, int & float, int & string, float & float, float & string, string & string), so you should have 36 lines of code at the end of this!
Do you see any patterns? Is there anything unexpected? (2 pt.)
3.2 Converting between data types
Copy this program into a new, temporary script, and try it out. What do you expect it to do? What does it do?
print "Enter first number: " num1 = raw_input() print "Enter second number: " num2 = raw_input() numsum = num1+num2 print "The sum is:" + numsum
3
Now try this program. What do you expect it to do? What does it do?
print "Enter a number: " num1 = raw_input() doublenum = num1 / 2 print "Half that number is:" + doublenum
And finally, try this program. What do you expect it to do? What does it do?
print "This one should be easy." four = 2 + 2 print "2 + 2 = " + four
The problem: all three of these programs are mixing data types! Specifically, they’re trying to combine strings and integers. When a user enters something for raw input, the result is a string, not a number.
Fortunately, Python is able to convert between data types, using these three functions:
int() – convert the argument to an integer
str() – convert the argument to a string
float() – convert the argument to a float (decimal) number
Try this example code to get an idea of how these functions work:
x = 5 y = ’13’ print x + int(y) print str(x) + y
Try it out:
(3 pt.) Fix all three of the short programs above so they have the desired behavior, and copy them into your lab02.py.
4 For loops
Functions are useful if we know we want to run some code a small amount of times. What happens if we want to run it thirty times? A hundred times? If that happens, it would be a huge hassle and waste of time to have to type out the function call thirty or a hundred times. We can use for loops to easily run code a specific number of times.
Example:
for i in range(<number of times to repeat>): <code goes here>
4
Using these loops, we only have to change the number in the range() function to alter the number of times the code gets repeated, instead of having to type out or delete the code each time. For instance, if we wanted to create a lot of triangles next to each other, we could create a triangle function to use in a for loop.
Example:
for i in range(30): triangle(50) turtle.forward(50)
What happens if you change the 30 in the range function to other values positive integer values? What about 0 or negative values? Floats?
Try it out:
(3 pt.) Create a “polygon” function that draws a polygon of any size, with any number of sides. For example, the command “polygon(5, 11)” should draw a hendecagon with sides of length 5.
You may need to remember a bit of geometry for this; try making a triangle function, a square function, a pentagon function, and see if you find a pattern. You’ll also need to use a for loop in your code, since we don’t know how many sides the polygon will have until we run the code.
Then, modify your script so that it asks the user for a size and number of sides, then draws the requested polygon.
5 What to turn in
When you’re finished, submit your lab02.py file to the Lab 2 dropbox on D2L.
Total Points: 10 points possible
5
- Turtle Review
- Functions and Parameters
- Data types
- Operators and data types
- Converting between data types
- For loops
- What to turn in