Activities: Week 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.

Write code to assign the number of characters in the string 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?)
There is a function we are providing in for you in this problem set called 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 *.
Write in a comment next to each line of code, what each line of this code does. (You should be very specific! This exercise will train your brain for when you write more complicated code.)

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
        
Write a program that uses the turtle module to draw something. It doesn’t have to be complicated, but draw something different than we did in the textbook or in class. (Optional but encouraged: post a screenshot of the artistic outcome to Piazza, or a short video of the drawing as it is created.) (Hint: if you are drawing something complicated, it could get tedious to watch it draw over and over. Try setting .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.

This is not a problem in the problem set. It is code that provides functions you’re using in this problem set. They’re hidden because you don’t yet need to understand how function definitions work.
Next Section - Activities: Week 2 (Note that PS1 is found in Week 1)