VIT1102 - Introduction to Programming
VU College
Lab 2 – Operators and Variables
Using Python Console
Ex0
Find out the values of following expressions using Python console and record your answers.
a. 2+6*9
b. 43 + 28 / 7
c. 90 + 30 + 40 + 22
d. 30 * 2
e. 50 + 20 * 8 +(3 +6)
Task 1 – Expressions
Open PyCharm and Create a Project called Lab 2. In Lab 2 create a Python package task1.
Ex1
In task1, create a Python file Ex1.py and write the code below.
Right click on the window and run your program. You will see the values of A, B, C and D
display in the output area.
Verify your answers by running them in Python Console.
Ex2
Add a new Python file Ex3.py to package task1.
Run above code and find values of A and B. Explain why they are different. Verify your
answers by running them in Python Console.
1
VIT1102 – Lab 2.docx
Oshadi Alahakoon 2020
VIT1102 - Introduction to Programming
VU College
Task 2 – variables
What is a variable?
Please refer to Week 2 lecture note and (Sweigart 2015) pages 19 -13. Practice examples
from the book to understand variables.
In project Lab 2 create a Python package task2.
Ex1
In a new python file Ex1.py repeat class exercise in Week 2 lecture note slide 16.
Ex2
In a new python file Ex2.py write a program to produce the following output. Code required
is similar to Ex 1 above.
Steps:
1. Write an input command with the question “What is your name?”
2. Read the input to a variable – name your variable according to convention.
3. Print by adding the word “Hi” and a space to the variable.
Ex3
In a new python file Ex3.py run the following code.
Improve your code to print the sum of the two numbers. Your output should look as follows.
NOTE: Use f’ string to format the output. Refer to Topic 2 Appendix.
2
VIT1102 – Lab 2.docx
Oshadi Alahakoon 2020
VIT1102 - Introduction to Programming
VU College
Ex4
In a new python file Ex4.py practice escape sequence examples given in lecture 2 slide 25.
Understand the effects of escape characters (\n and \t). You can also do this in Python
console.
Ex5
In a new python file Ex5.py enter and run the following code.
Observe how output is displayed. Update your code to produce the following output.
Note: in the input lines there are tab spaces next to the message. And the output appear as
two lines due to the \n character.
Ex6
In a new python file Ex6.py enter and run the following code.
3
VIT1102 – Lab 2.docx
Oshadi Alahakoon 2020
VIT1102 - Introduction to Programming
VU College
Run and observe the output. Then do the following changes to your code.
1. Add comments before lines 4 and 5 explaining what those lines do.
2. Change your output to print the last output line in two lines.
3. Run above program and try entering numbers 12.5 and 15.8 as the first and second
input numbers. What happens? Why? Explain to your teacher.
4. Update your program to read decimal numbers as inputs.
Ex7
Write a program to read two decimal numbers as width and the length of a room. Calculate
and display the area of the room.
Additional Exercises
Add1
Write a program to calculate total purchases at a book store.
The book store sells any new book for $10. Any old book cost $4.50. There are books for
renting/borrowing. They cost $2 per day.
For example, if you purchase 2 new books, 1 old book and rented a book for 5 days, your
payment should be $34.50.
Design your own interface.
Add2
In a restaurant, all take away VEGETARIAN meals cost $11.80. All orders with MEAT
(Chicken/ Beef/Lamb) cost $12.80. Any SEAFOOD meal costs $14.00. Write a program to
calculate customer order costs.
For example, if a customer orders 2 vegetarian meals and a seafood meal, then the total
cost is $37.60.
Design your own interface.
Add3
Write a Python program to calculate the cost to lay a concrete slab. Cost of laying concrete
is given as $85.00 per square metre.
Details are as follows:
The user will enter the width and the length of the area to lay concrete. Then the program
should calculate the area (in square metres).
Area = width * length
Once the area is known, the program can calculate the cost of the concrete laying.
4
VIT1102 – Lab 2.docx
Oshadi Alahakoon 2020
VIT1102 - Introduction to Programming
VU College
Cost = area * 85.00
Both the area of the slab, and the cost of concreting must be displayed to the user.
Add 4
Create a program that asks the user to enter their name and their age. Print out a message
addressed to them that tells them the year that they will turn 100 years old.
Add 5
Write a Python program to read 3 numbers. One of them should be a decimal value.
Display addition of the numbers with the following formatting (assume user entered 1, 6
and 5.5 as the numbers).
Addition of 3.0 numbers: 12.500
Add 6
Write a Python program to read the date, month and year and display the output in
following format.
5
VIT1102 – Lab 2.docx
Oshadi Alahakoon 2020
VIT1102 - Introduction to Programming
VU College
Add 7
Create a Python program that will solve the following problem.
Your application should calculate the total cost for paving a given area. Paving is done
always with a decorative edge as shown in the diagram.
The total tiling cost is calculated as follows:
Cost for a square meter of straight paving is $100.
Cost for a square meter of edge paving is $150.
User should provide the following data as inputs (Note: only these 3 datum are required).
Length of the area to be paved
Width of the area to be paved
Width of the decorative edge
Using the input data:
a. Write a formula to calculate the straight paving area.
b. Write a formula to calculate the decorative edge paving area.
c. Write a formula to calculate the total paving area.
d. Use the above formulas in your application to display each area and the paving costs.
Output from 3 sessions are given below.
6
VIT1102 – Lab 2.docx
Oshadi Alahakoon 2020
VIT1102 - Introduction to Programming
VU College
7
VIT1102 – Lab 2.docx
Oshadi Alahakoon 2020