Top 10 Python Interview Questions || Python Questions and Answers || Python Questions || placementshub

Rohith

Q1- What is Python and it's Key features?

Python is a high-level, interpreted programming language that's popular among inventors due to its simplicity, readability, and inflexibility. It was created by Guido van Rossum and released in 1991.
These are some of the crucial features of Python :
Easy to learn: Python has a simple syntax that's easy to read and write, making it a great language for newcomers.
Interpreted: Python is an interpreted language, meaning that the law is executed line by line rather than collected before prosecution.
Dynamic Typing: Python is stoutly compartmented, which means that you do not need to specify the data type of a variable when you declare it.
Object- Oriented: Python is an object- acquainted language, which means that it supports the creation and manipulation of objects.
High- Level: Python is a high- position language, which means that it objectifications down low- position details like memory operation and tackle access, making it easier to write programs.
Extensive standard library: Python comes with a large standard library that includes modules for tasks like working with regular expressions, reading and writing lines, and handling network protocols.
Cross-platform: Python law can run on multiple platforms, including Windows, macOS, and colorful flavors of Unix.
Large and active community: Python has a large and active community of inventors who contribute to the language, produce libraries, and give support through forums and other channels.


Q2- What are Python Modules ?

In python modules refers to a file containing python statements and definiitions. A file containing python code, for example " GetSum.py " is called as a module and its module name is GetSum.
We use these modules to break down large programs to smaller ones.
Modules are also used to group related code and make it easier to reuse and maintain.
We use import keyword to import python modules.
Example:
import GetSum
GetSum.add(10,20,30.5)
Examples of other Modules: JSON, Random, Math, os.


Q3- What are different Access Modifiers available in Python?

There are mainly 3types of access modifiers in python.
1- Global or Public
2- Protected
3-Private
1- Global: Global variables are also called as public variables that are defined in global space. To use this we use Global Keyword.
These global variables can be accessed and modified from anywhere.
Example:
x = "awesome"
def myfunc():
print("Python is " + x)
myfunc()
2- Protected:Protected Variables are defined with single underscore prefixed to their identifier.
Ex: "_ramcharan "
But they can be still accessed and modified from outside of the class.
Example:
Class Student
_name='ram'
_Gender='M'
3- Private:Private Variables are defined with double underscore prefixed to their identifier.
Ex: "__ramcharan "
They cannot be accessed and modified from outside of the class.
Example:
Class Student
__name='ram'
__Gender='M'

Q4- What is Python PEP8?

PEP 8 Stands for Python Enhancement Proposal
PEP 8 is a set of guidelines for writing Python code that was created to improve code readability and thickness. The guidelines cover things like naming, indentation, whitespace, and other stylistic choices.
These are some crucial points from PEP 8
1- Indentation: Use 4 spaces for indentation, not tabs.
2- Line length: Keep lines of law to a outside of 79 characters.
3- Naming conventions: Use lowercase for variable and function names, and separate words with underscores. Use CamelCase for class names.
4- Whitespace: Use whitespace to ameliorate readability. Use a space after commas, colons, and around drivers.
5- comments: Write comments to explain code when necessary, but do not overstate it. Use complete rulings and proper alphabet.
6- Import: Import modules at the begining of the file, one per line. Put standard library significances before third- party significances.
7- Function arguments: Use whitespace around the = operator when specifying dereliction values for function arguments.
8- Error checking: Use try/ except blocks to catch exceptions, and do not use a bare except clause.

Q5- How to Copy an Object in Python?

In Python, there are two ways to copy an object: 1- Shallow copy
2- Deep copy.
1- Shallow copy: A shallow copy creates a new object, but the contents of the object are still references to the original object. This means that any changes to the original object will affect to the copied object as well. To create a shallow copy of an object, you can use the copy() method.
Example:
import copy original_list = [1, 2,[ 3, 4]]
copied_list = original_list.copy()
# modify the original list
original_list[2].append(5)
# the copied list is also affected
print(copied_list) # prints [1, 2, [3, 4, 5]]
2- Deep Copy: A deep copy creates a new object with a new set of contents. This means that changes to the original object will not affect the copied object. To create a deep copy of an object, you can use the deepcopy() function from the copy module. Example:
import copy
original_list = [1, 2, [3, 4]]
copied_list = copy.deepcopy(original_list)
# modify the original list
original_list[2].append(5)
# the copied list is not affected
print(copied_list) # prints [1, 2, [3, 4]]

Post a Comment

Cookie Consent
We serve cookies on this site to analyze traffic, remember your preferences, and optimize your experience.
Oops!
It seems there is something wrong with your internet connection. Please connect to the internet and start browsing again.
Site is Blocked
Sorry! This site is not available in your country.