Program 1- Write programs to understand the use of Python Identifiers, Keywords,
Indentations, Comments in Python, Operators, Membership operator.
Program-
There are 33 keywords in Python 3.7. This number can vary slightly over the course of time.
False await else import pass
None break except in raise
True class finally is return
And continue for lambda try
As def from nonlocal while
Assert del global not with
Async elif if or yield
Python Identifiers
An identifier is a name given to entities like class, functions, variables, etc. It helps to
differentiate one entity from another.
global = 1
Output
File "<interactive input>", line 1
global = 1
SyntaxError: invalid syntax
1. We cannot use special symbols like !, @, #, $, % etc. in our identifier.
Program 8- Write programs to understand the use of Pandas for Reading and Writing Data
using CSV and Textual Files, HTML Files, XML, Microsoft Excel Files.
Write a CSV File
You can save your Pandas DataFrame as a CSV file with .to_csv():
>>>df.to_csv('[Link]')
That’s it! You’ve created the file [Link] in your current working directory. You can expand the
code block below to see how your CSV file should look:
[Link]/Hide
This text file contains the data separated with commas. The first column contains the row
labels. In some cases, you’ll find them irrelevant. If you don’t want to keep them, then you can
pass the argument index=False to .to_csv().
Read a CSV File
Once your data is saved in a CSV file, you’ll likely want to load and use it from time to time. You
can do that with the Pandas read_csv() function:
>>>
>>>df=pd.read_csv('[Link]',index_col=0)
>>>df
COUNTRY POP AREA GDP CONT IND_DAY
CHN China 1398.72 9596.96 12234.78 Asia NaN
IND India 1351.16 3287.26 2575.67 Asia 1947-08-15
USA US 329.74 9833.52 19485.39 [Link] 1776-07-04
IDN Indonesia 268.07 1910.93 1015.54 Asia 1945-08-17
BRA Brazil 210.32 8515.77 2055.51 [Link] 1822-09-07
PAK Pakistan 205.71 881.91 302.14 Asia 1947-08-14
NGA Nigeria 200.96 923.77 375.77 Africa 1960-10-01
BGD Bangladesh 167.09 147.57 245.63 Asia 1971-03-26
RUS Russia 146.79 17098.25 1530.75 NaN 1992-06-12
MEX Mexico 126.58 1964.38 1158.23 [Link] 1810-09-16
JPN Japan 126.22 377.97 4872.42 Asia NaN
DEU Germany 83.02 357.11 3693.20 Europe NaN
FRA France 67.02 640.68 2582.49 Europe 1789-07-14
GBR UK 66.44 242.50 2631.23 Europe NaN
ITA Italy 60.36 301.34 1943.84 Europe NaN
ARG Argentina 44.94 2780.40 637.49 [Link] 1816-07-09
DZA Algeria 43.38 2381.74 167.56 Africa 1962-07-05
CAN Canada 37.59 9984.67 1647.12 [Link] 1867-07-01
AUS Australia 25.47 7692.02 1408.68 Oceania NaN
KAZ Kazakhstan 18.53 2724.90 159.41 Asia 1991-12-16
Write an Excel File
Once you have those packages installed, you can save your DataFrame in an Excel file
with .to_excel():
>>>
>>>df.to_excel('[Link]')
The argument '[Link]' represents the target file and, optionally, its path. The above
statement should create the file [Link] in your current working directory. That file should
look like this:
The first column of the file contains the labels of the rows, while the other columns store data.
Read an Excel File
You can load data from Excel files with read_excel():
>>>
>>>df=pd.read_excel('[Link]',index_col=0)
>>>df
COUNTRY POP AREA GDP CONT IND_DAY
CHN China 1398.72 9596.96 12234.78 Asia NaN
IND India 1351.16 3287.26 2575.67 Asia 1947-08-15
USA US 329.74 9833.52 19485.39 [Link] 1776-07-04
IDN Indonesia 268.07 1910.93 1015.54 Asia 1945-08-17
BRA Brazil 210.32 8515.77 2055.51 [Link] 1822-09-07
PAK Pakistan 205.71 881.91 302.14 Asia 1947-08-14
NGA Nigeria 200.96 923.77 375.77 Africa 1960-10-01
BGD Bangladesh 167.09 147.57 245.63 Asia 1971-03-26
RUS Russia 146.79 17098.25 1530.75 NaN 1992-06-12
MEX Mexico 126.58 1964.38 1158.23 [Link] 1810-09-16
JPN Japan 126.22 377.97 4872.42 Asia NaN
DEU Germany 83.02 357.11 3693.20 Europe NaN
FRA France 67.02 640.68 2582.49 Europe 1789-07-14
GBR UK 66.44 242.50 2631.23 Europe NaN
ITA Italy 60.36 301.34 1943.84 Europe NaN
ARG Argentina 44.94 2780.40 637.49 [Link] 1816-07-09
DZA Algeria 43.38 2381.74 167.56 Africa 1962-07-05
CAN Canada 37.59 9984.67 1647.12 [Link] 1867-07-01
AUS Australia 25.47 7692.02 1408.68 Oceania NaN
KAZ Kazakhstan 18.53 2724.90 159.41 Asia 1991-12-16
ypoints = [Link]([0, 250])
[Link](xpoints, ypoints)
[Link]()
Result:
Plotting x and y points
The plot() function is used to draw points (markers) in a diagram.
By default, the plot() function draws a line from point to point.
The function takes parameters for specifying points in the diagram.
Parameter 1 is an array containing the points on the x-axis.
Parameter 2 is an array containing the points on the y-axis.
If we need to plot a line from (1, 3) to (8, 10), we have to pass two arrays [1, 8] and [3, 10] to
the plot function.
Program 2-Draw a line in a diagram from position (1, 3) to position (8, 10):
import [Link] as plt
import numpy as np
xpoints = [Link]([1, 8])
ypoints = [Link]([3, 10])
[Link](xpoints, ypoints)
[Link]()
Result:
Multiple Points
You can plot as many points as you like, just make sure you have the same number of points in
both axis.
Program 4-Draw a line in a diagram from position (1, 3) to (2, 8) then to (6, 1) and finally to
position (8, 10):
import [Link] as plt
import numpy as np
xpoints = [Link]([1, 2, 6, 8])
ypoints = [Link]([3, 8, 1, 10])
[Link](xpoints, ypoints)
[Link]()
Result:
Result:
Specify Which Grid Lines to Display
import numpy as np
import [Link] as plt
x = [Link]([80, 85, 90, 95, 100, 105, 110, 115, 120, 125])
y = [Link]([240, 250, 260, 270, 280, 290, 300, 310, 320, 330])
[Link]("Sports Watch Data")
[Link]("Average Pulse")
[Link]("Calorie Burnage")
[Link](x, y)
[Link](axis = 'x')
[Link]()
Matplotlib legend on bottom
To place the legend on the bottom, change the legend() call to:
[Link](loc='upper center', bbox_to_anchor=(0.5, -0.05), shadow=True, ncol=2)
Take into account that we set the number of columns two ncol=2 and set a shadow.
The complete code would be:
[Link]
importnumpyas np
y = [2,4,6,8,10,12,14,16,18,20]
y2 = [10,11,12,13,14,15,16,17,18,19]
x = [Link](10)
fig = [Link]()
ax = [Link](111)
[Link](x, y, label='$y = numbers')
[Link](x, y2, label='$y2 = other numbers')
[Link]('Legend inside')
[Link](loc='upper center', bbox_to_anchor=(0.5, -0.05), shadow=True, ncol=2)
[Link]()
Program-
Creating Bars
With Pyplot, you can use the bar() function to draw bar graphs:
Program 1-Draw 4 bars:
import [Link] as plt
import numpy as np
x = [Link](["A", "B", "C", "D"])
y = [Link]([3, 8, 1, 10])
[Link](x,y)
[Link]()
Result:
Creating Pie Charts
With Pyplot, you can use the pie() function to draw pie charts:
Program 2-A simple pie chart:
import [Link] as plt
import numpy as np
y = [Link]([35, 25, 25, 15])
[Link](y)
[Link]()
Result:
Create Histogram
In Matplotlib, we use the hist() function to create histograms.
The hist() function will use an array of numbers to create a histogram, the array is sent into the
function as an argument.
For simplicity we use NumPy to randomly generate an array with 250 values, where the values
will concentrate around 170, and the standard deviation is 10. Learn more about Normal Data
Distribution in our Machine Learning Tutorial.
Program 3-A Normal Data Distribution by NumPy:
import numpy as np
x = [Link](170, 10, 250)
print(x)
Result:
This will generate a random result, and could look like this:
[167.62255766 175.32495609 152.84661337 165.50264047 163.17457988
162.29867872 172.83638413 168.67303667 164.57361342 180.81120541
170.57782187 167.53075749 176.15356275 176.95378312 158.4125473
187.8842668 159.03730075 166.69284332 160.73882029 152.22378865
164.01255164 163.95288674 176.58146832 173.19849526 169.40206527
166.88861903 149.90348576 148.39039643 177.90349066 166.72462233
177.44776004 170.93335636 173.26312881 174.76534435 162.28791953
166.77301551 160.53785202 170.67972019 159.11594186 165.36992993
178.38979253 171.52158489 173.32636678 159.63894401 151.95735707
175.71274153 165.00458544 164.80607211 177.50988211 149.28106703
179.43586267 181.98365273 170.98196794 179.1093176 176.91855744
168.32092784 162.33939782 165.18364866 160.52300507 174.14316386
163.01947601 172.01767945 173.33491959 169.75842718 198.04834503
192.82490521 164.54557943 206.36247244 165.47748898 195.26377975
164.37569092 156.15175531 162.15564208 179.34100362 167.22138242
147.23667125 162.86940215 167.84986671 172.99302505 166.77279814
196.6137667 159.79012341 166.5840824 170.68645637 165.62204521
174.5559345 165.0079216 187.92545129 166.86186393 179.78383824
161.0973573 167.44890343 157.38075812 151.35412246 171.3107829
162.57149341 182.49985133 163.24700057 168.72639903 169.05309467
167.19232875 161.06405208 176.87667712 165.48750185 179.68799986
158.7913483 170.22465411 182.66432721 173.5675715 176.85646836
157.31299754 174.88959677 183.78323508 174.36814558 182.55474697
180.03359793 180.53094948 161.09560099 172.29179934 161.22665588
171.88382477 159.04626132 169.43886536 163.75793589 157.73710983
174.68921523 176.19843414 167.39315397 181.17128255 174.2674597
186.05053154 177.06516302 171.78523683 166.14875436 163.31607668
174.01429569 194.98819875 169.75129209 164.25748789 180.25773528
170.44784934 157.81966006 171.33315907 174.71390637 160.55423274
163.92896899 177.29159542 168.30674234 165.42853878 176.46256226
162.61719142 166.60810831 165.83648812 184.83238352 188.99833856
161.3054697 175.30396693 175.28109026 171.54765201 162.08762813
164.53011089 189.86213299 170.83784593 163.25869004 198.68079225
166.95154328 152.03381334 152.25444225 149.75522816 161.79200594
162.13535052 183.37298831 165.40405341 155.59224806 172.68678385
179.35359654 174.19668349 163.46176882 168.26621173 162.97527574
192.80170974 151.29673582 178.65251432 163.17266558 165.11172588
183.11107905 169.69556831 166.35149789 178.74419135 166.28562032
169.96465166 178.24368042 175.3035525 170.16496554 158.80682882