forked from csse120-201920/01-IntroductionToPython
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathm3_names_and_expressions.py
More file actions
130 lines (125 loc) · 5.07 KB
/
Copy pathm3_names_and_expressions.py
File metadata and controls
130 lines (125 loc) · 5.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
answer = 2 ** 5
print(answer * 100)
###############################################################################
# Done: 1.
# Read the 2 lines of code ABOVE this _TODO_. That code:
# 1. Computes 2 raised to the 5th power,
# yielding the object that is the integer 32.
# 2. Makes the name answer refer to that object.
# 3. Looks up the object to which the name answer refers (i.e., 32).
# 4. Multiplies that object (32) by 100
# (hence computing the object that is the integer 3,200).
# 5. Prints the newly-computed object. (It prints WITHOUT the comma.)
#
# Make sure that you understand that those two lines do the above,
# ** ASKING QUESTIONS AS NEEDED. **
# Once you completely understand the above, run this module,
# confirming that it prints 3200. Then change the above _TODO_ to DONE.
###############################################################################
###############################################################################
# done: 2.
# Immediately below this _TODO_, write code that:
# - Computes 77 plus the cosine of 2.75.
# HINT: You will need to import the math module (library).
# - Stores that computed value using a name of your own choosing.
# - Prints the square root of that computed value.
# Run your code (fix errors as needed), then change the above _TODO_ to DONE.
###############################################################################
import math
number = 77 + math.cos(2.75)
print(math.sqrt(number))
###############################################################################
# done: 3.
# Immediately below this _TODO_, write code that computes and prints:
# the square root of ((41 * 88) + (4 * the cosine of 2))
# Use as few or as many intermediate names as you feel appropriate.
# Run your code (fix errors as needed), then change the above _TODO_ to DONE.
###############################################################################
import math
num = math.sqrt((41 * 88) + (4* math.cos(2)))
print(num)
###############################################################################
# done: 4.
# Immediately below this _TODO_,
# write code that computes the square root of 2 in two ways:
# - By using the math.sqrt function.
# - By raising 2 to the 0.5 power (using ** for exponentiation).
# Print both of the expressions that you write.
# Run your code (fix errors as needed), then change the above _TODO_ to DONE.
###############################################################################
import math
num = math.sqrt(2)
number = 2 ** 0.5
print(num, number)
###############################################################################
# done: 5.
# Every object has a TYPE and a VALUE. For example,
# for the object that is computed by math.sqrt(2):
# Its TYPE is float (which is shorthand for "floating point number").
# Its VALUE is (approximately) 1.4142135623730951.
#
# The TYPE of an object is important because it determines:
# -- what the object KNOWS, and
# -- what the object can DO.
#
# The type function returns the TYPE of its argument. For example,
# type(3.14) returns the CLASS (synonym for TYPE) 'float'
# and so the code:
# print(type(3.14))
# will print <class 'float'>
# Try it now!
# (Just write print(type(3.14)) below this _TODO_ and run the program.)
#
# Now go through the BLAH objects listed below, one by one. For each:
# 1. Try to GUESS its TYPE.
# 2. Then write code of the form print(type(BLAH)).
# 3. THen RUN the code to LEARN its TYPE.
# "hello"
# 'hello'
# 'a b c'
# 3 + 3
# "3" + "3"
# 2 ** 100
# 2.0 ** 100
# math.sin(8)
# math.sin
# print
# math
# 'math'
#
# After you have written and run the code to learn the TYPE
# of each of the above, change the above _TODO_ to DONE.
###############################################################################
print(type(3.14))
print(type("hello"))
print(type('hello'))
print(type('a b c'))
print(type(3 + 3))
print(type("3" + "3"))
print(type(2 ** 100))
print(type(2.0 ** 100))
print(type(math.sin(8)))
print(type(math.sin))
print(type(print))
print(type(math))
print(type('math'))
###############################################################################
#
# Done: 6.
# Ensure that no blue bars on the scrollbar-thing to the right remain.
# Run one more time to be sure that all is still OK.
#
# Then COMMIT-and-PUSH your work as before:
# 1. Select VCS from the menu bar (above).
# 2. Choose Commit from the pull-down menu that appears.
# 3. In the Commit Changes window that pops up,
# press the Commit and Push button.
# (Note: If you see only a Commit button:
# - HOVER over the Commit button
# (in the lower-right corner of the window)
# - CLICK on Commit and Push.)
#
# You can COMMIT-and-PUSH as often as you like.
# DO IT FREQUENTLY; AT LEAST once per module.
#
###############################################################################