Lab Exam PYHTON
Lab Exam PYHTON
Instructions:
Time: 30 min.
1- Scenario: You are given a list of integers containing duplicate elements. The task is to generate a
second list, which contains only the duplicate elements. In simple words, the new list should contain
elements that appear no more than once. Write code that takes input from a user, creates a list and
then prints the cleaned-up list as output.
Example: Input_list = [10, 20, 30, 20, 20, 30, 40, 50, -20, 60, 60, -20, -20]
2- Scenario: Write a program to check a number that is inputted by the user then checks whether the
number entered by the user is positive, negative or zero.
3- Scenario: Write a program that accepts 3 integers as input and stores them as a list. Then write a
function that accepts this list and returns the sum of the squares of the 3 numbers as the output. Then
call the function in the code and it prints the output of that function.
4- Scenario: Write a program that accepts 3 integers as input and stores them as a list. Then write a
function that accepts this list and takes 2 to the power of each element. Then it returns the sum of
them as the output. Then call the function in the code and print the output of that function.
5- Scenario: Write a program that gets integer N as input and generates the sum of (0.5) i for i from 1 to
N. Try different values of N and check if the value can get larger than 1 or not.
Example: Input: “This is a test. Just try it.” Output: “This is a test Just try it”
7- Scenario: Write a program that accepts a string and replaces any “.” With “!”. from that string. If
there is no “.”, print ‘There is no “.” ’.
Example: Input: “This is a test. Just try it.” Output: “This is a test! Just try it!”
8- Scenario: Write a program that accepts 2 lists of 5 integers each from the user and compares them. If
they are equal, it prints “the same” and if not, it prints “different”
10- Scenario: Write a program that accepts a list of integers as input and checks whether it contains the
number “2”. If this is the case, it should print the location in the list. If not, it prints “There is no
number 2 in the list”. Note: If there is more than one “2”, the location of the last “2” has to be printed.
12- Scenario: Write a program that accepts a list of integers. Have it create a new list that contains only
those numbers that are squares of an integer. This number should then be printed. If there is no such
a number, it prints “none”. Hint: if the square root of a number is an integer, it means that it can be
square of another integer, for example 160.5 is an integer but 150.5 is not an integer.
14- Scenario: Write a program that accepts a list of integers. Have it create a new list that contains only
those numbers that are odd and then prints it. If there is no such a number, it then prints: “none”.
16- Scenario: Write a program that accepts a list of integers. Have it create a new list that contains only
those that are non-negative and then it prints it. If there is no such a number, it then prints: “none”.
18- Scenario: Write a program that accepts the integer N and prints the sum of the numbers from 1 to N.
If N is negative or zero, it should print: “Input Error”.
20- Scenario: Write a program that accepts the coefficients of a quadratic equation x 2+ax+b = 0 and
prints the roots as output. If there are no real roots, it prints “The equation has no roots”.
Note: In order for the equation to have real roots, D=a2-4b has to be non-negative. If this is the
case, the roots are (-a+D0.5)/2 and (-a-D0.5)/2
21- Scenario: Write a program that accepts a list L as input. If the length of L is an even number, then
define 2 lists: L1 and L2 and then copy the first half of L to L1 and the 2nd half of L to L2. If the
length of the list is not even, print “I can not break the list in half”.
Example: Input: L=[1 3 2 4 6 7 5 2], Output: L1=[1 3 2 4], L2=[6 7 5 2].
22- Scenario: Write a program that accepts a list L as input. If the length of L is an even number, define 2
tuples T1 and T2 and copy the first half of L to T1 and 2nd half of L to T2. If the length of the list is
not even, print “I cannot break the list into half”.
23- Scenario: Write a program that accepts a list L as input which contains a list of student weights in
Kilograms and converts them into Pounds. Print the individual weights in pounds and calculate the
total weight of all student in both Kg and pounds.
Example: Input: [50 100 70 80], Output: [110 220 154 176], Total_in_pounds=660, Total_in_Kg= 300
24- Scenario: Write a program that accepts list L1 of length 4 that contains the names of people and list
L2 of the same length that contains the phone numbers of these people. Then create a dictionary of
this data with the name as keys and phone number as values. Then print out the dictionary.
A sample output: {‘Mo’ : 56789129, ‘Nick’ : 31553082, ‘Bob’ : 31553067, ‘Khaled’ : 33224455}
25- Scenario: Write a program to print the first 10 multipliers of a given number.
Example: Input: 3
Output:
1x3=3
2x3=6
3x3=9
4 x 3 = 12
5 x 3 = 15
6 x 3 = 18
7 x 3 = 21
8 x 3 = 24
9 x 3 = 27
10 x 3 = 30
28- Scenario: Write a program to check if a given number is an Armstrong number, i.e., when each of its
digits is raised to the power of the number of digits in the number, it equals itself. For example, 153=1**3
+ 5**33 + 3**3 , is an Armstrong number.
29- Scenario: Write a program to create a list of tuples having first element as the number and the second
element as the cube of the number.
30- Scenario: Given a list of numbers, the task is to write a python program to find the second largest
number in the given list.
32- Scenario: Write a function that accepts 2 lists, each of length 3. The first list elements are inputs
from the user, they all should be strings and represent names. The second list elements are inputs from the
user, they all should be strings and represent nicknames. The Function should return a list where each
element is the combination of the names and nicknames.
Example: Input: [“John”, “Mary”, “Bob”], [“nice”, “beautiful”, “brave”]. Output: [“John the nice”, “Mary
the beautiful”, “Bob the brave”]
33- Scenario: Write a program that calculates the efficiency of light bulb and then output its energy
rating.
The program asks the user to enter two values: The light bulb output (in lumens, lm) and the power
consumption of the light bulb over 1000hour (in kW/1000h). Then by dividing them, it finds out the
efficiency ratio and reports the rating according to table below.
Energy rating Efficiency ratio
A 210 or above
B 185 or above
C 160 or above
D 135 or above
E 110 or above
F 85 or above
G Below 85
Example: Input: light bulb output = 12000 lm, power consumption = 45 kW/1000h
Output: the efficiency ratio = 266.6, Energy Ratio = A
34- Scenario: Write a program which will accept an integer number “n” from the user and then creates
and prints a dictionary that contains (i, i**3) for all numbers between 1 and n.
Example: Input: 6, Output: {1:1, 2:8, 3:27, 4:64, 5:125, 6:216}