What is Python?
Python is an interpreted language. Explain.
An interpreted language is any programming language where the source code is run through an interpreter which then reads statements line by line, “interpreting” the code into something the machine can understand on the fly.
This is opposed to compiled languages which must have their code parsed through a compiler that converts the source code into machine code before it can actually be run on a machine.
Programs written in Python run directly from the source code, with no intermediary compilation step.
What is the difference between lists and tuples?
Lists
list_1 = [10, ‘Intellipaat’, 20]
Tuples
tup_1 = (10, ‘Intellipaat’ , 20)
What is pep 8?
PEP in Python stands for Python Enhancement Proposal. It is a set of rules that specify how to write and design Python code for maximum readability.
What are the Key features of Python?
For example, in Python, the following code line will run without any error:
a = 100 a = “Intellipaat”
How is Memory managed in Python?
What is PYTHONPATH?
PYTHONPATH has a role similar to PATH. This variable tells Python Interpreter where to locate the module files imported into a program. It should include the Python source library directory and the directories containing Python source code. PYTHONPATH is sometimes preset by Python Installer.
What are Python Modules?
Files containing Python codes are referred to as Python Modules. This code can either be classes, functions, or variables and saves the programmer time by providing the predefined functionalities when needed. It is a file with “.py” extension containing an executable code.
Commonly used built modules are listed below:
What are python namespaces?
A Python namespace ensures that object names in a program are unique and can be used without any conflict. Python implements these namespaces as dictionaries with ‘name as key’ mapped to its respective ‘object as value’.
Let’s explore some examples of namespaces:
Explain Inheritance in Python with an example?
Inheritance allows us to define a class that inherits all the methods and properties from another class.
Inheritance provides the code reusability feature.
Parent class is the class being inherited from, also called base class or super class.
Child class is the class that inherits from another class, also called derived class.
The following types of inheritance are supported in Python:
What is scope resolution?
The scope of an object name is the region of a program in which that name has meaning.
Scope resolution is required when a variable is used to determine where should its value come from. The interpreter determines this at runtime based on where the name definition occurs and where in the code the name is referenced. Scope resolution in Python follows the LEGB rule.
L, Local — Names assigned in any way within a function (or lambda), and not declared global in that function.
E, Enclosing-function locals — Name in the local scope of any and all statically enclosing functions(or lambdas), from inner to outer.
G, Global (module) — Names assigned at the top-level of a module file, or by executing a global statement in a def within the file.
B, Built-in (Python) — Names preassigned in the built-in names module : open, range,SyntaxError, etc.
What is a dictionary in Python?
Python dictionary is one of the supported data types in Python. It is an unordered collection of elements. The elements in dictionaries are stored as key-value pairs. Dictionaries are indexed by keys.
For example, below we have a dictionary named ‘dict’. It contains two keys, Country and Capital, along with their corresponding values, India and New Delhi.
Syntax:
dict={‘Country’:’India’,’Capital’:’New Delhi’, }
Output: Country: India, Capital: New Delhi
What are functions in Python?
A function is a block of code which is executed only when a call is made to the function.
The def keyword is used to define a particular function as shown below:
def function():
print(“Hi, Welcome to Intellipaat”)
function(); # call to the function
Output:
Hi, Welcome to Intellipaat
What is __init__ in Python?
Equivalent to constructors in OOP terminology, __init__ is a reserved method in Python classes. The __init__ method is called automatically whenever a new object is initiated. This method allocates memory to the new object as soon as it is created. This method can also be used to initialize variables.
Syntax
(for defining the __init__ method):
class Human:
init method or constructor
def __init__(self, age):
self.age = age
Sample Method
def say(self):
print(‘Hello, my age is’, self.age)
h= Human(22)
h.say()
Output:
Hello, my age is 22
What are the common built-in data types in Python?
Python supports the below-mentioned built-in data types:
Immutable data types:
Mutable data types:
What are local variables and global variables in Python?
Local variable: Any variable declared inside a function is known as Local variable and it’s accessibility remains inside that function only.
Global Variable: Any variable declared outside the function is known as Global variable and it can be easily accessible by any function present throughout the program.
What is type conversion in Python?
Python provides you with a much-needed functionality of converting one form of data type into the needed one and this is known as type conversion.
Type Conversion is classified into types:
Various Functions of explicit conversion are shown below:
int() – function converts any data type into integer.
float() – function converts any data type into float.
ord() – function returns an integer representing the Unicode character
hex() – function converts integers to hexadecimal strings.
oct() – function converts integer to octal strings.
tuple() – function convert to a tuple.
set() – function returns the type after converting to set.
list() – function converts any data type to a list type.
dict() – function is used to convert a tuple of order (key,value) into a dictionary.
str() – function used to convert integer into a string.
complex(real,imag) – function used to convert real numbers to complex(real,imag) numbers.
How to install Python on Windows and set a path variable?
For installing Python on Windows, follow the steps shown below:
Click on this link for installing the python:
Download Python
cmd python.
What is the difference between Python Arrays and lists?
List:
Array:
Is python case sensitive?
Yes, Python is a case sensitive language. This means that Function and function both are different in pythons like SQL and Pascal.
What does [::-1] do?
[::-1] ,this is an example of slice notation and helps to reverse the sequence with the help of indexing.
[Start,stop,step count]
Let’s understand with an example of an array:
import array as arr
Array_d=arr.array(‘i’,[1,2,3,4,5])
Array_d[::-1] #reverse the array or sequence
Output: 5,4,3,2,1
What are Python packages?
A Python package refers to the collection of different sub-packages and modules based on the similarities of the function.
What are decorators in Python?
In Python, decorators are necessary functions that help add functionality to an existing function without changing the structure of the function at all. These are represented by @decorator_name in Python and are called in a bottom-up format.
Let’s have a look how it works:
def decorator_lowercase(function): # defining python
decorator def wrapper():
func = function()
input_lowercase = func.lower()
return input_lowercase
return wrapper @decorator_lowercase ##calling decoractor
def intro(): #Normal function
return ‘Hello,I AM SAM’
hello()
Is indentation required in Python?
Indentation in Python is compulsory and is part of its syntax.
All programming languages have some way of defining the scope and extent of the block of codes. In Python, it is indentation. Indentation provides better readability to the code, which is probably why Python has made it compulsory.