You are on page 1of 10

FAKULTI TEKNOLOGI KEJURUTERAAN

ELEKTRIK DAN ELEKTRONIK


UNIVERSITI TEKNIKAL MALAYSIA MELAKA

TECHNOLOGY SYSTEM PROGRAMMING 2

BEEL 1234 SEMESTER 2 SESI 2020/2021

LAB 1: INTRODUCTION TO PYTHON PROGRAMMING

NO. STUDENTS' NAME MATRIC. NO.

1.

2.

3.

PROGRAMME 1BEEL

SECTION / GROUP 1/1 1/2

1. AMAR FAIZ BIN ZAINAL ABIDIN


NAME OF
INSTRUCTOR(S)
2.

EXAMINER’S COMMENT(S) TOTAL MARKS

1
1.0 OBJECTIVES

1. To be familiar with Python Integrated Development Environment.


2. To understand basic structure of Python programming.
3. To write program that receive inputs from user and display output to user.
4. To be able to use basic mathematical operation in Python.
5. To produce flowchart and write program based on problem given.

2.0 EQUIPMENT

1. Personal Computer / desktop


2. Software: Visual Studio Code

3.0 THEORY

The Python Integrated Development Environment (IDE) used for all the laboratory sessions. Your
instructor will demonstrate to you on how to install it and explain the details on the User Interface
(UI).

As, this Lab is concerned with the basic elements used to construct Python. We begin by
considering a simple Python program as in Fig 1.1

1 # Program 1.1
2 # A first program in BEEC 1313 Lab
3
4 print(‘Welcome to BEEL 1234 Course\n’)
5 print(‘Lab 01 :\t Introduction to Phyton Programming’)

Fig 1.1

Write the codes in the text editor and click the Run and Debug button icon. The terminal
should produces the following result as shown in Fig1.2:

Welcome to BEEL 1234 Course


Lab 01 : Introduction to Phyton Programming

Fig 1.2

Lines 1 and 2 begin with #, indicating these 2 lines are comments. Comments are to
document the program and to improve program readability. Comment does not cause the computer
to perform any action when the program is run since they are ignored by the interpreter and do not
cause any machine-language object code to be generated.

2
Line 3 is simply a blank line. We use blank lines, space characters and tab characters
(“tabs”) to make programs easier to read. Together, these characters are known as white space.
White-space characters are normally ignored by the compiler.

Line 4 instructs the computer to perform an action, namely to print on the screen the string
(also called a character string, message, or a literal) of characters marked by the quotation marks.
The entire line, including the print function, its argument within the parentheses is called a
statement. The characters normally print as they appear between the single (’’) quotes in the
print statement.

Notice that character \n and \t were not printed on the screen. The backslash (\) as used
here is called an escape character. When encountering a backslash in a string, the compiler looks
ahead at the next character and combines it with the backslash to form an escape sequence. The
commonly used escape sequence are listed in Table 1.1

Table 1.1
Escape Sequence Meaning
\n Newline. Position the cursor at the beginning of the new line.
\t Horizontal Tab Move the cursor to the next tab stop.
\\ Backslash. Insert a backslash character in a string
\’ Single quote. Insert a double-quote character in a string

Consider the next program which uses the Standard Library function input to obtain two
numerical values typed by the user using the keyboard.

1 # Program 1.2
2 # Simple additional program
3
4 # function main begins program execution
5 print (‘Enter first integer:’) # prompt
6 integer1 = int(input())# read an integer
7
8 printf(‘Enter second integer:’) # prompt
9 integer2 = int(input())# read an integer
10
11 sum = integer1 + integer2 # assign total to sum
12 printf(‘Sum is’, sum) # print sum

Fig 1.3

A variable name in Python can be any valid identifier. An identifier is a series of characters
consisting of letters, digits and underscores that does not begin with a digit. Python is case sensitive
where uppercase and lowercase letters are different in Python, as a1 and A1 are different
identifiers.

3
Line 6 uses input function to obtain a value from the user. The function reads from the
standard input, which is usually the keyboard. The input function accepts input from the input
via the keyboard then store it in a string data type.

In the same line, int function is used to convert the input from the user that is in string
data type into integer. Table 1.2 shows several data casting that can be used in Python:

Table 1.2
Data type Function
integer int()
float float()
string str()

The assignment statement in line 11 calculates the total of variables integer1 and
integer2 and assigns the result to variable sum using the assignment operator =:

