Skip to content

Part 1 and 2 Completed #1141

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 13 commits into
base: master
Choose a base branch
from
5 changes: 4 additions & 1 deletion src/00_hello.py
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
# Print "Hello, world!" to your terminal
# Print "Hello, world!" to your terminal

hello = "Hello, world"
print(hello)
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: 3 additions & 2 deletions src/02_datatypes.py
Original file line number Diff line number Diff line change
@@ -14,8 +14,9 @@
# Write a print statement that combines x + y into the integer value 12

# YOUR CODE HERE

print(int(y) + x)

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

# YOUR CODE HERE
# YOUR CODE HERE
print(f'{x}{y}')
31 changes: 30 additions & 1 deletion src/03_modules.py
Original file line number Diff line number Diff line change
@@ -5,27 +5,56 @@
level operating system functionality.
"""

import getpass
import sys
import argparse
import platform
import os

# See docs for the sys module: https://docs.python.org/3.7/library/sys.html

# Print out the command line arguments in sys.argv, one per line:
# YOUR CODE HERE

# Total arguments
n = len(sys.argv)
print("Total arguments passed: " + str(n))

# Arguments passed:
print("\nName of python script:", sys.argv[0])

print("\nArguments passed:", end = " ")
for i in range(1, n):
print(sys.argv[i], end = " ")

# Print out the OS platform you're using:
# YOUR CODE HERE
print("My OS is", os.name)


# Print out the version of Python you're using:
# YOUR CODE HERE

print("My python Version is:", sys.version)
print("MY python version infi is:", sys.version_info)

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("This is my current process ID:", os.getpid())

# Print the current working directory (cwd):
# YOUR CODE HERE

print("My current working directory is", os.getcwd())

# Print out your machine's login name
# YOUR CODE HERE

username = os.getlogin()
username2 = getpass.getuser

print("My computers username is:", username)
print("MY computers username2 is", username2)
9 changes: 8 additions & 1 deletion src/04_printing.py
Original file line number Diff line number Diff line change
@@ -12,6 +12,13 @@
# y, and z:
# x is 10, y is 2.25, z is "I like turtles!"

print("x: %2d, y: %2.2f, z: \"%s\"" % (x, y, z))

print("=================================")


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

# Finally, print the same thing using an f-string
# Finally, print the same thing using an f-

print(f"x = {x}, \ny = {y:.2f}, \nz = \"{z}\"")
10 changes: 9 additions & 1 deletion src/05_lists.py
Original file line number Diff line number Diff line change
@@ -8,22 +8,30 @@

# 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
x += y
print(x)

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

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

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

# Print all the values in x multiplied by 1000
# YOUR CODE HERE
# YOUR CODE HERE

print(list(map(lambda a: a * 1000, x)))
6 changes: 4 additions & 2 deletions src/06_tuples.py
Original file line number Diff line number Diff line change
@@ -34,11 +34,13 @@ 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 element in tuple:
print(element)

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?
u = (1,) # What needs to be added to make this work?
print_tuple(u)
15 changes: 8 additions & 7 deletions src/07_slices.py
Original file line number Diff line number Diff line change
@@ -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()
print(a[2:4])

# 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()
# When using minus one and the colon to the left the compiler then goes all the way from the last number to the left (-1 to -6)
print(a[:-1])

# For string s...

s = "Hello, world!"

# Output just the 8th-12th characters: "world"
print()
print(s[7:12])
16 changes: 9 additions & 7 deletions src/08_comprehensions.py
Original file line number Diff line number Diff line change
@@ -10,32 +10,34 @@

# Write a list comprehension to produce the array [1, 2, 3, 4, 5]

y = []
y = [num for num in range(1, 6)]
print(y)

print (y)
y1 = []
for num in range(1, 6):
y1.append(num)
print(y1)

# 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 = [num**3 for num in range(10)]
print(y)

# Write a list comprehension to produce the uppercase version of all the
# elements in array a. Hint: "foo".upper() is "FOO".

a = ["foo", "bar", "baz"]

y = []
y = [word.upper() for word in a]

print(y)

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

x = input("Enter comma-separated numbers: ").split(',')

# What do you need between the square brackets to make it work?
y = []
y = [num for num in x if int(num) % 2 == 0]

print(y)
39 changes: 38 additions & 1 deletion src/09_dictionaries.py
Original file line number Diff line number Diff line change
@@ -14,6 +14,7 @@
- lon: a signed integer representing a longitude value
- name: a name string for this location
"""
import json

