Nothing Special   »   [go: up one dir, main page]

Mostly Asked FAQ's Python

Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 6

1. What is PEP-8 ?

Ans. PEP-8 fullform is "python enhancement proposal", It is a set of documentation


or guidlines to write a program in a effective way. It also helps and speicfies the
readability of code and
consistancey.

2. what is interpreter and compiler ?

Ans. Both Interpreter and complier works in the same way, they acts as translators.

Compiler conerts the high level language to low level language i.e machine language
or assembly language. low level language means (0's, 1's).

The main advantages of compiler is :

1. It runs faster than interpreter.


2. It doenst require source code for furture reference.
3. It generates the errors after compyling it in current time.
4. It returns o/p as .exe file.

Ex: C, C++, C#.

Interpreter converts the high level language to intermediate language with many
steps.It contains pre-compiled code and source code.

1. It runs code slower than compiler.


2. It doesn't generates any o/p.
3. It exexutes line by line and generates errors in every single line.
4. It requires source code for later execution.
5. objects are not stored for future execution.

3. How does Memory managed in python?

Ans. Pythons manages memory dynamically.


Python has 2 memories STACK Memory and HEAP Memory.
Vaiables are stored in stack memory and objects are stored in heap memory.
Objects in the heap memory has address and address is a hexa decimal number.
variables in the stack memory are pointed towords the heap memory there reference
count generated.
Garbage collector sweaps all the unassigned data automatically. So,python has
dynamic memory management.

4. What is dynamically typed language ?

Ans. In other languages, we have to declare the type of data. But, python itself
declares the varibles assingned and describes the data types.

5. fundamental datatypes

Ans . Data type definition : It is a type of data assigned to a variable


There are five types of fundamental data types
1. String : String indicated in double quotes (ex : "12345","Viraja"), string is
immutable
2. Int : It is a numerical data struture which defines only positive
intergers.
3. float
4. complex
5. boolean : It returns true or false statements.
6. What are reserve words in python?

Ans. These are also called keywords,which means they are predefined meaning and
syntax in the language.
ex : and, except, lambda, with, as yeild, try, except,finally.

7. what are identifiers in python?

Python Identifier is the name we give to identify a variable, function, class,


module or other object. That means whenever we want to give an entity a name,
that's called identifier.
Rules for Writing Identifiers
There are some rules for writing Identifiers. But first you must know Python is
case sensitive. That means Name and name are two different identifiers in Python.
Here are some rules for writing Identifiers in python.

1.Identifiers can be combination of uppercase and lowercase letters, digits or an


underscore(_). So myVariable, variable_1, variable_for_print all are valid python
identifiers.
2.An Identifier can not start with digit. So while variable1 is valid, 1variable is
not valid.
3.We can’t use special symbols like !,#,@,%,$ etc in our Identifier.
4.Identifier can be of any length.

8.What are operators ?

Ans. A special symbol that tells a particular computations should be performed.


There are 7 types of operators :
1. Arthematic operators : +,-,*,/,%,//,**
2. Assignment operators : +=,-=,*=,/=, //=, %=
3. Comparision operators : >=,<=,==,!=,<,>
4. logical operators : and, or, not
5. Identity operators : is,is not
6. Membership operators : in, not in
7. Bit-wise operators : &,|, ^or, ~, <<, >>

9. What are collection data types ?

Ans. collection data types are otherwise called as data structures/containers which
stores large number of data in a variable.
There are four types of data structures.
1. List
2. Tuple
3. Dict
4. Set

10. what is namespace ?

Ans. . A namespace is a collection of currently defined symbolic names along with


information about the object that each name references.
There are 3 types of name space
1. Local namespace
2. Global namespace
3. Built-in namespace

11. type casting ?


Ans. Conversion of one data type to another is called type casting.
Ex:- we can convert int data type to float and float to int but we can not convert
then to complex but we can convert complex data type to int or float.

12. string slicing?

Ans. extracting a piece or part of data in a string is called string slicing


syntax:
--> [start index: end index: step value]
start index default 0, end index default -1, step value default 1.

13. Difference B/n list and tuple ?

Ans. List and tuple are data structres in python where,


List is mutable,indexed,ordered, indexed, heterogenous data structure, allows
duplicates, CRUD operations can be performed.
Tuple is immutable,indexed,ordered, indexed, heterogenous data structure, allows
duplicates, CRUD operations can't be performed.

Tuple performs faster operations than list because of its static characterstics
also contain less build in functions and low utilization of memory.

14. apppend and extend

Ans. Append adds last element to the list


extend it increases the lenght of the list with elements.

15. list comprehension

Ans. comprising large number or lines of programs to small

Syntax :

L = [expression for item in iterable ]


l = [expression for item in iterable if condition]

16. pop and remove

Ans.del removes an element(s) by index. At the same time, pop() removes it by index
and also returns the value.

17. split and join

