Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Lab 4

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 11

CS 111- Lab Manual

Introduction to Computing and Programming


Date: 11/5/23

Department of Forensic Computing and Cyber Security


College of Computer Science and Information Technology

Lab 4 Part1

Objectives
1. Understand the basics of programming computers using Python
2. Learn elementary programming techniques to solve problems
3. Learn fundamental programming techniques, such as the use of the variables, operators,
expressions, and input and output.

Lab Learning Outcomes (LLO)


The students should be able to
1. Independently install Python and explore some basic functionality of python
2. Analyze a problem and design an algorithm to solve it
3. Use Python's fundamental concepts, such as conditional structures, looping, functions,
and program logic, to solve real-world problems

Requirements
 PC
 Python programming environment (http://www.python.org)
 PCAP | Programming Essentials in Python

Description

1-Install Python step by step

AIM:
 Install Python

Description:

 Open a web browser and enter the following URL: https://www.python.org/


 Click on the Download tab
CS 111- Lab Manual
Introduction to Computing and Programming
Date: 11/5/23

 Select Windows from the Options (If you are using windows) or whatever operating
system you are using.

 Then click on Save File when the pop-up box appears.


CS 111- Lab Manual
Introduction to Computing and Programming
Date: 11/5/23

 After the file downloads, install it and that’s it!


 When the Shell is open, please enter the following code print ("Welcome to CS111") and
the result should be like the image below

2- Explore some basic functionality of python

AIM:
Explore some basic functionality of python

Introduction
We talked about the fact that a computer is, among other things, a tool to perform high
speed arithmetic and logic operations. In order to learn how to write programs in Python, we
need to explore the basic building block instructions for these operations, and how Python
requires that we configure these operations (syntax).
During this lab you will complete a set of activities based on the material in chapter 1 in
your book. You were supposed to have read chapter 1 already, but you probably didn't have the
CS 111- Lab Manual
Introduction to Computing and Programming
Date: 11/5/23
ability to actually DO what the book asked. In this lab you will actually DO activities like those
listed in chapter 2.
________________________________________

Part A: Using IDLE as a Tool


During this course you will use IDLE (Integrated DeveLopment Environment) as a tool
to write and explore Python code. IDLE is a free IDE (Integrated Development Environment) of
the Python programming language. IDLE is not python -- instead, it is just one way to create and
run python programs (like using Microsoft Word is just one way to create a document file).
While you will not explore ALL of IDLE during today's lab, we want to get started so you have a
little bit of a feel for how things work.
1. Assuming that you are sitting at a Windows computer, launch IDLE by selecting
"Start | All Programs | Programming | Python 3.4 | IDLE (Python GUI)"
2. When IDLE starts up it should look like this:

The good news is that it will look like this whether you are in the labs on a Windows
machine or at home on your Windows, Mac, or Linux based machine. Please remember that
Python is free to download and you SHOULD install version 3.4 on your computer at home if
you have the option! (Download from www.python.org)

This is Python's interactive mode window (often called "the shell"). In this mode of
working with Python you can type in almost ANY valid Python statement at the prompt (the
CS 111- Lab Manual
Introduction to Computing and Programming
Date: 11/5/23
">>>"). When you do so, and then hit the Enter key, you send that command off to the Python
interpreter to be converted to machine language and then executed (assuming that it had proper
syntax and the semantics were meaningful to the interpreter).

________________________________________

Part B : Python Arithmetic


So we have talked about the fact that Python is very good at high speed arithmetic and
logic operations. Let's look at how python works with numbers.
[Q1] Enter each of the following mathematical statements at the command prompt
and press enter. Record what "result" the computer returns in response to your command
Arithmetic Expression Results via the command prompt
15 + 3
15 - 3
15 * 3
15 / 3

The results here shouldn't surprise you once you recognize that the asterisk (*) and the
forward slash (/) are the operators that python uses for multiplication and division.
Unlike "high school mathematics" - where there are four binary operators (addition,
subtraction, multiplication, and division) - python regularly uses seven binary operators. These
additional operators include the **, // and % symbols.
[Q2] Enter each of the following mathematical statements at the interactions pane
prompt and press enter. Record what "action" the computer takes in response to your command
Arithmetic Expression Results via the command prompt
2 ** 1
2 ** 2
2 ** 3
2 ** 4
3 ** 1
3 ** 2
3 ** 3
3 ** 4

[Q3] Given what you observed in the previous step, write a short explanation of how the
** operator works in Python.
CS 111- Lab Manual
Introduction to Computing and Programming
Date: 11/5/23
[Q4] Enter each of the following mathematical statements at the interactions pane
prompt and press enter. Record what "action" the computer takes in response to your
command
Arithmetic Results via the command Arithmetic Results via the command Arithmetic
Results via the command prompt
Expression prompt Expression prompt Expression
8/4 8 // 4 8%4
9/4 9 // 4 9%4
10 / 4 10 // 4 10 % 4
11 / 4 11 // 4 11 % 4
12 / 4 12 // 4 12 % 4
13 / 4 13 // 4 13 % 4

[Q5] Given what you observed in the previous step, write a short explanation of how
the /, //, and % operators work in Python.
For the most part, mathematics in python works the way that it worked in your high
school algebra class. That is, the order of operator precedence in Python (from highest to lowest)
is:
• Operations in parentheses
• Exponentiation (**)
• Unary negation (-) and positive (+)
• Multiplication (*), division (/), integer division (//) and remainder (%)
• Binary Addition (+) and subtraction (-)
• Assignment operator ( = )

[Q6] Given this knowledge, predict what will happen when you invoke the following
statements. After making your prediction, enter the statement at the interactions prompt and
check if you were correct. If you were incorrect, determine why.
CS 111- Lab Manual
Introduction to Computing and Programming
Date: 11/5/23

________________________________________

Activity C : Print Statements and Strings


Strings
A String is a sequence of characters (letters, numbers, etc). Print statements are intended
to display these Strings.

[Q1] Enter the following statements into the interactions pane and observe what happens
(note, one of these will cause an error).
Print Expression Results via the command prompt
print("Computer Science")
print("Computer" + "Science")
print("Computer" , "Science")
print("Computer , Science")
print(5+3)
print("5 + 3")
print("5" + "3")
print("5" , "3")
print("5 + 3 = " , 5+3)
print("5 + 3 = " + 5 +3)
print("5 + 3 = " + str(5+3) )

[Q2] What happens when you join two strings together using the plus sign? What
happens when you join two strings together using the comma?
CS 111- Lab Manual
Introduction to Computing and Programming
Date: 11/5/23
join two strings together using the plus sign:
join two strings together using the comma:

3.Writing a simple program

Activity on programming techniques to solve problems in the Python programming environment.


Group work. Quizzes

Q1: Write a program involves designing a strategy for solving the problem and then using a programming
language to implement that strategy.

Computing the area of a circle


Tip: it is always good practice to outline your program in the form of an algorithm before you
being coding.
Algorithm:
Start
Input radius
Set π to 3.14
Compute the area radius*radius* π
Output area

Coding:

Q2: Computing the area of (Rectangle area)

Code:
CS 111- Lab Manual
Introduction to Computing and Programming
Date: 11/5/23

Q3: Write program that display the sales tax then display the payment after adding the tax.
(The tax is 5%)
Algorithm:

start
input purchase
Compute the sales_tax purchase *0.5
Compute the total_payment purchase+ sales_tax
Output sales_tax and total_payment

Code:

Q4: You are planning a trip to Europe. Your bank will exchange US Dollars for Euros by first
subtracting a $10 service fee from your US Dollars and then processing the conversion on the remainder
of your US Dollars at the rate of 0.781. This means that if you give them $110 they will give you 78.10
in Euros. If you give them $500 they will give you 382.69 Euros.
Algorithm:
Coding:

Q5: Body mass index (BMI) is a number calculated from a person’s weight and height. According to the
Centers for Disease Control and Prevention, the BMI is a fairly reliable indicator of body fatness for most
people. BMI does not measure body fat directly, but research has shown that BMI correlates to direct
measures of body fat, such as underwater weighing and dual-energy X-ray absorptiometry. The formula
for BMI is weight/height2 where weight is in kilograms and height is in meters.

(a) Write a program that prompts for metric weight and height and outputs the BMI.

(b) Write a program that prompts for weight in pounds and height in inches, converts the values to metric,
and then calculates the BMI.

Q6: (Illegal expressions) In a Python shell, try some of the illegal expressions mentioned in the chapter
and observe the error messages. For example, try assigning a value to a keyword, such as and =4. Each
CS 111- Lab Manual
Introduction to Computing and Programming
Date: 11/5/23
error message begins with the name of the error, and that name is useful in checking for errors. Generate
at least five different error messages.
Error Name Description To Fix
Name Error A name error almost always Check the right hand side of
Name----is not defined on line ---- means that you have used a assignment statements
variable before it has a value.
Often this may be a simple
typo, so check the spelling
carefully.
Indentation Error
Type Error:
Value Error: A Value Error most often The error message gives
ValueError: invalid literal occurs when you pass a you a pretty good hint
for int() with base 10: parameter to a function and about the name of the
'None' on line --- the function is expecting one function as well as the
type and you pass another.
value that is incorrect.
Look at the error
message closely and
then trace back to the
variable containing the
problematic value.
TokenError: this error usually means that When you press OK on the
there was an open dialog box. Python will attempt
parenthesis somewhere on a to highlight the offending line in
line, but not a matching your source code. However,
closing parenthesis. Python since it had reached the end of
reached the end of the file the file, it will highlight the last
while looking for the closing line in the file! You can type a
parenthesis. right parenthesis at the end of
the file, and IDLE will highlight
the matching opening
parenthesis. You should then be
able to find out where your
missing parenthesis should be.
Remember to remove the extra
closing parenthesis at the end of
your file.

4. PCAP | Programming Essentials in Python

Module 1 Quiz Mark:


Module 2 Quiz Mark:
CS 111- Lab Manual
Introduction to Computing and Programming
Date: 11/5/23

Extra material
Introduction to Programming Using Python, Y. Daniel Liang, Pearson, 1st edition, ISBN-13:
978-0132747189, 12 Jan 2012.
Python programming environment (http://www.python.org)
Python Institute Certifications

You might also like