Python FAQ (Frequently Asked Questions and answers) for freshers

This post lists the Python Programming FAQ (Frequently Asked Questions and answers) also a collection of many trouble shooting use cases.

It is a widely-used general-purpose, high-level programming language. It is designed by Guido Van Rossum and further additions were done by the Python Software foundation. It allows the programmer to wrap the code in just a few lines.

Applications of Python:

  • Web development
  • Game development
  • System scripting
  • Mobile Application development
  • Software development

Features of Python Programming language:

  • It is a dynamically typed language. It means that there is no need to declare the data type of the variable.
  • It is interpreted language. It means it does not need to compile before the run. It checks the code line by line and if an error encountered, it will display an error message.
  • Python is an object-oriented programming language. It allows the concept of classes along with inheritance and composition. 
  • It is easy to understand and write but takes more in execution. 

The interpreter converts the source code into machine-level language code. Python follows the same process like it converts the written code into machine-level code. There is no intermediate code that exists. This code runs on every operating system. The advantage of this type of programming language there is no need to write different code for the different operating systems.

For more details, go through the link

https://tutorialslink.com/Articles/What-is-Python-Interpreter/1607 

 

PEP stands for Python Enhancement Proposal. It is a document that contains a set of rules to maximize code readability. 

Python memory management is managed by python private heap space. The private heap contains all the objects and structures. The programmer can not access the private heap space rather python interpreter takes care of the heap space. Python's memory manager allocates the memory to objects. Python has an inbuilt garbage collector. It frees up the memory space to re-use by heap space. 

Like others such as Java and C++, python also has the concept of the namespace. It is a naming system used to give a unique name to avoid conflicts. 

Before jumping into the concept of dynamically typed language first, we must be familiar with the concept of typing. Typing refers to checking the data types of the variables. There are two ways through which we can check the data types:

  • Static: The type-checking is done before the execution of the program.
  • Dynamic: The type-checking is done during the execution of the program.

In python, there is no need to define the types of variables. It allows to directly use the variables as its type-checking will be done during the execution of the program. The interpreter checks the program line-by-line and also examines the data type of the variable. 

Every object has its own scope. A scope is a block of code where the object is accessible. Namespace defines the scope of their objects. The type of scope exist:

  • Local scope: The object is accessible only inside the block such as the function where it is defined. 
  • Global scope: The object is defined outside all the functions and modules. So, it can be accessed anywhere in the program. 
  • Module-level scope: The object is accessible in the complete module as a global object where the program is written. 

Pythonpath is kind of an environment variable. It is used to import the modules. When the module is imported, it will find the presence of the particular module in the various directories. The interpreter finds which module needs to be loaded. 

Yes, python is a case-sensitive language without a doubt. If we write a variable in a small letter and want to use it further in the program, then use it in the same manner only otherwise it will be considered as you are using a new variable. 

Before jumping into how type-conversion is done first, we must be familiar with what is type-conversion. Type-conversion means converting a variable from one data type to another data type. The functions that are used in the conversion is:

  • int(): It converts any data type into the integer type.
  • float(): It converts any data type into the float type.
  • ord(): It converts characters into the integer. 
  • hex(): It converts integer data type into the hexadecimal type.
  • oct(): It converts the integer data type to the octal type. 

Indentation is the most important concept in python without indentation you can not run the program. The compiler will regularly throw an error. It specifies the block of code. Each and every loop, class, and function needs to be properly aligned to execute the program. 

List Tuple
A list consists of mutable objects. It means the value of the object can be changed. A tuple consists of immutable objects. It means the value of the object can not be changed.
The list has a massive memory size. The tuple has a compact memory size.
The list is kept in two blocks of memory.  The tuple is kept in a single block of memory.
List elements can be removed or replaced.  Tuple elements can not be removed or replaced. 
The list is stored in [] brackets. The tuple is stored in () brackets. 
Array List
The array contains homogenous elements. The list contains heterogeneous elements
Array size can not be expanded. List size can be expanded.
The mathematical functions are applied to the complete array. The mathematical function can not be applied to the complete list whereas it is applicable only to the individual elements.
The array requires to be imported before use.  There is no need to import anything for using the list.

A function is the same as it is in other classes. It is a block of code that is executed only when it is called. def keyword is used to define a function in python. 

Example:

def func():

printf("Welcome to TutorialsLink");

func()

OUTPUT: Welcome to TutorialsLink

 

_init_ is defined in every class. It is a method or constructor that is called every time whenever new object or instance of a class is created. 

Example:

class student:

def _init_(self, roll_no, name):

self.roll_no = roll_no;

self.name = name;

S = student(18, "TARUN")

print(S.roll_no)

print(S.name)

Lambda function is an anonymous function. The function can have any number of parameters but in a single statement only. 

Example:

sum = lambda x, y : x+y

print(sum(4, 5))