Conditionals, Loops, and Lists
Overview and Objectives
In this lab, you will practice using boolean values, conditional statements (if/elif/else
), loops (for
and while
), and list operations. These exercises will help you understand how to make decisions in code, repeat tasks, and manage collections of data. By the end of the lab, you will be able to:
- Write programs that use conditional execution to make decisions.
- Implement loops to repeat tasks or iterate over data.
- Utilize logical operators (
and
,or
,not
) and (optionally) bitwise operators. - Create and manipulate Python lists, including sorting and using basic list methods.
- Understand and implement a simple bubble sort algorithm (conceptual level).
- Use lists in a practical context (e.g., storing multiple inputs or records).
Instructions and Tasks
Part 1: Conditional Execution
Write a script grade_checker.py
that:
- Asks the user to input a numeric grade (0-100).
- Uses an
if-elif-else
structure to convert the numeric grade to a letter grade based on a typical scale:- 90-100: A
- 80-89: B
- 70-79: C
- 60-69: D
- 0-59: F
- Prints the letter grade. For example:
"Your grade is: B"
- Includes a final message using a conditional expression (for example, if the grade is passing (A-C) congratulate the user, if it is a D or F, encourage them to try again).
Make sure to handle edge cases (like inputting 100 or 0) so that they fall into the correct letter grade. You can assume the user enters a valid number for this task.
Write a script even_sum.py
that:
- Uses a
for
loop to iterate through the numbers 1 to 50. - Calculates the sum of all even numbers in that range.
- Prints the result as:
"The sum of even numbers from 1 to 50 is X."
(where X is the calculated sum).
Next, modify the script to perform the same task using a while
loop instead of a for
loop. You can include this in the same file or as a separate block of code within the file, and print the result again to verify it matches. Comment on whether both loop versions produce the same result and which approach you find clearer.
Part 2: Working with Lists
Write a script list_operations.py
that:
- Creates a list of at least 5 integers (you can choose them arbitrarily or ask the user to input numbers to populate the list).
- Prints the original list.
- Uses the built-in
sorted()
function to print a sorted version of the list without modifying the original list. - Uses the list’s
.sort()
method to sort the list in place, then prints the list to show it is now sorted. - Adds a new element to the list (append an integer), then prints the updated list.
- Removes an element from the list (you can remove by value or index), then prints the list again.
- Uses the
reverse()
method to reverse the list, then prints the reversed list.
Each step should be clearly separated and labeled in the output, for example by printing a message like "Original list:"
, "Sorted list:"
, etc.
Implement a simple bubble sort in a script bubble_sort_demo.py
:
- Use a list of unsorted integers (you can use a fixed example list like
[64, 25, 12, 22, 11]
or a list of random numbers). - Implement the bubble sort algorithm using nested loops:
- Loop through the list elements, and for each element, loop through the list again comparing adjacent pairs.
- Swap elements if they are in the wrong order.
- Continue until the list is sorted.
- Print the list at each pass of the outer loop to show the progress of the sorting.
- Finally, print the sorted list.
This exercise will demonstrate understanding of loops and list indexing.
In practice, you would use Python’s built-in sorting, but implementing bubble sort helps to better understand loops.
Part 3: Logic and Bitwise Operations
Create a short script logic_bits.py
that:
- Demonstrates the use of logical operators: for example, ask the user for two boolean inputs (True/False or 1/0) and show the results of
and
,or
, andnot
on those inputs. - Demonstrates bitwise operators (
&
,|
,^
,~
,<<
,>>
) on two small integers (for example, 5 and 3). Print the results in binary form usingbin()
to show what is happening at the bit level.
This task is for exploration and will not be heavily graded; it is to encourage you to play with these operators and see their effects.
Submission
Submit each script (grade_checker.py
, even_sum.py
, list_operations.py
, bubble_sort_demo.py
, and optionally logic_bits.py
) via a link to a single GitHub repository containing all the files. Provide outputs for each where applicable (especially for the bubble sort, include the printed progress of sorting). Ensure your code is commented to explain key parts of your logic, especially for the bubble sort algorithm.
Grading Rubric
The total number of points for this lab is 50 points (plus up to 5 bonus points for logic_bits.py
).
File | Criteria | Points | Description |
---|---|---|---|
grade_checker.py | Correct Conditional Logic | 8 | The if/elif/else structure correctly maps numeric grades to letter grades, including handling boundary values (0, 60, 90, 100). |
grade_checker.py | Output and Messaging | 3 | The program outputs the letter grade and an appropriate message (congratulations or encouragement) based on the result. |
grade_checker.py | Code Style | 2 | Code is well-formatted and includes at least one comment explaining the grading logic. |
even_sum.py | Correct Calculation | 5 | The program accurately computes the sum of even numbers from 1 to 50. |
even_sum.py | Use of Loops | 2 | Both a for-loop and a while-loop version are implemented, and they produce the same correct result. |
even_sum.py | Explanation | 3 | The student provides a brief comment or note on which loop was easier or explains that both loop implementations produce the same result. |
list_operations.py | List Manipulation | 8 | Successfully demonstrates creating a list and performing all required operations (printing original, sorted version using both temporary and in-place methods, appending, removing, and reversing) with correct outcomes. |
list_operations.py | Output Clarity | 3 | Each stage of list operations is clearly indicated in the output with descriptive text. |
list_operations.py | Code Clarity | 2 | Code is well-organized with comments delineating each step of the list operations. |
bubble_sort_demo.py | Implementation | 7 | Bubble sort is correctly implemented using nested loops. The algorithm eventually sorts the list correctly. |
bubble_sort_demo.py | Progress Output | 2 | The script prints the list (or relevant details) in each outer loop pass, showing the sorting progress. |
bubble_sort_demo.py | Final Output | 2 | The final sorted list is printed and is correct. |
bubble_sort_demo.py | Code Understanding | 3 | Comments in the code explain how the bubble sort works (e.g., what each loop is doing and when swaps occur). |
logic_bits.py [Bonus] | Logical Operators | 3 | Correctly demonstrates and , or , and not with user input or predefined booleans, and prints understandable results. |
logic_bits.py [Bonus] | Bitwise Operators | 2 | Shows use of at least two bitwise operators and prints results in binary form (using, for example, bin() to display the output). |
Points for each task will be awarded based on the criteria. Partial credit is possible: for example, if the bubble sort algorithm is attempted but has a minor mistake (not fully sorting, or an off-by-one error in loop ranges), some points can still be earned for effort and partial correctness. However, a fully working solution with well-documented code will earn full points for that section. The optional task can only add points (extra credit) and is not required for a full score of 50.