Skip to content

Instantly share code, notes, and snippets.

View IvanFrecia's full-sized avatar

Ivan Frecia IvanFrecia

View GitHub Profile
@IvanFrecia
IvanFrecia / Function_Basics.py
Last active April 29, 2021 22:37
Coursera Python 3 Programming Specialization - Course 2 - Week 3 - Function Basics - 12.5. Returning a value from a function
# 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)
@IvanFrecia
IvanFrecia / course_2_assessment_4.py
Created April 29, 2021 22:34
Coursera Python 3 Programming Specialization - Course 2 - Week 3 - Assessment: Functions - course_2_assessment_4
# 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.
@IvanFrecia
IvanFrecia / course_2_assessment_3.py
Created April 29, 2021 22:50
Coursera - Python 3 Programming Specialization - Functions Files and Dictionaries - Week2 - Assessment Dictionary Accumulation
# 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}
@IvanFrecia
IvanFrecia / 13_2_TuplePacking.py
Created April 29, 2021 23:19
Coursera - Python 3 Programming Specialization - Course 2 - 13.2 Tuple Packing
# 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’.
@IvanFrecia
IvanFrecia / 13_3_3_Excersises.py
Created April 30, 2021 00:49
13.3.3. The Pythonic Way to Enumerate Items in a Sequence
# 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.
@IvanFrecia
IvanFrecia / course_2_assessment_5.py
Created April 30, 2021 13:44
Coursera - Python 3 Programming Specialization - Functions Files and Dictionaries - Week3 - Assessment for Tuples lesson
# 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.
@IvanFrecia
IvanFrecia / 14_2_The-while-Statement.py
Created April 30, 2021 14:51
Coursera - Python 3 Programming Specialization - Course 2 - Week 4 - 14.2. The while Statement
# 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
@IvanFrecia
IvanFrecia / course_2_assessment_6.py
Created April 30, 2021 20:34
Python Functions, Files, and Dictionaries - Week 4 - Assessment: More about Iteration - course_2_assessment_6
# 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:
@IvanFrecia
IvanFrecia / course_2_assessment_7.py
Created May 1, 2021 00:08
Coursera - Python 3 Programming Specialization - Functions Files and Dictionaries - Week4 - Assessment: Advanced Functions - course_2_assessment_7
# 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
@IvanFrecia
IvanFrecia / course_2_assessment_8.py
Created May 3, 2021 14:18
Python Function, Files and Dictionaries - Week5 -course_2_assesment_8
# 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.