Skip to content

CSPT13-Karthik-Intro-to-Python1-Part 1 and 2 #1145

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 2 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
2 changes: 1 addition & 1 deletion src/01_bignum.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Print out 2 to the 65536 power
# (try doing the same thing in the JS console and see what it outputs)

# YOUR CODE HERE
print (f'2 to the 65536 power is:{2**65536}')
6 changes: 2 additions & 4 deletions src/02_datatypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,8 @@
y = "7"

# 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
print (str(x)+y)
12 changes: 6 additions & 6 deletions src/03_modules.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,23 @@
# 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'command line arguments in sys.argv:{sys.argv}')

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

# Print out the version of Python you're using:
# YOUR CODE HERE
print (f'Python version:{sys.version[0:5]}')


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 ID:{os.getpid()}')

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

# Print out your machine's login name
# YOUR CODE HERE
print(f'Machine login name:{os.getlogin()}')
5 changes: 4 additions & 1 deletion src/04_printing.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@
# 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,y,z))

# Use the 'format' string method to print the same thing
print('x is {}, y is {}, z is "{}"'.format(x, y, z))

# Finally, print the same thing using an f-string
# Finally, print the same thing using an f-string
print(f'x is {x}, y is {y}, z is "{z}"')
12 changes: 6 additions & 6 deletions src/05_lists.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,23 @@
# For the following, DO NOT USE AN ASSIGNMENT (=).

# 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
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(f'Length of list x is:{len(x)}')

# Print all the values in x multiplied by 1000
# YOUR CODE HERE
print(f'all the values in x multiplied by 1000 is:{[element * 1000 for element in x]}')
9 changes: 6 additions & 3 deletions src/06_tuples.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,14 @@ def dist(a, b):

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

# YOUR CODE HERE
def print_tuple(a):
for element in a:
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?
print_tuple(u)
u = (1,) # What needs to be added to make this work?
#print(len(u))
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])
10 changes: 4 additions & 6 deletions src/08_comprehensions.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,13 @@

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

y = []

y = list(map(lambda x: x+1, 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 = list(map(lambda x: x**3, range(10)))

print(y)

Expand All @@ -26,16 +25,15 @@

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

y = []
y = list(map(lambda x:x.upper(),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 = [b for a,b in enumerate(x) if b%2==0]

print(y)
12 changes: 9 additions & 3 deletions src/09_dictionaries.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,20 @@
]

# Add a new waypoint to the list
# YOUR CODE HERE
b = {"lat": 45, "lon": -125, "name": "fourth place"}
waypoints.append(b)



# 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
d1 = {"lat": 43, "lon": -130, "name": "not a real place"}
waypoints[0].update(d1)
#print(waypoints[0].values())

# Write a loop that prints out all the field values for all the waypoints
# YOUR CODE HERE
for items in waypoints:
print (items.values())
11 changes: 9 additions & 2 deletions src/10_functions.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
# Write a function is_even that will return true if the passed-in number is even.

# YOUR CODE HERE
def is_even(a):
if a%2 == 0:
return True
else:
return False

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

20 changes: 15 additions & 5 deletions src/11_args.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,17 @@
# Write a function f1 that takes two integer positional arguments and returns
# 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))

# Write a function f2 that takes any number of integer arguments and returns the
# sum.
# Note: Google for "python arbitrary arguments" and look for "*args"

# YOUR CODE HERE
def f2(*args):
return sum(args)

print(f2(1)) # Should print 1
print(f2(1, 3)) # Should print 4
Expand All @@ -22,14 +24,15 @@
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.

# YOUR CODE HERE
def f3(x,y=1):
return x+y

print(f3(1, 2)) # Should print 3
print(f3(8)) # Should print 9
Expand All @@ -43,7 +46,10 @@
#
# Note: Google "python keyword arguments".

# YOUR CODE HERE
def f4(**kargs):
for key, value in kargs.items():
print ("key: %s, value: %s" %(key,value))
# print(f'key: {kargs["key"]}, value: {kargs["value"]}')

# Should print
# key: a, value: 12
Expand All @@ -61,5 +67,9 @@
"hp": 3
}

def f4(x):
for key, value in x.items():
print ("key: %s, value: %s" %(key,value))

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

def change_x():
global x
x = 99

change_x()
Expand All @@ -14,11 +15,12 @@ def change_x():


# This nested function has a similar problem.

y =120
def outer():
y = 120
global y

def inner():
global y
y = 999

inner()
Expand Down
8 changes: 6 additions & 2 deletions src/13_file_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,15 @@
# Print all the contents of the file, then close the file
# Note: pay close attention to your current directory when trying to open "foo.txt"

# 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
f1 = open('bar.txt','w')
f1.write("File was new stuff in it")
f1.close()
44 changes: 14 additions & 30 deletions src/14_cal.py
Original file line number Diff line number Diff line change
@@ -1,32 +1,16 @@
"""
The Python standard library's 'calendar' module allows you to
render a calendar to your terminal.
https://docs.python.org/3.6/library/calendar.html

Write a program that accepts user input of the form
`14_cal.py [month] [year]`
and does the following:
- If the user doesn't specify any input, your program should
print the calendar for the current month. The 'datetime'
module may be helpful for this.
- If the user specifies one argument, assume they passed in a
month and render the calendar for that month of the current year.
- If the user specifies two arguments, assume they passed in
both the month and the year. Render the calendar for that
month and year.
- Otherwise, print a usage statement to the terminal indicating
the format that your program expects arguments to be given.
Then exit the program.

Note: the user should provide argument input (in the initial call to run the file) and not
prompted input. Also, the brackets around year are to denote that the argument is
optional, as this is a common convention in documentation.

This would mean that from the command line you would call `python3 14_cal.py 4 2015` to
print out a calendar for April in 2015, but if you omit either the year or both values,
it should use today’s date to get the month and year.
"""

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

#print (type(sys.argv[2]))
today = datetime.today()
cal = calendar.TextCalendar()
#print (sys.argv)
if (len(sys.argv)==1):
print(cal.prmonth(today.year,today.month))
elif (len(sys.argv)==2):
print(cal.prmonth(today.year,int(sys.argv[1])))
elif (len(sys.argv)==3):
print(cal.prmonth(int(sys.argv[2]),int(sys.argv[1])))
else:
print("Error! Usage: Python3 14_cal.py <month> <year>")
Loading