0% found this document useful (0 votes)
41 views6 pages

CIT 101 Practical 03

This document provides instructions and examples for a Python practical on input and output. It explains how to read different data types from keyboard input, including strings, integers, floats, complexes, booleans, lists, tuples, dictionaries and sets. Examples are given to read each data type and check the type of the resulting object. The practical tasks students to identify valid/invalid Python identifiers, write code to perform calculations from input values, and write applications to calculate volume of a cone, capacity of a cool box, ratio of students, length of a square room side, and simple interest.

Uploaded by

bait-in-059
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)
41 views6 pages

CIT 101 Practical 03

This document provides instructions and examples for a Python practical on input and output. It explains how to read different data types from keyboard input, including strings, integers, floats, complexes, booleans, lists, tuples, dictionaries and sets. Examples are given to read each data type and check the type of the resulting object. The practical tasks students to identify valid/invalid Python identifiers, write code to perform calculations from input values, and write applications to calculate volume of a cone, capacity of a cool box, ratio of students, length of a square room side, and simple interest.

Uploaded by

bait-in-059
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/ 6

Bachelor of Applied Information Technology

Module Code CIT101


Module Title: Fundamentals of Programming

Date: 20th February 2023

Practical 3 – Input and Output


The objective of this practical is to get familiarized with Standard Input and Standard Output
methods in Python.

Before you start I suggest you to study the following.

1. Supplement 1 Numeric Data


2. CIT 101 Py Lsn 2 Final
3. CIT 101 Py Lsn 3 Final
4. CIT 101 Py Lsn 4 Final
5. CIT 101 Py Lsn 5 Final

What you learn?

We will learn the following:

 How to read different objects through the keyboard?


 How to write meaningful user prompts?
 How to write meaningful outputs?
 How to use python reference variables?

Reading Standard Objects through the Keyboard

 In Python we consider everything as objects.


 Objects exist individually and as groups.
 Python divides objects basically into 'int', 'float', 'complex', 'bool', 'list', 'tuple', 'dict',
'str' and 'set' types.
 The following table provides examples for each type.

Type Object
int …-3,-2,-1,0, 1, 2,3 …
float -123.56, 2.657E+07
complex 2+3j, -3 + 4j, 0 + j
bool True, False
list [1,2,3,4]
tuple (3,-6,,8), ('a', 'e', 'd')
dict {1:'a', 2:'b', 3:'c'}
str 'Python'
set {3,5,,1,2}

Page 1 of 6
Bachelor of Applied Information Technology
Module Code CIT101
Module Title: Fundamentals of Programming

Reading a String through the Keyboard

We use input() function to read input from the user through the keyboard. The input()
function converts the object you enter into a string object. Strings are non-numeric
objects. (They do not take part in calculations)

Example:
Variable Function User Prompt

>>> word = input('Enter a word:') 


Enter a word: Python 
Check type
User Input
>>> type(word)
<class 'str'> 'Python' object belongs to 'str' class

1. Reading an Integer through the Keyboard


Integer Variable Convert String into integer.

>>> int_num = int(input('Enter an Integer:')) 


Enter an Integer:12 
User Input: Integer

How do you know int_num represents an integer?

>>> type(int_num)
<class 'int'> '12' object belongs to 'int' class

2. Reading a Floating Point Number through the Keyboard

>>> fnum = float(input('Enter a floating point number:'))


Enter a floating point number:12.3
User Input: Floating Point Number
>>> type(fnum)
<class 'float'> '12.3' object belongs to 'float' class

Page 2 of 6
Bachelor of Applied Information Technology
Module Code CIT101
Module Title: Fundamentals of Programming

3. Reading a Complex Number through the Keyboard

>>> comp_num = complex(input('Input complex number:'))


Input complex number:2+3j
>>> comp_num User Input: Complex Number
(2+3j)

>>> type(comp_num)
<class 'complex'> '2 + 3j' object belongs to 'complex' class

4. Reading a Boolean Object through the Keyboard

>>> boo = bool(input('Are you an adult?'))


Are you an adult?True
User Input: Boolean value
>>> type(boo)
<class 'bool'> '12.3' object belongs to 'bool' class

5. Reading a List Object through the Keyboard

>>> lst = list(input('Enter a list:'))


Enter a list:123
User Input: List values
>>> lst
['1', '2', '3']
List Created
>>> type(lst) '1', '2', '3' objects belongs to 'list' class
<class 'list'>

