Job Saarnee

# 2 Marks Question of Python Programming

Q1 Describe
the behavior of “range (s,e)” in python.[2020-2021]

Solution:

In Python, range(s, e) is a built-in function that
returns a sequence of numbers starting from s (inclusive) up to e (exclusive),
incremented by 1. It is commonly used to create a sequence of numbers that can
be used to iterate over in a loop.

Here is an example:

for i in range(0, 5):

print(i)

In this example, the range() function is called with
arguments 0 and 5, which creates a sequence of numbers starting from 0 and
ending at 4. The loop then iterates over this sequence and prints each number
to the console. The output of this code would be:

0

1

2

3

4

The range() function can also take a third argument,
which specifies the step size for the sequence. For example, range(0, 10, 2)
would create a sequence of even numbers from 0 to 8.

It’s important to note that the range() function
itself returns a sequence object, not a list. This can be useful for conserving
memory when working with large sequences, as only the elements needed at each
iteration are generated on the fly, rather than storing the entire sequence in
memory at once. However, if you need to use the entire sequence multiple times,
you can convert the sequence to a list using the list() function.

Q2 What
will be the output of following python code?

While
i<=3:

Print(i)

i+=
1

else:

print(0)

[2021-22]

Solution:

The
given code will print the numbers 1, 2, and 3, each on a separate line, and
then print 0 on a new line because the else block is associated with the while
loop and executes only when the loop terminates normally (i.e., without encountering
a break statement). The output of the code will be:

1

2

3

0

Note
that the variable i needs to be initialized before the while loop, otherwise a
NameError will be raised.

Q3 what are the factors for expression
evaluation?

Solution:

In
Python, expressions are evaluated based on the following factors, in order of
precedence: