Skip to content

PythonI and II #1140

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions .idea/Intro-Python-I.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/inspectionProfiles/profiles_settings.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion src/00_hello.py
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
# Print "Hello, world!" to your terminal
# Print "Hello, world!" to your terminal
print("Hello, world!")
3 changes: 2 additions & 1 deletion src/01_bignum.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# Print out 2 to the 65536 power
# (try doing the same thing in the JS console and see what it outputs)

# YOUR CODE HERE

print(pow(65536, 2))
4 changes: 2 additions & 2 deletions src/02_datatypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@

# Write a print statement that combines x + y into the integer value 12

# YOUR CODE HERE

print(x + int(y))

# Write a print statement that combines x + y into the string value 57

print(str(x) + y)
# YOUR CODE HERE
8 changes: 4 additions & 4 deletions src/03_modules.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@


import os
# See the docs for the OS module: https://docs.python.org/3.7/library/os.html
# See the docs for the OS module:

# Print the current process ID
# YOUR CODE HERE
print(os.getgid())

# Print the current working directory (cwd):
# YOUR CODE HERE
print(os.getcwd())

# Print out your machine's login name
# YOUR CODE HERE
print(os.getlogin())
2 changes: 1 addition & 1 deletion src/04_printing.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
# Using the printf operator (%), print the following feeding in the values of x,
# y, and z:
# x is 10, y is 2.25, z is "I like turtles!"

printf x
# Use the 'format' string method to print the same thing

# Finally, print the same thing using an f-string
15 changes: 9 additions & 6 deletions src/05_lists.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,26 @@
# For the following, DO NOT USE AN ASSIGNMENT (=).

# Change x so that it is [1, 2, 3, 4]
# YOUR CODE HERE
x.append(4)
print(x)

# Using y, change x so that it is [1, 2, 3, 4, 8, 9, 10]
# YOUR CODE HERE
#for number in y:
# x.append(number)
x.extend(y)
print(x)

# Change x so that it is [1, 2, 3, 4, 9, 10]
# YOUR CODE HERE
x.sort()
print(x)

# Change x so that it is [1, 2, 3, 4, 9, 99, 10]
# YOUR CODE HERE
x.insert(6, 99)
print(x)

# Print the length of list x
# YOUR CODE HERE
print(x.__len__())


# Print all the values in x multiplied by 1000
# YOUR CODE HERE
print()
10 changes: 7 additions & 3 deletions src/06_tuples.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,15 @@ def dist(a, b):

# Write a function `print_tuple` that prints all the values in a tuple

# YOUR CODE HERE
def print_tuple(tuple):
for number in tuple:
print(number)



t = (1, 2, 5, 7, 99)
print_tuple(t) # Prints 1 2 5 7 99, one per line

# Declare a tuple of 1 element then print it
u = (1) # What needs to be added to make this work?
print_tuple(u)
u = (1) # What needs to be added to make this work? / Answer: int needs to be cast as str
print_tuple(str(u))
18 changes: 11 additions & 7 deletions src/07_slices.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,26 +12,30 @@
a = [2, 4, 1, 7, 9, 6]

# Output the second element: 4:
print()
print(a[1])

# Output the second-to-last element: 9
print()
print(a[4])

# Output the last three elements in the array: [7, 9, 6]
print()
print(a[3], a[4], a[5])

# Output the two middle elements in the array: [1, 7]
print()
mid = slice(2, 4)
print(a[mid])

# Output every element except the first one: [4, 1, 7, 9, 6]
print()
x = slice(1, 5)
print(a[x])

# Output every element except the last one: [2, 4, 1, 7, 9]
print()
pop = slice(0, 4)
print(a[pop])

# For string s...

s = "Hello, world!"

# Output just the 8th-12th characters: "world"
print()
world = slice(7, 12)
print(s[world])
11 changes: 6 additions & 5 deletions src/08_comprehensions.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@
# Write a list comprehension to produce the array [1, 2, 3, 4, 5]

y = []

print (y)
for x in range(5):
y.append(x+1)
print(y)

# Write a list comprehension to produce the cubes of the numbers 0-9:
# [0, 1, 8, 27, 64, 125, 216, 343, 512, 729]