Ans. Splits the string at the specified separator, and returns a list,Joins the
elements of an iterable to the end of the string

18. break, continue and pass

Ans. These are part of control flow statements and also called as transfer
statements.

Break : break terminates the entire loop if the conditon is true and moves to next
step.
Continue : countine moves to next iteration when the condition is true and doesnt
terminates the loop.
pass : pass passes the input and helps to run program smoothly.

19. Inbuilt /predefined functions in Python


Ans. Python provides a wide range of inbuilt (predefined) functions that are
readily available for use in your programs. These functions are included in the
Python standard library and cover various aspects of programming, from basic
operations to more specialized tasks.
Ex:-print(), int(), sorted(),max(), min(),sum() etc.,

20. How to write a function.

Ans. Syntax to write a program:


1. Function Definition:
==> function is defined as "def" key word
--> def functionname():
2. Function Parameters:
==> Parameter is the variable that are send to the function definition
--> def functionname(name) #here name is the parameter
3. Function Body:
==> Function body is the place where you write a code that defines the
functionality of the function
--> def functionname(name)
print('Hello,(name)') #function body
4.Function Call:
==> To use the function, you need to call it. You can call the function by using
its name followed by parentheses and providing the required arguments (values).
-->def functionname(name)
print('Hello,(name)')
functionname(Rithika) # Rithika is the value given to the function.

21. What is lambda and why we use it.

Ans. Lambda is a User define function, it is a small, onetime Anonymous function.


Lambda can have any arguments, but will have only one Expression.

Syntax:- lambda arguments: expression

22. Recursion.

Ans. Recursion means function calling itself.


Recursion is defined as the process of defining something in terms of itself.
In recursion:
-> Complicated function can be split down into smaller sub-problems.
-> Sequence creation is simplier in recusion compared with nested iterations.
-> Code creation using recursion is simplier and more effective.

23. Map and filter.

Ans. map and filter are two built-in functions in Python that are commonly used
with lambda functions to process and manipulate data in iterable objects, such as
lists or tuples.
==> map function applies a given function to each elements of an iteration and
returns an iterator that contain results.
Syntax for map:
--> map(lambda function, items)

==> Filter funtion filters and create a list of elements for which condition become
True.
Syntax for Filter:
--> filter(function, items)

24. Principles of OOP's and types of inheritance.


OOPS:
*Object Oreinted Program
--> The OOPs concept focuses on writing the reusable code.
--> OOPs deals with objects with its data and its functions.
--> OOPS have a way of using Class and Objects.

CLASS:-
=> Class is a logical entity that create template/blueprint like structure.
=> Class is a combination of Attributes and its functions.
=> Class can be defines as the collection of Objects.
EX:- if we have a class called Employee
than their attribute and methods are mailid, Name, Age, Salary, etc.
Create class:
-> Class is denoted using 'class'
Syntax:- Class classname:

OBJECT:-
=> Everything is an object in the python and it has its own address.
or()
=> Object is the entity that has state and behavios.
=> Object is the collection of variables and functions.
EX:-List,Dict,String (everything is an object)

Concepts in OOPS:

there are 4 concepts in oops.


1). Encapsulation
2). Polymorphism
3). Abstraction and
4). Inheritance.

1). Encapsulation:-
*It is defined as Binding of Attributes and methonds of an object in a class.
*Mainly used to protect the data.
3types of access specifiers:
1). public
2).private __
3) protect _

example:- Carrying a bag which contains pen,book,bottle,money,cards etc


Here, bag acts like a class and the objects that are carried inside the bad called
encapsulation.

2). Polymorphism:-
*In the name itself Poly = Many and Morphism = changes
*it is defines as Objects behave diffently as pr the need.
@ Using of the same function name but with different behaviors.
example:- animals and speak.
Here, animal is a class and the speak has different way like dog can speak as
barking
so here the accrion is speak is performed in different ways but the class is same.

3). Abstraction :-
* Used to hide the internal functionality of the function from the user.
* User only interact with the basic implementation of the function but the
inner waor is hidden
=> The process of hiding the certain details and showing only the essential
information to the user.
example:- Using TV remote to increase the volume.
here we only know how to press the +button to increase the volume but we do not
know hpw actually uit works inside.

4). Inheritance :-
* Receiving attributes and methods from one class to another class.
=> The class whose attributes and methods are inherited is called as 'PARENT
class (or)BASE class.'
=> The class that recieve attributes and methods from the base class is called as
'CHILD class (or) DERIVED class'
Types of inheritance:-
---------------------

1). Single Inheritance:-


*Derive properities from one parent class to one child class.
2). Multiple Inheritance:-
* derive properities from two parent calss to two child class.
3). Multi level Inheritance:-
*Properties of base class is derived to Intermediate class and then to Child
class.
4). Hirarical Inheritance:-
*Derive properties from one bas class and create multiple child or derived
class

You might also like