Pure Programmer
Blue Matrix


Cluster Map

Lists

L1

This page is under construction. Please come back later.

Lists1.py
#!/usr/bin/env python3;
import Utils

# Begin Main
NUM_SQUARES = 5
squares = []
# Put the squares into the list
for i in range(0, NUM_SQUARES) :
	squares.append(i * i)
# Print out the squares from the list
for i in range(0, len(squares)) :
	print("{0:d}^2 = {1:d}".format(i, squares[i]))

print(Utils.listToString(squares))

Output
$ python3 Lists1.py 0^2 = 0 1^2 = 1 2^2 = 4 3^2 = 9 4^2 = 16 [0, 1, 4, 9, 16]
Lists2.py
#!/usr/bin/env python3;
import Utils

# Begin Main
squares = [0, 1, 4, 9, 16, 25]

# Print out the squares from the list
for i in range(0, len(squares)) :
	print("{0:d}^2 = {1:d}".format(i, squares[i]))

Output
$ python3 Lists2.py 0^2 = 0 1^2 = 1 2^2 = 4 3^2 = 9 4^2 = 16 5^2 = 25
Lists3.py
#!/usr/bin/env python3;
# Begin Main
names = ["Fred", "Wilma", "Barney", "Betty"]

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

names = ["Harry", "Ron", "Hermione"]
for name in names :
	print("Hello, " + name + "!")

Output
$ python3 Lists3.py Hello, Fred! Hello, Wilma! Hello, Barney! Hello, Betty! Hello, Harry! Hello, Ron! Hello, Hermione!
Lists4.py
#!/usr/bin/env python3;
import Utils
import sys

# Begin Main
# Print out the command line arguments
for i in range(1, (len(sys.argv) - 1) + 1) :
	print(str(i) + ":" + sys.argv[i])

Output
$ python3 Lists4.py Fred Barney Wilma Betty 1:Fred 2:Barney 3:Wilma 4:Betty $ python3 Lists4.py 10 20 30 40 1:10 2:20 3:30 4:40
Lists5.py
#!/usr/bin/env python3;
import Utils
import sys

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

# Print out the name based on command line argument 1-4
for i in range(1, (len(sys.argv) - 1) + 1) :
	x = Utils.stoiWithDefault(sys.argv[i], 0)
	print(str(i) + ":" + names[x - 1])

Output
$ python3 Lists5.py 1 2 3 4 1:Fred 2:Wilma 3:Barney 4:Betty $ python3 Lists5.py 4 3 2 1 1:Betty 2:Barney 3:Wilma 4:Fred $ python3 Lists5.py 1 3 1:Fred 2:Barney

Questions

Projects

More ★'s indicate higher difficulty level.

References