Skip to content
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: 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
# YOUR CODE HERE
print(2**65536)
5 changes: 4 additions & 1 deletion src/02_datatypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@

# YOUR CODE HERE

print(x + int(y))

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

# YOUR CODE HERE
# YOUR CODE HERE

print(str(x) + y)
11 changes: 7 additions & 4 deletions src/03_modules.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,25 @@

# Print out the command line arguments in sys.argv, one per line:
# YOUR CODE HERE
for x in sys.argv:
print(x)

# Print out the OS platform you're using:
# YOUR CODE HERE

print(sys.platform);
# Print out the version of Python you're using:
# YOUR CODE HERE

print(sys.version);

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

# Print the current process ID
# YOUR CODE HERE

print(os.getpid());
# 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());
7 changes: 4 additions & 3 deletions src/04_printing.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
# 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!"

print('x is %2d, y is %4.2f, z is "%s"' % (x, y, z))
# Use the 'format' string method to print the same thing

# Finally, print the same thing using an f-string
print('x is {0}, y is {1:4.2f}, z is "{2}"'.format(x, y, z))
# Finally, print the same thing using an f-string
print(f"x is {x}, y is {y}, z is {z}")
16 changes: 13 additions & 3 deletions src/05_lists.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,32 @@

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

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

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can also use extend here


# Change x so that it is [1, 2, 3, 4, 9, 10]
# YOUR CODE HERE
for i in range(len(x)-1):
if (x[i] == 8):
del x[i]
print(x)

# Change x so that it is [1, 2, 3, 4, 9, 99, 10]
# YOUR CODE HERE
for i in range(len(x)):
if x[i] == 9:
x.insert(i+1, 99)
print(x)

# Print the length of list x
# YOUR CODE HERE

print(len(x))
# Print all the values in x multiplied by 1000
# YOUR CODE HERE
# YOUR CODE HERE
for a in x:
print(a*1000)
7 changes: 6 additions & 1 deletion src/06_tuples.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,12 @@ def dist(a, b):
# Write a function `print_tuple` that prints all the values in a tuple

# YOUR CODE HERE

def print_tuple(c):
if type(c) == list:
for d in c:
print(d)
else:
print(c)
t = (1, 2, 5, 7, 99)
print_tuple(t) # Prints 1 2 5 7 99, one per line

Expand Down
15 changes: 8 additions & 7 deletions src/07_slices.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,26 +12,27 @@
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[-2])

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

# Output the two middle elements in the array: [1, 7]
print()
b=int(len(a)/2)
print(a[b-1:b+1])

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

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

# For string s...

s = "Hello, world!"

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

y = []

i=1
while i < 6:
y.append(i)
i=i+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 = []

i=0
while i < 10:
y.append(i**3)
i=i+1
print(y)

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

y = []

for j in range(len(a)):
y.append(a[j].upper())
print(y)

# Use a list comprehension to create a list containing only the _even_ elements
# the user entered into list x.

# x=[1,2,3,4]
x = input("Enter comma-separated numbers: ").split(',')

# What do you need between the square brackets to make it work?
y = []

print(y)
for j in x:
if(int(j)%2 == 0):
y.append(j)
print(y)
12 changes: 9 additions & 3 deletions src/09_dictionaries.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,19 @@

# Add a new waypoint to the list
# YOUR CODE HERE

waypoints.append({'lat':40,"lon":-130,"name":"home"})
# 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

for x in range(len(waypoints)):
if(waypoints[x]['name'] == 'a place'):
waypoints[x]['name'] = 'not a real place'
waypoints[x]['lon'] = -130
# Write a loop that prints out all the field values for all the waypoints
# YOUR CODE HERE
# YOUR CODE HERE
for x in range(len(waypoints)):
for y in waypoints[x]:
print(waypoints[x][y])
8 changes: 6 additions & 2 deletions src/10_functions.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
# Write a function is_even that will return true if the passed-in number is even.

# YOUR CODE HERE

def is_even(x):
return (x%2 == 0)
# 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

if( is_even(num) ):
print("Even!")
else:
print("Odd")
22 changes: 17 additions & 5 deletions src/11_args.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,24 @@
# the sum. This is what you'd consider to be a regular, normal function.

# YOUR CODE HERE

def f1(a,b):
return a+b
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(*x):
s = 0
for i in x:
if(type(i) == list):
for j in i:
s = s+j
else:
s = s+i
return s
print(f2(1)) # Should print 1
print(f2(1, 3)) # Should print 4
print(f2(1, 4, -12)) # Should print -7
Expand All @@ -30,7 +39,8 @@
# Note: Google "python default arguments" for a hint.

# YOUR CODE HERE

def f3(a,b=1):
return a+b
print(f3(1, 2)) # Should print 3
print(f3(8)) # Should print 9

Expand All @@ -44,7 +54,9 @@
# Note: Google "python keyword arguments".

# YOUR CODE HERE

def f4(**x):
for i in x:
print('key:',i,', value:',x[i])
# Should print
# key: a, value: 12
# key: b, value: 30
Expand All @@ -62,4 +74,4 @@
}

# How do you have to modify the f4 call below to make this work?
f4(d)
f4(monster=d['monster'], hp=d['hp'])
6 changes: 4 additions & 2 deletions src/12_scopes.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@

def change_x():
x = 99
return x

change_x()
x=change_x()

# This prints 12. What do we have to modify in change_x() to get it to print 99?
print(x)
Expand All @@ -20,8 +21,9 @@ def outer():

def inner():
y = 999
return y

inner()
y=inner()

# This prints 120. What do we have to change in inner() to get it to print
# 999?
Expand Down
15 changes: 13 additions & 2 deletions src/13_file_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,21 @@
# Note: pay close attention to your current directory when trying to open "foo.txt"

# YOUR CODE HERE

f = open('foo.txt', "r")
print(f.read())
f.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
# YOUR CODE HERE
f = open('bar.txt', "w")
f.write("Now the file has content \r")
f.write("Now the file has more content \r")
f.write("Now the file has new content \r")
f.close()

f = open('bar.txt', "r")
print(f.read())
f.close()
15 changes: 14 additions & 1 deletion src/14_cal.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,17 @@

import sys
import calendar
from datetime import datetime
from datetime import datetime
if (len(sys.argv) > 2):
month = int(sys.argv[1])
year = int(sys.argv[2])
print(calendar.TextCalendar().formatmonth(year, month))
elif (len(sys.argv) > 1):
month = int(sys.argv[1])
year = datetime.now().year
print(calendar.TextCalendar().formatmonth(year, month))
else:
year = datetime.now().year
month = datetime.now().month
print(calendar.TextCalendar().formatmonth(year, month))
exit()
Loading