Gadgets 4 Students Online Courses
Free Tutorials  Go to Your University  Placement Preparation 
0 like 0 dislike
569 views
in Python Programming by Goeduhub's Expert (3.1k points)
Python Interview Questions and answers

2 Answers

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

Question 32: Write a Python program to compute the square of first n Fibonacci numbers, using map function and generate a list of the numbers.

Answer:  In mathematics, the Fibonacci numbers, commonly denoted Fn, where n is number of terms. F1, F2, F3.....Fn.

First and second terms of a fibonacci series are fixed and formula for next terms given below. 

First and second term :

{\displaystyle F_{0}=0,\quad F_{1}=1,}

 and Formula for next terms 

{\displaystyle F_{n}=F_{n-1}+F_{n-2}}

for n> 1

The beginning of the sequence is thus:

{\displaystyle 0,\;1,\;1,\;2,\;3,\;5,\;8,\;13,\;21,\;34,\;55,\;89,\;144,\;\ldots }

 Source : Wikipedia 

how to program fibonacci in python and square of fibonacci series 

#displaying fibonacci series 

number_of_terms = int(input("enter n terms"))

n1, n2 = 0, 1

count = 0

fib=[]

while count < number_of_terms:

  nth = n1 + n2

  fib.append(n1)

  n1 = n2

  n2 = nth

  count += 1

print("fibonacci series", fib)

#printing sqaure of fibonacci series using map function 

Sq=lambda n: n*n

map_obj= map(Sq, fib)

sq_fib=list(map_obj)

print("sequare of fibonacci series", sq_fib)

Output :

fibonacci map


Question 33: How to scrap data from a website in python ?

Answer: Web scraping:  As we know nowadays internet is a storage of vast data. Off course we don't need all the data and in such situation, we  need only the data that meets our need. 

For example form a shopping website you want to buy a mobile phone then you can just write a few line python code and can extract the data for mobiles only (price, model etc...).

Now you can ask a question here that the websites already give us filter options then why we need web scraping ?.

In this case filter has no limit , meaning you can add filters to make your search simple. 

And you can also use web scraped data to do python programming. As we already know if we want to use data in programming it should be in structure form, If not we have to make it in structure form. 

You can also use API of website to extract data for your use but the problem is not all websites have API. In such case you can use web scraping to extract data from a web page. 

How to extract data from a web page using python ? 

For this it will be helpful to you if you have knowledge of basic html, and python beautiful soup library

Click here to see a practical example beautiful soup.


Question 34: Write a Python program to remove lowercase substrings from a given string.

Answer:  We can do this problem by using for loop and conditions. But what if we a large string, in that case we might have problem in traversing.

Python Regular Expression Library  is basically used in such cases. It is used to search, extract and update a sequence of character.

Regular expressions also called REs, or regexes, or regex patterns made available through the re module in Python.

Python Regular Expression (re module) full tutorial (Concepts and Examples)

#regular expression in python

import re

string = 'GOEDUHUB TECHNOLOGIES provide training in PYTHON and MACHINE LEARNING.'

lower_case= re.sub('[a-z]', ' ',string)

print(lower_case)

    

Output:

re


Q.35. What is type conversion in Python?

Answer:- Type Conversion is the conversion of object from one data type to another data type. Python has two types of type conversion.

  • Implicit Type Conversion
  • Explicit Type Conversion

Implicit Type Conversion- Python automatically converts from one data type to another data type without user intervention.Example-

A=10

B=10.5 

print(type(A), type(B))   // here data type of A is integer and B is float

C=A+B

print(type(C))  

Note- Here data type of C is float, python automatically converts from smaller data type to larger data type to save data loss. This is example of implicit Type Conversion.

Explicit Type Conversion- Python have certain functions to convert from one data type to another. List of functions-

int() – converts any data type into integer type

float() – converts any data type into float type

ord() – converts characters into integer

hex() – converts integers to hexadecimal

oct() – converts integer to octal

tuple() – This function is used to convert to a tuple.

set() – This function returns the type after converting to set.

list() – This function is used to convert any data type to a list type.

dict() – This function is used to convert a tuple of order (key,value) into a dictionary.

str() – Used to convert integer into a string.


Q.36. What are the supported standard data types in Python?

Answer:- Here is the list of most commonly used built-in types that Python supports:

Immutable built-in datatypes of Python

  • Numbers
  • Strings
  • Tuples

Mutable built-in datatypes of Python

  • List
  • Dictionaries
  • Sets

