-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPython_01_problemset
More file actions
235 lines (159 loc) · 5.63 KB
/
Python_01_problemset
File metadata and controls
235 lines (159 loc) · 5.63 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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
10_17_2017
Python_01_problemset
1. Start up the Python Interactive Interpreter. Print out "Hello New York"
pfb14:~ admin$ python3
Python 3.6.2 |Anaconda, Inc.| (default, Sep 21 2017, 18:29:43)
[GCC 4.2.1 Compatible Clang 4.0.1 (tags/RELEASE_401/final)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>>print("Hello New York")
2. Store your name in a variable and print the contents of this variable.
>>> variable1=sabriya
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'sabriya' is not defined
>>> variable1 = "sabriya"
>>> print(variable1)
sabriya
#variable1 points to a string (= sabriya)
3. Use nano to write a script. Make sure to include # !/usr/bin/env python3 at the top!!
The script should print out, your name, favorite color, favorite activity, and your favorite animal.
Make it executable using chmod (only have to do this one time per script).
Run it from the command line.
pfb14:Python2017_problemsets admin$ nano myscript_1.py
in nano
#!/usr/bin/env python3
print("sabriya") #name
print("green") #favorite color
print("eating") #favorite activity
print("cat") #favorite animal
pfb14:Python2017_problemsets admin$ ls -l myscript_1.py
-rw-r--r-- 1 admin staff 144 Oct 17 11:14 myscript_1.py
pfb14:Python2017_problemsets admin$ chmod +x myscript_1.py
pfb14:Python2017_problemsets admin$ ls -l myscript_1.py
-rwxr-xr-x 1 admin staff 144 Oct 17 11:14 myscript_1.py
pfb14:Python2017_problemsets admin$ python3 myscript_1.py
sabriya
green
eating
cat
pfb14:Python2017_problemsets admin$ ./myscript_1.py
sabriya
green
eating
cat
4. Use sys.argv (make sure to import sys!!!) to retrieve your name, favorite color, favorite activity, and favorite animal from the command line.
pfb14:Python2017_problemsets admin$ nano myscript_2.py
#!/usr/bin/env python3
import sys
a= sys.argv[str(sabriya)] #name
b= sys.argv[str(green)] #favorite color
c= sys.argv[str(eating)] #favorite activity
d= sys.argv[str(cats)] #favorite animal
print(a,b,c,d) #print a,b,c,d strings
pfb14:Python2017_problemsets admin$ python3 myscript_2.py
Traceback (most recent call last):
File "myscript_2.py", line 4, in <module>
a= sys.argv[str(sabriya)] #name
NameError: name 'sabriya' is not defined
pfb14:Python2017_problemsets admin$ nano myscript_2.py
#!/usr/bin/env python3
import sys
a= str(sys.argv[sabriya]) #name
b= str(sys.argv[green]) #favorite color
c= str(sys.argv[eating]) #favorite activity
d= str(sys.argv[cats]) #favorite animal
print(a,b,c,d) #print a,b,c,d strings
pfb14:Python2017_problemsets admin$ python3 myscript_2.py
Traceback (most recent call last):
File "myscript_2.py", line 4, in <module>
a= str(sys.argv[sabriya]) #name
NameError: name 'sabriya' is not defined
pfb14:Python2017_problemsets admin$ nano myscript_2.py
#!/usr/bin/env python3
import sys
a= str(sys.argv["sabriya"]) #name
b= str(sys.argv["green"]) #favorite color
c= str(sys.argv["eating"]) #favorite activity
d= str(sys.argv["cats"]) #favorite animal
print(a,b,c,d) #print a,b,c,d strings
pfb14:Python2017_problemsets admin$ python3 myscript_2.py
Traceback (most recent call last):
File "myscript_2.py", line 4, in <module>
a= str(sys.argv["sabriya"]) #name
TypeError: list indices must be integers or slices, not str
pfb14:Python2017_problemsets admin$ nano myscript_2.py
#!/usr/bin/env python3
import sys
a= sys.argv["sabriya"] #name
b= sys.argv["green"] #favorite color
c= sys.argv["eating"] #favorite activity
d= sys.argv["cats"] #favorite animal
print(a,b,c,d) #print a,b,c,d strings
pfb14:Python2017_problemsets admin$ python3 myscript_2.py
Traceback (most recent call last):
File "myscript_2.py", line 4, in <module>
a= sys.argv["sabriya"] #name
TypeError: list indices must be integers or slices, not str
#!/usr/bin/env python3
import sys
a= sys.argv["sabriya"] #name
b= sys.argv["green"] #favorite color
c= sys.argv["eating"] #favorite activity
d= sys.argv["cats"] #favorite animal
print(a)
print(b)
print(c)
print(d)
#print a,b,c,d strings
#!/usr/bin/env python3
import sys
a= sys.argv[1] #name
b= sys.argv[2] #favorite color
c= sys.argv[3] #favorite activity
d= sys.argv[4] #favorite animal
print(a b c d)
#print a,b,c,d inputed
pfb14:Python2017_problemsets admin$ nano myscript_2.py
#!/usr/bin/env python3
import sys
a= sys.argv[1] #name
b= sys.argv[2] #favorite color
c= sys.argv[3] #favorite activity
d= sys.argv[4] #favorite animal
print(a)
print(b)
print(c)
print(d)
#print a,b,c,d inputed
pfb14:Python2017_problemsets admin$ python3 myscript_2.py sabriya green eating cats
sabriya
green
eating
cats
pfb14:Python2017_problemsets admin$ nano myscript_2.py
#!/usr/bin/env python3
import sys
a= sys.argv[1] #name
b= sys.argv[2] #favorite color
c= sys.argv[3] #favorite activity
d= sys.argv[4] #favorite animal
print(a, b, c, d)
#print a,b,c,d inputed
pfb14:Python2017_problemsets admin$ python3 myscript_2.py sabriya green eating cats
sabriya green eating cats
pfb14:Python2017_problemsets admin$ git add files/myscript_1.py
pfb14:Python2017_problemsets admin$ git commit -m "Python_01 script1)
> "
[master 8d26e6c] Python_01 script1)
1 file changed, 7 insertions(+)
create mode 100755 files/myscript_1.py
pfb14:Python2017_problemsets admin$ git push origin master
Counting objects: 4, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (4/4), done.
Writing objects: 100% (4/4), 447 bytes | 0 bytes/s, done.
Total 4 (delta 1), reused 0 (delta 0)
remote: Resolving deltas: 100% (1/1), completed with 1 local object.
To https://github.com/sabrsyed/Python2017_problemsets.git
5c89ab1..8d26e6c master -> master
did the same for myscript_2 and the answer key