0% found this document useful (0 votes)
56 views6 pages

COS 102: Python Problem Solving Manual

Lab manual for Problem solving using python

Uploaded by

odeinanyanwu
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
56 views6 pages

COS 102: Python Problem Solving Manual

Lab manual for Problem solving using python

Uploaded by

odeinanyanwu
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

CLARETIAN UNIVERSITY OF NIGERIA

NEKEDE IMO STATE

SCHOOL OF SCIENCES AND COMPUTING


DEPARTMENTS
COMPUTER SCIENCE
SOFTWARE ENGINEERING
CYBERSECURITY

LABORATORY MANUAL

for

PROBLEM SOLVING WITH PYTHON


(COS 102)

For First Year Students


Semester II
********24/05/2024********
********************************************
LAYOUT
SECTION CONTENT PAGE

Introduction Course Overview 3

Section 1 Python Basics 4

Section 2 Flow Control 5

Section 3 List Data Structure 6

Page | 2
INTRODUCTION
Course Overview
This course shall introduce students to the fundamental programming and problem solving techniques
using Python, a high-level, versatile programming language. The course will cover basic programming
concepts such as variables, data types, data structures and algorithms, control structures, functions, and
file input/output. Students will learn how to design and implement algorithms, use data structures, and
write efficient code.

Learning Outcome

By the end of this course, students will be able to:


 Explain the fundamental concepts of programming such as variables, data structures and types,
control structures, functions, and file input/output.
 Design and implement algorithms using Python programming techniques.
 Use data structures such as lists, dictionaries, and sets to store and manipulate data.
 Write efficient code using Python libraries and modules.

Laboratory Experimental Work


Students are to write codes to:
 Implement tasks to practice their skills in using variables, control structures, functions, data
structures, file input/output, and object-oriented programming using Python 3.
 Solve problems and implement algorithms on various data types and structures
 solve computer problems using algorithms, flowcharts, pseudocode.

Laboratory Requirements
The following are required for a seamless laboratory session:
 A Desktop or Laptop Computer
 Integrated Development Environment (Visual Studio Code, Python IDLE, Sublime Text,
Atom, Jupyter Notebook, Jupyter Lab)

