0% found this document useful (0 votes)
5 views10 pages

Python Code Output Predictions

Uploaded by

jyotirmoy286
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)
5 views10 pages

Python Code Output Predictions

Uploaded by

jyotirmoy286
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

Prepared By: Jayatu Bhowmick

FIND THE OUTPUT

1. What will be the output of the following code?


def add_numbers(a, b=5):
return a + b

print(add_numbers(10))
print(add_numbers(10, 20))

2. Predict the output:


x = [1, 2, 3]
y = x
[Link](4)
print(y)

3. Output of the following code:


a = [1, 2, 3, 4]
b = a[:]
a[0] = 100
print(b)

4. What will be printed?


text = "HELLO world"
print([Link]())
print([Link]())

5. Analyze and provide the output:


numbers = [1, 2, 3, 4, 5]
print(numbers[1:4])

6. Output of this code:


a = [1, 2, 3]
b = [4, 5, 6]
c = a + b
print(c)

7. Predict the output:


for i in range(3):
print(i, end=' ')

8. What will be printed?


text = "Python"
print(text[::-1])

9. Find the output:


def factorial(n):
if n == 0:
return 1
else:

Page | 1
Prepared By: Jayatu Bhowmick

return n * factorial(n - 1)

print(factorial(3))

[Link] of the following code:


a = [1, 2, 3, 4, 5]
print(a[-1])
print(a[-3])

[Link] will be printed?


a = [1, 2, 3]
[Link](1, 10)
print(a)

[Link] the output:


text = "computer"
print(len(text))

[Link] of this code:


a = [1, 2, 3, 4]
print([Link]())
print(a)

[Link] will be printed?


text = "Python Programming"
print([Link]('o'))

[Link] and provide the output:


text = "Computer"
print(text[::2])

[Link] will be the output of the following code?


def power(x, y=2):
return x ** y

print(power(3))
print(power(3, 3))

[Link] the output:


a = [1, 2, 3]
b = a
a += [4]
print(b)

[Link] of the following code:


text = "Data Science"
print([Link]('S'))
print([Link]('s'))

Page | 2
Prepared By: Jayatu Bhowmick

[Link] will be printed?


lst = [1, 2, 3, 4, 5]
print(lst[::3])

[Link] and provide the output:


a = [1, 2, 3, 4]
print(sum(a))

[Link] of this code:


def greet(name):
return "Hello " + name

print(greet("Alice"))

[Link] the output:


text = "hello"
print([Link]())

[Link] will be printed?


a = [1, 2, 3]
[Link](2)
print(a)

[Link] the output:


a = [1, 2, 3]
[Link]([4, 5])
print(a)

[Link] of the following code:


text = "abcde"
print(text[1:4])

[Link] will be printed?


a = [1, 2, 3]
b = a * 2
print(b)

[Link] the output:


text = "Python"
print([Link]('P'))
print([Link]('n'))

[Link] of this code:


a = [1, 2, 3, 4]
print([Link](3))

[Link] will be printed?


text = "Data Science"
print([Link]('Data', 'Big Data'))

Page | 3
Prepared By: Jayatu Bhowmick

[Link] and provide the output:


a = [5, 3, 1, 4, 2]
[Link]()
print(a)

[Link] will be the output of the following code?


a = [1, 2, 3]
b = [Link]()
[Link](4)
print(b)

[Link] the output:


text = "Python Programming"
print([Link]('P'))

[Link] of the following code:


def func(x):
return x[::-1]

print(func([1, 2, 3, 4]))

[Link] will be printed?


a = [1, 2, 3, 4, 5]
print(a[2:])

[Link] and provide the output:


a = [10, 20, 30, 40]
print(a[::2])

[Link] of this code:


text = "python"
print([Link]())
print([Link]())

[Link] the output:


a = [1, 2, 3, 4]
print(len(a))

[Link] will be printed?


a = [1, 2, 3]
[Link]([4, 5])
print(a)

