Computer Science
Predict the output of the following code:
line = [4, 9, 12, 6, 20]
for I in line:
for j in range(1, I%5):
print(j, '#', end="")
print()
Answer
1 #2 #3 #
1 #2 #3 #
1 #
Working
The list
line
contains the values [4, 9, 12, 6, 20].The outer loop iterates over each element (
I
) in theline
list.First Iteration (I = 4):
I % 5 = 4 % 5 = 4
Inner Loop Range:range(1, 4)
→ This generates the numbers 1, 2, 3.
Output:1 #2 #3 #
Second Iteration (I = 9):
I % 5 = 9 % 5 = 4
Inner Loop Range:range(1, 4)
→ This generates the numbers 1, 2, 3.
Output:1 #2 #3 #
Third Iteration (I = 12):
I % 5 = 12 % 5 = 2
Inner Loop Range:range(1, 2)
→ This generates the number 1.
Output:1 #
Fourth Iteration (I = 6):
I % 5 = 6 % 5 = 1
Inner Loop Range:range(1, 1)
→ This generates no numbers (empty range).
Print: No output for this iteration (just a new line is printed).Fifth Iteration (I = 20):
I % 5 = 20 % 5 = 0
Inner Loop Range:range(1, 0)
→ This generates no numbers (empty range).
Print: No output for this iteration (just a new line is printed).
The final output of the code will be:
1 #2 #3 #
1 #2 #3 #
1 #
Related Questions
Write the definition of a user-defined function
push_even(N)
which accepts a list of integers in a parameterN
and pushes all those integers which are even from the listN
into a Stack namedEvenNumbers
.Write function pop_even() to pop the topmost number from the stack and return it. If the stack is already empty, the function should display "Empty".
Write function Disp_even() to display all element of the stack without deleting them. If the stack is empty, the function should display 'None'.
For example: If the integers input into the list
VALUES
are: [10, 5, 8, 3, 12]Then the stack
EvenNumbers
should store: [10, 8, 12]Predict the output of the following code:
d = {"apple": 15, "banana": 7, "cherry": 9} str1 = "" for key in d: str1 = str1 + str(d[key]) + "@" + "\n" str2 = str1[:-1] print(str2)
Consider the table ORDERS as given below
O_Id C_Name Product Quantity Price 1001 Jitendra Laptop 1 12000 1002 Mustafa Smartphone 2 10000 1003 Dhwani Headphone 1 1500 Note: The table contains many more records than shown here.
A) Write the following queries:
(I) To display the total Quantity for each Product, excluding Products with total Quantity less than 5.
(II) To display the orders table sorted by total price in descending order.
(III) To display the distinct customer names from the Orders table.
(IV) Display the sum of Price of all the orders for which the quantity is null.
OR
B) Write the output
(I)
Select c_name, sum(quantity) as total_quantity from orders group by c_name;
(II)
Select * from orders where product like '%phone%';
(III)
Select o_id, c_name, product, quantity, price from orders where price between 1500 and 12000;
(IV)
Select max(price) from orders;
A csv file "Happiness.csv" contains the data of a survey. Each record of the file contains the following data:
- Name of a country
- Population of the country
- Sample Size (Number of persons who participated in the survey in that country)
- Happy (Number of persons who accepted that they were Happy)
For example, a sample record of the file may be:
[‘Signiland’, 5673000, 5000, 3426]
Write the following Python functions to perform the specified operations on this file:
(I) Read all the data from the file in the form of a list and display all those records for which the population is more than 5000000.
(II) Count the number of records in the file.