8000 v2 · pythonminna/Intro-Python@4d67dce · GitHub
[go: up one dir, main page]

Skip to content

Commit 4d67dce

Browse files
author
Beej Jorgensen
committed
v2
1 parent 1ac3430 commit 4d67dce

File tree

7 files changed

+117
-0
lines changed

7 files changed

+117
-0
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,11 +70,14 @@ Suggested order for implementing the toy programs:
7070
* `hello` -- Hello world
7171
* `bignum` -- Print some big numbers
7272
* `datatypes` -- Experiment with type conversion
73+
* `modules` -- Learn to import from modules
7374
* `printf` -- Formatted print output
7475
* `lists` -- Python's version of arrays
76+
* `slice` -- Accessing parts of lists
7577
* `comp` -- List comprehensions
7678
* `dicts` -- Dictionaries
7779
* `func` -- Functions
80+
* `fileio` -- Read and write from files
7881
* `cal` -- Experiment with module imports
7982
* `obj` -- Classes and objects
8083

src/comp.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,25 @@
1+
# Write a list comprehension to produce the array [1, 2, 3, 4, 5]
2+
3+
y = []
4+
5+
print (y)
6+
7+
# Write a list comprehension to produce the cubes of the numbers 0-9:
8+
# [0, 1, 8, 27, 64, 125, 216, 343, 512, 729]
9+
10+
y = []
11+
12+
print(y)
13+
14+
# Write a list comprehension to produce the uppercase version of all the
15+
# elements in array a. Hint: "foo".upper() is "FOO".
16+
17+
a = ["foo", "bar", "baz"]
18+
19+
y = []
20+
21+
print(y)
22+
123
# Use a list comprehension to create a list containing only the _even_ elements
224
# the user entered into list x.
325

src/dicts.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,23 @@
77
# Make up three entries of various values.
88

99
waypoints = [
10+
{
11+
"lat": 43,
12+
"lon": -121,
13+
"name": "a place"
14+
},
15+
{
16+
"lat": 41,
17+
"lon": -123,
18+
"name": "another place"
19+
},
20+
{
21+
"lat": 43,
22+
"lon": -122,
23+
"name": "a third place"
24+
}
1025
]
1126

1227
# Write a loop that prints out all the field values for all the waypoints
28+
29+
# Add a new waypoint to the list

src/fileio.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# Use open to open file "foo.txt" for reading
2+
3+
# Print all the lines in the file
4+
5+
# Close the file
6+
7+
8+
# Use open to open file "bar.txt" for writing
9+
10+
# Use the write() method to write three lines to the file
11+
12+
# Close the file

src/foo.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
Do you bite your thumb at us, sir?
2+
I do bite my thumb, sir.
3+
Do you bite your thumb at us, sir?
4+
No, sir. I do not bite my thumb at you, sir, but I bite my thumb, sir.
5+
Do you quarrel, sir?
6+
Quarrel, sir? No, sir.

src/modules.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import sys
2+
import os
3+
4+
# Module "sys"
5+
#
6+
# See docs for the sys module: https://docs.python.org/3.7/library/sys.html
7+
8+
# Print out the command line arguments in sys.argv, one per line:
9+
10+
11+
# Print out the plaform from sys:
12+
print()
13+
14+
# Print out the Python version from sys:
15+
print()
16+
17+
18+
19+
# Module "os"
20+
#
21+
# See the docs for the OS module: https://docs.python.org/3.7/library/os.html
22+
23+
# Print the current process ID
24+
print()
25+
26+
# Print the current working directory (cwd):
27+
print()
28+
29+
# Print your login name
30+
print()
31+

src/slice.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
a = [2, 4, 1, 7, 9, 6]
2+
3+
# Output the second element: 4:
4+
print()
5+
6+
# Output the second-to-last element: 9
7+
print()
8+
9+
# Output the last three elements in the array: [7, 9, 6]
10+
print()
11+
12+
# Output the two middle elements in the array: [1, 7]
13+
print()
14+
15+
# Output every element except the first one: [4, 1, 7, 9, 6]
16+
print()
17+
18+
# Output every element except the last one: [2, 4, 1, 7, 9]
19+
print()
20+
21+
# For string s...
22+
23+
s = "Hello, world!"
24+
25+
# Output just the 8th-12th characters: "world"
26+
print()

0 commit comments

Comments
 (0)
0