Pure Programmer
Blue Matrix


Cluster Map

Functions

L1

This page is under construction. Please come back later.

Functions1.py
#!/usr/bin/env python3;
def printHello() :
	print("Hello, world!")

# Begin Main
printHello()

Output
$ python3 Functions1.py Hello, world!
Functions2.py
#!/usr/bin/env python3;
def printHello(name) :
	print("Hello, " + name + "!")

# Begin Main
printHello("Fred")
printHello("Wilma")
printHello("Barney")
printHello("Betty")

Output
$ python3 Functions2.py Hello, Fred! Hello, Wilma! Hello, Barney! Hello, Betty!
Functions3.py
#!/usr/bin/env python3;
import Utils

def squared(j) :
	return j * j

# Begin Main
for i in range(1, 10 + 1, 1) :
	print("{0:d}^2 = {1:d}".format(i, squared(i)))

Output
$ python3 Functions3.py 1^2 = 1 2^2 = 4 3^2 = 9 4^2 = 16 5^2 = 25 6^2 = 36 7^2 = 49 8^2 = 64 9^2 = 81 10^2 = 100
Functions4.py
#!/usr/bin/env python3;
import Utils
import numpy

def exponentiation(base, powerArg) :
	power = powerArg
	result = 1
	while (power > 0) :
		result *= base
		power -= 1
	return result

# Begin Main
for b in numpy.arange(1.1, 2.05, 0.1) :
	for p in range(2, 10 + 1) :
		print("{0:f} ^ {1:d} = {2:f}".format(b, p, exponentiation(b, p)))

Output
$ python3 Functions4.py Traceback (most recent call last): File "/Users/rich/Desktop/Resources/pureprogrammer/py/examples/Functions4.py", line 3, in <module> import numpy ModuleNotFoundError: No module named 'numpy'
Functions5.py
#!/usr/bin/env python3;
import Utils
import numpy

def factorial(x) :
	if (x <= 1) :
		return 1
	return x * factorial(x - 1)

# Begin Main
for x in numpy.arange(1, 10) :
	print("{0:d}! = {1:d}".format(x, factorial(x)))

Output
File not found!: /kunden/homepages/39/d957328751/htdocs/pureprogrammer/py/examples/output/Functions5.out
Lists6.py
#!/usr/bin/env python3;
import Utils

def reverseList(x) :
	y = []
	for i in range(len(x) - 1, 0 + -1, -1) :
		y.append(x[i])
	return y

# Begin Main
names = ["Fred", "Wilma", "Barney", "Betty"]

for name in names :
	print("Hello, " + name + "!")

names = reverseList(names)
for name in names :
	print("Hello, " + name + "!")

print(Utils.listToString(names))

Output
$ python3 Lists6.py Hello, Fred! Hello, Wilma! Hello, Barney! Hello, Betty! Hello, Betty! Hello, Barney! Hello, Wilma! Hello, Fred! ["Betty", "Barney", "Wilma", "Fred"]
Maps3.py
#!/usr/bin/env python3;
import Utils
import math

def convertKMtoMiles(x) :
	y = {}
	for planet in x.keys() :
		y[planet] = math.trunc(x[planet] * 0.621371 + 0.5)
	return y

# Begin Main
planetDiametersInKM = {
	"Mercury": 4879,
	"Venus": 12103,
	"Earth": 12756,
	"Mars": 6794,
	"Jupiter": 142985,
	"Saturn": 120534,
	"Uranus": 51115,
	"Neptune": 49534,
	"Pluto": 2374,
	"Ceres": 946,
	"Eris": 2326,
	"Makemake": 1430
}

planetDiametersInMiles = convertKMtoMiles(planetDiametersInKM)
for planet in planetDiametersInMiles.keys() :
	print(planet + " has a diameter of " + str(planetDiametersInMiles[planet]) + " miles")

Output
$ python3 Maps3.py Mercury has a diameter of 3032 miles Venus has a diameter of 7520 miles Earth has a diameter of 7926 miles Mars has a diameter of 4222 miles Jupiter has a diameter of 88847 miles Saturn has a diameter of 74896 miles Uranus has a diameter of 31761 miles Neptune has a diameter of 30779 miles Pluto has a diameter of 1475 miles Ceres has a diameter of 588 miles Eris has a diameter of 1445 miles Makemake has a diameter of 889 miles
Tuples3.py
#!/usr/bin/env python3;
import Utils

def swap(x) :
	y = [x[1], x[0]]
	return y

# Begin Main
pair1 = ("Hello", 5)
pair2 = swap(pair1)
print(Utils.tupleToString(pair1) + " becomes " + Utils.tupleToString(pair2))

Output
$ python3 Tuples3.py <"Hello", 5> becomes <5, "Hello">
python

Questions

Projects

More ★'s indicate higher difficulty level.

References