Python Programming
Section 01 : 60 Marks
Section 02 : 40 Marks
Section 01
Write python code for all below questions. The student should demonstrate executing all scripts
01. Variables, Builtin Functions
a. Declare a variable is_married and assign a value to it
b. Declare a variable is_true and assign a value to it
c. Use the built-in input function to get first name, last name, country and age from a user
and store the value to their corresponding variable names
d. Run help('keywords') in Python shell or in your file to check for the Python reserved
words or keywords
02. Operators
a. Write a script that prompts the user to enter base and height of the triangle and calculate
an area of this triangle (area = 0.5 x b x h).
b. Write a script that prompts the user to enter side a, side b, and side c of the triangle.
Calculate the perimeter of the triangle (perimeter = a + b + c).
c. Write a script that prompts the user to enter number of years. Calculate the number of
seconds a person can live. Assume a person can live hundred years
d. Write a Python script that displays the following table
11111
21248
3 1 3 9 27
4 1 4 16 64
5 1 5 25 125
e. I hope this course is not full of jargon. Use in operator to check if jargon is in the
sentence
03. Strings
a. Concatenate the string 'Thirty', 'Days', 'Of', 'Python' to a single string, 'Thirty Days Of
Python'.
b. Use index or find to find the position of the first occurrence of the word 'because' in the
following sentence: 'You cannot end a sentence with because because because is a
conjunction'
c. Slice out the phrase 'because because because' in the following sentence: 'You cannot
end a sentence with because because because is a conjunction'
d. Does ''Coding For All' start with a substring Coding?
04. Lists
The following is a list of 10 students ages:
ages = [19, 22, 19, 24, 20, 25, 26, 24, 25, 24
a. Sort the list and find the min and max age
b. Add the min age and the max age again to the list
c. Find the median age (one middle item or two middle items divided by two)
05. Tuples
a. Create an empty tuple
b. Create a tuple containing names of your sisters and your brothers (imaginary siblings are
fine)
c. Join brothers and sisters tuples and assign it to siblings
d. How many siblings do you have?
e. Modify the siblings tuple and add the name of your father and mother and assign it to
family_members
06. Dictionaries
a. Create an empty dictionary called dog
b. Add name, color, breed, legs, age to the dog dictionary
c. Create a student dictionary and add first_name, last_name, gender, age, marital status,
skills, country, city and address as keys for the dictionary
d. Get the length of the student dictionary
e. Get the value of skills and check the data type, it should be a list
f. Modify the skills values by adding one or two skills
07. Conditionals
a. Write a code which gives grade to students according to theirs scores:
80-100, A
70-89, B
60-69, C
50-59, D
0-49, F
b. The following list contains some fruits:
fruits = ['banana', 'orange', 'mango', 'lemon'
If a fruit doesn't exist in the list add the fruit to the list and print the modified list.
If the fruit exists print('That fruit already exist in the list')
c. Here we have a person dictionary. Feel free to modify it!
person={
'first_name': 'Tharaka',
'last_name': 'Mahabage',
'age': 250,
'country': 'Sri Lanka',
'is_marred': True,
'skills': ['JavaScript', 'React', 'Node', 'MongoDB', 'Python'],
'address': {
'street': 'Space street',
'zipcode': '02210'
}
}
d. Check if the person dictionary has skills key, if so print out the middle skill in the skills list.
e. Check if the person dictionary has skills key, if so check if the person has 'Python' skill and
print out the result.
08. Loops
a. Write a loop that makes seven calls to print(), so we get on the output the following
triangle:
#
##
###
####
#####
######
#######
b. Use nested loops to create the following:
########
########
########
########
########
########
########
########
c. Print the following pattern:
0x0=0
1x1=1
2x2=4
3x3=9
4 x 4 = 16
5 x 5 = 25
6 x 6 = 36
7 x 7 = 49
8 x 8 = 64
9 x 9 = 81
10 x 10 = 100
d. Iterate through the list, ['Python', 'Numpy','Pandas','Django', 'Flask'] using a for loop and
print out the items.
e. Use for loop to iterate from 0 to 100 and print only even numbers
09. Functions
a. Declare a function add_two_numbers. It takes two parameters and it returns a sum.
b. Area of a circle is calculated as follows: area = π x r x r. Write a function that calculates
area_of_circle.
c. Write a function called is_prime, which checks if a number is prime.
Section 02 (40 Marks)
Write a python flask blogging application. The application should have following functionalities
1. Login
2. Add blog
3. Edit blog
4. Delete blog
5. List available blogs