Lab 2 Manual(1)

Download as pdf or txt
Download as pdf or txt
You are on page 1of 9

Lab 2 - Getting Started with Python II

Chapter 1. Introduction to Computers, Programs, and Python Lab


Time 80 Minutes 2
Objectives
 To familiarize with the programming environment.
 To write simple programs.

Current Lab Learning Outcomes (LLO)


By completion of the lab the students should be able to
 Use the programming environment.
 Use print function.
 Write simple programs that include simple calculations.
 Document the code.

Lab Requirements
 PyCharm (IDE).

Version 2.0 – Last Update 9/9/2020


Practice Activities with Lab Instructor (25 minutes)

Problem 1 Programming Exercises (1.5)

Write a program that displays the result of the following equation:

9.5 × 4.5 − 2.5 × 3


45.5 − 3.5
0.8392857142857143

Solution

Phase 1: Problem-Solving Phase:


1- Decompose the equation (Step 1):
o From:

9.5 × 4.5 − 2.5 × 3


45.5 − 3.5
o To:

(9.5 ∗ 4.5) − (2.5 ∗ 3)


45.5 − 3.5
o Note: in Python, ( / ) means division sign and ( * ) multiplication sign.
2- Decompose the equation (Step 2):
o From:

(9.5 ∗ 4.5) − (2.5 ∗ 3)


45.5 − 3.5
o To:

((9.5 ∗ 4.5) − (2.5 ∗ 3)) / (45.5 − 3.5)


3- Print the decomposed equation.
o Note: do not treat it as a string (do not enclose it with quotation marks)
o print( ((9.5 * 4.5) - (2.5 * 3)) / (45.5 - 3.5) )

2
Phase 2: Implementation Phase:
1. Open PyCharm.
2. Then, click on “ New Project”.
3. Then, name it “Lab 2”.
4. Then, the new project is created and opened. After that, you have to create a new
Python file inside the project to write the code on it.
5. Select the project name on the left menu, right click on it and select “New” → “Python
File”.
6. Then, name the new file “activity_1”, and click on “Python file”.
7. Now, the new file is created and opened. Write the code in it:
activity_1.py
1 '''
2 ( (9.5 * 4.5) - (2.5 * 3) ) Numerator
3 / Divided by
4 (45.5 - 3.5) Denominator
5 '''
6 print( ((9.5 * 4.5) - (2.5 * 3)) / (45.5 - 3.5) )

8. To run the file, right click on any area of the editor and click on (Run 'activity_1'), which
is the name of the file.

3
9. After that, PyCharm is going to run the file using the Python interpreter, and then display
the output of the file to you.

Problem 2 Programming Exercises (1.7)

π can be computed using the following formula:


1 1 1 1 1
𝜋 = 4 × (1 − + − + − + ⋯)
3 5 7 9 11
1 1 1 1 1
Write a program that displays the result of 𝜋 = 4 × (1 − + − + − )
3 5 7 9 11

4
1 1 1 1 1 1 1
and 𝜋 = 4 × (1 − + − + − + − )
3 5 7 9 11 13 15

2.9760461760461765
3.017071817071818

Solution

Phase 1: Problem-Solving Phase:


1- Decompose the first equation:
a. From:
1 1 1 1 1
4 × (1 − + − + − )
3 5 7 9 11
b. To:

4 ∗ (1 − (1/3) + (1/5) − (1/7) + (1/9) − (1/11))


2- Decompose the second equation:
a. From:
1 1 1 1 1 1 1
4 × (1 − + − + − + − )
3 5 7 9 11 13 15
b. To:

4 ∗ (1 − (1/3) + (1/5) − (1/7) + (1/9) − (1/11) + (1/13) − (1/15))


3- Print the first decomposed equation.
a. print(4 * (1-(1/3)+(1/5)-(1/7)+(1/9)-(1/11)))
4- Print the second decomposed equation.
a. print(4 * (1-(1/3)+(1/5)-(1/7)+(1/9)-(1/11)+(1/13)-(1/15)))

Phase 2: Implementation Phase:


1. Open the project “Lab 2” if it was not opened.
2. Create a new file and name it “activity_2.py”.

5
3. Write the following code in the file:
activity_2.py
1 # Displays the result of the first equation
2 print(4 * (1-(1/3)+(1/5)-(1/7)+(1/9)-(1/11)))
3
4 # Displays the result of the second equation
5 print(4 * (1-(1/3)+(1/5)-(1/7)+(1/9)-(1/11)+(1/13)-(1/15)))

4. Run the code:


6
Individual Activities (50 minutes)

Problem 3 Programming Exercises (1.6)

Write a program that displays the result of 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9.

45

Problem 4 Programming Exercises (1.8)

Write a program that displays the area and perimeter of a circle that has a radius of 5.5
using the following formulas (Note: π = 3.14159):
𝑎𝑟𝑒𝑎 = 𝑟𝑎𝑑𝑖𝑢𝑠 × 𝑟𝑎𝑑𝑖𝑢𝑠 × 𝜋
𝑝𝑒𝑟𝑖𝑚𝑒𝑡𝑒𝑟 = 2 × 𝑟𝑎𝑑𝑖𝑢𝑠 × 𝜋
7
95.0330975
34.55749

8
Extra Exercises (Homework)

From the Textbook


 Programming Exercises:
o 1.10
o 1.11

From MyProgrammingLab (https://pearson.turingscraft.com)


 1.5
o 60155
o 60156
 1.8
o 60169
o 60170
o 60183

Upload Your Solutions


Upload your solutions of the lab activities to Blackboard.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy