Activities: Week 1¶
- As soon as you can / after class 9/5:
- Read the course syllabus
- Fill out this google form assignment
- Check out our Piazza forums on the Canvas site.
- Read this piece, called Coding Magic by Marie LeBlanc Flanagan
- Watch this introductory video. You’ll notice it says “SI 106”, which is an undergraduate course similar to this one. That’s okay – pretend it says “506” instead of “106”. (We’ll provide more from this video series as the semester goes on, for which the same goes – pretend 106 is 506, unless of course it’s a number we’re doing math on!)
- Before class 9/7:
- Read General Intro, Simple Python Data, and Objects and Turtle Graphics.
- Read Debugging tips and Sequences.
- There are also some video resources for a bunch of these topics. If you find these helpful, I strongly suggest watching them. Each is a few minutes long:
- When reading, run the activecodes and answer the multiple choice questions. There may also be exercises at the end of each chapter. These are optional (though trying them is recommended, trying all of those is not required, and getting them correct is also not required).
- We are starting out with a lot of material. That is because we are going to speed through a lot of the beginning foundational material, and slow down in a couple weeks – this stuff is the basis of everything else we’ll do, so it is important to absorb and review as we go – you can always come back to this! When you have questions about this stuff, you should ask them, come to office hours, post on Piazza!
- Go to your discussion section this week, Wednesday or Thursday afternoon
- By Sunday 9/10 at 11:59 PM:
- Complete the first Demonstrate Your Understanding that goes with the readings.
- By Sunday 9/17 at 11:59 PM:
- Save answers to the exercises in Problem Set 1.
Problem Set¶
Instructions: Write the code you want to save in the provided boxes, and click Save & Run for each one. That will run your code, so you can see the output, if any, and the result of the tests, if there are any. It will also save your code. You should run your code each time you want to save it. Even if you only add comments, you should always click Save & Run. You can then load the history of the code you have run and saved. The last new code you have saved for each problem by the deadline is what will be graded.
rv
to a variable num_chars
. Then write code to assign the number of words in the string rv
to the variable num_words
. (Hint: remember how to split strings?)
square
. It takes one integer and returns the square of that integer value. Write code to assign a variable called xyz
the value 5*5
(five squared). Use the square function, rather than just multiplying with *
.
There are a couple functions we’re giving you in this problem set. One is a function called greeting
, which takes any string and adds "Hello, "
in front of it. (You can see examples in the code.) Another one is a function called random_digit
, which returns a value of any random integer between 0 and 9 (inclusive). (You can also see examples in the code.)
Write code that assigns to the variable func_var
the function greeting
(without executing the function).
Then, write code that assigns to the variable new_digit
the return value from executing the function random_digit
.
Then, write code that assigns to the variable digit_func
the function random_digit
(without executing the function).
There is a function we are giving you for this problem set that takes two strings as inputs, and returns the length of both of those strings added together, called add_lengths
. We are also including functions called random_digit
and square
in this problem set.
Now, take a look at the following code and related questions, in this code window.
Assign the value of the third element of num_lst
to a variable called third_elem
.
Assign the value of the sixth element of num_lst
to a variable called elem_sixth
.
Assign the length of num_lst
to a variable called num_lst_len
.
Consider: what is the difference between mixed_bag[-1]
and mixed_bag[-2]
(you may want to print out those values or print out information about those values, so you can make sure you know what they are!)?
Write code to print out the type of the third element of mixed_bag
.
Write code to assign the type of the fifth element of mixed_bag
to a variable called fifth_type
.
Write code to assign the type of the first element of mixed_bag
to a variable called another_type
.
Keep in mind: All ordinal numbers in instructions, like “third” or “fifth” refer to the way HUMANS count. How do you write code to find the right things?
Q-1: Here's another complicated expression, using the Turtle framework we talked about. Arrange these sentences in the order they are executed in the following code, like you did in an exercise in Chapter 2 of the textbook. (It may help to think about what specifically is happening in the first four lines of code as well.)
.. sourcecode:: python
import turtle
ella = turtle.Turtle()
x = "hello class".find("o") - 1
ella.speed = 3
ella.move(square(x*ella.speed))
Order the code fragments in the order in which the Python interpreter would evaluate them, when evaluating that last line of code.
Not graded for pset points. But important practice!
Look up the variable ella and find that it is an instance of a Turtle object
---
Look up the attribute move of the Turtle ella and find that it's a method object
---
Look up the function square
---
Look up the value of the variable x and find that it is an integer
---
Look up the value of the attribute speed of the instance ella and find that it is an integer
---
Evaluate the expression x * ella.speed to one integer
---
Call the function square on an integer value
---
Call the method .move of the Turtle ella on its input integer
.speed(10)
for the turtle to draw fast, or .speed(0)
for it to draw super fast with no animation.)
That’s the end of the problem set. In the hidden code below, you will find the definitions of functions that were used elsewhere in the problem set. They’re hidden because you don’t yet need to understand how function definitions work. But if you want a preview, feel free to click on Show/Hide Code.