Computer Applications
(a) What will be the output produced by following code fragments ?
(i)
a, b = 2, 3
c, b = a, c + 1
print a, b, c
(ii)
x, y = 2, 6
x, y = y, x + 2
print x, y
(b) What will the output produced if we interchange the first two lines of above two codes [given in part (a)], e.g., as shown below for code (i) ? Will it give any error ? Give reason(s).
c, b = a, c + 1
a, b = 2, 3
print a, b, c
Python Funda
11 Likes
Answer
(a)
(i) This code fragment will generate the below error:
NameError: name 'c' is not defined
Reason — The error is generated because in line 2, the variable c is not defined before being used.
(ii)
Output
6 4
Explanation
Statement | Value of X | Value of Y | Remark |
---|---|---|---|
x, y = 2, 6 | 2 | 6 | Both x, y are initialized |
x, y = y, x + 2 | 6 | 4 | x = 6, y = 2 + 2 = 4 |
print (x, y) | 6 | 4 | 6 (x), 4 (y) are printed |
(b) When we interchange the first two lines of above codes, we get the following outputs:
(i) It will give the same error as before because even after interchanging first two lines variable C is still used before it is defined.
(ii) Interchanging the first two lines will generate the below error:
NameError: name 'y' is not defined
Reason — The error is generated because now in line 1, the variable y is not defined before being used.
Answered By
3 Likes
Related Questions
Write a program to read a number n and print n2, n3 and n4.
Write a program to compute simple interest and compound interest.
Write a program to read details like name, class, age of a student and then print the details firstly in same line and then in separate lines. Make sure to have two blank lines in these two different types of prints.
A bank account contains ₹10,000 and earns interest at the rate of 5 percent per year. The interest is added to the account. After one year the bank account contains the original sum of ₹10,000 plus the interest, which is 5% of ₹10,000, i.e., 10,000 * (5/100).
So, the final account balance is 10000 + 10000 * (5/100) = 10000 * (1 + (5/100)) = 10000 * 1.05.
(a) Write Python code script to calculate the balance in account as per this after one year.
(b) Write Python code script to calculate the balance in account as per this after four years while showing balance after every year.
(c) Write Python script that displays the number of takes for the account balance to be at least double the original balance.