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

Basics #1143

Open
wants to merge 20 commits into
base: master
Choose a base branch
from
Open

Basics #1143

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

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)
6 changes: 4 additions & 2 deletions src/02_datatypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@
# Write a print statement that combines x + y into the integer value 12

# YOUR CODE HERE

#print(f'{x} + {y} = 12')
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}')
39 changes: 39 additions & 0 deletions src/03_modules.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,65 @@
"""

import sys
import argparse
import platform
import os
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

# Total Arguments:
n = len(sys.argv)
print("Total arguments passed:", 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 = " ")

# Addition of numbers:
Sum = 0
# Using argparse module
for i in range(1, n):
Sum += int(sys.argv[i])
print("\n\nResult:", Sum)

# for arg in sys.argv[1:]:
# print(arg)

# 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 Info 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 Process ID:", os.getpid())

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

print("My Current working directory:", os.getcwd())


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

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

print("My Computer Username2 is:", username2)
print("My Computer Username is:", username)
12 changes: 11 additions & 1 deletion src/04_printing.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,16 @@
# 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
print("x = {} \ny = {:.2f} \nz = \"{}\"".format(x, y, z))

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

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

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

# 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

# This could be written also like this:
#newArray = []
#newArray.append(x + y)
# x = newArray

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) # We're making sure that we're specifying the index first and the number we want to add to the list.
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
Expand Up @@ -33,12 +33,14 @@ 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)
29 changes: 22 additions & 7 deletions src/07_slices.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,26 +12,41 @@
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])
# print(a[-2]) This print the second to last element of the array, no matter hon long the array is.
# print(a[:4]) This prints [2, 4, 1, 7]
# print(a[4:]) Starts at 4 and includes the las number of the array.

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

myArray = [2, 4, 1, 7, 9, 6]
myNewArray = []

for (index, numbers) in enumerate(myArray):
if index >= len(myArray) - 3:
myNewArray.append(numbers)
print(myNewArray)


# Output the two middle elements in the array: [1, 7]
print()
print(a[2:4])
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[:5])
print(a[:6:2]) #This is includes all the negative numbers (wrap around) from -1, -2, -3, -4, -5 **** The third number shows how the steping should be.
print(a[:-1]) #It prints the entire list up to the last number, we're just using the wrap around system with negative numbers using the negative index.

# For string s...

s = "Hello, world!"

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

# Write a list comprehension to produce the array [1, 2, 3, 4, 5]
# Do a For in loop in one line and a regular For in loop.

y = []
y = [n for n in range(1, 6)] # Same line For in loop.
print ("This is the short version:", y)

print (y)
#Long version:
y1 = []
for num in range(1, 6):
y1.append(num)
print("This is the long version:", 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 = [n**3 for n in range(10)]
print("This is the short version:", y)

print(y)
#Long version:
y2 = []

for num1 in range(10):
y2.append(num1 ** 3)
print("This is my long version", y2)

# 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("This is the short version:", y)

print(y)
# The long version:
a1 = ["foo", "bar", "baz"]
y3 = []
for word in a1:
y3.append(word.upper())
print("This is the long version:", y3)

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

print("Please enter a number:")
x = input("Enter comma-separated numbers: ").split(',')
print(x)

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

print(y)
y = [num for num in x if int(num) %2 == 0]
#print(y)
35 changes: 34 additions & 1 deletion src/09_dictionaries.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
- name: a name string for this location
"""

import json

waypoints = [
{
"lat": 43,
Expand All @@ -36,12 +38,43 @@
# Add a new waypoint to the list
# YOUR CODE HERE


print("\n\n=========================================")
print("\n\nThis is how a json list is printed out on Python")
waypoints.append( {

"lat": 120,
"lon": -11,
"name": "A place"
})

print("\nThis is my waypoint:", waypoints)

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)

print("\n\nAnother way to add waypoint to the list: ====================")
anotherPlace = { "lat": 42, "lon": -120, "name": "a fourth place"}
waypoints.append(anotherPlace)
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
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: 7 additions & 0 deletions 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 isEven(n):
return n % 2 == 0

# Read a number from the keyboard
num = input("Enter a number: ")
Expand All @@ -10,3 +12,8 @@

# YOUR CODE HERE

if isEven(num):
print("Even!")
else:
print("Odd!")

Loading