Question: 1. Variables Exercise 1.1 There are a square S and a circle C. The dimensions of them are stored in the following variables: In [1]:
1. Variables
Exercise 1.1
There are a square S and a circle C. The dimensions of them are stored in the following variables:
In [1]:
pi = 3.14159
diameter = 11.2
side = 4.5
where diameter contains the diameter of the circle C, side contains the measurement of the side of the square S, and pi contains the value of .
Use the above variables to compute the total areas of square S and circle C:
- First, compute the area of square S and store it in areaS.
- Then compute the area of circle C and store it in areaC.
- Add the two areas together and store it in total.
- Print the total area.
NOTES: You must use the variable defined above for your computation, do NOT use the actual numeric values.
In [ ]:
# write your code here
Exercise 1.2
Take a look at the following code segments (run it if needed), then answer the questions below.
In [ ]:
a = [1, 2, 3]
b = a
Do a and b have the same value? Your answer [Yes/No]:
Do a and b reference the same object? Your answer [Yes/No]:
In [ ]:
a = [1, 2, 3]
b = list(a)
Do a and b have the same value? Your answer [Yes/No]:
Do a and b reference the same object? Your answer [Yes/No]:
2. Data Types
Exercise 2.1
a. Add a command to print out the data type of the variable x in the following code segment:
In [ ]:
x = 2.56
b. Use type casting to change x to a string and assign it to variable y
In [ ]:
c. Check if y is indeed a string by printing out the data type of variable y
In [ ]:
Exercise 2.2
You're given a string s. Write a program that prints the last character in string s.
Notes: You do NOT know how many characters there are in s.
In [ ]:
# use this to test your code
s = 'hello'
# add your code below
Extra credit
Add a cell BELOW this cell and write a program that prints the last 2 characters in string s.
3. Control Flow
Exercise 3.1
Write a program that examines three variables, x, y and z, and prints the largest number among them.
Note: The use of the max(), min(), sort(), or sorted() functions is NOT allowed! Please use the if, elif, and else statements!
In [ ]:
# Use these variables to test your code
x = 1
y = 2
z = 3
# Write your code here
Use the following test cases to test your code (by editing the values of x, y, and z variables above, then re-run the cell:
- x = 4, y = 4, z = 4 (expected output: 4)
- x = 7, y = 2, z = 6 (expected output: 7)
- x = 2, y = 2, z = 1 (expected output: 2)
- x = 2, y = 4, z = 2 (expected output: 4)
NOTES: The test cases used for grading will be similar to the ones above but not exactly the same.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