waypoints = [
{
@@ -36,12 +37,48 @@
# Add a new waypoint to the list
# YOUR CODE HERE

waypoints.append( {
"lat": 120,
"lon": -11,
"name": "Some random place"
})

# Another way
# anotherPlace = { "lat": 42, "lon": -120, "name": "a fourth place"}
# waypoints.append(anotherPlace)
# print(waypoints)

# *************How to print json data **************
# json_data = '[{"ID":10,"Name":"Pankaj","Role":"CEO"},' \
# '{"ID":20,"Name":"David Lee","Role":"Editor"}]'

# json_object = json.loads(json_data)
# json_formatted_str = json.dumps(json_object, indent=2)
# print(json_formatted_str)
# dont forget to import json

# with open('EmployeeData.json', 'r') as json_file:
# json_object = json.load(json_file)

# print(json_object)
# print(json.dumps(json_object))
# print(json.dumps(json_object, indent=1))
# **********************************************************


# 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
waypoints[0]["lon"] = -130
waypoints[0]["name"] = "not a real place"

# Write a loop that prints out all the field values for all the waypoints
# YOUR CODE HERE

# YOUR CODE HERE
for i, waypoint in enumerate(waypoints):
print(f"Waypoint #{i + 1}")
for key, value in waypoint.items():
print(f" { key }: {value}")
7 changes: 6 additions & 1 deletion src/10_functions.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# Write a function is_even that will return true if the passed-in number is even.

# YOUR CODE HERE
def is_even(num):
return num % 2 == 0

# Read a number from the keyboard
num = input("Enter a number: ")
@@ -9,4 +11,7 @@
# Print out "Even!" if the number is even. Otherwise print "Odd"

# YOUR CODE HERE

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

# YOUR CODE HERE
def f1(num1, num2):
return num1 + num2

print(f1(1, 2))

@@ -14,6 +16,12 @@

# YOUR CODE HERE

def f2(*nums):
sum = 0
for num in nums:
sum += num
return sum

print(f2(1)) # Should print 1
print(f2(1, 3)) # Should print 4
print(f2(1, 4, -12)) # Should print -7
@@ -22,7 +30,7 @@
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
@@ -31,6 +39,9 @@

# YOUR CODE HERE

def f3(num1, num2 = 1):
return num1 + num2

print(f3(1, 2)) # Should print 3
print(f3(8)) # Should print 9

@@ -45,6 +56,10 @@

# YOUR CODE HERE

def f4(**keyValues):
for key, value in keyValues.items():
print("Key: %s, Value: %s" % (key, value))

# Should print
# key: a, value: 12
# key: b, value: 30
@@ -62,4 +77,4 @@
}

# How do you have to modify the f4 call below to make this work?
f4(d)
f4(**d)
5 changes: 4 additions & 1 deletion src/12_scopes.py
Original file line number Diff line number Diff line change
@@ -5,9 +5,11 @@
x = 12

def change_x():
global x
x = 99

change_x()
change_x()


# This prints 12. What do we have to modify in change_x() to get it to print 99?
print(x)
@@ -19,6 +21,7 @@ def outer():
y = 120

def inner():
nonlocal y
y = 999

inner()
12 changes: 11 additions & 1 deletion src/13_file_io.py
Original file line number Diff line number Diff line change
@@ -11,9 +11,19 @@

# 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('Some random text \n')
f.write('This is some new line of code \n')
f.write('Some more random stuff to write \n')
f.close()
15 changes: 14 additions & 1 deletion src/14_cal.py
Original file line number Diff line number Diff line change
@@ -29,4 +29,17 @@

import sys
import calendar
from datetime import datetime
from datetime import datetime

def cal(month = datetime.now().month, year = datetime.now().year):
print(calendar.month(theyear = int(year), themonth = int(month)))

arg = sys.argv
if len(arg) == 2:
cal(month = arg[1])
elif len(arg) == 3:
cal(arg[1], arg[2])
elif len(arg) > 3:
print('Please provide a month and a year')
else:
cal()
29 changes: 29 additions & 0 deletions src/15_classes.py
Original file line number Diff line number Diff line change
@@ -3,20 +3,47 @@

# YOUR CODE HERE

class LatLon():
def __init__(self, lat, lon):
self.lat = lat
self.lon = lon
# Make a class Waypoint that can be passed parameters `name`, `lat`, and `lon` to the
# constructor. It should inherit from LatLon. Look up the `super` method.

# YOUR CODE HERE
class Waypoint(LatLon):
def __init__(self, name, lat, lon):
self.name = name
super().__init__(lat, lon)

def __str__(self):
return f'Name: {self.name}, Lat: {self.lat}, Lon: {self.lon}'

# Make a class Geocache that can be passed parameters `name`, `difficulty`,
# `size`, `lat`, and `lon` to the constructor. What should it inherit from?

# YOUR CODE HERE

class Geocache(Waypoint):
def __init__(self, name, difficulty, size, lat, lon):
super().__init__(name, lat, lon)
self.difficulty = difficulty
self.size = size

def __str__(self):
return (f'Name: {self.name}, '\
f'Difficulty: {self.difficulty}, '\
f'Size: {self.size}, '\
f'Lat: {self.lat}, '\
f'Lon: {self.lon}')

# Make a new waypoint and print it out: "Catacombs", 41.70505, -121.51521

# YOUR CODE HERE

waypoint = Waypoint('Catacombs', 41.70505, -121.51521)
print(waypoint)

# Without changing the following line, how can you make it print into something
# more human-readable? Hint: Look up the `object.__str__` method
print(waypoint)
@@ -25,5 +52,7 @@

# YOUR CODE HERE

geocache = Geocache('Newberry Views', 1.5, 2, 44.052137, -121.41556)

# Print it--also make this print more nicely
print(geocache)
3 changes: 3 additions & 0 deletions src/bar.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Some random text
This is some new line of code
Some more random stuff to write