2. Hello World

Local Python interpreters can execute lines of code as you input them – for example, on a Mac from Terminal you may run the command ‘python3’, which will load the Python version 3 interpreter.

Input math functions

1
2 + 2

Output

1
4

Input print function

1
2
3
4
5
6
# The print command echos back the data called within that function.
def main():
print ("hello world!")

if __name__ == "__main__":
main()

Output

1
Hello World

Python lets you write code that is either procedural, or object oriented, so you may use classes. Classes are a great way of encapsulating functionality that can be kept together and passed around for use in other projects.

Classes

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
class myClass:
def method1(self):
print("myClass method1")

# The 'self' argument refers to the object itself where it is being operated.

def method2(self, someString):
print("myClass method2 " + someString)

class anotherClass(myClass):
def method2(self):
print ("anotherClass method2")

def method1(self):
myClass.method1(self);
print ("anotherClass method1")

# 'c' instantiates method1 by calling on the class, 'myClass'.
def main():
c = myClass()
c.method1()
c.method2("This is a string")

c2 = anotherClass()
c2.method1()
c2.method2()

if __name__ == "__main__":
main()

The output from this script is

1
2
3
4
5
myClass method1
myClass method2 This is a string
myClass method1
anotherClass method1
anotherClass method2