Functions

This page is under construction. Please come back later.
#!/usr/bin/env python3;
def print_hello() :
print("Hello, world!")
# Begin Main
print_hello()
Output
$ python3 Functions1.py
Hello, world!
#!/usr/bin/env python3;
def print_hello(name) :
print("Hello, " + name + "!")
# Begin Main
print_hello("Fred")
print_hello("Wilma")
print_hello("Barney")
print_hello("Betty")
Output
$ python3 Functions2.py
Hello, Fred!
Hello, Wilma!
Hello, Barney!
Hello, Betty!
#!/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
#!/usr/bin/env python3;
import Utils
import numpy
def exponentiation(base, power_arg) :
power = power_arg
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'
#!/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
#!/usr/bin/env python3;
import Utils
def reverse_list(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 = reverse_list(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"]
#!/usr/bin/env python3;
import Utils
import math
def convert_kmto_miles(x) :
y = {}
for planet in x.keys() :
y[planet] = math.trunc(x[planet] * 0.621371 + 0.5)
return y
# Begin Main
planet_diameters_in_km = {
"Mercury": 4879,
"Venus": 12103,
"Earth": 12756,
"Mars": 6794,
"Jupiter": 142985,
"Saturn": 120534,
"Uranus": 51115,
"Neptune": 49534,
"Pluto": 2374,
"Ceres": 946,
"Eris": 2326,
"Makemake": 1430
}
planet_diameters_in_miles = convert_kmto_miles(planet_diameters_in_km)
for planet in planet_diameters_in_miles.keys() :
print(planet + " has a diameter of " + str(planet_diameters_in_miles[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
#!/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">
Questions
- {{Who's on first?}}
- {{Who's on second?}}
- {{Who's on third?}}
Projects
More ★'s indicate higher difficulty level.
- Alouette Lyrics
- Bingo Lyrics
- 99 Bottles of Beer Lyrics
- Bubble Sort
- Calculate Euler's Number
- Compute π Function
- Compute π (Monte Carlo with Central Limit Theorem)
- Credit Card Validator
- The Farmer in the Dell (Revisited)
- Fibonacci Numbers
- Football Passer Rating (Revisited)
- GCD
- Insertion Sort
- LCG Pseudo-Random Number Generator
- License Plate Generator (Revisited)
- Merge Sort
- Min/Max Functions
- Old MacDonald Lyrics
- Quicksort
- Renard Numbers with Rounding Function
- Roman Numerals
- Sign Function
- Spreadsheet Columns
- Tower of Hanoi
- Twelve Days of Christmas Lyrics
References
- [[Python Language Reference]]
- [[Python Standard Library]]
- [[Python at TutorialsPoint]]
Pure Programmer