[Link] the output:


def reverse_string(s):
return s[::-1]

print(reverse_string("Data"))

Page | 4
Prepared By: Jayatu Bhowmick

[Link] of the following code:


a = [1, 2, 3]
[Link]()
print(a)

[Link] will be printed?


text = "Hello World"
print([Link]())

[Link] the output:


a = [1, 2, 3, 4]
print(a[1:3])

[Link] of this code:


text = "Computer"
print([Link]('p'))

[Link] will be printed?


a = [1, 2, 3]
print(a + [4, 5])

[Link] and provide the output:


a = [3, 1, 4, 1, 5]
print([Link](1))

[Link] will be the output of the following code?


a = [1, 2, 3]
print([Link](2))

[Link] the output:


text = "Data Science"
print([Link]())
print([Link]())

[Link] of the following code:


a = [1, 2, 3, 4, 5]
print(a[:3])

[Link] will be printed?


text = "Python"
print(text[0])
print(text[-1])

[Link] and provide the output:


a = [1, 2, 3]
[Link](4)
print(a)

Page | 5
Prepared By: Jayatu Bhowmick

[Link] of this code:


text = "Hello"
print([Link]())
print([Link]())

[Link] the output:


a = [1, 2, 3]
a[1] = 100
print(a)

[Link] will be printed?


text = "python programming"
print([Link]())

[Link] the output:


def sum_elements(lst):
return sum(lst)

print(sum_elements([1, 2, 3, 4]))

[Link] of the following code:


a = [1, 2, 3, 4, 5]
print(a[::-1])

[Link] will be printed?


a = [1, 2, 3]
[Link](2)
print(a)

[Link] the output:


text = "Data Science"
print([Link]('S'))
print([Link]('s'))

[Link] of this code:


a = [5, 4, 3, 2, 1]
[Link]()
print(a)

[Link] will be printed?


text = "Python"
print([Link]())
print([Link]())

[Link] and provide the output:


a = [1, 2, 3]
[Link]([4, 5])
print(a)

Page | 6
Prepared By: Jayatu Bhowmick

[Link] will be the output of the following code?


text = "Data Science"
print([Link]('Data', 'Big Data'))

[Link] the output:


a = [1, 2, 3, 4]
print(a[::2])

[Link] of the following code:


a = [1, 2, 3]
print(a * 3)

[Link] will be printed?


text = "Python Programming"
print([Link]('Pro'))

[Link] and provide the output:


a = [1, 2, 3]
b = [Link]()
[Link](4)
print(b)

[Link] of this code:


a = [1, 2, 3, 4]
print([Link](1))
print(a)

[Link] the output:


text = "PYTHON"
print([Link]())

[Link] will be printed?


text = "Python"
print([Link]())

[Link] the output:


a = [1, 2, 3]
[Link](1, 10)
print(a)

[Link] of the following code:


a = [1, 2, 3, 4, 5]
print(a[1:-1])

[Link] will be printed?


text = "Computer Science"
print([Link]('e'))

Page | 7
Prepared By: Jayatu Bhowmick

[Link] the output:


a = [1, 2, 3]
[Link]([4, 5])
print(a[3])

[Link] of this code:


text = "Data"
print(text * 3)

[Link] will be printed?


a = [5, 4, 3, 2, 1]
[Link](reverse=True)
print(a)

[Link] and provide the output:


text = "Python Programming"
print([Link]('o', '0'))

[Link] will be the output of the following code?


a = [1, 2, 3, 4]
print(a[:2])

[Link] the output:


text = "Python"
print(text[1:5:2])

[Link] of the following code:


a = [1, 2, 3]
b = [4, 5]
print(a + b)

[Link] will be printed?


a = [1, 2, 3, 4]
[Link](3)
print(a)

[Link] and provide the output:


text = "Hello World"
print([Link]())

[Link] of this code:


a = [10, 20, 30, 40]
print([Link](30))

[Link] the output:


a = [1, 2, 3]
a[1] = a[1] + 5
print(a)

Page | 8
Prepared By: Jayatu Bhowmick

[Link] will be printed?


text = "Data Science"
print([Link]())

[Link] the output:


a = [1, 2, 3, 4]
print(a[::-1])

[Link] of the following code:


text = "Python"
print([Link]())

[Link] will be printed?


a = [1, 2, 3, 4]
print(len(a))

[Link] the output:


text = "Python"
print(text[2:])

[Link] of this code:


a = [1, 2, 3, 4, 5]
print(a[::2])

[Link] will be printed?


text = "Java Programming"
print([Link]('Java', 'Python'))

[Link] and provide the output:


a = [3, 1, 4, 2]
[Link]()
print(a)

[Link] will be the output of the following code?


a = [1, 2, 3]
b = [Link]()
a += [4, 5]
print(b)

[Link] the output:


text = "Hello World"
print([Link]('H'))
print([Link]('d'))

[Link] of the following code:


a = [1, 2, 3]
[Link](2, 10)
print(a)

Page | 9
Prepared By: Jayatu Bhowmick

[Link] will be printed?


text = "Data Science"
print([Link]('S'))

[Link] and provide the output:


a = [5, 4, 3, 2, 1]
[Link]()
print(a)

[Link] of this code:


text = "Python"
print([Link]())
print([Link]())

[Link] the output:


a = [1, 2, 3, 4]
print(sum(a))

[Link] will be printed?


text = "Data"
print(text * 4)

[Link] the output:


a = [1, 2, 3]
[Link]([4, 5])
print(a)

100. Output of the following code:


text = "Computer"
print(text[::2])

Page | 10

Common questions

Powered by AI

The output will be [1, 2, 3, 4]. In Python, lists are mutable, and 'y = x' creates a reference to the same list object in memory. Therefore, any modifications to 'x' will also reflect in 'y', resulting in both lists showing the appended item .

The output will be [1, 2, 3, 4, 5, 6]. The '+' operator concatenates lists in Python, creating a new list by joining elements of 'a' and 'b' in order. This operation does not alter the original lists, making 'c' a result of the concatenation .

The output will be [2, 3, 4]. The slicing operation 'lst[1:4]' extracts a sublist starting from index 1 up to, but not including, index 4. This type of slicing is useful for extracting contiguous parts of a list .

The output will be [5, 4, 3, 2, 1]. Reversing a list using the slicing operator 'a[::-1]' creates a reversed copy of the list, which is efficient as it does not involve sorting algorithms or in-place modifications. Unlike sorting, which reorders elements based on criteria, reversing simply rearranges the order without altering data relationships .

The output will be [1, 2, 3]. Using 'b = a.copy()' creates a separate copy of 'a', unlike a direct assignment which would reference the same object. Therefore, changes to 'a', such as appending an element, do not affect 'b' .

The output will be "hello world" followed by "HELLO WORLD". The 'lower()' method converts all characters in the string to lowercase, while the 'upper()' method converts all characters to uppercase. These transformations are useful for case-insensitive comparisons and data normalization .

The output will be [1, 2, 3, 4]. Slicing a list in Python creates a shallow copy of the list. Changes made to the original list 'a' do not affect 'b' since 'b' is a separate copy, although derived from the same initial elements .

The output will be 6. The recursive function 'factorial' calculates the factorial of a given number 'n'. When factorial(3) is called, it computes 3 * factorial(2) -> 2 * factorial(1) -> 1 * factorial(0), which returns 1. Therefore, the calculation resolves to 3 * 2 * 1 * 1 = 6 .

The output will be [1, 2, 3, 4, 5]. The 'sort()' method sorts the list 'a' in ascending order in place, meaning it alters the original list structure .

The output will be 4 followed by [1, 2, 3]. The 'pop()' method removes and returns the last element of the list if no index is specified. After this operation, the list 'a' is shortened by one element at its end .

You might also like