Lists
This page is under construction. Please come back later.
#!/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]
#!/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
#!/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!
#!/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
#!/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
- {{Who's on first?}}
- {{Who's on second?}}
- {{Who's on third?}}
Projects
More ★'s indicate higher difficulty level.
- Array Last Element
- Central Limit Theorem
- Chromatic Scale Frequencies
- Income Tax (Revisited)
- Morse Code
- Renard Numbers (Preferred Numbers)
- Sample Mean and Standard Deviation
- Sieve of Eratosthenes
References
- [[Python Language Reference]]
- [[Python Standard Library]]
- [[Python at TutorialsPoint]]