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

arturo-obregon-dspt3-intro-python-I #1137

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
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
hi = 'Hello, world!'

print(hi)
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)
big = 2**65536

# YOUR CODE HERE
print(big)
8 changes: 4 additions & 4 deletions src/02_datatypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
Python is a strongly-typed language under the hood, which means
that the types of values matter, especially when we're trying
to perform operations on them.

Note that if you try running the following code without making any
changes, you'll get a TypeError saying you can't perform an operation
on a string and an integer.
Expand All @@ -12,10 +11,11 @@
y = "7"

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

# YOUR CODE HERE
in = x + int(y)


# Write a print statement that combines x + y into the string value 57
st = str(x) + y

# YOUR CODE HERE
print(in)
print(st)
30 changes: 20 additions & 10 deletions src/03_modules.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,37 @@
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
arguements = [arg for arg in sys.argv]
for i, arg in enumerate(arguements):
print(f'Arguments{i}: {arg}')
print()

# Print out the OS platform you're using:
# YOUR CODE HERE
OS_platform = sys.platform
print(f'Operating System: {OS_platform}')
print()

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

python_version = sys.version
print(f'Python Version: {python_version}')
print()

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
process_ID = os.getpid()
print(f'Current Process ID: {process_ID}')
print()

# Print the current working directory (cwd):
# YOUR CODE HERE
current_dir = os.getcwd()
print(f'CWD: {current_dir}')
print()

# Print out your machine's login name
# YOUR CODE HERE
name = os.getlogin()
print(f'Login Name: {name}')
print()
18 changes: 14 additions & 4 deletions src/04_printing.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,27 @@
"""
Python provides a number of ways to perform printing. Research
how to print using the printf operator, the `format` string
how to print using the printf operator, the `format` string
method, and by using f-strings.
"""

x = 10
y = 2.24552
z = "I like turtles!"

# Using the printf operator (%), print the following feeding in the values of x,
# 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!"
format_1 = 'x is %d, y is %2.2f, z is "%s"' % (x, y, z)
print(format_1)
print()

# Use the 'format' string method to print the same thing
format_2 = 'x is {}'.format(x)
+ ', y is {:.2f}'.format(y) + ', z is "{}"'.format(z)
print(format_2)
print()

# Finally, print the same thing using an f-string
# Finally, print the same thing using an f-string
format_3 = f'x is {x}, y is {y:.{2}f}, z is "{z}"'
print(format_3)
print()
24 changes: 14 additions & 10 deletions src/05_lists.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# For the exercise, look up the methods and functions that are available for use
# For the exercise, look up the methods and
# functions that are available for use
# with Python lists.

x = [1, 2, 3]
Expand All @@ -7,23 +8,26 @@
# For the following, DO NOT USE AN ASSIGNMENT (=).

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

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

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

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

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

# Print all the values in x multiplied by 1000
# YOUR CODE HERE
# YOUR CODE HERE
list_comp = [n * 1000 for n in x]
print(list_comp)
print()
19 changes: 10 additions & 9 deletions src/06_tuples.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,9 @@
are usually used to hold heterogenous data, as opposed to lists
which are typically used to hold homogenous data. Tuples use
parens instead of square brackets.

More specifically, tuples are faster than lists. If you're looking
to just define a constant set of values and that set of values
never needs to be mutated, use a tuple instead of a list.

Additionally, your code will be safer if you opt to "write-protect"
data that does not need to be changed. Tuples enforce immutability
automatically.
Expand All @@ -17,6 +15,7 @@

import math


def dist(a, b):
"""Compute the distance between two x,y points."""
x0, y0 = a # Destructuring assignment
Expand All @@ -26,19 +25,21 @@ def dist(a, b):

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


print()

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

# YOUR CODE HERE

def print_tuple(tup):
for i in tup:
print(i)

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

# 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)
print()
15 changes: 10 additions & 5 deletions src/07_slices.py
Original file line number Diff line number Diff line change
@@ -1,37 +1,42 @@
"""
Python exposes a terse and intuitive syntax for performing
Python exposes a terse and intuitive syntax for performing
slicing on lists and strings. This makes it easy to reference
only a portion of a list or string.

only a portion of a list or string.
This Stack Overflow answer provides a brief but thorough
overview: https://stackoverflow.com/a/509295

Use Python's slice syntax to achieve the following:
"""

a = [2, 4, 1, 7, 9, 6]

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

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

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

# Output the two middle elements in the array: [1, 7]
print(a[2:-2])
print()

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

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

# For string s...

s = "Hello, world!"

# Output just the 8th-12th characters: "world"
print()
print(s[7:12])
print()
28 changes: 15 additions & 13 deletions src/08_comprehensions.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,40 +2,42 @@
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
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)
ar = [n for n in range(1, 6)]
print(ar)
print()

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

y = []

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

# 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 = []

print(y)
up = [s.upper() for s in a]
print(up)
print()

# 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 = []

print(y)
ev = [int(n) for n in x if int(n) % 2 == 0]
print(ev)
print()
19 changes: 14 additions & 5 deletions src/09_dictionaries.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,8 @@
you'll find in other languages (though you can also initialize and
populate dictionaries using comprehensions just like you can with
lists!).

The docs can be found here:
https://docs.python.org/3/tutorial/datastructures.html#dictionaries

For this exercise, you have a list of dictionaries. Each dictionary
has the following keys:
- lat: a signed integer representing a latitude value
Expand All @@ -34,14 +32,25 @@
]

# Add a new waypoint to the list
# YOUR CODE HERE
waypoints.append(
{
"lat": 37,
"lon": 122,
"name": "GGB"
}
)

# 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

for dic in waypoints:
for key, value in zip(dic.keys(), dic.values()):
print(f'{key}: {value}')
print()
15 changes: 10 additions & 5 deletions src/10_functions.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
# Write a function is_even that will return true if the passed-in number is even.
# Write a function is_even that will return
# true if the passed-in number is even.

# YOUR CODE HERE

def is_even(n):
if n % 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')
Loading