0% found this document useful (0 votes)
13 views21 pages

Python Unit1 QB

The document is a comprehensive question bank covering various aspects of Python programming, including membership and identity operators, indentation, variables, applications, and features of Python. It also discusses conditional statements, loops, and types of operators, providing examples and explanations for each topic. Additionally, it lists Python IDEs and the history of Python's development.

Uploaded by

gadesairaj0
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)
13 views21 pages

Python Unit1 QB

The document is a comprehensive question bank covering various aspects of Python programming, including membership and identity operators, indentation, variables, applications, and features of Python. It also discusses conditional statements, loops, and types of operators, providing examples and explanations for each topic. Additionally, it lists Python IDEs and the history of Python's development.

Uploaded by

gadesairaj0
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
You are on page 1/ 21

Unit 1 Question Bank

Q.1 Give membership operators in python


Ans: Python Membership Operators:

Membership operators are used to test if a sequence is presented in an object

 in :
Returns True if a sequence with the specified value is present in the
object

Example:
x = ["apple", "banana"]

print("banana" in x)

output:
True

 not in:
Returns True if a sequence with the specified value is not present in the
object

Example:
x = ["apple", "banana"]

print("pineapple" not in x)

output:
True

Q.2 Explain with example:


i) Indentation
ii) Variables
Ans: i) Indentation:
In Python, indentation is used to define blocks of code. It tells the Python
interpreter that a group of statements belongs to a specific block. All
statements with the same level of indentation are considered part of the
same block. Indentation is achieved using whitespace (spaces or tabs) at
the beginning of each line.

Example:

if 10 > 5:
print("This is true!")
print("I am tab indentation")

print("I have no indentation")

Output:

This is true!
I am tab indentation
I have no indentation

ii) Variables:
Variables are containers for storing data values.
Python variables are the reserved memory locations used to store values
with in a Python Program. This means that when you create a variable
you reserve some space in the memory.

Example:
x=5
y = "John"
print(x)
print(y)

Output:
5
John

Q.3 Enlist applications for python programming


Ans:
 1. Web Development
 2. Machine Learning and Artificial Intelligence
 3. Data Science
 4. Game Development
 5. Audio and Visual Applications
 6. Software Development
 7. CAD Applications
 8. Business Applications
 9. Desktop GUI
 10. Web Scraping Application

Q.4 Write the use of elif keyword in python.


Ans:
In Python, The elif keyword is used as one of the conditional statements,
and it stands for else if. When we have a set of conditional statements to
check, we use elif. This keyword must be used along with an if statement,
or else it will result in an error.

Example:
a = 33
b = 33
if b > a:
print("b is greater than a")
elif a == b:
print("a and b are equal")

Output:
a and b are equal

Q.5 Describe the Role of indentation in python


Ans: In Python, indentation is used to define blocks of code. It tells
the Python interpreter that a group of statements belongs to a specific
block. All statements with the same level of indentation are considered part
of the same block.

Q.6 Explain Identity operators in Python.


Ans:
i)
is : Returns true if both variables are the same
object

Example:
x = ["apple", "banana"]
y = ["apple", "banana"]
z=x

print(x is z)

print(x is y)

print(x == y)

Output:
True
False
True

ii)

is not : Returns true if both variables are not the same object
Example:
x = ["apple", "banana"]
y = ["apple", "banana"]
z=x

print(x is not z)

print(x is not y)

print(x != y)

Output:
False
True
False

Q.7 Explain building blocks of python.


Ans: i) Indentation: In Python, indentation is used to define blocks of code. It
tells the Python interpreter that a group of statements belongs to a specific block.
All statements with the same level of indentation are considered part of the same
block. Indentation is achieved using whitespace (spaces or tabs) at the beginning
of each line.

ii) Identifiers:
Identifiers in Python
Identifier is a user-defined name given to a variable, function, class, module, etc.
The identifier is a combination of character digits and an underscore. They are
case-sensitive i.e., ‘num’ and ‘Num’ and ‘NUM’ are three different identifiers in
python. It is a good programming practice to give meaningful names to identifiers
to make the code understandable.

iii) Variable: Variables are containers for storing data values.


A variable is created the moment you first assign a value to it.
Variables do not need to be declared with any particular type, and can even change
type after they have been set.

iv) Comments: Comments in Python are the lines in the code that are ignored by
the interpreter during the execution of the program.

Comments can be used to explain Python code.

Comments can be used to make the code more readable.

Comments can be used to prevent execution when testing code.

Comments starts with a #, and Python will ignore them

v) Keywords: Keywords in Python are reserved words that have special meanings
and serve specific purposes in the language syntax. Python keywords cannot be
used as the names of variables, functions, and classes or any other identifier.

