-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathclasses.py
32 lines (24 loc) · 946 Bytes
/
classes.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
# Classes are used to group functions and variables together.
class Counter():
# The init or initializer function is where you declare variables.
def __init__(self, count):
# Preface all variables with "self."
self.count = count
# Member functions can be defined below.
# Be sure to also include the self in the arguments.
def increase(self):
self.count += 1
def decrease(self):
self.count -= 1
# Classes must be instantiated before being used.
# Create a class called students and set it to 10.
students = Counter(10)
# Create a class called students and set it to 3.
teachers = Counter(3)
# One student dropped out so decrease the number of students.
students.decrease()
# One teacher was hired so increase the number of teachers.
teachers.increase()
# Print the number of students and teachers.
print('Students: ' + str(students.count))
print('Teachers: ' + str(teachers.count))