PYTHON (Model Paper-01)
PYTHON (Model Paper-01)
07. Explain the steps to create and read a text file in python?
ANS:
• Python provides build-in-functions to create or write and to read the files so there is
no need of importing libraries like we do in JAVA.
Create or write text file:
• To create or write the text file in python Open() built-in function is used.
• This method accepts many arguments but majority of time we use only two,
• We have multiple modes, but here we are using “w” mode because we are writing
the text file.
• After creating the file it is importing to close the file by using close() method.
Example: file_open(“file name”,’mode’)
Program: file = open(“myfile_text”,”w”)
File. Write(“data to enter in file”)
File. Close()
(OR)
With open(“myfile.txt”,”w”)as file
File. Write(“data to enter in file”)
Read text file:
• To read the text file, we use the open() function in similar manner as for writing the
file.
• We use “r” mode to read the text file.
• The content of file can be read using the read() method.
• After reading the file it is important to close the file using close() method.
Example:
File = open (“myfile.txt”, “r”)
Content = file. Read()
Print(content)
File. Close()
Output:
This is the first line
This is the second line
Lists:
Lists are mutable, and their items are accessed via indexing. Items cannot be added,
removed or replaced in a tuple.
Example:
List = [2, 3, 5, 6]
Print (“initial list:”)
List [2] = 4
Print (“list:”)
Output:
Initial list: [2, 3, 5, 6]
List: [2, 3, 4, 5, 6]
Def area(self):
Return 3.14 * self.radius**2
Def circumference(self):
Return 2 * 3.14 * self.radius
Circle = circle(10)
Print(“Radius:”, circle.radius)
Print(“area:”, circle.area())]
Print(“circumference:”, circle.circumference())
Output:
Radius: 10
Area: 314.0
Circumference: 62.800000000000004
11. Explain the purpose of using web APIs for data visualization in python. Explain with
an example?
ANS:
• Web APIs are very useful in implementation of RESTFUL web services using NET
frame work.
• Web API helps in enabling the development of HTTP services to reach out to
client entities like browser, devices or tablets.
• ASP.NET web API can be used with MVC for any type of application.
• A web API can help you develop ASP.NET application via AJAX.
• Hence, web API makes it easier for the developer to build an ASP.NET application
that is compatible with any browser and almost any device.
12. Discuss the steps to install plotly and generate a line graph in python?
ANS:
Steps to install the plotly:
• Open the command prompt
• Type pip install plotly command to install it
• Press the enter key to execute the command. This will start the installation
process.
• Wait for the installation process to complete.
• Once it is completed, we can type pip show plotly to verify the installation.
• This should display information about the installed version of plotly including its
location and version number.
• Now we can use the plotly, but to use plotly in code, we need to import the
library using import import plotly
•
• Simple line plot between X and Y data
(b) Discuss the steps to install and use the pickle module in python?
ANS: python pickle package is used for serializing and de-serializing a python object
structure. Any object in python can be pickled so that it can be stored on the local
disk.
Steps to install pickle module on linux using PIP :
Step 1: The first step we will use the apt manager to install python 3
Step 2:to install any python package, we’ll require a PIP manager, which is a python
package installation program. The installation of the PIP manager using apt is
described in the steps below.
Step 3:in this step, we’ll actually us e the PIP manager to install the pickle package.
To install the pickle package, simply run the command below on the terminal.
Step 4: the next step is to double-check it, so that we can only use the terminal to
get the information about the installed pickle package.
Class car:
Def type(self):
Print(“SUV”)
Def color(self):
Print(“black”)
Class Apple:
Def type(self):
Print(“fruit”)
Def color(self)
Print(“red”)
Def fun(obj):
obj.type()
obj.color()
obj_car = Car()
obj_apple = Apple()
Fun(obj_car)
Fun(obj_apple)
Output:
SUV
Black
Fruit
red
Example:
Vowels ={‘a’, ‘e’, ‘I’, ‘o’, ‘u’}
fset= frozenset(vowels)
Print(fset)
Output:
Frozenset({‘I’, ‘o’, ‘u’, ‘e’, ‘a’})
Program to demonstrate frozenset using method tuple:
Tuple=()
Fnum= frozenset(tuple)
Print(“frozen object is:”, fnum)
Output:
Frozenset object is : frozenset()
Break statement:
The break statement or keyword is used to terminate the loop. A break statement is
used inside both the while and for loops. It terminates the loop immediately and
transfers the execution to the immediate statement after the loop.
Example:
For I in range(1,10):
If i==5:
Break
Print(i)
Print(“out of the loop”)
Output:
1
2
3
4
Out of the loop
(b)Discuss the process of data visualization using plotly and JSON format in python?
ANS: