Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 10

PFP - Labwork 01

Full-Name:
ID card:
Tel.:
Email:

Q1 (2.5 marks). Set up the Visual Studio Code environment:


1. Install the Visual Studio Code: https://code.visualstudio.com/ (Click Download for Windows).

2. Install the Python extension for VS Code from the Visual Studio Marketplace. For additional
details on installing extensions, see Extension Marketplace. The Python extension is
named Python, and Microsoft publishes it. (https://marketplace.visualstudio.com/items?
itemName=ms-python.python).

3. Install a Python interpreter. (https://www.python.org/downloads/).

4. Verify the Python installation. (py -3 –version)


5. Start VS Code in a project (workspace) folder.
- Create by Explorer

- Create by command line

6. Select a Python interpreter.


- Click “View” -> “Command Pallete…” or press “Ctrl + Shift + P”

- Type Python: Select Interpreter


- Select command

7. Create a Python Hello World source code file.


8. Run Hello World.

There are three ways to run a python file in VSS:


- Right-click anywhere in the editor window and select Run Python File in Terminal (which
saves the file automatically)
- Select one or more lines, then press Shift+Enter or right-click and select Run
Selection/Line in Python Terminal. This command is convenient for testing just a part of a
file.
- From the Command Palette (Ctrl+Shift+P), select the Python: Start REPL command to
open a REPL terminal for the currently selected Python interpreter. In the REPL, you can
then enter and run lines of code one at a time.

Reference: https://code.visualstudio.com/docs/python/python-tutorial

Q2 (2.5 marks). Set up the Google Colab:


There are two ways to run a Python program in the Google Colab. The first way you can copy
your source codes to the code cell in the colab page and click run.

The second way, you can create a .py file in your local disk and then mount your file to the Google
Colab following the guideline below:
1. Enter the Google Colab: https://colab.research.google.com/notebooks/welcome.ipynb?hl=in

2. Mounting drive
- Click “Tool” -> “Command Pallette” or “Ctrl + Shift + P”
- Type “m” and select “Mount Drive”

- Click Run cell to mount the drive

3. Listing drive content


4. Create a hello.py file on your local disk.

5. Upload the file on the drive.

6. Run the hello.py file

Q3 (2.5 marks). Write a program that performs the following tasks:


1. Calculate the multiplication and sum of two numbers

>>>
Code Insert\Scrennshot

# AI1701 - PFP191 - 24-4-2022


def multiplication_or_sum(num1,
num2):
# calculate product of two number
product = num1 * num2
# check if product is less then
1000
if product <= 1000:
return product
else:
# product is greater than 1000
calculate sum
return num1 + num2

# first condition
result = multiplication_or_sum(30,
40)
print("The result is", result)

# Second condition
result = multiplication_or_sum(40,
50)
print("The result is", result)

2. Print the sum of the current number and the previous number
Write a program to iterate the first 10 numbers and in each iteration, print the sum of the current
and previous number.

>>>
Code Insert\Scrennshot
# AI1701 - PFP191 - 24-4-2022
print("Printing current and previous number
and their sum in a range(10)")
previous_num = 0

# loop from 1 to 10
for i in range(1, 11):
x_sum = previous_num + i
print("Current Number", i, "Previous
Number ", previous_num, " Sum: ",
previous_num + i)
# modify previous number
# set it to the current number
previous_num = i

3. Display three string “Name”, “Is”, “James” as “Name**Is**James”


Use the print() function to format the given words in the mentioned format. Display
the ** separator between each string.

>>>
Code Insert\Scrennshot
# AI1701 - PFP191 - 24-4-2022
print('My', 'Name', 'Is', 'AI1701 - PFP191',
sep='**')

Q4 (2.5 marks). Write a program to accept 3 real numbers a, b, c, then:


1. Convert Decimal number to octal using print() output formatting

>>>
Code Insert\Scrennshot
# AI1701 - PFP191 - 24-4-2022

num = 81
print("The octal number of decimal number
is:",'%o' % num)
2. Display float number with 2 decimal places using print()

>>>
Code Insert\Scrennshot

# AI1701 - PFP191 - 24-4-2022

num = 458.541315
print("Display float number with 2 number:",
'%.2f' % num)

You might also like