This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Let’s start by creating a very simple mathematical function that we will call square. | |
# The square function will take one number as a parameter and return the result of squaring that number. | |
# Here is the black-box diagram with the Python code following. | |
def square(x): | |
y = x * x | |
return y | |
toSquare = 10 | |
result = square(toSquare) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 1)Write a function called int_return that takes an integer as input and returns the same integer. | |
# Answer: | |
def int_return(n): | |
return int(n) | |
print(int_return(2.5)) # This line is not needed | |
# 2) Write a function called add that takes any number as its input and returns that sum with 2 added. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Assessment: Dictionary Accumulation - course_2_assessment_3 | |
# 1) The dictionary Junior shows a schedule for a junior year semester. | |
# The key is the course name and the value is the number of credits. | |
# Find the total number of credits taken this semester and assign it to the variable credits. | |
# Do not hardcode this – use dictionary accumulation! | |
# Answer: | |
Junior = {'SI 206':4, 'SI 310':4, 'BL 300':3, 'TO 313':3, 'BCOM 350':1, 'MO 300':3} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 2) Create a tuple called practice that has four elements: ‘y’, ‘h’, ‘z’, and ‘x’. | |
# Answer: | |
practice = 'y', 'h', 'z', 'x' | |
print(practice) | |
# 3) Create a tuple named tup1 that has three elements: ‘a’, ‘b’, and ‘c’. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 1) With only one line of code, assign the variables water, fire, electric, and grass to the values | |
# “Squirtle”, “Charmander”, “Pikachu”, and “Bulbasaur” | |
water, fire, electric, grass = "Squirtle", "Charmander", "Pikachu", "Bulbasaur" | |
# 2) With only one line of code, assign four variables, v1, v2, v3, and v4, to the following four values: 1, 2, 3, 4. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 1) Create a tuple called olympics with four elements: “Beijing”, “London”, “Rio”, “Tokyo”. | |
# Amswer: | |
olympics = "Beijing", "London", "Rio", "Tokyo" | |
# 2) The list below, tuples_lst, is a list of tuples. | |
# Create a list of the second elements of each tuple and assign this list to the variable country. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Here is a new version of the summation program that uses a while statement. | |
def sumTo(aBound): | |
""" Return the sum of 1+2+3 ... n """ | |
theSum = 0 | |
aNumber = 1 | |
while aNumber <= aBound: | |
theSum = theSum + aNumber | |
aNumber = aNumber + 1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Python Functions, Files, and Dictionaries - Week 4 - Assessment: More about Iteration - course_2_assessment_6 | |
# 1) Write a function, sublist, that takes in a list of numbers as the parameter. | |
# In the function, use a while loop to return a sublist of the input list. | |
# The sublist should contain the same values of the original list up until it reaches the number 5 ( | |
# it should not contain the number 5). | |
# Answer: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 1) Create a function called mult that has two parameters, the first is required and should be an integer, | |
# the second is an optional parameter that can either be a number or a string but whose default is 6. | |
# The function should return the first parameter multiplied by the second. | |
# Answer: | |
mult = lambda a, b = 6: a * b | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 1) Sort the following string alphabetically, from z to a, and assign it to the variable sorted_letters. | |
# Answer: | |
letters = "alwnfiwaksuezlaeiajsdl" | |
sorted_letters = sorted(letters, reverse = True) | |
# 2) Sort the list below, animals, into alphabetical order, a-z. Save the new list as animals_sorted. |
OlderNewer