Skip to content

Commit 001627f

Browse files
committed
Add code/
1 parent c224f1c commit 001627f

57 files changed

Lines changed: 843 additions & 0 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
321 Bytes
Binary file not shown.
1.05 KB
Binary file not shown.
1.37 KB
Binary file not shown.

code/arguments.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#!/usr/bin/env python3
2+
3+
import sys
4+
5+
print(sys.argv)
6+
7+
if len(sys.argv) < 2:
8+
print("You've given me nothing to work with.")
9+
else:
10+
print(sys.argv[1] +"? Well I disagree!")

code/boo.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
ya!
2+
Boo

code/classes/.DS_Store

6 KB
Binary file not shown.
1.36 KB
Binary file not shown.
1.94 KB
Binary file not shown.

code/classes/student.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
class Student:
2+
3+
# Note that each method must have a first parameter named 'self'.
4+
# self is used to refer to an instance of the class from within
5+
# the class definition.
6+
7+
# This is the class's constructor. It is called whenever
8+
# you create a new object of this class.
9+
def __init__(self, id, name, exams):
10+
# If we leave off the self. on the left side, we're not
11+
# creating attributes, a.k.a. instance variables
12+
self.id = id
13+
self.name = name
14+
self.exams = exams
15+
16+
# This method 'overrides' the defulat string representation
17+
# provided by Python.
18+
def __str__(self):
19+
# Notice that we must use self. when referring to attributes
20+
return 'new' + self.name + ', ' + str(self.id) + ', ' + 'Exams: ' + \
21+
','.join([str(i+1) + ': ' + str(score)
22+
for i, score in enumerate(self.exams)])
23+
24+
def exam(self, number):
25+
if (number < 1) or (number > len(self.exams)):
26+
msg = 'Exam number must be in range [1, '+str(len(self.exams))+']'
27+
raise ValueError(msg)
28+
return self.exams[number - 1]
29+
30+
def average(self):
31+
return sum(self.exams) / len(self.exams)
32+
33+
def add_exam(self, score):
34+
self.exams.append(score)
35+
36+
def change_score(self, exam_number, new_score):
37+
self.exams[exam_number] = new_score
38+
39+
def adjust_score(self, exam_number, adjustment):
40+
exams[exam_number - 1] += adjustment
41+
42+
print("I'm at the top level of the student module");
43+
44+
if __name__=="__main__":
45+
print("Also top level of student,but in a __name__=='__main__' block.");

code/data_structures.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# '#' is the comment character in Python.
2+
# Everything from # to end of line is ignored by Python interpreter
3+
4+
# Create a list by enclosing elements in []
5+
boys = ['Stan', 'Kyle', 'Cartman', 'Kenny']
6+
print("After boys = ['Stan', 'Kyle', 'Cartman', 'Kenny']")
7+
# Notice we have to convert the list to a string before concatenating
8+
# with a string, otherwise we'd get a type error
9+
print('boys == ' + str(boys))
10+
11+
# Can also create a list with list() constructor function
12+
girls = list('Bebe', 'Wendy')
13+
print("After girls = list('Bebe', 'Wendy')")
14+
print("girls == " + str(girls))
15+
16+
# Access list elements by list position
17+
print('boys[0] == ' + boys[0]) # prints Stan
18+
19+
# Notice that we have to put butters in a list.
20+
# If we tried boys + 'Butters' we'd get a type error for trying to
21+
# add a list to a string
22+
boys + ['Butters']
23+
print() # Blank like to make output more readable
24+
print("After boys + ['Butters']")
25+
print('boys == ' + str(boys))
26+
27+
# 'Butters' isn't in boys. What happened?
28+
# The + operator creates a new list, which we have to assign to a variable
29+
more_boys = boys + ['Butters']
30+
print()
31+
print("After more_boys = boys + ['Butters']")
32+
print('more_boys == ' + str(more_boys))
33+
34+
# If we want to add to the original list, we can assign to interpreter
35+
boys = boys + ['Butters']
36+
print()
37+
print("After boys = boys + ['Butters']")
38+
print('boys == ' + str(boys))
39+
40+
41+
# Note that we can combine + and = with a shortcut assignment:
42+
boys += ['Tweak']
43+
print()
44+
print("After boys += ['Tweak']")
45+
print('boys == ' + str(boys))

0 commit comments

Comments
 (0)