6. Reading a Tuple Object through the Keyboard

>>> tup = tuple(input('Enter a tuple:'))


Enter a tuple:abc
User Input: Tuple values
>>> tup
('a', 'b', 'c')
Tuple Created
>>> type(tup)
<class 'tuple'> 'a', 'b', 'c' objects belongs to 'tuple' class

Page 3 of 6
Bachelor of Applied Information Technology
Module Code CIT101
Module Title: Fundamentals of Programming

7. Reading a Set Object through the Keyboard

>>> st = set(input('Input a set:'))


Input a set:1234
User Input: Set values
>>> st
{'2', '3', '4', '1'}
Set Created
>>> type(set)
<class 'type'> '2', '3', '4', '1' objects belongs to 'type' class

10 int

10.5 float

2+3j complex

True bool

"Lanka" str

[1,2,3] list

('a','b','c') tuple

{1:'a',2:'b} dict

{4,1,3} set

Practical 3 Submission

You have to submit all your workings to LMS clearly in a .pdf file by putting file name as
follows. (Delete all error messages)

Module Code Registration number Pra3.pdf


Eg. CIT101- 22UG3 xxxx Pra3.pdf
You may discuss things with your friends, but do not copy. Copying will be a disqualification and you
will get 0 marks if you get caught. For late submissions 10% of the marks will be deducted for each
day.
Time Allocated: As notified in the LMS

Page 4 of 6
Bachelor of Applied Information Technology
Module Code CIT101
Module Title: Fundamentals of Programming

Practical 3:
1. With justifications, identify invalid or unsuitable Identifiers in Python from the following.

Example:

Identifier Usage* Justification


Dfertyw5izvtr (L) (NR) Legal but not meaningful

Identifier Usage* Justification


i) -continue
ii) 5boys
iii) Boys and Girls
iv) _my_stuff
v) !Oh
vi) jzcpriwqkfvc
vii) b2b
viii) pass
ix) number_of_birds=10
x) Sum
*Usage: Legal (L), Illegal (I), Recommended (R), Not Recommended (NR)

2. For each of the following, write a set of single Python statements in the interactive mode to
display the resulting output in a meaningful way. You should also use appropriate variable
names.

Example:
A can contains 42.35 gallons of coconut oil. What is the capacity in litres.
[1 gallon = 3.79 litres]

>>> volume_in_gallons = 42.35


>>> volume_in_litres = 42.35*3.79
>>> print(f'The capacity of coconut oil in the can = {volume_in_litres:.2f} litres')

a) Find the length of a 10 m cable in inches rounded to nearest first decimal place.
(1m = 39.37 inches).

b) Express the speed of a car driven at 60 kmph in mph (miles per hour).
(8 km = 5 miles)

c) Find the hypotenuse of a right angled triangle in cm, if the lengths of the other two
sides are 3.5 cm and 1.73 cm respectively.

d) Find the area of a square in cm2 whose side is 2.65 cm.

Page 5 of 6
Bachelor of Applied Information Technology
Module Code CIT101
Module Title: Fundamentals of Programming

3. Example:

Write a Python application to input the radius and the height of a right circular cone in cm, and
display its volume in cm3 rounded to 2 decimal places.

# Volume of a right circular cone

import math

radius = float(input("Enter the radius of the cone in cm:")) # Read radius in cm


height = float(input("Enter the height of the cone in cm:")) # Read Height in cm
volume = math.pi*radius*radius*radius*height # Calculate volume
print(f"The volume of the cone is {volume:.2f} cm\u00B3") # Display volume in cm3

a) Write a Python application to read the length, breadth and height of a cool box in
meters and display its capacity in m3.

b) Write a Python application to input the number of boys and girls in a class and display the
ratio of girls and boys as a percentage of the total number of students.

c) Write a Python application to input the area of a square shaped room in square feet and
find the length of a side in feet.

d) Write a Python application to calculate the simple interest when the principal amount,
annual rate and the period are input by the user.

e) A machine fills (x) number of bottles of water every (t) minutes where x > 0. Write an
expression to find the number of hours (h), it takes this machine to fill (y) number of bottles.
Write a Python application to input feasible values for x, t, y and calculate h in hours and
minutes.

End of Practical 3

Page 6 of 6

You might also like