Intermediate Level

What is PYTHON PATH?

PYTHONPATH is an environment variable that allows the user to add
additional folders to the sys.path directory list for Python. In a nutshell,
it is an environment variable that is set before the start of the Python
interpreter.

What are Python modules?

A Python module is a collection of Python commands and definitions in
a single file. In a module, you may specify functions, classes, and
variables. A module can also include executable code. When code is
organized into modules, it is easier to understand and use. It also
logically organizes the code.

What are local variables and global variables in Python?

Local variables are declared inside a function and have a scope that is
confined to that function alone, whereas global variables are defined
outside of any function and have a global scope. To put it another way,
local variables are only available within the function in which they
were created, but global variables are accessible across the program
and throughout each function.

Local Variables
Local variables are variables that are created within a function and are
exclusive to that function. Outside of the function, it can’t be accessed.
Global Variables
Global variables are variables that are defined outside of any function
and are available throughout the program, that is, both inside and
outside of each function.

Explain Scope in Python?

Think of scope as the father of a family; every object works within a
scope. A formal definition would be this is a block of code under which
no matter how many objects you declare they remain relevant. A few
examples of the same are given below:
- Local Scope: When you create a variable inside a function that
belongs to the local scope of that function itself and it will only
be used inside that function.
Example:
def harshit_fun():
y = 100
print (y)
harshit_func()
100

- Global Scope: When a variable is created inside the main body of
python code, it is called the global scope. The best part about
global scope is they are accessible within any part of the python
code from any scope be it global or local.
Example:

y = 100
def harshit_func():
print (y)
harshit_func()
print (y)

- Nested Function: This is also known as a function inside a
function, as stated in the example above in local scope variable
y is not available outside the function but within any function
inside another function.
Example:

def first_func():
y = 100
def nested_func1():
print(y)
nested_func1()
first_func()

- Module Level Scope: This essentially refers to the global objects
of the current module accessible within the program.
- Outermost Scope: This is a reference to all the built-in names
that you can call in the program.

What are global, protected, and private attributes in Python?

The attributes of a class are also called variables. There are three
access modifiers in Python for variables, namely
a. public – The variables declared as public are accessible everywhere,
inside or outside the class.
b. private – The variables declared as private are accessible only
within the current class.
c. protected – The variables declared as protected are accessible only
within the current package.
Attributes are also classified as:
– Local attributes are defined within a code-block/method and can be
accessed only within that code-block/method.
– Global attributes are defined outside the code-block/method and
can be accessible everywhere.

class Mobile:
m1 = "Samsung Mobiles" //Global attributes
def price(self):
m2 = "Costly mobiles" //Local attributes
return m2
Sam_m = Mobile()
print(Sam_m.m1)

What are Keywords in Python?

Keywords in Python are reserved words that are used as identifiers,
function names, or variable names. They help define the structure and
syntax of the language.
There are a total of 33 keywords in Python 3.7 which can change in the
next version, i.e., Python 3.8

What is the difference between lists and tuples in Python?

List and tuple are data structures in Python that may store one or more
objects or values. Using square brackets, you may build a list to hold
numerous objects in one variable. Tuples, like arrays, may hold
numerous items in a single variable and are defined with parenthesis.
Lists
- Lists are mutable.
- The impacts of iterations are Time Consuming.
- The list is more convenient for actions like insertion and deletion.
- Lists take up more memory.
- There are numerous techniques built into lists.
- Changes and faults that are unexpected are more likely to occur.
- They consume a lot of memory given the nature of this data structure
- Syntax: list = [100, “Great Learning”, 30]

Tuples
- Tuples are immutable.
- Iterations have the effect of making things go faster.
- The items may be accessed using the tuple data type.
- When compared to a list, a tuple uses less memory.
- There aren’t many built-in methods in Tuple.
- It is difficult to take place in a tuple.
- They consume less memory
- Syntax: tup_2 = (100, “Great Learning”, 20)

How can you concatenate two tuples?

Let’s say we have two tuples like this ->
tup1 = (1,”a”,True)
tup2 = (4,5,6)
Concatenation of tuples means that we are adding the elements of one
tuple at the end of another tuple.
Now, let’s go ahead and concatenate tuple2 with tuple1:
Code:

tup1=(1,"a",True)
tup2=(4,5,6)
tup1+tup2
Output: (1, 'a', True, 4, 5, 6)

All you have to do is, use the ‘+’ operator between the two tuples and
you’ll get the concatenated result.
Similarly, let’s concatenate tuple1 with tuple2:
Code:

tup1=(1,"a",True)
tup2=(4,5,6)
tup2+tup1
Output: (4, 5, 6, 1, 'a', True)

What are functions in Python?

Functions in Python refer to blocks that have organized, and
reusable codes to perform single, and related events. Functions are
important to create better modularity for applications that reuse a high
degree of coding. Python has a number of built-in functions like print().
However, it also allows you to create user-defined functions.

How can you initialize a 5*5 numpy array with only zeroes?

We will be using the .zeros() method.
import numpy as np
n1=np.zeros((5,5))
n1
Use np.zeros() and pass in the dimensions inside it. Since we want a
5*5 matrix, we will pass (5,5) inside the .zeros() method.
This will be the output:
array([[0., 0., 0., 0., 0.], [0., 0., 0., 0., 0.]
[0., 0., 0., 0., 0.], [0., 0., 0., 0., 0.], [0., 0., 0., 0., 0.]])