Python
Python
Python
Q.2.) List the different Object- Oriented Features, supported by Python. (2 Marks)
Ans.)
Encapsulation,
Inheritance,
Polymorphism,
Abstraction.
Q.3.) Describe Python Interpreter. (2 Marks)
Ans.)
An interpreter is a kind of program that executes other programs. When you write Python
programs, it converts source code written by the developer into intermediate language
which is again translated into the native language / machine language that is executed.
It is basically the program that runs Python code.
It is a command-line program that allows you to execute Python commands and scripts
interactively or from a file. The interpreter reads and executes each line of code one by one,
interpreting the code and producing output as it goes. It is available on most operating
systems, including Windows, macOS, and Linux. It can be run from a command prompt or
terminal window, and it can be used interactively or to execute Python scripts.
The Python interpreter first reads the command, then evaluates the command, prints the
results, and then again loops it back to read the command and because of this only Python
is known as REPL i.e., (Read, Evaluate, Print, Loop).
Jupyter Notebook:-
Multiple- Line Comments:- Multi-line comments are used to explain multiple lines of code.
In Python, multi-line comments are enclosed in triple quotes (“””). They can span across
multiple lines and are typically used to provide documentation for functions or modules.
If we see, we use Strings as a Multiple- Line Comments. This is because, Strings will not get
stored, or displayed, until they are assigned to some variable. So, this is the reason for using
Triple Quotes as a Double- Line Comments.
For example:
“””
This is a multi-line comment.
It can span across multiple
lines. “””
print(“Hello, World!”)
Output:-
Jupyter Notebook:-
Q.5.) Explain Variables in Python, with it’s Rules and Conventions of it’s declaration. (2 Marks)
Ans.)
In Python, a variable is a reserved memory location used to store data values. Variables in
Python are dynamic, which means they don’t need to be declared with a specific data type
before they are assigned a value. The data type of the variable is determined automatically based
on the type of value assigned to it.
Rules for declaring variables in Python:
Variable names must begin with a letter or an underscore. They cannot begin with a number.
Variable names can contain letters, numbers, and underscores.
Variable names are case sensitive.
Variable names should be descriptive and meaningful.
Conventions for declaring variables in Python:
Use lowercase letters for variable names.
Use underscores to separate words in variable names. For example, “first_name” instead of
“firstName”.
Use camelCase notation for function names.
Avoid using reserved words as variable names. For example, “if” and “while” are
reserved words in Python.
Overall, the conventions for variable naming in Python are focused on readabilityOverall, the
conventions for variable naming in Python are focused on readability and clarity, making it
easier for programmers to understand the purpose and use of each variable in their code.
Q.6.) List the Features of Python. (2 Marks)
Ans.)
Q.2.) Mention the use of Floor Division (//), Exponential Operator (**), and Modulo Operator
(%). (2 Marks)
Ans.)
1.) Floor Division (//):
The floor division operator in Python is represented by //. It returns the integer quotient of
dividing the left operand by the right operand. Unlike the regular division operator /, the
result of floor division is always rounded down to the nearest integer. This operator is useful
when you want to find how many times one number fits into another without considering the
remainder.
Example:
10 // 3 # Output: 3
2.) Exponential Operator (**):
The exponential operator in Python is represented by **. It raises the left operand to the
power of the right operand. This operator is useful when you want to perform calculations
involving exponents.
Example:
2 ** 3 # Output: 8
3.) Modulo Operator (%):
The modulo operator in Python is represented by %. It returns the remainder of dividing the
left operand by the right operand. This operator is useful when you want to perform
operations that involve finding the remainder of a division.
Example:
10 % 3 # Output: 1
3.) Greater Than Operator (>): It is used to check whether the first value is greater than the
second value. If the first value is greater than the second value, it returns True, otherwise
False.
Example:
x=5
y = 10
print (y > x) # Output: True
z=5
print (y > z) # Output: True
4.) Less Than Operator (<): It is used to check whether the first value is less than the second
value. If the first value is less than the second value, it returns True, otherwise False.
Example:
x=5
y = 10
print (x < y) # Output: True
z=5
print (z < y) # Output: True
5.) Greater Than or Equal To Operator (>=): It is used to check whether the first value is
greater than or equal to the second value. If the first value is greater than or equal to the
second value, it returns True, otherwise False.
Example:
x=5
y = 10
print(y >= x) # Output: True
z=5
print(x >= z) # Output: True
6.) Less Than or Equal To Operator (<=): It is used to check whether the first value is less
than or equal to the second value. If the first value is less than or equal to the second value, it
returns True, otherwise False.
Example:
x=5
y = 10
print(x <= y) # Output: True
z=5
print(z <= x) # Output: True
Q.4.) Explain Building Blocks of Python. (4
Marks) Ans.)
In Python, the basic building blocks are:
1.) Variables:
These are containers that hold values, such as numbers or text strings. In Python, you
don't need to specify the type of a variable; the type is inferred based on the value it holds.
2.) Data Types:
Python has several built-in data types, including integers, floating-point numbers, strings,
lists, tuples, and dictionaries. Each data type has its own set of methods and functions
that you can use to manipulate the data.
3.) Operators:
These are symbols or words that perform operations on values or variables. Python has
arithmetic operators (such as +, -, *, and /), comparison operators (such as ==, !=, <, and >),
and logical operators (such as and, or, and not).
4.) Control Structures:
These are constructs that allow you to control the flow of your program. Python has if/else
statements, for and while loops, and try/except blocks for error handling.
5.) Functions: These are reusable blocks of code that perform a specific task. You can define
your own functions in Python or use built-in functions from the standard library.
6.) Modules:
These are files that contain Python code that you can import into your own programs.
Python has a large standard library of modules for tasks such as file I/O, networking, and
web development.
7.) Classes and Objects:
These are constructs for creating own data types and functions that operate on them.
Python is an object-oriented language, so everything in Python is an object.
Output:
Yes, banana is in the fruit list.
Yes, pear is not in the fruit
list.
Assignment Operators:- Assignment operators are used to assign values to variables. These
operators are a combination of the equal sign (=) and another arithmetic or bitwise operator. The
resulting value is then assigned to the variable on the left-hand side of the operator.
Here are some examples of assignment operators in Python:
1.) Addition Assignment Operator (+=): The addition assignment operator adds the value on
the right-hand side to the value on the left-hand side and assigns the result to the left-hand
side variable.
Example:-
x=5
x += 3 # equivalent to x = x + 3
print(x) # Output: 8
2.) Subtraction Assignment Operator (-=): The subtraction assignment operator subtracts the
value on the right-hand side from the value on the left-hand side and assigns the result to the
left-hand side variable.
Example:-
x = 10
x -= 4 # equivalent to x = x - 4
print(x) # Output: 6
3.) Multiplication Assignment Operator (*=): The multiplication assignment operator
multiplies the value on the left-hand side by the value on the right-hand side and assigns the
result to the left-hand side variable.
Example:-
x=2
x *= 5 # equivalent to x = x * 5
print(x) # Output: 10
4.) Division Assignment Operator (/=): The division assignment operator divides the value on
the left-hand side by the value on the right-hand side and assigns the result to the left-hand
side variable.
Example:-
x = 20
x /= 4 # equivalent to x = x / 4
print(x) # Output: 5.0
5.) Modulus Assignment Operator (%=): The modulus assignment operator finds the
remainder of the division of the value on the left-hand side by the value on the right-hand
side and assigns the result to the left-hand side variable.
Example:-
x = 17
x %= 5 # equivalent to x = x % 5
print(x) # Output: 2
6.) Exponentiation Assignment Operator (**=): The exponentiation assignment operator
raises the value on the left-hand side to the power of the value on the right-hand side
and assigns the result to the left-hand side variable.
Example:-
x=3
x **= 4 # equivalent to x = x ** 4
print(x) # Output: 81
7.) Floor Division Assignment Operator (//=): The floor division assignment operator divides
the value on the left-hand side by the value on the right-hand side and returns the floor of
the quotient. The result is then assigned to the left-hand side variable.
Example:-
x = 23
x //= 4 # equivalent to x = x // 4
print(x) # Output: 5
These assignment operators are useful for reducing the amount of code needed to perform
common operations on variables.
This program first takes the input of the number of terms 'n' from the user. It then initializes the
first two terms of the sequence as 'a' and 'b', which are 0 and 1 respectively. Then it checks if the
input value of 'n' is positive or not, and if it's equal to or greater than 1.
Finally, it prints the Fibonacci sequence by iterating through the range of 'n' terms, and updating
the values of 'a' and 'b' at each iteration.
Q.9.) Explain different Loops available in Python, with suitable Example. (4 Marks)
Ans.)
In Python, loops are used to execute a block of code repeatedly until a specific condition is met.
There are two main types of loops in Python: for and while.
Q.10.) List the Loop Manipulation Statements in Python, and explain any 2 in detail. (4 Marks)
Ans.)
In Python, loop manipulation statements are used to control the execution of loops. There are
three main loop manipulation statements in Python:
1.) Break:- The break statement is used to exit a loop prematurely. When executed inside a
loop, it immediately terminates the loop and control flows to the statement following
the loop. For Example:
for i in range(1, 11):
if i == 6:
break
print(i)
Output:
12 3 4 5
2.) Continue
The continue statement is used to skip the current iteration of a loop and move on to the next
iteration. For Example:
for i in range(1, 11):
if i == 6:
continue
print(i)
Output:
1 2 3 4 5 7 8 9 10
3.) Pass
The pass statement is used as a placeholder when a statement is required syntactically but
you don’t want to execute any code. For Example:
for i in range(1, 11):
if i == 6:
pass
else:
print(i)
Output:
1 2 3 4 5 7 8 9 10
Note:- In this example, the pass statement does nothing, but it’s required syntactically to
create a valid if/else block.