Page | 3
Section 1: Python Basics
1-1. [Link]: Explore the Python home page ([Link] to find topics that interest you.
As you become familiar with Python, different parts of the site will be more useful to you.

1-2. Hello World: Create a file titled hello_world.py. Open the file you just created and write proper
comments to support the answer to each of the questions below:

1. Which of the following are operators, and which are values?


*
'hello'
-88.8
-
/
+
5
2. Which of the following is a variable, and which is a string?
spam
'spam'
3. Name three data types.
4. What is an expression made up of? What do all expressions do?
5. Python basics introduced assignment statements, like spam = 10. What is the difference between an
expression and a statement?
6. What does the variable bacon contain after the following code runs?
bacon = 20
bacon + 1
7. What should the following two expressions evaluate to?
'spam' + 'spamspam'
'spam' * 3
8. Why is eggs a valid variable name while 100 is invalid?
9. What three functions can be used to get the integer, floating-point number, or string version of a
value?
10. Why does this expression cause an error? How can you fix it?
'I have eaten ' + 99 + ' burritos.'

Page | 4
Section 2: Flow Control
Create a file in your current directory titled flow_control.py
In this file attempt the following the preliminary exercises that tests your basic understanding of flow
control and write programs to address the problem statements that follows:

1. What are the two values of the Boolean data type? How do you write them?
2. What are the three Boolean operators?
3. Write out the truth tables of each Boolean operator (that is, every possible combination of Boolean
values for the operator and what they evaluate to).
4. What do the following expressions evaluate to?
(5 > 4) and (3 == 5)
not (5 > 4)
(5 > 4) or (3 == 5)
not ((5 > 4) or (3 == 5))
(True and True) and (True == False)
(not False) or (not True)

5. What are the six comparison operators?


6. What is the difference between the equal to operator and the assignment operator?
7. Explain what a condition is and where you would use one.
8. What keys can you press if your program is stuck in an infinite loop?
10. What is the difference between break and continue?

Problem Statement
1. Write a program that accepts daily temperatures and returns the average temperature in
degree Celsius and also returns the number of days above average temperature.
Program Requirements:
i. If the program is run, it should prompt the user to enter the number of days of measured temperature
and sequential input prompts to for user to enter these values
ii. Write a pseudocode to implement your solution algorithms
Step 1: Calculate average temperature
Step 2: Calculate the number of days above average temperature

Page | 5
Section 3: List Data Structure
Create a new file titled list_data_structures.py for this lab exercise.

3-1. Names: Store the names of a few of your friends in a list called names. Print each person’s name
by accessing each element in the list, one at a time.
3-2. Greetings: Start with the list you used in Exercise 3-1, but instead of just printing each person’s
name, print a message to them. The text of each message should be the same, but each message
should be personalized with the person’s name.
3-3. Your Own List: Think of your favorite mode of transportation, such as a motorcycle or a car, and
make a list that stores several examples. Use your list to print a series of statements about these items,
such as “I would like to own a Honda motorcycle.”
3-4. Guest List: If you could invite anyone, living or deceased, to dinner, who would you invite? Make
a list that includes at least three people you’d like to invite to dinner. Then use your list to print a
message to each person, inviting them to dinner.
3-5. Changing Guest List: You just heard that one of your guests can’t make the dinner, so you need
to send out a new set of invitations. You’ll have to think of someone else to invite.
Start with your program from Exercise 3-4. Add a print() call at the end of your program, stating the
name of the guest who can’t make it. Modify your list, replacing the name of the guest who can’t
make it with the name of the new person you are inviting. Print a second set of invitation messages,
one for each person who is still in your list.
3-6. More Guests: You just found a bigger dinner table, so now more space is available. Think of
three more guests to invite to dinner. Start with your program from Exercise 3-4 or 3-5. Add a print()
call to the end of your program, informing people that you found a bigger table.
Use insert() to add one new guest to the beginning of your list.
Use insert() to add one new guest to the middle of your list.
Use append() to add one new guest to the end of your list.
Print a new set of invitation messages, one for each person in your list.
3-7. Shrinking Guest List: You just found out that your new dinner table won’t arrive in time for
the dinner, and now you have space for only two guests.
Start with your program from Exercise 3-6. Add a new line that prints a message saying that you can
invite only two people for dinner.
Use pop() to remove guests from your list one at a time until only two names remain in your list.
Each time you pop a name from your list, print a message to that person letting them know you’re
sorry you can’t invite them to dinner.
Print a message to each of the two people still on your list, letting them know they’re still invited.
Use del to remove the last two names from your list, so you have an empty list. Print your list to
make sure you actually have an empty list at the end of your program.
3-8. Seeing the World: Think of at least five places in the world you’d like to visit. Store the
locations in a list. Make sure the list is not in alphabetical order.
Print your list in its original order. Don’t worry about printing the list neatly;
just print it as a raw Python list. Use sorted() to print your list in alphabetical order without
modifying the actual list. Show that your list is still in its original order by printing it.
Use sorted() to print your list in reverse-alphabetical order without changing the order of the original
list. Show that your list is still in its original order by printing it again. Use reverse() to change the
order of your list. Print the list to show that its order has changed. Use reverse() to change the order
of your list again. Print the list to show it’s back to its original order. Use sort() to change your list
so it’s stored in alphabetical order. Print the list to show that its order has been changed. Use sort() to
change your list so it’s stored in reverse-alphabetical order. Print the list to show that its order has
changed.
3-9. Dinner Guests: Working with one of the programs from Exercises 3-4 through 3-7, use len() to
print a message indicating the number of people you’re inviting to dinner.
Page | 6

Common questions

Powered by AI

List-based programming tasks are significant in enhancing students' practical skills because they offer direct experience with manipulating and operating on data collections, which is crucial in handling real-world data. Through exercises involving list operations like inserting, appending, modifying, and sorting, students learn how to efficiently manage data sets, optimize code performance, and apply these techniques to more complex data structure implementations .

Conditional expressions affect the flow of a Python program by dictating which set of statements get executed based on Boolean conditions. Using conditional statements like if, elif, and else, programmers can introduce decision-making processes, effectively enabling programs to respond differently to varying inputs and states, thus contributing to dynamic and context-sensitive code behavior .

Python basics such as assignment statements, expressions, and variables provide the foundation for programming as they enable the storage, manipulation, and management of data. Assignment statements allow for the definition and initialization of variables, expressions describe computations involving values and operators, and variables serve as identifiers for data storage. Together, they contribute to building blocks of writing functional programs .

Using data structures like lists, dictionaries, and sets in Python programming is advantageous because they allow for efficient data storage and manipulation. Lists provide ordered collections of items, dictionaries offer a flexible means of storing pairs of keys and values for quick reference, and sets facilitate operations on distinct elements. These structures enable programmers to handle complex data requirements effectively and optimize program performance .

The Python course emphasizes problem-solving by teaching students to design and implement algorithms using Python programming techniques. It incorporates exercises that require practicing the use of variables, control structures, functions, data structures, and file input/output, as well as solving problems using algorithms, flowcharts, and pseudocode. This approach fosters the development of practical skills necessary for tackling computer problems efficiently .

The fundamental concepts of programming introduced in a Python Programming course for first-year students include variables, data structures and types, control structures, functions, and file input/output. Students are expected to design and implement algorithms, utilize data structures such as lists, dictionaries, and sets, and write efficient code using Python libraries and modules .

Designing algorithms using Python can present challenges such as choosing the appropriate algorithmic approach, ensuring efficiency in time and space, and correctly implementing logic. Students can overcome these challenges by gaining a deep understanding of algorithmic concepts and flow control, practicing extensively with various types of problems, analyzing existing solutions, and learning iterative and testing techniques to refine their logic and improve problem-solving skills .

Flow control plays a critical role in programming as it determines the order in which individual statements, instructions, or function calls are executed within a script. In Python, flow control can be implemented using Boolean operators, conditions, loops, and control statements like if-else, while, and for loops. By leveraging these constructs, programmers can manage program execution paths effectively and handle complex logical conditions .

The pop() method is used to remove and return an element from a list at a specified position; by default, it removes and returns the last item. Its usage impacts program logic by facilitating the removal of elements as required by the program's objectives, such as managing dynamic datasets or in implementations where the deletion and subsequent processing of list items need to occur systematically .

The function sorted() in Python returns a new list containing all items from the original list in a sorted order without altering the original list. In contrast, sort() sorts the list in place and modifies the original list's order permanently. These differences are significant for scenarios where the preservation of the original data order is crucial versus when in-place modification based on sorting criteria is more efficient or desired .

You might also like