What are the key features of Python?
Python is a object-oriented,open source programming language
It runs on different platforms like Windows, Linux,Macintosh etc.
It is easy to read as it has a clean syntax
It can be integrated with different languages such as Java,C,C++,C#.
function
A function is used to modularize application.Instead of defining application in a single module,application code is defined in different functions.
This helps to keep the application easier to maintain.After you have defined a function you can call it by using its name.
What is a function.How do you define a function?
You can define a function using the def keyword:
def FunctionName(): statements
to call the function you use:
FunctionName()
you can also define and pass parameters to the function:
def FunctionName(param1): statement block
Write to a file in python?
Python provides several functions for working with files.One of the most important functions is open.To open a file for reading:
f = open("file name")
or
f = open("file name",mode)
So to open a file for reading mode should be “r”:
f = open("fileName.txt","r")
if you want to append to an existing file:
f = open("fileName.txt","a+") f.write("Append Data" )
Pass in python?
Sometimes when defining a class you don’t want to implement some of the methods.As you can not write any method without any statement so you can use pass
statement:
class ClassName(object): def method1(self): pass def method2(self): pass
Exception handling in Python?
You use the try and except keywords.If the code in try block throws any exception then it is handled by the matching except block.
try: statement block except exception1: print("exception 1") except exception2: print("exception 2") except exception3: print("exception 3")
What is PEP 8?
It is a set of coding conventions for the Python code and defines things such as method names,variable names,exception names etc.
Explain the ternary operator in Python?
It specifies a boolean expression and returns one of the two values based on the value of the boolean expression
Following is a ternary operator which returns the value y.
x,y=1,2
num = x if x < y else y
What is the key difference between a list and the tuple?
Bothe list and tuple are containers of items but list is mutable while tuple is immutable
How does Python handle the memory management?
Memory management in Python is handled using Python memory manager.The Python memory manager consists of different components for different aspects of memory maangement such as:
- sharing
- segmentation
- preallocation
- caching
The user has no control over Python heap as it is performed by the the interpreter.
What is the “self” keyword in Python?
Self is a variable used in a class method which refers to the current instance of the class.
In the following example we are setting a variable called num in SimpleClass as 2.
class SimpleClass(object): num = 1 def set_num(self): self.num=2
What is NumPy?
- NumPy is a package for scientific computing with Python.It has features such as:
N-dimensional array - linear algebra, Fourier transform, and random number capabilities
Explain help() and dir() functions in Python?
- help() function provides information related to modules, keywords, attributes, etc.
- dir() returnd a list of attributes of the object.