VIT1102 - Introduction to Programming
VU College
Lab 1 – Writing simple Python programs
Some housekeeping work before starting lab work:
File management in your work is IMPORTANT. We are going to store all our Python projects
in a folder on a USB/Flash Drive. This way you can work on your projects/labs from any
desired location (home or uni).
Before starting work on lab 1, in Windows Explorer create a special folder for this unit
(VIT1102). Then create a folder for all your lab work – name this as Projects. Create a new
folder for Labs inside the Project folder. Each week, you can add a new Python project to
this Labs folder - with the lab name (Lab1, Lab2 so on).
Back to the Lab:
Starting PyCharm Edu
Start PyCharm from windows Start menu by selecting All Programs>JetBrains PyCharm Edu
2.0.3. (Or by double clicking on the desktop icon).
If you are using this software for the first time, you will see the following screen. Use second
option to start a new project.
Otherwise use File>New Project from the main menu.
1
VIT1102 – Lab 1.docx
Oshadi Alahakoon 2020
VIT1102 - Introduction to Programming
VU College
The following dialog will appear with default Pure Python project type selected.
’
Creating a Python project
1. Use the button marked with the green arrow to navigate to your Labs folder.
2. To create a new Python project name Lab 1 change the untitled to Lab 1
(Note: Usually project name starts with an uppercase letter). You are to work on this
project for the entire Lab 1 exercises.
3. Click Create button.
2
VIT1102 – Lab 1.docx
Oshadi Alahakoon 2020
VIT1102 - Introduction to Programming
VU College
Adding a package to Lab 1 project
1. To add a package called task 1, right click on Lab 1 in project window and select Python
Package.
2. Name the package task 1
(Note: packages are named using all lowercase letters).
Adding a Python file to task 1 package
1. Now to, create a Python file. Right click on task 1 package, and select Python file.
2. Name the file Ex1. Now you are ready to write Python programs.
3
VIT1102 – Lab 1.docx
Oshadi Alahakoon 2020
VIT1102 - Introduction to Programming
VU College
Run simple Python commands using Python console.
In this section use Python console (in PyCharm) to run simple Python commands. These are
not proper programs.
Starting Python console
On the lower left corner of the PyCharm window, locate Python Console icon.
Ex1
Enter and run the commands given below in Python console. They are same as in the in
lecture note.
Do the following in console.
1. Run a different greeting to ‘Hello World’.
2. Try the same greeting with double quotations (“”) rather than single quotation marks
(‘ ’).
3. What is the difference using print() method?
4. Print a “Hello” greeting with your name. Make sure there is a space between the two
words.
4
VIT1102 – Lab 1.docx
Oshadi Alahakoon 2020
VIT1102 - Introduction to Programming
VU College
Write, save and run Python programs in PyCharm
In this section use PyCharm to write simple Python programs. These programs will be saved
to your USB for future use.
Ex1: Display using the print() function
Note: Refer to Lecture note Week 1 and (Sweigart 2015) pages 20 -21 for descriptions on print()
function.
Right click on the window and run your program. You will see your greeting display in the output
area.
Add more instructions to your existing program to print the following
a. “Hello <your name>!” (eg. “Hello John”)
b. “My name is <your name>”
c. “ I am learning Python programming”
d. “My teacher’s name is <your teacher’s name>”
e. “ This is my first semester at Victoria University”
5
VIT1102 – Lab 1.docx
Oshadi Alahakoon 2020
VIT1102 - Introduction to Programming
VU College
Ex2: Interact using the input() and print() functions
Note: Refer to (Sweigart 2015) pages 20 -21 for descriptions on input() function.
Add a new Ex2 python file to task1 package. Type the following code.
Implement and run your program. In your code print() will always print what is given in the
brackets to the output area.
More on print(), input() and string concatenation
Ex3
Add a new Ex3 python file to the same task1 package. Type the following code.
Alter your program to get the following output. As you can see instead of just showing the number,
output shows a string “Your age is “.
Ex4
Add a new Ex4 python file to the same task1 package. Type the following code.
Your output should be something similar to the followng.
Note: In the screen output below, user inputs are in green.
6
VIT1102 – Lab 1.docx
Oshadi Alahakoon 2020
VIT1102 - Introduction to Programming
VU College
Ex 5
Add a new Ex5 python file to the same task1 package.
Write a python program that interacts with the user to ask user details. The output of the
program should be as follows. Your code will only need print() and input commands with
variables. You are to determine when to use print(), input() commands and variables.
Note: In the screen output below, user inputs are in green.
Ex6
Try following commands on the Python Console.
Write code to produce a list of ingredients and quantities as output, part of the code is as
follows:
Read any 4 ingredients, and their quantities. Then print the list of ingredients as follows.
Ingredient List
7
VIT1102 – Lab 1.docx
Oshadi Alahakoon 2020
VIT1102 - Introduction to Programming
VU College
************
Butter 100g
Flour 2kg
Sugar 500g
Condensed milk 1 tin
Ex7
You are to write a Python program to produce a customise news item for any
college/discipline in Victoria University. Sample of questions to ask as follows:
Then the program should display a news item (as follows) customised with the above inputs:
References
Sweigart, A. (2015). Invent Your Own Computer Games with Python 3rd Edition.
8
VIT1102 – Lab 1.docx
Oshadi Alahakoon 2020