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 :
and Formula for next terms
for n> 1
The beginning of the sequence is thus:
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 :
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:
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
Mutable built-in datatypes of Python
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.
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.