Q.8 List features of Python.


Ans: Features in Python
In this section we will see what are the features of Python programming language:
1. Free and Open Source
Python language is freely available at the official website and you can download
it from the given download link below click on the Download
Python keyword. Download Python Since it is open-source, this means that
source code is also available to the public. So you can download it, use it as well
as share it.
2. Easy to code
Python is a high-level programming language. Python is very easy to learn the
language as compared to other languages like C, C#, Javascript, Java, etc. It is
very easy to code in the Python language and anybody can learn Python basics in
a few hours or days. It is also a developer-friendly language.

3. Easy to Read
As you will see, learning Python is quite simple. As was already established,
Python’s syntax is really straightforward. The code block is defined by the
indentations rather than by semicolons or brackets.

4. Object-Oriented Language
One of the key features of Python is Object-Oriented programming. Python
supports object-oriented language and concepts of classes, object encapsulation,
etc.

5. GUI Programming Support


Graphical User interfaces can be made using a module such as PyQt5, PyQt4,
wxPython, or Tk in Python. PyQt5 is the most popular option for creating
graphical apps with Python.

6. High-Level Language
Python is a high-level language. When we write programs in Python, we do not
need to remember the system architecture, nor do we need to manage the
memory.

7. Large Community Support


Python has gained popularity over the years. Our questions are constantly
answered by the enormous StackOverflow community. These websites have
already provided answers to many questions about Python, so Python users can
consult them as needed.

8. Easy to Debug
Excellent information for mistake tracing. You will be able to quickly identify
and correct the majority of your program’s issues once you understand how
to interpret Python’s error traces. Simply by glancing at the code, you can
determine what it is designed to perform.
9. Python is a Portable language
Python language is also a portable language. For example, if we have Python
code for Windows and if we want to run this code on other platforms such
as Linux, Unix, and Mac then we do not need to change it, we can run this code
on any platform.

10. Python is an Integrated language


Python is also an Integrated language because we can easily integrate Python
with other languages like C, C++, etc.

11. Interpreted Language:


Python is an Interpreted Language because Python code is executed line by line
at a time. like other languages C, C++, Java, etc. there is no need to compile
Python code this makes it easier to debug our code. The source code of Python is
converted into an immediate form called bytecode.

12. Large Standard Library


Python has a large standard library that provides a rich set of modules and
functions so you do not have to write your own code for every single thing. There
are many libraries present in Python such as regular expressions, unit-testing,
web browsers, etc.

Q.9 Describe Loop manipulation statements


Ans: break Statement:
The break statement is used to exit the loop prematurely when a certain condition
is met.

Example:
# Using break to exit the loop
for i in range(10):
if i == 5:
break
print(i)
output
0
1
2
3
4
continue Statement:
The continue statement skips the current iteration and proceeds to the next
iteration of the loop.

Example:
# Using continue to skip an iteration
for i in range(10):
if i % 2 == 0:
continue
print(i)
Output:
1
3
5
7
9
pass Statement:
The pass statement is a null operation; it does nothing when executed. It’s useful
as a placeholder for code that you plan to write in the future.

Example:
# Using pass as a placeholder
for i in range(5):
if i == 3:
pass
print(i)
output:
0
1
2
3
4
Q.10 Explain Following Terms i) input() ii) print()
Ans: i) input():Python input() function is used to take user input. By default, it
returns the user input in form of a string.
Example:
name = input("Enter your name: ")
print("Hello,", name, "! Welcome!")
Output:
Enter your name: GeeksforGeeks
Hello, GeeksforGeeks ! Welcome!

ii)Print():

The print() function prints the specified message to the screen, or other standard
output device.

The message can be a string, or any other object, the object will be converted into a
string before written to the screen.
Example:
print("Hello", "how are you?")
output:
Hello , how are you?

Q.11.Describe History of Python

Ans:Python was developed by Guido van Rossum (a Dutch programmer) in the


late 1980s and early nineties at the National Research Institute for Mathematics
and Computer Science in the Netherlands.

Python is derived from many other languages, including ABC, Modula-3, C, C++,
Algol-68, SmallTalk, and Unix shell and other scripting languages. Guido van
Rossum wanted Python to be a high-level language that was powerful yet readable
and easy to use.

Q.12 List python IDE’S


Ans:
An IDE (Integrated Development Environment) is software that combines
commonly used developer tools into a compact GUI (graphical user interface)
application. It is a combination of tools like a code editor, code compiler,
and code debugger with an integrated terminal
 Top Python IDEs for Windows