sum = integer1 + integer2;

The calculation is performed as assignments. The = and + operators are called binary operators
because each has two operands. The + operator’s operands are integer1 and integer2.
The = operator's operands are sum and the value of the expression integer1 + integer2.
Integer division yields an integer result. Other basic arithmetic operators as shown in Table 1.3:
:
Table 1.3
Arithmetic Algebraic C expression
C operation
operator expression
Addition + f + 7 f + 7
Subtraction - p – c p – c
Multiplication * bm b * m
Division / x / y or x÷y x / y
Remainder % r mod s r % s

The rules for operator precedence specify the order Python uses to evaluate the expression.
When we say evaluation proceeds from left to right, we are referring to the associativity of the
operators.

Table 1.4
Operator Operation Order of evaluation (precedence)
() Parentheses Evaluated first. If the parentheses are nested, the expression in the
innermost pair is evaluated first.
* Multiplication Evaluated second. If there are several, they are evaluated left to right.
/ Division
% Remainder
+ Addition Evaluated third. If there are several, they are evaluated left to right.
- Subtraction
= Assignment Evaluated last

4
4.0 LABORATORY ACTIVITIES

1. Identify and correct the errors in the following statements.

a) x := input()

b) printf(‘Welcome to BEEL 1234’);

c) x = str(input(‘Enter the first integer: ’))

d) print(‘The total value is’ + total)

Answer:

a)

b)

c)

d)

2. Write a complete and executable Python statement to accomplish the following tasks. Save
your codes and print screen your output console as a result.

a) Prompt the user to enter an integer in inverted commas.


Like this ‘Enter a floating value:’

b) Read two integers from the user and store the value entered in the variable a & b. Your
program should also display the valued stored in variable a and b.

Code:

a)

b)

5
3. You are asked to build a simple program that can find an average of 3 integers. Those 3
integers are input by the user. Your average value should display the floating point.
Save your code and print screen your Terminal console as a result.

Code:

Terminal (Output):

6
4. Create a BMI calculator application that reads the user’s weight in kilograms and height in
meters, then calculates and displays the user’s body mass index. The formula for calculating
BMI is

ܹ݄݁݅݃‫ݏ݉ܽݎ݃݋݈݅݇ ݊݅ ݐ‬
‫= ܫܯܤ‬
‫ݏݎ݁ݐ݁݉ ݊݅ ݐ݄݃݅݁ܪ × ݏݎ݁ݐ݁݉ ݊݅ ݐ݄݃݅݁ܪ‬

Your BMI value should display the floating point. Also, the application should display the
following information from the Department of Health and Human Services/National Institute of
Health so the user can evaluate his/her BMI:

BMI VALUES
Underweight : less than 18.5
Normal : between 18.5 and 24.9
Overweight : between 25 and 29.9
Obese : 30 or greater

Code:

7
Terminal (Output):

8
5.0 Laboratory Assessment Rubrics

Course BEEL1234 TECHNOLOGY SYSTEM PROGRAMMING 2


Programme BEEL
Semester/ Session SEM 2, 2020/2021
Lab 1 Matrix ID MARK

Students' Names 2

Very Weak Weak Fair Good Excellent


Assessment Level Assessment Criteria STUDENT 0 1 2 3 4 Weightage Score Marks
1
P1 Ability to relate between
2 0.13
(Awareness) theory and practical
L E 3
ax
1
bp Ability to shows proper
P2
o e standard 2 0.25
(Readiness)
r r in C program. 3
a i
tm Construct program 1
o e P3 codes that free from
2 0.25
r n (Attempt) syntax/logic or runtime
y t errors. 3
1
P4 Display the output/result of
2 0.63
(Basic Proficiency) experiment.
3

ACTAUL MARK (5%)

1
6.0 VIDEO AND REPORT PREPARATION AND SUBMISSION

1. Laboratory activities should be done in group of two.


2. You are required to prepare a video presentation of not more than five minutes that demonstrate
and explain your answers concisely for all the tasks in the laboratory activities.
3. Both of the members must present and must show their face during the video presentation.
Example of the video presentation: https://www.youtube.com/watch?v=RA53rou1NuM
4. Please submit the video in .mp4 file via the Google Drive URL (will be given later to the class
representative).
5. Fill up the laboratory sheet (must be handwritten), scan and merged the laboratory sheets into
a single PDF file.
6. Please submit the PDF file via the Google Drive URL (will be given later to the class
representative.

You might also like