Week 2
Refresher on important 131 concepts.
For all of the following, write both the function and some code that calls and tests the function.
Complete the following without using ANY outside aid. If necessary, refer only to the python help() command or the official documentation at https://python.org.
Use your preferred programming environment (IDLE, Pycharm, Visual Studio Code, etc.)
- Write a function with two parameters: a string and an integer
n. Using a for loop, print the stringntimes. - Write a function with one parameter: a list. Print each element of the list using a for loop.
- Write a function with two parameters: a list and a
target. Using a for loop, count the number of timestargetappears in the list and print the total. - Write a function with two parameters: a list and a
target. Using a while or for loop, print the first index in the list wheretargetappears. Printnot presentif it does not exist in the list. - Write a function with one parameter: a list of integers. Using a loop, count the number of even and odd integers in the list. Print the total count of evens and odds.
Solutions
There are a few ways to solve each problem. Here are a few: week2-soln.py
Submission
Submit your .py file to Canvas for a check. You are not graded on completeness or correctness – this is for learning and feedback.
Key Skills
- Function definition with parameters.
- Function calling.
- Selection using
if-else. - Iteration using a
forloop and working with the listindex. - Combining iteration and selection.