-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExercise_37.py
91 lines (76 loc) · 2.31 KB
/
Exercise_37.py
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
#Exercise 37: Symbol Review
# use each once to prove you know them!
keywords = ['and','del','from','not','while','as','elif','global','or','with','assert','else','if','pass','yield','break','except',
'import','print','class','exec','in','raise','continue','finally','is','return','def','for','lambda','try']
#and not or else if
if(2 < 3 and 3 >= 2):
if(not False or False):
print "Hello"
elif(2 > 3):
print "Wont reach this!"
else:
print "Wont reach this either!"
#del deletes an item from a list
del keywords[0]
#from
#from sys import argsv
#script, input_file = argv
# used to import files, functions, or arguments
# while
x = 0
while x < 6:
x = x + 1
print x
# global
globvar = 0
def edit_globvar():
global globvar
globvar = 2
def print_globvar():
print "The global variable equals: ", globvar
print_globvar()
edit_globvar()
print_globvar()
#create a class named fruit.
#an instance of that class (an object) would be a single fruit
#you can have several instances of one class
class fruit:
#properties(
fruit = ""
color = ""
taste = ""
# )end properties
#methods(
def print_fruit(self):
print "%s :" % self.fruit
print self.color
print self.taste
# has to be self because what if multiple fruits!
# self is a reference to the object that is currently being manipulated
#)
#methods are functions contained within a class. The diefference is that you put a method inside a class,
# and it belongs to that class. If you ever want to call that method you have to reference an object
# of that class first, just like the variables.
# KEY! Big difference between method and function is a method always has to have an argument! called
# self between the parentheses.
# a class on its own isnt't something you can directly manipulate: first we have to create
# an instance of the class to play with. You can store the instance in a variable.
#INHERITANCE
class apple(fruit):
fruit = "apple"
color = "red"
taste = "sweet"
def stem(self):
print "Have a stem"
class orange(fruit):
fruit = "orange"
color = "orange"
taste = "sour"
def rhine(self):
print "Have a rhine"
Apple = apple()
Apple.print_fruit()
Apple.stem()
Orange = orange()
Orange.print_fruit()
Orange.rhine()