Skip to content
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

completed half of the exercises #1127

Open
wants to merge 3 commits 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: 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!')
6 changes: 5 additions & 1 deletion src/01_bignum.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
# 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
a = 2
b = 65536

print(a**b)
6 changes: 5 additions & 1 deletion src/02_datatypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@

# YOUR CODE HERE

print(5 + 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)
15 changes: 13 additions & 2 deletions src/03_modules.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,39 @@
methods, and the os module, which gives you access to lower-
level operating system functionality.
"""

import os
import sys
import getpass
# 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
print(f'sysargv: {sys.argv} \n')

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

print(f'OS platform: {sys.platform} \n')

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

print(f'Python version: {sys.version} \n')


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(f'Current Process: {os.getpid()} \n')

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

print(f'Current working directory (cwd): {os.getcwd()} \n')

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

print(f'Machine\'s login name: {getpass.getuser()} \n')
17 changes: 16 additions & 1 deletion src/04_printing.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,21 @@
# y, and z:
# x is 10, y is 2.25, z is "I like turtles!"

print('x is %i, y is %.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:.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:.2f}, z is {z}')

class Animal:
def __init( self, name, hunger, diet ):
self.name = name
self.hunger = hunger
self.diet = diet

def eat(self, food, hunger):
if food > 0 and hunger < 25:
hunger += food
10 changes: 9 additions & 1 deletion src/05_lists.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.extend(y)
print(x)

# Change x so that it is [1, 2, 3, 4, 9, 10]
# YOUR CODE HERE
del x[4]
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 all the values in x multiplied by 1000
# YOUR CODE HERE
# YOUR CODE HERE

for num in x:
print(num*1000)
8 changes: 6 additions & 2 deletions src/06_tuples.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,12 @@ def dist(a, b):
# YOUR CODE HERE

t = (1, 2, 5, 7, 99)
print_tuple(t) # Prints 1 2 5 7 99, one per line
def print_tuple(t):
for num in t:
print(num)
# Prints 1 2 5 7 99, one per line
print_tuple(t)

# 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)
14 changes: 7 additions & 7 deletions src/07_slices.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,26 +12,26 @@
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()
print(a[:-1])

# For string s...

s = "Hello, world!"

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

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

y = []
y = [i + 1 for i in range(5)]

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 = [i**3 for i in range(10)]

print(y)

Expand All @@ -26,7 +26,7 @@

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

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

print(y)

Expand All @@ -35,7 +35,11 @@

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

y= [int(num) % 2 == 0 for num in x ]

print(y)

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

print(y)
15 changes: 14 additions & 1 deletion src/09_dictionaries.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@

# Add a new waypoint to the list
# YOUR CODE HERE
waypoints.append({
"lat": 24,
"lon": -232,
"name": "the last place"
})

# Modify the dictionary with name "a place" such that its longitude
# value is -130 and change its name to "not a real place"
Expand All @@ -43,5 +48,13 @@

# YOUR CODE HERE

waypoints[0]['name'] = 'not a real place'
waypoints[0]['lon'] = -130

# print(waypoints)

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

for d in waypoints:
print(d)
7 changes: 7 additions & 0 deletions src/10_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,10 @@

# YOUR CODE HERE

while num:
if num % 2 == 0:
print('Even!')
break
else:
print('Odd!')
break
27 changes: 20 additions & 7 deletions src/11_args.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
# the sum. This is what you'd consider to be a regular, normal function.

# YOUR CODE HERE
def f1(x, y):
return x + y

print(f1(1, 2))

Expand All @@ -14,6 +16,9 @@

# YOUR CODE HERE

def f2(*args):
return sum(args)

print(f2(1)) # Should print 1
print(f2(1, 3)) # Should print 4
print(f2(1, 4, -12)) # Should print -7
Expand All @@ -22,14 +27,18 @@
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.
# # 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(a, b=1):
return a + b

# YOUR CODE HERE

print(f3(1, 2)) # Should print 3
print(f3(8)) # Should print 9
Expand All @@ -45,6 +54,10 @@

# YOUR CODE HERE

def f4(**kwargs):
for key, value in kwargs.items():
print(f'key: {key}, value: {value}')

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

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

def change_x():
x = 99
global x
x = 99

change_x()

Expand All @@ -19,6 +20,7 @@ def outer():
y = 120

def inner():
nonlocal y
y = 999

inner()
Expand Down
18 changes: 17 additions & 1 deletion src/13_file_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,25 @@

# YOUR CODE HERE

with open('foo.txt') as f:
read_data = f.read()

print(read_data + '\n')
print(f.closed + '\n')

# 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

with open('bar.txt', 'w+') as f:
write_data = f.write('Some stuff,\nsome more stuff,\nanother instance of stuff')

f.seek(0)

data = f.read()

print(data + '\n')
print(f.closed)
Loading