Computational Lab 1
Part A.1: Introduction to Python 3.7, continued
This portion of lab 1 will continue to introduce you to the Python programming language version 3.7. To begin this portion of the assignment, copy over a directory with starting scripts for this lab. You can do this by typing cp -r /home/fri/lab_files_2022/lab1_part_a . into the terminal. Complete all problems listed in the programming exercises below. Have your script print output for each questions where editing your file is required.
Part 1A: Loops
Part 1B: List
Part 1C: More Numpy tools
Part 1D: Debugging
Part A.1.A: Loops
Suggested Reading: Sections 7.3 and 7.4First, we will introduce loops. This allows our program to repeat tasks for a set number of times or until some criteria is met. Open the file loops.py.
Programming Exercises
- First go into the Your_script directory.
Here you will find a blank python script called your_script.py.
This will be the file that should contain your code for lab 1 part 1.
- Without using a for or while loop create a function called count_to_n that takes one variable, n.
The variable n can be equal to 1, 2, or 3.
Using a conditional statement, get your code to print all integers starting from 1 up to n.
Each integer should be printed on it's own line.
Check to make sure your function works properly for the integers 1, 2, and 3.
- Next, create two functions called count_to_n_for and count_to_n_while that takes one variable, n.
This variable can be equal to any integer.
Using a for and while loop respectively, get these functions to print all integers starting from 1 up to n.
Each integer should be printed on it's own line.
Check to make sure both functions work properly for integers 2 and 10.
- Create a function that computes the sum of all numbers divisible by 2 and 7 that are less than 200 using a loop and a conditional statement.
Print the sum you compute. i
Note: This question maybe interpreted as summing all numbers divisible by 14 or summing numbers divisible by 2 and numbers divisible by 7. Either interpretation will be correct.