Lab Activity 2 - Student
Lab Activity 2 - Student
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
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.
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.
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.
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.
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
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 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
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
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.
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
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 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 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:
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.
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
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:
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