0% found this document useful (0 votes)
335 views

Lab Activity 2 - Student

This document provides instructions for a lab activity on variables, data types, operators, and conditional statements in Python. It includes multiple exercises for students to practice defining variables of different data types, using arithmetic operators, and writing conditional logic using if, else, and elif statements. Students are asked to write and run Python code for various tasks like converting between units and checking eligibility. The goal is for students to understand and apply core Python concepts by the end of the tutorial session.

Uploaded by

ezulham
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)
335 views

Lab Activity 2 - Student

This document provides instructions for a lab activity on variables, data types, operators, and conditional statements in Python. It includes multiple exercises for students to practice defining variables of different data types, using arithmetic operators, and writing conditional logic using if, else, and elif statements. Students are asked to write and run Python code for various tasks like converting between units and checking eligibility. The goal is for students to understand and apply core Python concepts by the end of the tutorial session.

Uploaded by

ezulham
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
You are on page 1/ 12

DFP40203 PYTHON PROGRAMMING/PBU

LAB ACTIVITY 2: Variable, Data Type, Operators &


Conditional Statement in Python
Duration: 3 Hours

Learning Outcomes
By the end of this tutorial session, you should be able to:
1. Explain variables and data types in Python
2. Apply Operators and Operators Precedence
3. Define and Write conditional Statement

Software: Pycharm IDE

Activity 2A(i)
Activity Outcome: Python Data Types : Numbers (Integer and Float)

Step 1: Type the following code in Pycharm IDE. Save as act2a(i).py then run the code.

Step 2: Write the output for act2a(i).py

1
DFP40203 PYTHON PROGRAMMING/PBU

Activity 2A(ii)
Activity Outcome: Python Data Type : String

Step 1: Type the following code in Pycharm IDE. Save as act2a(ii).py then run the code.

Step 2: Write the output for act2a(ii).py

Activity 2A(iii)
Activity Outcome: Python Data Type : Boolean

Step 1: Type the following code in Pycharm IDE. Save as act2a(iii).py then run the code.

Step 2: Write the output for act2a(iii).py

2
DFP40203 PYTHON PROGRAMMING/PBU

Activity 2A(iv)
Activity Outcome: Python Data Type : String to Numbers(Integer, Float,
Bool)

Step 1: Type the following code in Pycharm IDE. Save as act1a(iv).py then run the code.

a = int(input('Enter any number : '))


print(type(a))
b = float(input('Enter any number : '))
print(type(b))
c = bool(input('Type 1 to Proceed or Type 0 to Cancel '))
print(type(c))

Step 2: Write the output for act1a(iv).py

Activity 2A(v)
Activity Outcome: Numbers and String Values

Step 1: Use the two variables declare below in the print to create output: Aileen's CGPA is
3.87

Step 2: Based on the following variables, use print to display:


The multiplication of 10 and 10 is 100

3
DFP40203 PYTHON PROGRAMMING/PBU

Activity 2C
Activity Outcome: Python String

NOTES :
In this activity, you should observe and understand:
• use of single quotes(‘’), double quotes(“”) and triple quotes(“”””””) in Pyhton
• comment in Python
• Newline
• Concatenation in String : plus (+) and comma(,)

Step 1: Type the following code in Pycharm IDE. Save as act2c(i).py then run the code.

Step 2: Write the output for act2c(i).py

Step 3: Type the following code in Pycharm IDE. Save as act2c(ii).py then run the code.

x= '''
A cube has 8 corners:
7------8
/| /|
3------4 |
| | | |
| 5----|-6
|/ |/
1------2
''';
print(x);
4
DFP40203 PYTHON PROGRAMMING/PBU

Step 4: Write the output for act2c(ii).py

Step 5: Identify and correct the errors in the following code.

Activity 2D(i)
Activity Outcome: Arithmetic Operators in Python

Step 1: Type the following code in Pycharm IDE. Save as act2d(i).py then run the code.

5
DFP40203 PYTHON PROGRAMMING/PBU

Step 2: Write the output for act2d(i).py

Activity 2D(ii)
Activity Outcome: Arithmetic Operators in Python: Formatting Expression

Step 1: Type the following code in Pycharm IDE. Save as act1d(ii).py then run the code.

Step 2: Write the output for act2d(ii).py

Activity 2E
Activity Outcome: Write if statement

Step 1: Type the following code in Pycharm IDE. Save as act2e.py then run the code.

6
DFP40203 PYTHON PROGRAMMING/PBU

Step 2: Write the output for act2e(i).py

Activity 2F
Activity Outcome: Write if.. else statement

Step 1: Type the following code in Pycharm IDE. Save as act2f.py then run the code.

Step 2: Write the output for act2f.py

Test Data : Any numbers except 0 Test Data : Number 0

Step 3:

Write a python code to asked user to enter the age. Users are eligible for voting if their age
is larger than or equal to 18. If the condition meet, print message “You are eligible for voting”
otherwise print “You are not eligible for voting”.

7
DFP40203 PYTHON PROGRAMMING/PBU

Activity 2G
Activity Outcome: Write the elif statement

Step 1: Type the following code in Pycharm IDE. Save as act2g.py then run the code.

Step 2: Write the output for act2g.py

Step 3: Edit the code in act2a.py Step 3 to add one more condition if the numbers are
equal. The output shall looks like below :

Step 3:

Write python code to receive two input from user which are num1 and num2. Compare the
two numeric values. The output should fit all the test data shown below:

Output 1 Output 2 Output 3

8
DFP40203 PYTHON PROGRAMMING/PBU

Activity 2H
Activity Outcome: Write nested if.. else

Step 1: Type the following code in Pycharm IDE. Save as act2h.py then run the code.

Step 2: Write the output for act2h.py

Exercise 1
Write a program to convert a distance in Kilometres into a distance in miles.
1. Take input from the user for a given distance in Kilometres. This can be done using
the input() function.
2. Convert the value returned by the input() function from a string into an integer using
the int() function.
3. Now convert this value into miles—this can be done by dividing the kilometres
by 0.6214
4. Print out a message telling the user what the kilometres are in miles.

9
DFP40203 PYTHON PROGRAMMING/PBU

Exercise 2
Write a program to convert from degree Farenheit to degree Celcius. The program should
receive degree Farenheit from input() function.

Formulae:

Exercise 3
Write a program calculate weight lost plan. The program receive current weight and height,
goal weight and days to achieve new weight using input function. Calculate the BMI and
weight per kg need to lose to achieve new weight.

Sample output

Exercise 4

Write a python program to check if a number entered by user is between 0..10.


Sample Output:

Test Data : 101 Test Data : 101

10
DFP40203 PYTHON PROGRAMMING/PBU

Exercise 5
Write a program to convert a find whether a given year is a leap year or not. Prompt user to
enter the year.

Sample Output:

Test Data : 2010 Test Data: 2020

Exercise 6
Create a Python program to find either the candidate is qualified to join Computer Science
course or not.
Marks in Mathematic >= 70
Marks in Problem Solving >= 50
Marks in Bahasa Melayu >= 65
Total in three subject >= 200 Or Total in Mathematic and Problem Solving >=160

Sample Output:

11
DFP40203 PYTHON PROGRAMMING/PBU

12

You might also like