Python Interview Question Set 3

Categories: Python

What is the use of break statement?

The break statement is used to terminate the execution of the current loop. Break always breaks the current execution and transfer control to outside the current block. If the block is in a loop, it exits from the loop, and if the break is in a nested loop, it exits from the innermost loop.

Example:

list_1 = ['X', 'Y', 'Z']  

list_2 = [11, 22, 33]  

for i in list_1:  

    for j in list_2:  

        print(i, j)  

        if i == 'Y' and j == 33:  

            print('BREAK')  

            break  

    else:  

        continue  

    break  

Output:

2

X 11

X 22

X 33

Y 11

Y 22

Y 33

BREAK


What is tuple in Python?

A tuple is a built-in data collection type. It allows us to store values in a sequence. It is immutable, so no change is reflected in the original data. It uses () brackets rather than [] square brackets to create a tuple. We cannot remove any element but can find in the tuple. We can use indexing to get elements. It also allows traversing elements in reverse order by using negative indexing. Tuple supports various methods like max(), sum(), sorted(), Len() etc.

To create a tuple, we can declare it as below.

Example:

# Declaring tuple  

tup = (2,4,6,8)  

# Displaying value  

print(tup)  

  

# Displaying Single value  

print(tup[2])  

Output:

(2, 4, 6, 8)

6


Which are the file related libraries/modules in Python?

The Python provides libraries/modules that enable you to manipulate text files and binary files on the file system. It helps to create files, update their contents, copy, and delete files. The libraries are os, os.path, and shutil.

Here, os and os.path - modules include a function for accessing the filesystem


while shutil - module enables you to copy and delete the files.


What are the different file processing modes supported by Python?

Python provides four modes to open files. The read-only (r), write-only (w), read-write (rw) and append mode (a). 'r' is used to open a file in read-only mode, 'w' is used to open a file in write-only mode, 'rw' is used to open in reading and write mode, 'a' is used to open a file in append mode. If the mode is not specified, by default file opens in read-only mode.


a) Read-only mode (r): Open a file for reading. It is the default mode.

b) Write-only mode (w): Open a file for writing. If the file contains data, data would be lost. Other a new file is created.

c) Read-Write mode (rw): Open a file for reading, write mode. It means updating mode.

d) Append mode (a): Open for writing, append to the end of the file, if the file exists.


What is an operator in Python?

An operator is a particular symbol which is used on some values and produces an output as a result. An operator works on operands. Operands are numeric literals or variables which hold some values. Operators can be unary, binary or ternary. An operator which requires a single operand known as a unary operator, which require two operands known as a binary operator and which require three operands is called ternary operator.


What are the different types of operators in Python?

Python uses a rich set of operators to perform a variety of operations. Some individual operators like membership and identity operators are not so familiar but allow to perform operations.


1. Arithmetic OperatorsRelational Operators

2. Assignment Operators

3. Logical Operators

4. Membership Operators

5. Identity Operators

6. Bitwise Operators


How to create a Unicode string in Python?

In Python 3, the old Unicode type has replaced by "str" type, and the string is treated as Unicode by default. We can make a string in Unicode by using art.title.encode("utf-8") function.

Example:

unicode_1 = ("\u0123", "\u2665", "\U0001f638", "\u265E", "\u265F", "\u2168")  

print (unicode_1)  

Output:

unicode_1: ('ģ', '♥', '????', '♞', '♟', 'Ⅸ')


is Python interpreted language?

Python is an interpreted language. The Python language program runs directly from the source code. It converts the source code into an intermediate language code, which is again translated into machine language that has to be executed.

Unlike Java or C, Python does not require compilation before execution.


How is memory managed in Python?

Memory is managed in Python in the following ways:

a) Memory management in python is managed by Python private heap space. All Python objects and data structures are located in a private heap. The programmer does not have access to this private heap. The python interpreter takes care of this instead.

b) The allocation of heap space for Python objects is done by Python's memory manager. The core API gives access to some tools for the programmer to code.

c) Python also has an inbuilt garbage collector, which recycles all the unused memory and so that it can be made available to the heap space.



What is the Python decorator?

Decorators are very powerful and a useful tool in Python that allows the programmers to add functionality to an existing code. This is also called metaprogramming because a part of the program tries to modify another part of the program at compile time. It allows the user to wrap another function to extend the behaviour of the wrapped function, without permanently modifying it.

Top Blogs
Interview Question and Answer of Python Published at:- Basic Concepts of Python Programming Published at:- Learn Python Programming Language Published at:- Let’s know about Python basics Published at:- Let’s know about the Python Shell and idle Published at:- How to running Python Locally Published at:- What is Lists, tuples and strings in Python? Published at:- Some Modules In Python how to it works Published at:- Program control: Looping, conditionals and functions Published at:- Program control: Looping, conditionals and functions ( Part 2) Published at:- Top iOS interview questions for Beginners Published at:- Python Interview Question Set 1 Published at:- Python Interview Question Set 2 Published at:- Python Interview Question Set 3 Published at:- Python Interview Question Set 6 Published at:- Python Interview Question Set 7 Published at:- Python Interview Question Set 8 Published at:- How Could I at any point Manage Python Programming Language Published at:- Top Python Advancement Patterns to Be Aware In 2022 Published at:- Advantages of Learning Python Published at:- What Is Python Utilized For Published at:- What is new in Python Programming Published at:- Top Python Libraries for Legitimate Information Science Experts Published at:- Why Django is the most popular Python Framework Published at:-
R4R.co.in Team
The content on R4R is created by expert teams.