Q.37. What is Indentation in Python and Why is Python Indentation Important?

Answer:- It is very important to understand indentation in Python if you want to run your program without any error, otherwise you will get indentation error when you run the program.

Indentation specifies a block of code. All code within loops, classes, functions, etc is specified within an indented block.

In languages like C, C++, Java, we use curly braces { } to indicate the start and end of a block of code. In Python to give indentation after colon is , after pressing enter key either you give some space (Key) or you give tab (Key).

In simple, all the statements with the same distance (space) to the right, belong to the same block. In other words, statements belonging to a block will necessarily start from the same vertical line. You can imagine it this way.

python indentation


Q.38. How would you define a block in Python?

Answer:- For any kind of statements, we possibly need to define a block of code under them. However, Python does not support curly braces. This means we must end such statements with colons and then indent the blocks under those with the same amount.


Q.39. What is the difference between .py and .pyc files in Python?

Answer:- 

  • .py files contain the source code of a program. Whereas, .pyc file contains the bytecode of your program. We get bytecode after compilation of .py file (source code). A .pyc is not created for your main program file that you execute (only for imported modules).
  • Before executing a python program python interpreter checks for the compiled files. If the file is present, the virtual machine executes it. If not found, it checks for .py file. If found, compiles it to .pyc file and then python virtual machine executes it.
  • Having .pyc file saves you the compilation time.

Q.40.  How do you invoke the Python interpreter for interactive use?

Answer:- 

On Windows, bring up the command prompt and type "py", or start an interactive Python session by selecting "Python (command line)", "IDLE", or similar program from the task bar / app menu.

On macOS or linux, open a terminal and simply type "python".


Q.41.  Is Python fully object oriented?

Answer:- Python does follow an object-oriented programming paradigm and has all the basic OOPs concepts such as inheritance, polymorphism, and more, with the exception of access specifiers. Python doesn’t support strong encapsulation (adding a private keyword before data members). Although, it has a convention that can be used for data hiding, i.e., prefixing a data member with two underscores.

 

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

Q.42. Do we need to declare variables with data types in Python?

 Answer:- No. Python is a dynamically typed language, i.e., Python Interpreter automatically identifies the data type of a variable based on the type of value assigned to the variable.


Q.43. What is the difference between lists and tuples?

Answer:- 

Lists Tuples
Lists are mutable, i.e., they can be edited.

Tuples are immutable (they are lists that cannot be edited).

Lists are usually slower than tuples.

Tuples are faster than lists.

Syntax:

 list_1 = [100, ‘Goeduhub’, 200]

Syntax:

 tup_1 = (10, ‘Goeduhub’ , 20)



Q.44. Is there a switch or case statement in Python? If not then what is the reason for the same?

Answer:-  Python does not have a switch or case statement.

We can use the powerful dictionary mappings  to implement switch statement , also known as associative arrays, that provide simple one-to-one key-value mappings.
Here is a example-
# Switcher is dictionary data type here or a dictionary which contains key-values
def xyz(x):
  switcher = {
        0: "zero",
        1: "one",
        2: "two",
    }
  return switcher.get(x, "nothing")
  # get() method of dictionary data type which takes maximum of two parameters  
  #key - key to be searched in the dictionary
  #value (optional) - Value to be returned if the key is not found. The default value is None.

n=int(input("enter the choice"))
c=xyz(n)
print(c)


Q.45. What is a map function in Python?

Answer:- The map() function in Python has two parameters, function and iterable. The map() function takes a function as an argument and then applies that function to all the elements of an iterable, passed to it as another argument. It returns an object list of results.

 For e.g. def calculateSq(n):

 return n*n numbers = (2, 3, 4, 5)

 result = map( calculateSq, numbers)

 print(result)


Is python numpy better than lists?

Q.46. Is python numpy better than lists?

Answer:- We use python numpy array instead of a list because of the below three reasons:

Less Memory

Fast

Convenient

Best Online Learning Opportunities

UDEMY::  Attend All Udemy Courses in Just INR 450[Coupon]
Coursera:: Join For FREE
UDACITY (Best Nanodegrees)::

Machine Learning Engineer || Artificial Intelligence
Data Scientist

3.3k questions

7.1k answers

395 comments

4.5k users

Related questions

 Important Lists:

Important Lists, Exams & Cutoffs Exams after Graduation PSUs

 Goeduhub:

About Us | Contact Us || Terms & Conditions | Privacy Policy || Youtube Channel || Telegram Channel © goeduhub.com Social::   |  | 

 

Free Online Directory
...