These exercises were designed to provide additional coding practice and allow learners to prepare for a proctored practical exam where they are asked to write code to solve simple problems.
Learners will copy/paste these exercises into a .py file and write answers below each commented instruction.
Objective 1
The learner integrates Python elements including data types, constants, variables, operators, and expressions to create programming solutions.
# Task 1
# declare 3 variables with one assignment statement and assign each one an integer value
# Task 2
# convert each of your previous variables to float objects
# Task 3
# convert each of your previous variables to string objects
# Task 4
# Print the result of dividing 783.56 by 123.2 and ensure that the answer results in an integer
# expected outcome: 6
# Task 5
# Determine if 2019 is a leap year and print the result.
# expected outcome: 3
# Task 6
# Print the calculated length of myFirstString.
# expected outcome: 35
myFirstString = ‘I love working with Python so much!’
# Task 7
# Create a string value and print it with the first letter of each word capitalized using a Python method.
# Task 8
# Create a string value and determine if the string consists of only lowercase characters. Print the results.
# expected outcome: True or False
# Task 9
# Use the given variable to construct a python statement that counts how many times the word pizza is used. Print the final count.
# expected outcome: 3
commercial = ‘In the Little Ceasars pizza commercial the character says, “pizza, pizza”!’
# Task 10
# Use the given username and phone to create a message that lets the user know that you will be calling at a specified number for your appointment. Use the format method to insert data into the printed message.
# expected outcome: Hi, Allen. I will call you at 888-555-0011 for our appointment.
username = ‘Allen’
phone = 8885550011
Objective 2
The learner constructs functions and control structures to interact with data structures and direct program flow.
# Task 1
# create a function that receives two integers as input, adds them, and returns the sum
# run your function and print the result with integers 7 and 9
# expected outcome: 16
# run your function and print the result with integers 20 and 49
# expected outcome: 69
# Task 2
# Run the same function above with integers 2 and 8, and save the output to a new variable called myNewSum. Print myNewSum.
# expected outcome: 10
# Task 3
# You are provided a student’s score on the recent exam. Create a function that will print a reply based on the score. Students who score 90 points or more receive an A and pass the course. Students receiving 80 points or more receive a B and pass the course. Students receiving 79 points or less do not pass and need to retake the exam. Students receiving a score of 0 have not attempted the exam and need instructions to schedule.
print(grade_report(90)) # expected outcome: You received an A and have passed the course.
print(grade_report(70)) # expected outcome: You have not passed the course. Please retake the exam.
# Task 4
# use the range method to print out numbers 6-12
# Task 5
# create a list containing the names Dana, Cemal, Jessica, Mike, and Dana
# Task 6
# Print the length of the list.
# expected outcome: 5
# Task 7
# Check to see if David is a part of the list. If not in the list, add her and print the list.
# expected outcome: [‘Dana’, ‘Cemal’, ‘Jessica’, ‘Mike’, ‘Dana’, ‘David’]
# Task 8
# Print a single string containing all of the names separated by commas
# expected outcome: Dana, Cemal, Jessica, Mike, Dana, David
# Task 9
# Print only the names Dana and Mike from myNames. Note the date type below.
# expected outcome: [“Mike”,”Dana”]
# Task 10
# ensure that each name is only listed once and print the list of unique values
# expected outcome: [‘Dana’, ‘Cemal’, ‘Jessica’, ‘Mike’, ‘David’] *note: order of items in the list may vary
# Task 11
# create an individual message for each unique name and welcome them to WGU
# expected outcome: Welcome to WGU, Dana
# Welcome to WGU, Jessica
# Welcome to WGU, Mike
# Welcome to WGU, David
# Welcome to WGU, Cemal
# Task 12
# Given the following dictionary of employees and salaries, create a personalized salary message letting each employee know they have been given a 2% raise and the new total of their salary.
# expected outcome:
# John, your current salary is 54000.00. You received a 2% raise. This makes your new salary 55080.0
# Judy, your current salary is 71000.00. You received a 2% raise. This makes your new salary 72420.0
# Albert, your current salary is 38000.00. You received a 2% raise. # This makes your new salary 38760.0
# Alfonzo, your current salary is 42000.00. You received a 2% raise. This makes your new salary 42840.0
employeeDatabase = {
‘John’: 54000.00,
‘Judy’: 71000.00,
‘Albert’: 38000.00,
‘Alfonzo’: 42000.00
}
# Task 13
# starting with the year 2000, create a list containing 5 leap years. When the list is complete, print the full list with a message
# expected outcome: These are the leap years: [2000, 2004, 2008, 2012, 2016]
# Task 14
# A nurse is monitoring a patient’s rising temperature. The temp is rising in increments of .5 degrees continually. The nurse needs to be shown a message when the temp reaches 104 and the monitoring should end at that time.
# expected outcome: The temp has reached 104.0
# Task 15a
# create a tuple to store the WGU phone number 877-435-7948. Store the phone number as three groups of numbers without the hyphens.
# Task 15b
# use the tuple to print the last four digits of the phone number
# expected outcome: 7948
# Task 15c
# use the tuple to print the entire phone number with the message to Call WGU now
# expected outcome: Call WGU now at 877-435-7948
# Task 16
#Finish the function to take as input a list of fruits and return a string value containing the first two fruits from the list
def fruitFunction(fruits):
print(fruitFunction([‘banana’,’apple’,’orange’,’grapes’,’pineapple’])) #expected outcome: banana apple
print(fruitFunction([‘mango’,’avocado’,’pear’])) #expected outcome: mango avocado
# Task 17
#Finish the function to take as input a list of fruits and return a string value letting us know if the avocado is in the list or not. If so, state that the avocado is in the list. If not, state avocado not found.
def fruitFunction2(fruits):
print(fruitFunction2([‘pear’,’apple’,’lemon’,’grapes’,’pineapple’])) #expected outcome: avocado not found
print(fruitFunction2([‘lime’,’avocado’,’pear’])) #expected outcome: avocado is in the list
# Task 18
#Finish the function that takes as input a list of foods and returns a count of the number of times pizza is included in the list of favorite foods.
def favFoods(x):
print(favFoods([‘apple’,’banana’,’pizza’,’Pizza’,’ice cream’,’cupcakes’])) #expected output: 2
print(favFoods([‘almonds’,’spaghetti’,’pizza’,’kombucha’,’Pizza’,’pizza’])) #expected output: 3
# Task 19
#Completed the function that takes as input a string value of names and returns each name as an individual item in a list.
def makeList(names):
print(makeList(‘Jessica John Paul Grace Ginger Billy Arlene’)) #expected output: [‘Jessica’, ‘John’, ‘Paul’, ‘Grace’, ‘Ginger’, ‘Billy’, ‘Arlene’]
print(makeList(‘David Cemal Dana Rodger Jerry Jessica Mike’)) #expected output: [‘David’, ‘Cemal’, ‘Dana’, ‘Rodger’, ‘Jerry’, ‘Jessica’, ‘Mike’]
# Task 20
#Complete the function that takes one argument as a list of expenses and returns the total cost of all purchases.
def costCount(purchases):
print(costCount([39.90, 40.21, 8.73, 9.57])) #expected output: 98.41
print(costCount([139.90, 10.11, 1.53, 3.57])) #expected output: 155.10999999999999
Objective 3
The learner writes code in the Python environment, incorporating libraries to support data analytics tasks including data collection, manipulation, and storage
# —- Math Module — ##
import math
# Task 1
# use the math module to determine the factorial of the number 7 and print the result
# expected outcome: 5040
# Task 2
# use the math module to determine the square root of the number 27 and print the result
# expected outcome: 5.196152422706632
# Task 3
# use the math module to determine the largest integer less than or equal to 15.87 and print the result
# expected outcome: 15
# Task 4
# use the math module to determine the smallest integer integer greater than or equal to 15.87 and print the result
# expected outcome: 16
# Task 5
# use the math module to determine e to the power of 4
# expected outcome: 54.598150033144236
# Task 6
# complete the function that determines the nearest whole number for x and prints the result
def nearest(x):
nearest(15.87) #expected result: 16
nearest(7.21) #expected result: 7
#Task 7
# complete the function that determines the square root of x, rounded to the nearest whole number.
def square(x):
square(38) #expected result: 6
square(58) #expected result: 8
## —- Random Module — ##
import random
# Task 1
# use the random module to print a random integer between 2 and 20, exclusive
# Task 2
# use the random module to print a random number from the range 1 to 100, inclusive
# Task 3
# use the random module to return a random floating-point number
# Task 4
# Create a list of 6 words. Then use the random module to return a random element from that list.
## —- OS Module — ##
import os
# Task 1
# use the os module to create a hard link to C://myfile.txt at C://myPython/myfile.txt
# Task 2
# use the os module to delete the file C://myfile.txt
# Task 3
# use the os module to return the current working directory
# Task 4
# use the os module to change the root directory to C://home/
## —- Datetime Module — ##
import datetime
# Task 1
# use the datetime module to print the current year
# Task 2
# use the datetime module to print the current month
# Task 3
# use the datetime module to print the current day