o 1. PyCharm
o 2. Visual Studio Code
o 3. Spyder
o 4. Anaconda
o 5. Thonny
Q.13 List comparison operator in python
a) Ans: List comparision operators in Python.
Ans: Comparison operators in Python are
Operator Operator Meaning
== Equal to
!= Not Equal to
< Less than
> Greater than
<= Less than and Equal to
>= Greater than and Equal to

Q.14 Describe bitwise operators in Python with example.


Ans Bitwise operators acts on bits and performs bit by bit operation. Assume
a=10 (1010) and b=4 (0100)
Operator Meaning Description Example
& Binary AND This a &b =
operation 1010 &
performs 0100 =
AND 0000 =0
operation
between
operands.
Operator
copies a
bit, to the
result, if it
exists in
both
operands
| Binary OR This a|b =
operation 1010 |
performs 0100 =
OR 1110 = 14
operation
between
operands.
It copies a
bit, if it
exists in
either
operand.
^ Binary XOR This a^b=1010
operation ^ 0100 =
performs 1110 =14
XOR
operations
between
operands.
It copies
the bit, if it
is set in
one
operand
but not
both.
~ Binary Ones It is unary ~a= ~
Complement operator 1010 =
and has 0101
the effect
of 'flipping'
bits i.e.
opposite
the bits of
operand
<< Binary Left The left 1010 << 2
shift operand's =101000
value is = 40
moved left
by the
number of
bits
specified
by the
right
operand.
>> Binary Right The left a>>2 =
Shift operand's 1010 >> 2
value is =0010 = 2
moved
right by
the
number of
bits
specified
by the
right
operand.

Q15. Enlist the types of operators in python

Ans: Python divides the operators in the following groups:

 Arithmetic operators
 Assignment operators
 Comparison operators
 Logical operators
 Identity operators
 Membership operators
 Bitwise operators

Q16. Explain following operators


i) Arithmetic operators
ii) Logical operators
iii) Assignment operators
Ans:
i) Arithmetic operators are used with numeric values to perform common
mathematical operations
ii) Logical operators:
Logical operators are used to combine conditional statements:
iii) Assignment operators are used to assign values to variables

Q17.Explain conditional statements


Ans:
 if Statement in Python:
If the simple code of block is to be performed if the condition holds true then
the if statement is used. Here the condition mentioned holds then the code of the
block runs otherwise not.

Example:

if 10 > 5:

print("10 greater than 5")

print("Program ended")

output:
10 greater than 5
Program ended

 if else Statement in Python


In conditional if Statement the additional block of code is merged as else
statement which is performed when if condition is false.

Example:

x=3

if x == 4:

print("Yes")

else:

print("No")

output:
No
 Nested if Statement
if statement can also be checked inside other if statement. This conditional
statement is called a nested if statement. This means that inner if condition will
be checked only if outer if condition is true and by this, we can see multiple
conditions to be satisfied.

Example:

num = 10

if num > 5:

print("Bigger than 5")

if num <= 15:

print("Between 5 and 15")

output:

Bigger than 5
Between 5 and 15

 if-elif Statement in Python


The if-elif statement is shortcut of if..else chain. While using if-elif statement at
the end else block is added which is performed if none of the above if-elif
statement is true.

Example:

letter = "A"

if letter == "B":

print("letter is B")

elif letter == "C":


print("letter is C")

elif letter == "A":

print("letter is A")

else:

print("letter isn't A, B or C")

Output:

letter is A

Q.18 Explain loops in python


Ans: Loops in Python are used to repeat actions efficiently. The main types are For
loops (counting through items) and While loops (based on conditions).
Additionally, Nested Loops allow looping within loops for more complex tasks.
While all the ways provide similar basic functionality, they differ in their syntax
and condition-checking time. In this article, we will look at Python loops and
understand their working with the help of examples.

i) While Loop in Python:


In Python, a while loop is used to execute a block of statements repeatedly until a
given condition is satisfied. When the condition becomes false, the line
immediately after the loop in the program is executed.

Example:

cnt = 0

while (cnt < 3):

cnt = cnt + 1

print("Hello Geek")

Output:
Hello Geek
Hello Geek
Hello Geek

ii) For Loop in Python:


For loops are used for sequential traversal. For example: traversing
a list or string or array etc. In Python, there is “for in” loop which is similar
to foreach loop in other languages. Let us learn how to use for loops in Python for
sequential traversals with examples.

Example:

n=4

for i in range(0, n):

print(i)

Output:

0
1
2
3

iii) Nested Loops in Python:


Python programming language allows to use one loop inside another loop which
is called nested loop. Following section shows few examples to illustrate the
concept.

Example:

from __future__ import print_function


for i in range(1, 5):

for j in range(i):

print(i, end=' ')

print()

Output:

1
22
333
4444

You might also like