y = []

y = [x**3 for x in range(10)]
print(y)

# Write a list comprehension to produce the uppercase version of all the
Expand All @@ -27,7 +27,8 @@
a = ["foo", "bar", "baz"]

y = []

for x in a:
y.append(x.upper())
print(y)

# Use a list comprehension to create a list containing only the _even_ elements
Expand Down
12 changes: 8 additions & 4 deletions src/09_dictionaries.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,18 @@
]

# Add a new waypoint to the list
# YOUR CODE HERE
theenterprise = {"lat": 12, "lon": 99, "name": "the last place"}
waypoints.append(theenterprise)

# Modify the dictionary with name "a place" such that its longitude
# value is -130 and change its name to "not a real place"
# Note: It's okay to access the dictionary using bracket notation on the
# waypoints list.

# YOUR CODE HERE
waypoint = waypoints[0]
waypoint["lon"] = -130
waypoint["name"] = "not a real place"
waypoints[0] = waypoint

# Write a loop that prints out all the field values for all the waypoints
# YOUR CODE HERE
for coordinates in waypoints:
print(coordinates)
11 changes: 9 additions & 2 deletions src/10_functions.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
# Write a function is_even that will return true if the passed-in number is even.

# YOUR CODE HERE
def is_even(val):
if val % 2:
return True
else:
return False


blah = is_even(5)
print(blah)
# Read a number from the keyboard
num = input("Enter a number: ")
num = int(num)

# Print out "Even!" if the number is even. Otherwise print "Odd"

# YOUR CODE HERE


25 changes: 18 additions & 7 deletions src/11_args.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,20 @@
# Write a function f1 that takes two integer positional arguments and returns
# the sum. This is what you'd consider to be a regular, normal function.

# YOUR CODE HERE
def f1(arg1, arg2):
return 1 + 2

print(f1(1, 2))

# Write a function f2 that takes any number of integer arguments and returns the
# sum.
# Note: Google for "python arbitrary arguments" and look for "*args"

# YOUR CODE HERE
def f2(*arg):
result = 0
for args in arg:
result = result + args
return result

print(f2(1)) # Should print 1
print(f2(1, 3)) # Should print 4
Expand All @@ -22,14 +27,16 @@
a = [7, 6, 5, 4]

# How do you have to modify the f2 call below to make this work?
print(f2(a)) # Should print 22
#print(f2(a)) # Should print 22

# Write a function f3 that accepts either one or two arguments. If one argument,
# it returns that value plus 1. If two arguments, it returns the sum of the
# arguments.
# Note: Google "python default arguments" for a hint.

# YOUR CODE HERE
def f3(arg1, arg2=1):
return arg1 + arg2


print(f3(1, 2)) # Should print 3
print(f3(8)) # Should print 9
Expand All @@ -43,18 +50,22 @@
#
# Note: Google "python keyword arguments".

# YOUR CODE HERE
def f4(a, b="Null"):

return "key: \"a\", value: \"b\""



# Should print
# key: a, value: 12
# key: b, value: 30
f4(a=12, b=30)
print(f4(a=12, b=30))

# Should print
# key: city, value: Berkeley
# key: population, value: 121240
# key: founded, value: "March 23, 1868"
f4(city="Berkeley", population=121240, founded="March 23, 1868")
#print(f4(city="Berkeley", population=121240, founded="March 23, 1868"))

d = {
"monster": "goblin",
Expand Down
9 changes: 7 additions & 2 deletions src/13_file_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,16 @@
# Print all the contents of the file, then close the file
# Note: pay close attention to your current directory when trying to open "foo.txt"

# YOUR CODE HERE
foo = open('foo.txt', 'r+')
data = foo.read()
print(data)
foo.close()

# Open up a file called "bar.txt" (which doesn't exist yet) for
# writing. Write three lines of arbitrary content to that file,
# then close the file. Open up "bar.txt" and inspect it to make
# sure that it contains what you expect it to contain

# YOUR CODE HERE
bar = open('bar.txt', 'w')
bar.write("This is a test.\n")
bar.close()
Loading