Namma Kalvi 12th Computer Science Unit 5 Sura Guide em
Namma Kalvi 12th Computer Science Unit 5 Sura Guide em
com
Namma Kalvi
www.nammakalvi.org
UNIT-V INTEGRATING PYTHON WITH MYSQL AND C++
Importing C++
Chapter
Programs in Python
14
ns
io
CHAPTER SNAPSHOT
at
ic
14.1 Introduction
14.2 Scripting Language
bl
14.6.1 Module
14.7 Python program Executing C++ Program using control statement
14.8 How Python is handling the errors in C++
14.9 Python program Executing C++ Program Containing Arrays
14.10 Python program Executing C++ Program Containing Functions
14.11 Python program to Illustrate the inheritance of a Class
[201]
Evaluation
This material only for sample www.surabooks.com
www.nammakalvi.org
ns
[Ans. (a) wrapping] (c) C++ (d) PYTHON
3. The expansion of API is [Ans. (a) HTML]
io
(a) Application Programming Interpreter
10. What does __name__ contains ?
(b) Application Programming Interface at (a) c++ filename
(c) Application Performing Interface
(c) main() name
(d) Application Programming Interlink
ic
(c) python filename
[Ans. (b) Application Programming
Interface] (d) os module name
bl
4. A framework for interfacing Python and C++ is [Ans. (d) os module name]
Pu
202
[email protected] Ph: 81242 01000 / 812430 1000 / 96001 75757
www.surabooks.com This material only for sample
ns
faster. Data type is not Data type is required
Programming Programming (iii) required while while declaring
io
language like C, language like declaring variable variable
(iii)
C++ use compilers. Python, Ruby use at It can act both It is a general
interpreters. as scripting and purpose language
(iv)
3. Write the expansion of (i) SWIG (ii) MinGW general purpose
ic
language
Ans. (i)
SWIG – Simplified Wrapper Interface
bl
(ii) Modules provide reusability of code. and glue complex systems together
Define our most used functions in a 3. What is MinGW? What is its use?
module and import it, instead of copying
Ans. (i) MinGW refers to a set of runtime header
their definitions into different programs.
files, used in compiling and linking the
5. What is the use of cd command. Give an code of C, C++ and FORTRAN to be run
example. on Windows Operating System.
Ans. ‘cd’ command refers to change directory and (ii) MinGw-W64 (version of MinGW) is the
absolute path refers to the couple path. best compiler for C++ on Windows. To
(Eg) “cd c:\program files\open office 4\program” compile and execute the C++ program,
203
[email protected] Ph: 81242 01000 / 812430 1000 / 96001 75757
Unit V - Chapter 14
www.surabooks.com
www.nammakalvi.org
(vi) In Python, a function may accept an
argument of any type, and return multiple
dynamically through Python program values without any kind of declaration
using g++. beforehand. Whereas in C++ return
(iii) Python program that contains the C++ statement can return only one value.
coding can be executed only through
minGW-w64 project’ run terminal. The run 2. Explain each word of the following command.
terminal open the command-line window Python <filename.py> -<i> <C++ filename
through which Python program should be without cpp extension>
executed.
Ans. Python <filename.py> -i <C++ filename without
4. Identify the module ,operator, definition name
cpp extension>
for the following
welcome.display() Python Keyword to execute
the Python program
Ans. Welcome.display()
from command-line
ns
filename.py- i Name of the Python
program to executed
definition name input mode
io
dot operator C++ filename Name of C++ file
without cpp to be compiled and
module name
at extension executed
5. What is sys.argv? What does it contain?
ic
Ans. sys.argv is the list of command-line arguments 3. What is the purpose of sys, os, getopt module
passed to the Python program. argv contains in Python? Explain.
bl
all the items that come along via the command- Ans. This module provides access to some variables
Pu
line input, it's basically an array holding the used by the interpreter and to functions that
command-line arguments of the program. interact strongly with the interpreter.
Part - IV sys.argv :
Answer the following questions
ra
204
[email protected] Ph: 81242 01000 / 812430 1000 / 96001 75757
OS :
www.surabooks.com This material only for sample
Sura’s
www.surabooks.com
ns
(ii) For Example to compile C++ program g++
will be passed as string and ‘i’ also will be
compiler should be invoked. To do so the
passed along with to indicate it as the input
following command is used.
io
file.
os.system (‘g++’ + <varaiable_name1> at
‘-<mode>’ + <variable_name2> getopt() method returns value consisting of
getopt module : two elements. Each of these values are stored
ic
(i) The getopt module of Python helps you to separately in two different list (arrays) opts and
parse (split) command-line options and args. Opts contains list of splitted strings like
bl
(i) This method parses command-line options For example, The Python code which is going
and parameter list. Following is the syntax to execute the C++ file p4 in command line will
Su
for this method − have the getopt() method like the following one.
(ii) <opts>,<args>=getopt.getopt(argv, options,
opts, args = getopt.getopt (argv, "i:",['ifile='])
[long_options])
4. Write the syntax for getopt() and explain its where opts [('-i', 'c:\\pyprg\\p4')]
arguments and return values. contains
Ans. getopt.getopt method : This method parses -i :- option nothing but
command-line options and parameter list. mode should be
followed by :
Following is the syntax for this method −
<opts>,<args>=getopt.getopt(argv, options, 'c:\\pyprg\\p4' value nothing but the
absolute path of C++
[long_options])
file.
205
[email protected] Ph: 81242 01000 / 812430 1000 / 96001 75757
Unit V - Chapter 14
www.surabooks.com
www.nammakalvi.org
import sys, os, getopt
def main(argv):
the second argument args will be empty []. If
cpp_file = ''
args is displayed using print() command it dis-
exe_file = ''
plays the output as [].
opts, args = etopt.getopt(argv, "i:",['ifile='])
5. Write a Python program to execute the for o, a in opts:
following c++ coding if o in ("-i", "--ifile"):
#include <iostream> cpp_file = a + '.cpp'
exe_file = a + '.exe'
using namespace std;
run(cpp_file, exe_file)
int main() def run(cpp_file, exe_file):
{ cout<<“WELCOME”; print("Compiling " + cpp_file)
return(0); os.system('g++ ' + cpp_file + ' -o ' + exe_ file)
ns
} print("Running " + exe_file)
print("-------------------------")
The above C++ program is saved in a file
print
io
welcome.cpp
os.system(exe_file)
Ans. #Now select File→New in Notepad and type the
print
Python program as main.py
at
if __name__ =='__main__':
# Program that compiles and executes a .cpp file
ic
main(sys.argv[1:])
# Python main.py -i welcome
bl
Pu
Choose the Correct Answer 1 MARK 4. In which language datatype or not required
ra
206
[email protected] Ph: 81242 01000 / 812430 1000 / 96001 75757
Unit V - Chapter 14
www.surabooks.com
www.nammakalvi.org
Write a note on
(i) CD command
1. Differentiate static typed language and (ii) Cls command
dynamic typed language. Ans. (i) C
D command : The "cd" command refers to
Ans. (i) A static typed language like C++ requires changes directory and absolute path refers to
the programmer to explicitly tell the the complete path where Python is installed.
computer what “data type” each data value (ii) Cls command : To clear the screen in
is going to use. command window use cls command
(ii) A dynamic typed language like Python, 8. What is meant by module?
doesn’t require the data type to be given Ans. (i) Modular programming is a software design
technique to split your code into separate
explicitly for the data. Python manipulate parts.
the variable based on the type of value. (ii) These parts are called modules. The focus
for this separation should have modules
2. List some scripting language.
with no or just few dependencies upon
Ans. The most widely used scripting languages are
ns
other modules.
JavaScript, VBScript, PHP, Perl, Python, Ruby,
9. Write a note on main (sys.argv [1]).
ASP and Tcl.
Ans. main(sys.argv[1]) : Accepts the program file
io
3. What is garbage collection in python? at (Python program) and the input file (C++ file)
Ans. (i) Python deletes unwanted objects (built-in as a list(array). argv[0] contains the Python
program which is need not to be passed because
types or class instances) automatically to
by default __main__ contains source code
ic
free the memory space. reference and argv[1] contains the name of the
bl
(ii) The process by which Python periodically C++ file which is to be processed.
frees and reclaims blocks of memory that 10. Write the syntax and example of
Pu
210
[email protected] Ph: 81242 01000 / 812430 1000 / 96001 75757
www.surabooks.com This material only for sample www.surabooks.com
Chapter DATA MANIPULATION
15 THROUGH SQL
CHAPTER SNAPSHOT
www.nammakalvi.org
15.1 Introduction
15.2 SQLite
15.3 Creating a Database using SQLite
ns
15.4 SQL Query Using Python
15.5 The SQL AND, OR and NOT Operators
15.6 Querying A Date Column
io
15.7 Aggregate Functions
at
15.8 Updating A Record
15.9 Deletion Operation
ic
15.10 Data input by User
15.11 Using Multiple Table for Querying
bl
Evaluation
ra
[215]
www.nammakalvi.org
4. Write the command to populate record in a
table. Give an example.
(a) Add() (b) SUM() Ans. To populate (add record) the table "INSERT"
(c) AVG() (d) AVERAGE() command.
[Ans. (c) AVG()] Example :
7. The function that returns the largest value of connection = sqlite3.connect ("Academy.db")
the selected column is cursor = connection.cursor()
(a) MAX() (b) LARGE() 5. Which method is used to fetch all rows from
(c) HIGH() (d) MAXIMUM()
the database table?
[Ans. (a) MAX()]
Ans. cursor.fetchall() - fetchall () method is to fetch
8. Which of the following is called the master all rows from the database table.
table?
(a) sqlite_master (b) sql_master
Part - III
(c) main_master (d) master_main Answer the following questions
[Ans. (b) sql_master] (3 marks)
ns
9. The most commonly used statement in SQL is 1. What is SQLite? What is it advantage?
(a) cursor (b) select Ans. (i) SQLite is a simple relational database
io
(c) execute (d) commit system, which saves its data in regular data
[Ans. (b) select] at files or even in the internal memory of the
10. Which of the following clause avoid the computer.
duplicate? (ii) It is designed to be embedded in
ic
(a) Distinct (b) Remove applications, instead of using a separate
database server program such as MySQLor
bl
1. Mention the users who use the Database. Ans. (ii) cursor.fetchone() - The fetchone () method
Ans. Users of database can be human users, other
returns the next row of a query result set or
Su
ns
Ans. import sqlite3 (viii) For executing the command use the
connection = sqlite3 . connect ("organization.db") cursor method and pass the required sql
io
cursor = connection . cursor () command as a parameter. Many number of
cursor. execute ("SELECT * FROM Employee commands can be stored in the sql_comm
ORDER BY Eno DESC")
at and can be executed one after other.
result = cursor . fetchall () (ix) Any changes made in the values of the
ic
print (*result, sep = "\n") record should be saved by the commend
bl
1. Write in brief about SQLite and the steps used Icode Item Name Rate
ra
217
[email protected] Ph: 81242 01000 / 812430 1000 / 96001 75757
www.surabooks.com This material only for sample
ns
3. Which method has a major role in working (c) fectchmany () (d) fetchall ()
with python? [Ans. (d) fetchall ()]
(a) cursor ()
io
11. Which SQlite method is used to fetch the
(b) connect ()
required number of rows in the database table?
(c) execute ()
(d) close
at (a) fetch () (b) fetchamany ()
[Ans. (a) cursor ()] (c) fetchrows () (d) tablerows ()
ic
[Ans. (b) fetchamany ()]
4. The SQlite command opens the already created
bl
open the already created database from 13. Which SQlite keyword is used to fetch only the
the statement connection = sqlite3.connect unique values from the database table?
("ABC.db")
Su
Chapter DATA VISUALIZATION USING PYPLOT:
16 LINE CHART, PIE CHART AND BAR CHART
CHAPTER SNAPSHOT
www.nammakalvi.org
16.1 Data Visualization Definition
16.2 Getting Started
16.3 Special Plot Types
ns
Evaluation
io
Part - I
at
Choose the best answer (1 mark) 4. Read the following code: Identify the purpose of
ic
this code and choose the right option from the
1. Which is a python package used for 2D
bl
following.
graphics?
(a) matplotlib.pyplot (b) matplotlib.pip C:\Users\Your Name\AppData\Local Programs\
Pu
packages, or modules.
(c) Install PIP
(a) Matplotlib (b) PIP
Su
3. Read the following code: Identify the purpose 5. To install matplotlib, the following function will
of this code and choose the right option from be typed in your command prompt. What does
the following. “-U”represents?
C : \ Us e r s \ Yo u r N a m e \ A p p D a t a \ L o c a l Python –m pip install –U pip
Programs\Python\Python36-32\Scripts>pip
–version (a) downloading pip to the latest version
(a) Check if PIP is Installed (b) upgrading pip to the latest version
(b) Install PIP (c) removing pip
(c) Download a Package (d) upgrading matplotlib to the latest version
(d) Check PIP version [Ans. (b) upgrading pip to the latest version]
[Ans. (a) Check if PIP is Installed]
[223]
Part - II
This material only for sample
Sura’s
www.surabooks.com
Data Visualization Using Pyplot: Line Chart, Pie Chart and Bar Chart
Answer the following questions functions: plt.plot([1,2,3,4]), plt.plot([1,2,3,4],
[1,4,9,16]).
(2 marks) Ans.
plt.plot([1,2,3,4]) plt.plot([1,2,3,4],
1. Define: Data Visualization.
[1,4,9,16])
Ans. Data Visualization is the graphical representation After installing A single list or array
of information and data. The objective of Data Matplotlib, we will provided to the plot ()
Visualization is to communicate information begin coding by command, matplotlib
visually to users. For this, data visualization assumes it is a sequence of
importing Matplotlib
uses statistical graphics. Numerical data may y values, and automatically
using the command: generates the x values for
be encoded using dots, lines, or bars, to visually import matplotlib. you. Since python ranges
communicate a quantitative message. pyplot as plt start with 0, the default x
vector has the same length
2. List the general types of data visualization. Now you have
as y but starts with 0.
ns
Ans. (i) Charts imported Matplotlib Hence the x data are
(ii) Tables in your workspace. [0, 1, 2, 3].
io
You need to display plot() is a versatile
(iii) Graphs
the plots. Using command, and will take
(iv) Maps
at
Matplotlib from an arbitrary number of
(v) Infographics within a Python arguments.
ic
(vi) Dashboards script, you have This .plot takes many
to add plt.show() parameters, but the first
bl
3. List the types of Visualizations in Matplotlib. two here are 'x' and 'y'
method inside the file
Ans. There are many types of Visualizations under coordinates. This means,
Pu
4. How will you install Matplotlib? 1. Draw the output for the following data
visualization plot.
Ans. You can install the latest version of pip from
import matplotlib.pyplot as plt
your command prompt using the following plt.bar([1,3,5,7,9],[5,2,7,8,2], label="Example
command. one")
Python–m pip install –U pip plt.bar([2,4,6,8,10],[8,6,2,5,6],
label="Example two", color='g')
plt.legend()
225
[email protected] Ph: 81242 01000 / 812430 1000 / 96001 75757
Unit V - Chapter 16
www.surabooks.com
www.nammakalvi.org
Ans. import matplotlib.pyplot as plt
val = [29.2, 54.2, 6.3, 8.35]
plt.title('Epic Graph\nAnother Line! Whoa') label = ["sleeping", "playing", "working", eating"]
plt.show() plt.pie (val, label = label)
Ans. plt.axes (). set_aspect ("equal")
9
plt.title ("Interesting Graph Check it out")
8 plt.show()
7
6
Part - IV
5 Answer the following questions
4
3
(5 marks)
2 1. Explain in detail the types of pyplots using
1 Matplotlib.
ns
2 4 6 8 9 10 Ans. Matplotlib allows you to create different kinds of
The above code represents the following: plots ranging from histograms and scatter plots
to bar graphs and bar charts.
io
Lables → specifics labels for the bars. Line Chart :
Usage → Assign values to the labels specified. at (i) A Line Chart or Line Graph is a type of
Range → create sequence of numbers. chart which displays information as a series
2. Write any three uses of data visualization. of data points called ‘markers’ connected
ic
Ans. (i) Data Visualization help users to analyze by straight line segments.
bl
and interpret the data easily. (ii) A Line Chart is often used to visualize
(ii) It makes complex data understandable and a trend in data over intervals of time – a
usable.
Pu
226
[email protected] Ph: 81242 01000 / 812430 1000 / 96001 75757
www.surabooks.com This material only for sample
Data Visualization Using Pyplot: Line Chart, Pie Chart and Bar Chart
rectangular bars. Each bar has a height window.
corresponds to the value it represents. The Ans. Buttons in the output : In the output figure,
bars can be plotted vertically or horizontally.
you can see few buttons at the bottom left corner.
(iii) It’s useful when we want to compare a given
numeric value on different categories. To Let us see the use of these buttons.
make a bar chart with Matplotlib, we can Subplots
Home Button Pan Axis Button Button
use the plt.bar() function.
(iv) Program :
import matplotlib.pyplot as plt
# Our data Forward / Back Buttons Zoom
Button
Save the
Figure Button
labels = ["TAMIL", "ENGLISH", "MATHS", (i) Home Button → The Home Button will
"PHYSICS", "CHEMISTRY", "CS"] help one to begun navigating the chart. If
usage = [79.8, 67.3, 77.8, 68.4, 70.2, 88.5] you ever want to return back to the original
# Generating the y positions. Later, we'll view, you can click on this.
use them to replace them with labels.
ns
(ii) Forward/Back buttons → These buttons
y_positions = range (len(labels)) can be used like the Forward and Back
# Creating our bar plot buttons in browser. Click these to move
io
plt.bar (y_positions, usage) back to the previous point you were at, or
plt.xticks (y_positions, labels) at forward again.
plt.ylabel ("RANGE") (iii) Pan Axis → This cross-looking button
allows you to click it, and then click and
plt.title ("MARKS")
ic
drag graph around.
plt.show() (iv) Zoom → The Zoom button lets you click on
bl
Pie Chart : it, then click and drag a square would like
to zoom into specifically. Zooming in will
Pu
227
[email protected] Ph: 81242 01000 / 812430 1000 / 96001 75757
Unit V - Chapter 16
www.surabooks.com
Sura’s
This material only for sample
www.nammakalvi.org
ns
Visualization? [Ans. (d) Many]
(a) Graphs (b) Picture
11. Which of following is not a visualization under
io
(c) Maps (d) Infographics matplotlib?
[Ans. (b) Picture] at (a) Scatter plot (b) Table plot
4. Which of the following is the representation of (c) Histogram (d) Box plot
information in a graphic format? [Ans. (b) Table plot]
ic
(a) Info graphics (b) Graphics 12. Which of the following is a type of plot that
bl
5. Which of the following is a collection of (c) Line plot (d) Scatter plot
resources assembled to create a single unified [Ans. (d) Scatter plot]
visual display? 13. Which plot displays the distribution of data
ra
(a) Info graphics (b) Dashboard based on the five number summary?
(c) Graphics (d) Chats
(a) Scatter plot (b) Line plot
Su
www.nammakalvi.org
228
[email protected] Ph: 81242 01000 / 812430 1000 / 96001 75757