0 like 0 dislike
551 views

edited
 Where can I do online courses From Word's Top Instructors? UDEMY:: Attend All Udemy Courses in Just INR 450[Coupon] Coursera:: Join For FREE

0 like 0 dislike
by Goeduhub's Expert (3.1k points)

### Question 16: Write a python program to check that there is no common element in three sets. (and there is at least one common elements in three sets )

Answer: Python Sets : Check here for basic concepts of python sets

#checking if there is a common elements in sets

sn1 = {1,2,3,8,9}

sn2 = {4,5,6}

sn3 = {45,69}

news=sn1&sn2&sn3

print(news)

Output:

set()

There is at least one common elements in three sets

#checking if there is a common elements in sets

sn1 = {1,2,3,8,9,6,45}

sn2 = {4,5,6,2,45}

sn3 = {45,69,6,7,3}

news=sn1&sn2&sn3

print(news)

Output:

{45, 6}

Note: As we know that python sets follow the concept of intersection, union and difference same as sets in mathematics, we can easily conclude the elements of a set.

If we have two sets A and B then-

Intersection = (A&B), Union = (A|B) and Difference = (A-B) , (B-A)

### Question 17: What are the Built-in data types in python ?

Answer:  If you are ever confused about telling data type in a Python program, then you can use the simple type () function to see the type of data.

Why should we have to know about the data type in python ? The answer is to avoid the errors. For example if you have a string type of data and want to perform a arithmetic operation it will give you a error.

Now let's see the data types in python

Numeric - integer, float, complex , boolean

Sequential - list and tuple  (string is also a type of text sequence)

text- string

Non Sequential - Sets and dictionaries

Note: Note that range also consider as sequential data type. But it is obvious as range function in python gives a sequence of passed values.  range (start, stop, step)

### Question 18: What is the difference List, Tuple and String ?

Properties ListTupleString
Defining objectl=[1,2,3] , list(1,2,3)T=(1,2,3)
T=1,2,3
s="string", str("string")
UpdationMutable Immutable Immutable
Explanation A list a sequence of elements (characters, numeric or even another list)A tuple is similar to list but immutable.A string is a sequence of characters  (everything in between double quotes and single quotes is string (" "))

### Question 19: Write a Python program to find those numbers which are divisible by 11 and multiple of 5, between 1000 and 1500.

Answer: Click on the links given to check conceptual theory of Python Conditions and Python Loop.

#understanding concept of loop and condition

l=[]

for x in range(1000,1500):

if (x%11==0) and (x%5==0):

l.append(x)

print (l)

Output:

[1045, 1100, 1155, 1210, 1265, 1320, 1375, 1430, 1485]

### Question 20: Write a python program to reverse a string (What is Negative indexes in python?).

#reversing string

word = input("Input a word to reverse: ")

word[::-1]

Output:

```Input a word to reverse: Goeduhub
```
`'`
` buhudeoG`

Note: In this example we used concept of slicing (negative index) to get our output if you want you can also use loop and conditions to get the result. But it is just to show how small concepts works for problems.

Try it : Write a python program to reverse the numbers ?

In python sequences are indexed meaning kind of labelling is done to sequence data type. For example

GOEDU-

 Positive indexing 0 1 2 3 4 Word G O E D U Negative Indexing -5 -4 -3 -2 -1

The positive index start from 0 to len (Word) and negative index start from -1 to start of string.

### Question 21 : What is Lambda in Python ?

Lambda is a python anonymous function that can accept any number of arguments and return an object.

Lambda function is useful when we  just need need to explain our function in one line expression.  For example if we want to make a program to add numbers then we can use lambda because it is short and simple.

But if we want to a function have more then one  expression in this state we can't use lambda.

How to use lambda ?

#lambda fucntion

Output:

11

0 like 0 dislike
by Goeduhub's Expert (3.1k points)

### Question 22:  What is Namespace in Python ?

Answer:  A Namespace in python is a space to hold unique names (object names) for a python program to avoid ambiguity of object names.

Now assume that in  a sports academy their are three sports Hockey, Cricket and Tennis. And each sport has at least one member in team with same with other sports team. Suppose the name is "Ram".

Meaning name "Ram" is their in hockey ,Cricket and Tennis. Meaning there are three person of same name in sports academy. Now assume if a delivery boy want to deliver something to the person named "Ram" , But problem is that the delivery boy don't know the sport name of "Ram" (We have three ram's ). Means the situation here created a ambiguity of names. The delivery boy can avoid this ambiguity if he had know the sport name of person "Ram".

How the theory works in python ?

Python actually create namespace to each python module.

Now try to understand it with an example

There are two ways to import modules in python.  1. import module  (And we get access to use all objects of imported module by using module.object )

**Note that in this case python creates a namespace and local object name have no conflict (ambiguity) with module name and module's object name.

#understanding namespace

import math

fac=math.factorial(6)

print(fac)

Output : 720

Now let's defined a new variable (or object) with the same name as module's function. (Here module - math and Function - Factorial) and see if the  function name factorial overridden by variable name or not.

#understanding namespace

import math

factorial = lambda a : a*3

fac=math.factorial(6)

print(fac)

Output : 720

Note : Note that in this case whenever we want to use object or module's function we have to add math. before the function. Same as using sport name before name of person (Cricket.Ram) then there is no confusion at all. In this case function name of module is not overridden by variable (object) name.

So, in  this case even if we have the same variable (objects) name as the function name of the Module, we will always get the right result because always because module name before the function name. Meaning python create a namespace for module in this case.

2. from module import  objects (or function) (And we get access of imported function without module name )

#understanding namespace

from math import factorial

fac = factorial (6)

print(fac)

Output: 720

Now let's defined a new variable (or object) with the same name as module's function. (Here module - math and Function - Factorial) and see if the function name factorial overridden by variable name or not.

#understanding namespace

from math import factorial

factorial = lambda a : a*3

fac = factorial (6)

print(fac)

Output : 18

Note:  In this case function name of module is not overridden by variable (object) name.

### Question 23: What are types of Namespace and the lifetime of a namespace ?

Answer :  Anything that you first write in python is actually print () function. We use print

() anywhere in a python program without importing and defining anything. help () and dir () also work as print (). These are built in namespace.

When we use a module and a module is created  this is called global namespace. And when we define local function's variables it creates local namespace. The Lifetime and Lifecycle of namespace:

The Lifetime and Lifecycle of namespace depends upon the scope of objects.  Meaning if scope for a object ends the lifetime for the namespace also end.

It is simple. Meaning as long as we are using a namespace (objects and variables names) in a python program, it has lifetime.

### Question 24:  What is Scope in python and scope resolution ?

Answer : A scope is basically a block of code where a object/variable functions. In simple terms a object/variable useful within scope and after scope the lifetime of objects/variables end.

Now let's  understand with an example

#scope in python

var= "global scope variable"

def func():

var= "local-scope variable"

print(var)

print(var)

func()

print(var)

Output: Note: As you see in the above example the difference between local and global scope variable / objects. The Local variable/object  (in the function) only function when we called the function  (func) and after that the lifetime of local scope variable (namespace, name of variable- var) ends.

On the other hand global scope variable available throughout the code.

Now let's change the scope of local variable/ object to global variable/object.

#scope in python

var= "global scope variable"

def func():

global var

var= "local-scope variable"

print(var)

print(var)

func()

print(var)

Output: Note : As you see in above example we can override the local variable and simply can use a local variable scope as global variable scope by using global keyword.

Scope Resolution: In some cases (for example in above examples) a scope have same variable name but function differently. In such cases python automatically draw a line between their functionality is called scope resolution. 