Unit
2
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:
- Operator
precedence: Some operators, such as multiplication and division, have a higher
precedence than others, such as addition and subtraction. This means that they
are evaluated first. - Parentheses:
Parentheses can be used to group parts of an expression together, and to
specify the order in which operations should be performed. - Data
types: Different data types, such as integers and floating-point numbers, may
behave differently when used with certain operators or functions. - Function
calls: If an expression includes a function call, the function will be
evaluated first, and its return value will be used in the rest of the
expression. - Variable
names: If an expression includes variables, their values will be looked up and
used in the expression. - Assignment
statements: If an expression includes an assignment statement, such as x = 5,
the value on the right-hand side of the equals sign will be evaluated first,
and then assigned to the variable on the left-hand side.
By
understanding these factors, you can write more complex expressions in Python
and ensure that they are evaluated correctly. It’s important to keep these
factors in mind when writing code to avoid errors and unexpected behavior.
Q4 What is range() function? Give and
example of range() function?
Solution:
In
Python, range() is a built-in function that generates a sequence of numbers
according to specified start, stop, and step values. The function returns an
object that represents the sequence of integers, which can be used in a for
loop or converted to a list.
Here
is an example of using the range() function to print the first five even
numbers:
for
i in range(0, 10, 2):
print(i)
In
this example, range(0, 10, 2) creates a sequence of even numbers from 0 to 8
(inclusive), with a step of 2. The for loop then iterates over this sequence
and prints each number to the console.
The
range() function can also take only one argument, in which case it generates a
sequence starting from 0 up to (but not including) the specified value. For
example, range(5) creates a sequence of numbers from 0 to 4.
It’s
important to note that the range() function itself returns a range 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 range object
to a list using the list() function.
Q5 Explain begin and end arguments
passed by range() function?
Solution:
In
Python, the range() function takes up to three arguments: start, stop, and
step. The start argument specifies the first value in the sequence, while the
stop argument specifies the last value in the sequence (which is not included).
The step argument specifies the amount by which the sequence should be
incremented between values (the default is 1 if step is not specified).
Here
is an example of using the range() function with all three arguments:
for
i in range(2, 10, 2):
print(i)
In
this example, the range() function generates a sequence of even numbers
starting from 2 and ending before 10 (i.e., [2, 4, 6, 8]), with a step of 2.
The for loop then iterates over this sequence and prints each number to the
console.
The
start and step arguments are both optional. If start is not specified, the
sequence starts at 0. If step is not specified, it defaults to 1. If only one
argument is passed to the range() function, it is assumed to be the stop
argument, and the sequence starts at 0 with a step of 1.
Additionally,
the range() function can take negative values for any of the arguments. In this
case, the sequence will count down from the start value to the stop value
(excluding the stop value) with a step of -1 * step. For example, range(10, 0,
-2) generates a sequence of even numbers from 10 down to 2 (i.e., [10, 8, 6, 4,
2]).
Q6 Define the term alternative
execution?
Solution:
In
programming, alternative execution refers to the ability of a program to
execute different blocks of code depending on certain conditions. Typically,
alternative execution involves using conditional statements like if, elif, and
else to direct the flow of the program based on the values of variables or
other data.
For
example, consider a program that prompts the user to enter their age and then
responds with a different message depending on the age entered:
age
= int(input(“Enter your age: “))
if
age < 18:
print(“You are not old enough to
vote.”)
elif
age < 21:
print(“You can vote but not
drink.”)
else:
print(“You can vote and drink!”)
In
this example, the program uses an if statement to check whether the age entered
by the user is less than 18. If so, it prints a message saying that the user is
not old enough to vote. If not, it moves on to the next elif statement, which
checks whether the age is less than 21. If so, it prints a message saying that
the user can vote but not drink. Finally, if neither of the previous conditions
is true (i.e., the age is 21 or older), the program executes the else block and
prints a message saying that the user can vote and drink.
This
is an example of alternative execution because the program can take different
paths depending on the value of the age variable. By using conditional
statements, the program can adapt to different scenarios and provide
appropriate responses based on the user’s input.
Q7 What do you mean by block?
Solution:
In
programming, a block refers to a group of one or more statements that are
treated as a single unit of code. Blocks are typically used to group related
statements together, such as the statements that make up a function, loop, or
conditional statement.
In
Python, blocks are defined by their indentation level. All statements that are
indented at the same level are considered to be part of the same block. For
example, consider the following code:
if x
< 0:
print(“x is negative”)
x = -x
print(“The
absolute value of x is”, x)
In
this code, the if statement and the two statements indented beneath it (i.e.,
print(“x is negative”) and x = -x) are all part of the same block.
The print statement that follows is not indented, so it is not part of the
block.
Blocks
can be nested within other blocks, allowing for complex logic and control flow.
For example:
if x
> 0:
print(“x is positive”)
if x > 10:
print(“x is greater than 10”)
else:
print(“x is less than or equal to
10”)
else:
print(“x is not positive”)
In
this code, there are two nested blocks: the if statement and its associated
block, and the else statement and its associated block. Each block is indented
one level deeper than the block that contains it. By using blocks to group
related statements together, you can make your code more organized, readable,
and maintainable.
Q8 What will be the output of the
following code
Str[0:4] if str =
“Hello”
Solution:
There
is a typo in the code as str is used as an identifier but not defined as a
variable, so assuming it is defined and assigned the value “Hello”,
the output of str[0:4] would be “Hell”. This is because the slice
notation [0:4] means “give me the characters from index 0 up to, but not
including, index 4”, so it will extract the first four characters of the
string “Hello”.
Q9 What are control statment?
Solution:
In
programming, control statements are used to control the flow of execution of a
program based on certain conditions or criteria. Control statements allow you
to make decisions, repeat code, or skip over certain parts of your code
depending on the situation.
There
are three types of control statements in Python:
- Conditional
statements: Conditional statements, such as if, elif, and else, are used to
execute certain code based on the evaluation of a condition. For example, you
might use a conditional statement to check if a number is positive or negative,
and execute different code based on the result. - Looping
statements: Looping statements, such as while and for, are used to repeat
certain code multiple times. For example, you might use a for loop to iterate
over a list of values and perform the same operation on each value. - Jump
statements: Jump statements, such as break, continue, and return, are used to
change the normal flow of execution. For example, you might use a break
statement to exit a loop early if a certain condition is met.
By
using control statements effectively, you can create more complex and powerful
programs that can handle a wide range of situations and input data.
Q10 Define the terms: header, suite and
clause?
Solution:
In
Python, a statement consists of a header and a suite. The header defines the
type of the statement and its syntax, while the suite contains the actual code
that the statement executes.
A
header typically includes a keyword that indicates the type of statement,
followed by one or more clauses that provide additional information or
parameters.
A
clause is a part of a statement that provides additional information or
modifies the behavior of the statement. For example, in an if statement, the
clause is the condition that the statement tests.
A
suite is a block of code that executes when a statement is executed. The suite
is indented under the header using whitespace, and it can contain one or more
statements. For example, in an if statement, the suite is the block of code
that executes if the condition is true.
Here
is an example of an if statement with a header, clause, and suite:
if x
> 0:
print(“x is positive”)
In
this example, the header is if x > 0:, the clause is x > 0, and the suite
is print(“x is positive”). The header and clause together form the
condition that the statement tests, and the suite is the code that executes if
the condition is true.
Q11 What is raw_input() function?
Solution:
raw_input()
was a built-in function in Python 2 that allowed a user to input data from the
console or command prompt. It would return the user’s input as a string.
However,
raw_input() has been removed in Python 3 and replaced by the input() function.
In Python 3, input() function behaves like raw_input() in Python 2, but returns
the user’s input as a string.
Here
is an example of using the input() function to prompt the user for their name
and age, and then printing out a message based on that input:
name
= input(“Enter your name: “)
age
= input(“Enter your age: “)
if
int(age) >= 18:
print(“Welcome, ” + name +
“! You are old enough to vote.”)
else:
print(“Sorry, ” + name + “.
You are not old enough to vote yet.”)
In
this example, the input() function is used to prompt the user for their name
and age. The int() function is used to convert the age input from a string to
an integer so that it can be compared to the value 18. Based on the user’s
input, the program prints out a message indicating whether or not the user is
old enough to vote.
Q 12 Does nested if-else are
allowed in Python?
Solution:
Yes,
nested if-else statements are allowed in Python. In fact, if-else statements
can be nested to any level.
A
nested if-else statement is simply an if-else statement that appears within
another if or else block. The inner if-else statement is executed only if the
condition of the outer if statement is true. Here is an example of a nested
if-else statement:
x =
10
y =
5
if x
> y:
if x > 0:
print(“x is positive”)
else:
print(“x is negative”)
else:
print(“x is less than or equal to
y”)
In
this example, the outer if statement tests if x is greater than y. If this
condition is true, the inner if-else statement is executed. If x is greater
than 0, the message “x is positive” is printed, otherwise the message
“x is negative” is printed. If the condition of the outer if
statement is false, the message “x is less than or equal to y” is
printed.
You may Like to Read this
PYTHON PROGRAMMING ALL STUDY MATERIAL || 99+ MOST IMPORTANT QUESTION WITH SOLUTION