Skip to content

Commit

Permalink
Merge pull request #1 from MaryamMosstoufi/maryam-mosstoufi
Browse files Browse the repository at this point in the history
Maryam Mosstoufi
  • Loading branch information
MaryamMosstoufi authored Nov 13, 2020
2 parents aeb295d + a3f7015 commit 7b88351
Show file tree
Hide file tree
Showing 17 changed files with 179 additions and 43 deletions.
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: 3 additions & 2 deletions src/02_datatypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,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

# YOUR CODE HERE
# YOUR CODE HERE
print(str(x) + y)
13 changes: 8 additions & 5 deletions src/03_modules.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,30 @@
level operating system functionality.
"""

import os
import sys
# 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
for arg in sys.argv:
print(arg, end='\n')

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

print("Platform: ", sys.platform)
# Print out the version of Python you're using:
# YOUR CODE HERE
print("Python Version: ", 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("Process ID: ", os.getpid())
# Print the current working directory (cwd):
# YOUR CODE HERE

print("CWD: ", os.getcwd())
# Print out your machine's login name
# YOUR CODE HERE
print("Login Name : ", 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 %d, y is %f, z is %s!" % (x, round(y, 2), z))
# Use the 'format' string method to print the same thing

# Finally, print the same thing using an f-string
print(("x is {}, y is {}, z is {}!").format(x, round(y, 2), z))
# Finally, print the same thing using an f-string
print(f"x is {x}, y is {round(y, 2)}, z is {z}!")
10 changes: 7 additions & 3 deletions src/05_lists.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,26 @@

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

# Change x so that it is [1, 2, 3, 4, 9, 10]
# YOUR CODE HERE
x.extend([9, 10])
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
print([num*1000 for num in x])
9 changes: 7 additions & 2 deletions src/06_tuples.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,28 +17,33 @@

import math


def dist(a, b):
"""Compute the distance between two x,y points."""
x0, y0 = a # Destructuring assignment
x1, y1 = b

return math.sqrt((x1 - x0)**2 + (y1 - y0)**2)


a = (2, 7) # <-- x,y coordinates stored in tuples
b = (-14, 72)

# Prints "Distance is 66.94"
print("Distance is: {:.2f}".format(dist(a, b)))



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

# YOUR CODE HERE
def print_tuple(x):
for item in x:
print(item)


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)
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[:-2])

# For string s...

s = "Hello, world!"

# Output just the 8th-12th characters: "world"
print()
print(s[7:12])
16 changes: 7 additions & 9 deletions src/08_comprehensions.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,20 @@
List comprehensions are one cool and unique feature of Python.
They essentially act as a terse and concise way of initializing
and populating a list given some expression that specifies how
the list should be populated.
the list should be populated.
Take a look at https://docs.python.org/3/tutorial/datastructures.html#list-comprehensions
for more info regarding list comprehensions.
"""

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

y = []

print (y)
y = [i for i in range(1, 6)]
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 +24,7 @@

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

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

print(y)

Expand All @@ -36,6 +34,6 @@
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)
print(y)
17 changes: 14 additions & 3 deletions src/09_dictionaries.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,24 @@

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

waypoints.append({
"lat": 53,
"lon": -24,
"name": "my place"
},)
print(waypoints)
# 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 place in waypoints:
if place["name"] == "a place":
place["lon"] = -130
place["name"] = "not a real place"

print(waypoints)
# Write a loop that prints out all the field values for all the waypoints
# YOUR CODE HERE
# YOUR CODE HERE
for place in waypoints:
print(place)
10 changes: 9 additions & 1 deletion src/10_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,19 @@

# YOUR CODE HERE

def is_even(x):
if x % 2 == 0:
return True


# 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!")
27 changes: 25 additions & 2 deletions src/11_args.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
# 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))

Expand All @@ -14,6 +17,14 @@

# YOUR CODE HERE


def f2(*x):
sum = 0
for each in x:
sum = sum + each
return sum


print(f2(1)) # Should print 1
print(f2(1, 3)) # Should print 4
print(f2(1, 4, -12)) # Should print -7
Expand All @@ -22,7 +33,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
Expand All @@ -31,6 +42,14 @@

# YOUR CODE HERE


def f3(*x):
if len(x) == 1:
return x[0] + 1
else:
return x[0]+x[1]


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

Expand All @@ -44,6 +63,10 @@
# Note: Google "python keyword arguments".

# YOUR CODE HERE
def f4(**data):
for key, value in data.items():
print("key: {}, value: {}".format(key, value))


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

# How do you have to modify the f4 call below to make this work?
f4(d)
f4(**d)
4 changes: 4 additions & 0 deletions src/12_scopes.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@
# When you use a variable in a function, it's local in scope to the function.
x = 12


def change_x():
global x
x = 99


change_x()

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

def inner():
nonlocal y
y = 999

inner()
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

with open("foo.txt") as f:
for i in f:
print(i)
# 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 b:
b.write("line one \n")
b.write("line two \n")
b.write("line three \n")
b.close()

# c = open("foo.txt", "w")
# c.write("something")
# c.close()
Loading

0 comments on commit 7b88351

Please sign in to comment.