Latihan Python
February 28, 2024
1 Latihan Python
Nama : Istiqomah
Nim : 115090010
Kelas: EL-43-D
1.1 Installing and Import Python
1.1.1 Installing library
[ ]: pip install pandas
Requirement already satisfied: pandas in
/Users/istiqomah/anaconda3/lib/python3.10/site-packages (1.5.3)
Requirement already satisfied: python-dateutil>=2.8.1 in
/Users/istiqomah/anaconda3/lib/python3.10/site-packages (from pandas) (2.8.2)
Requirement already satisfied: pytz>=2020.1 in
/Users/istiqomah/anaconda3/lib/python3.10/site-packages (from pandas) (2022.7)
Requirement already satisfied: numpy>=1.21.0 in
/Users/istiqomah/anaconda3/lib/python3.10/site-packages (from pandas) (1.22.3)
Requirement already satisfied: six>=1.5 in
/Users/istiqomah/anaconda3/lib/python3.10/site-packages (from python-
dateutil>=2.8.1->pandas) (1.16.0)
Note: you may need to restart the kernel to use updated packages.
[ ]: pip list
Package Version
----------------------------- -----------
absl-py 1.4.0
alabaster 0.7.12
anaconda-client 1.11.2
anaconda-navigator 2.4.0
anaconda-project 0.11.1
anyio 3.5.0
appdirs 1.4.4
applaunchservices 0.3.0
appnope 0.1.2
1
appscript 1.1.2
argon2-cffi 21.3.0
argon2-cffi-bindings 21.2.0
array-record 0.2.0
arrow 1.2.3
astroid 2.14.2
astropy 5.1
asttokens 2.0.5
astunparse 1.6.3
atomicwrites 1.4.0
attrs 22.1.0
Automat 20.2.0
autopep8 1.6.0
Babel 2.11.0
backcall 0.2.0
[Link]-lru-cache 1.6.4
[Link] 1.0
[Link] 1.0.post1
bcrypt 3.2.0
beautifulsoup4 4.11.1
binaryornot 0.4.4
black 22.6.0
bleach 4.1.0
bokeh 2.4.3
boltons 23.0.0
Bottleneck 1.3.5
brotlipy 0.7.0
cachetools 5.3.0
certifi 2023.5.7
cffi 1.15.1
chardet 4.0.0
charset-normalizer 2.0.4
click 8.0.4
cloudpickle 2.0.0
clyent 1.2.2
colorama 0.4.6
colorcet 3.0.1
comm 0.1.2
conda 23.3.1
conda-build 3.24.0
conda-content-trust 0.1.3
conda-pack 0.6.0
conda-package-handling 2.0.2
conda_package_streaming 0.7.0
conda-repo-cli 1.0.41
conda-token 0.4.0
conda-verify 3.4.2
constantly 15.1.0
2
contourpy 1.0.5
cookiecutter 1.7.3
cryptography 39.0.1
cssselect 1.1.0
cycler 0.11.0
cytoolz 0.12.0
dask 2022.7.0
datashader 0.14.4
datashape 0.5.4
debugpy 1.5.1
decorator 5.1.1
defusedxml 0.7.1
diff-match-patch 20200713
dill 0.3.6
distributed 2022.7.0
dm-tree 0.1.8
docstring-to-markdown 0.11
docutils 0.18.1
entrypoints 0.4
et-xmlfile 1.1.0
etils 1.3.0
executing 0.8.3
fastjsonschema 2.16.2
filelock 3.9.0
flake8 6.0.0
Flask 2.2.2
flatbuffers 23.5.9
flit_core 3.6.0
fonttools 4.25.0
fsspec 2022.11.0
future 0.18.3
gast 0.4.0
gensim 4.3.0
glob2 0.7
gmpy2 2.1.2
google-auth 2.18.1
google-auth-oauthlib 1.0.0
google-pasta 0.2.0
googleapis-common-protos 1.59.0
greenlet 2.0.1
grpcio 1.54.2
h5py 3.6.0
HeapDict 1.0.1
holoviews 1.15.4
huggingface-hub 0.10.1
hvplot 0.8.2
hyperlink 21.0.0
idna 3.4
3
imagecodecs 2021.8.26
imageio 2.26.0
imagesize 1.4.1
imbalanced-learn 0.10.1
importlib-metadata 4.11.3
importlib-resources 5.12.0
incremental 21.3.0
inflection 0.5.1
iniconfig 1.1.1
intake 0.6.7
intervaltree 3.1.0
ipykernel 6.19.2
ipython 8.10.0
ipython-genutils 0.2.0
ipywidgets 8.0.6
isort 5.9.3
itemadapter 0.3.0
itemloaders 1.0.4
itsdangerous 2.0.1
jax 0.4.10
jedi 0.18.1
jellyfish 0.9.0
Jinja2 3.1.2
jinja2-time 0.2.0
jmespath 0.10.0
joblib 1.1.1
json5 0.9.6
jsonpatch 1.32
jsonpointer 2.1
jsonschema 4.17.3
jupyter_client 7.3.4
jupyter-console 6.6.3
jupyter_core 5.2.0
jupyter-server 1.23.4
jupyterlab 3.5.3
jupyterlab-pygments 0.1.2
jupyterlab_server 2.19.0
jupyterlab-widgets 3.0.7
keras 2.12.0
keyring 23.4.0
kiwisolver 1.4.4
lazy-object-proxy 1.6.0
libarchive-c 2.9
libclang 16.0.0
llvmlite 0.39.1
locket 1.0.0
lxml 4.9.1
lz4 3.1.3
4
Markdown 3.4.1
MarkupSafe 2.1.1
matplotlib 3.7.0
matplotlib-inline 0.1.6
mccabe 0.7.0
mistune 0.8.4
ml-dtypes 0.1.0
mock 4.0.3
mpmath 1.2.1
msgpack 1.0.3
multipledispatch 0.6.0
munkres 1.1.4
mypy-extensions 0.4.3
navigator-updater 0.3.0
nbclassic 0.5.2
nbclient 0.5.13
nbconvert 6.5.4
nbformat 5.7.0
nest-asyncio 1.5.6
networkx 2.8.4
nltk 3.7
notebook 6.5.2
notebook_shim 0.2.2
numba 0.56.4
numexpr 2.8.4
numpy 1.22.3
numpydoc 1.5.0
oauthlib 3.2.2
openpyxl 3.0.10
opt-einsum 3.3.0
packaging 22.0
pandas 1.5.3
pandocfilters 1.5.0
panel 0.14.3
param 1.12.3
parsel 1.6.0
parso 0.8.3
partd 1.2.0
pathlib 1.0.1
pathspec 0.10.3
patsy 0.5.3
pep8 1.7.1
pexpect 4.8.0
pickleshare 0.7.5
Pillow 9.4.0
pip 22.3.1
pkginfo 1.9.6
platformdirs 2.5.2
5
plotly 5.9.0
pluggy 1.0.0
ply 3.11
pooch 1.4.0
poyo 0.5.0
prometheus-client 0.14.1
promise 2.3
prompt-toolkit 3.0.36
Protego 0.1.16
protobuf 4.23.1
psutil 5.9.0
ptyprocess 0.7.0
pure-eval 0.2.2
py 1.11.0
pyasn1 0.4.8
pyasn1-modules 0.2.8
pycodestyle 2.10.0
pycosat 0.6.4
pycparser 2.21
pyct 0.5.0
pycurl 7.45.1
PyDispatcher 2.0.5
pydocstyle 6.3.0
pyerfa 2.0.0
pyflakes 3.0.1
Pygments 2.11.2
PyHamcrest 2.0.2
PyJWT 2.4.0
pylint 2.16.2
pylint-venv 2.3.0
pyls-spyder 0.4.0
pyobjc-core 9.0
pyobjc-framework-Cocoa 9.0
pyobjc-framework-CoreServices 9.0
pyobjc-framework-FSEvents 9.0
pyodbc 4.0.34
pyOpenSSL 23.0.0
pyparsing 3.0.9
PyQt5-sip 12.11.0
pyrsistent 0.18.0
PySocks 1.7.1
pytest 7.1.2
python-dateutil 2.8.2
python-lsp-black 1.2.1
python-lsp-jsonrpc 1.0.0
python-lsp-server 1.7.1
python-slugify 5.0.2
python-snappy 0.6.1
6
pytoolconfig 1.2.5
pytz 2022.7
pyviz-comms 2.0.2
PyWavelets 1.4.1
PyYAML 6.0
pyzmq 23.2.0
QDarkStyle 3.0.2
qstylizer 0.2.2
QtAwesome 1.2.2
qtconsole 5.4.0
QtPy 2.2.0
queuelib 1.5.0
regex 2022.7.9
requests 2.28.1
requests-file 1.5.1
requests-oauthlib 1.3.1
requests-toolbelt 0.9.1
rope 1.7.0
rsa 4.9
Rtree 1.0.1
[Link] 0.17.21
[Link] 0.2.6
ruamel-yaml-conda 0.17.21
ruptures 1.1.8
scikit-image 0.19.3
scikit-learn 1.2.2
scipy 1.10.0
Scrapy 2.8.0
seaborn 0.12.2
Send2Trash 1.8.0
service-identity 18.1.0
setuptools 65.6.3
sip 6.6.2
six 1.16.0
smart-open 5.2.1
sniffio 1.2.0
snowballstemmer 2.2.0
sortedcontainers 2.4.0
soupsieve 2.3.2.post1
Sphinx 5.0.2
sphinxcontrib-applehelp 1.0.2
sphinxcontrib-devhelp 1.0.2
sphinxcontrib-htmlhelp 2.0.0
sphinxcontrib-jsmath 1.0.1
sphinxcontrib-qthelp 1.0.3
sphinxcontrib-serializinghtml 1.1.5
spyder 5.4.1
spyder-kernels 2.4.1
7
SQLAlchemy 1.4.39
stack-data 0.2.0
statsmodels 0.13.5
sympy 1.11.1
tables 3.7.0
tabulate 0.8.10
TBB 0.2
tblib 1.7.0
tenacity 8.0.1
tensorboard 2.12.3
tensorboard-data-server 0.7.0
tensorflow-datasets 4.9.2
tensorflow-estimator 2.12.0
tensorflow-macos 2.12.0
tensorflow-metadata 1.13.1
tensorflow-metal 0.8.0
termcolor 2.3.0
terminado 0.17.1
text-unidecode 1.3
textdistance 4.2.1
threadpoolctl 2.2.0
three-merge 0.1.1
tifffile 2021.7.2
tinycss2 1.2.1
tldextract 3.2.0
tokenizers 0.11.4
toml 0.10.2
tomli 2.0.1
tomlkit 0.11.1
toolz 0.12.0
torch 1.12.1
tornado 6.1
tqdm 4.64.1
traitlets 5.7.1
transformers 4.24.0
Twisted 22.2.0
typing_extensions 4.4.0
ucimlrepo 0.0.3
ujson 5.4.0
Unidecode 1.2.0
urllib3 1.26.14
w3lib 1.21.0
watchdog 2.1.6
wcwidth 0.2.5
webencodings 0.5.1
websocket-client 0.58.0
Werkzeug 2.2.2
whatthepatch 1.0.2
8
wheel 0.38.4
widgetsnbextension 4.0.7
wrapt 1.14.1
wurlitzer 3.0.2
xarray 2022.11.0
xlwings 0.29.1
yapf 0.31.0
zict 2.1.0
zipp 3.11.0
[Link] 5.4.0
zstandard 0.19.0
Note: you may need to restart the kernel to use updated packages.
1.1.2 Import library
[ ]: import pandas as pd
import numpy as np
import matplotlib as plot
1.2 Variable
[ ]: x = "Hallo Word"
print (x)
print (type(x))
Hallo Word
<class 'str'>
[ ]: x = 20
print (x)
print (type(x))
20
<class 'int'>
[ ]: x = 20.5
print (x)
print (type(x))
20.5
<class 'float'>
[ ]: x = 20 + 2j
print (x)
print (type(x))
(20+2j)
<class 'complex'>
9
[ ]: x = ['Sepedah','Motor', 'Mobiil']
print (type(x))
<class 'list'>
[ ]: print (x[1:3])
['Motor', 'Mobiil']
[ ]: x = ['Sepedah','Motor', 1]
print (type(x))
<class 'list'>
[ ]: print (x[2])
[ ]: x[1]= 'ganti'
print (x)
['Sepedah', 'ganti', 1]
[ ]: x = ('Sepedah','Motor', 1)
print(x)
('Sepedah', 'Motor', 1)
[ ]: x[1]= 'ganti'
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-16-6c3ed76a0c63> in <module>
----> 1 x[1]= 'ganti'
TypeError: 'tuple' object does not support item assignment
List and tupple is array, the diffrences is list is mutable and tupple is immutable
Link reference
[ ]: x = {"Nama": "Istiqomah", "Kelas": "EL-43-D", "Nim": "115090010"}
print (x)
print (type(x))
{'Nama': 'Istiqomah', 'Kelas': 'EL-43-D', 'Nim': '115090010'}
<class 'dict'>
[ ]: print ([Link]())
dict_keys(['Nama', 'Kelas', 'Nim'])
10
[ ]: for key in [Link]():
print(x[key])
Istiqomah
EL-43-D
115090010
[ ]: print (x['Nama'])
Istiqomah
Link reference
[ ]: x = {'Sepedah','Motor', 'Mobiil'}
[ ]: print (x)
print (type(x))
{'Sepedah', 'Motor', 'Mobiil'}
<class 'set'>
[ ]: print (x[2])
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-26-7efdf4864447> in <module>
----> 1 print (x[2])
TypeError: 'set' object is not subscriptable
[ ]: print (x)
{'Sepedah', 'Motor', 'Mobiil'}
[ ]: [Link]('Truk')
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-28-bf911adb1f2b> in <module>
----> 1 [Link]('Truk')
AttributeError: 'set' object has no attribute 'append'
[ ]: x = True
print (x)
print (type(x))
11
True
<class 'bool'>
[ ]: x = str(10)
print (x)
print (type(x))
10
<class 'str'>
[ ]: x = range(10)
print (x)
print (type(x))
range(0, 10)
<class 'range'>
[ ]: x = range(5,10)
print (x)
print (type(x))
range(5, 10)
<class 'range'>
[ ]: x = range(5,50,5)
print (x)
print (type(x))
range(5, 50, 5)
<class 'range'>
[ ]: for i in x :
print (i)
5
10
15
20
25
30
35
40
45
[ ]: for i in range(len(x)):
print (i)
0
1
2
3
12
4
5
6
7
8
1.3 Control Flow
[ ]: x=int(input("Please enter an integer: "))
if x % 2 == 0:
print('your input is even number')
elif x % 2 == 1:
print('your input is odd number')
Please enter an integer: 50
your input is even number
[ ]: x=int(input("Please enter an integer: "))
nilai = x % 2
print('your input is even number') if nilai == 0 else print('your input is odd␣
↪number')
Please enter an integer: 50
your input is even number
[ ]: x = ['Sepedah','Motor', 'Mobiil']
for word in x :
print(word)
Sepedah
Motor
Mobiil
[ ]: x = {"Nama": "Istiqomah", "Kelas": "EL-43-D", "Nim": "115090010"}
for word in [Link]() :
print(word)
Nama
Kelas
Nim
1.4 Function
[ ]: def nilaiMaxlist(nilai):
return (max(nilai))
[ ]: Hasil = nilaiMaxlist([10,11,122])
print(Hasil)
122
13
[ ]: def nilaiMaxlist(nilai):
print(max(nilai))
[ ]: nilaiMaxlist([10,11,122])
122
1.5 Numpy
[ ]:
[ ]:
1.6 Pandas
[ ]: pdFromCSV = pd.read_excel("[Link]")
pdFromCSV
[ ]: umur_rumah jarak_terdekat_MRTstation Jumlah_minimarket latitude \
0 16.4 289.3248 5 24.98203
1 16.2 2103.5550 3 24.96042
2 37.7 490.3446 0 24.97217
3 13.6 4197.3490 0 24.93885
4 13.6 319.0708 6 24.96495
.. … … … …
202 31.7 1159.4540 0 24.94960
203 11.6 201.8939 8 24.98489
204 27.6 515.1122 5 24.96299
205 30.9 6396.2830 1 24.94375
206 17.4 995.7554 0 24.96305
longitude harga_rumah_per_unit
0 121.54348 41.2
1 121.51462 25.6
2 121.53471 37.0
3 121.50383 13.0
4 121.54277 47.4
.. … …
202 121.53018 13.8
203 121.54121 55.9
204 121.54320 37.4
205 121.47883 12.2
206 121.54915 25.5
[207 rows x 6 columns]
[ ]: import seaborn as sns
iris = sns.load_dataset('iris')
14
[Link]()
[ ]: sepal_length sepal_width petal_length petal_width species
0 5.1 3.5 1.4 0.2 setosa
1 4.9 3.0 1.4 0.2 setosa
2 4.7 3.2 1.3 0.2 setosa
3 4.6 3.1 1.5 0.2 setosa
4 5.0 3.6 1.4 0.2 setosa
[ ]: type(iris)
[ ]: [Link]
[ ]: [Link][:,:2]
[ ]: sepal_length sepal_width
0 5.1 3.5
1 4.9 3.0
2 4.7 3.2
3 4.6 3.1
4 5.0 3.6
.. … …
145 6.7 3.0
146 6.3 2.5
147 6.5 3.0
148 6.2 3.4
149 5.9 3.0
[150 rows x 2 columns]
[ ]: from [Link] import load_digits
digits = load_digits()
digits
[ ]: {'data': array([[ 0., 0., 5., …, 0., 0., 0.],
[ 0., 0., 0., …, 10., 0., 0.],
[ 0., 0., 0., …, 16., 9., 0.],
…,
[ 0., 0., 1., …, 6., 0., 0.],
[ 0., 0., 2., …, 12., 0., 0.],
[ 0., 0., 10., …, 12., 1., 0.]]),
'target': array([0, 1, 2, …, 8, 9, 8]),
'frame': None,
'feature_names': ['pixel_0_0',
'pixel_0_1',
'pixel_0_2',
'pixel_0_3',
'pixel_0_4',
15
'pixel_0_5',
'pixel_0_6',
'pixel_0_7',
'pixel_1_0',
'pixel_1_1',
'pixel_1_2',
'pixel_1_3',
'pixel_1_4',
'pixel_1_5',
'pixel_1_6',
'pixel_1_7',
'pixel_2_0',
'pixel_2_1',
'pixel_2_2',
'pixel_2_3',
'pixel_2_4',
'pixel_2_5',
'pixel_2_6',
'pixel_2_7',
'pixel_3_0',
'pixel_3_1',
'pixel_3_2',
'pixel_3_3',
'pixel_3_4',
'pixel_3_5',
'pixel_3_6',
'pixel_3_7',
'pixel_4_0',
'pixel_4_1',
'pixel_4_2',
'pixel_4_3',
'pixel_4_4',
'pixel_4_5',
'pixel_4_6',
'pixel_4_7',
'pixel_5_0',
'pixel_5_1',
'pixel_5_2',
'pixel_5_3',
'pixel_5_4',
'pixel_5_5',
'pixel_5_6',
'pixel_5_7',
'pixel_6_0',
'pixel_6_1',
'pixel_6_2',
'pixel_6_3',
16
'pixel_6_4',
'pixel_6_5',
'pixel_6_6',
'pixel_6_7',
'pixel_7_0',
'pixel_7_1',
'pixel_7_2',
'pixel_7_3',
'pixel_7_4',
'pixel_7_5',
'pixel_7_6',
'pixel_7_7'],
'target_names': array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]),
'images': array([[[ 0., 0., 5., …, 1., 0., 0.],
[ 0., 0., 13., …, 15., 5., 0.],
[ 0., 3., 15., …, 11., 8., 0.],
…,
[ 0., 4., 11., …, 12., 7., 0.],
[ 0., 2., 14., …, 12., 0., 0.],
[ 0., 0., 6., …, 0., 0., 0.]],
[[ 0., 0., 0., …, 5., 0., 0.],
[ 0., 0., 0., …, 9., 0., 0.],
[ 0., 0., 3., …, 6., 0., 0.],
…,
[ 0., 0., 1., …, 6., 0., 0.],
[ 0., 0., 1., …, 6., 0., 0.],
[ 0., 0., 0., …, 10., 0., 0.]],
[[ 0., 0., 0., …, 12., 0., 0.],
[ 0., 0., 3., …, 14., 0., 0.],
[ 0., 0., 8., …, 16., 0., 0.],
…,
[ 0., 9., 16., …, 0., 0., 0.],
[ 0., 3., 13., …, 11., 5., 0.],
[ 0., 0., 0., …, 16., 9., 0.]],
…,
[[ 0., 0., 1., …, 1., 0., 0.],
[ 0., 0., 13., …, 2., 1., 0.],
[ 0., 0., 16., …, 16., 5., 0.],
…,
[ 0., 0., 16., …, 15., 0., 0.],
[ 0., 0., 15., …, 16., 0., 0.],
[ 0., 0., 2., …, 6., 0., 0.]],
17
[[ 0., 0., 2., …, 0., 0., 0.],
[ 0., 0., 14., …, 15., 1., 0.],
[ 0., 4., 16., …, 16., 7., 0.],
…,
[ 0., 0., 0., …, 16., 2., 0.],
[ 0., 0., 4., …, 16., 2., 0.],
[ 0., 0., 5., …, 12., 0., 0.]],
[[ 0., 0., 10., …, 1., 0., 0.],
[ 0., 2., 16., …, 1., 0., 0.],
[ 0., 0., 15., …, 15., 0., 0.],
…,
[ 0., 4., 16., …, 16., 6., 0.],
[ 0., 8., 16., …, 16., 8., 0.],
[ 0., 1., 8., …, 12., 1., 0.]]]),
'DESCR': ".. _digits_dataset:\n\nOptical recognition of handwritten digits
dataset\n--------------------------------------------------\n\n**Data Set
Characteristics:**\n\n :Number of Instances: 1797\n :Number of Attributes:
64\n :Attribute Information: 8x8 image of integer pixels in the range
0..16.\n :Missing Attribute Values: None\n :Creator: E. Alpaydin (alpaydin
'@' [Link])\n :Date: July; 1998\n\nThis is a copy of the test set of the
UCI ML hand-written digits datasets\n[Link]
ical+Recognition+of+Handwritten+Digits\n\nThe data set contains images of hand-
written digits: 10 classes where\neach class refers to a digit.\n\nPreprocessing
programs made available by NIST were used to extract\nnormalized bitmaps of
handwritten digits from a preprinted form. From a\ntotal of 43 people, 30
contributed to the training set and different 13\nto the test set. 32x32 bitmaps
are divided into nonoverlapping blocks of\n4x4 and the number of on pixels are
counted in each block. This generates\nan input matrix of 8x8 where each element
is an integer in the range\n0..16. This reduces dimensionality and gives
invariance to small\ndistortions.\n\nFor info on NIST preprocessing routines,
see M. D. Garris, J. L. Blue, G.\nT. Candela, D. L. Dimmick, J. Geist, P. J.
Grother, S. A. Janet, and C.\nL. Wilson, NIST Form-Based Handprint Recognition
System, NISTIR 5469,\n1994.\n\n.. topic:: References\n\n - C. Kaynak (1995)
Methods of Combining Multiple Classifiers and Their\n Applications to
Handwritten Digit Recognition, MSc Thesis, Institute of\n Graduate Studies in
Science and Engineering, Bogazici University.\n - E. Alpaydin, C. Kaynak (1998)
Cascading Classifiers, Kybernetika.\n - Ken Tang and Ponnuthurai N. Suganthan
and Xi Yao and A. Kai Qin.\n Linear dimensionalityreduction using relevance
weighted LDA. School of\n Electrical and Electronic Engineering Nanyang
Technological University.\n 2005.\n - Claudio Gentile. A New Approximate
Maximal Margin Classification\n Algorithm. NIPS. 2000.\n"}
[ ]:
[ ]:
18
1.7 Matplotlib
[ ]: import [Link] as plt
[Link]()
[Link]([Link][1])
[Link]()
<Figure size 432x288 with 0 Axes>
[ ]: [Link][1]
[ ]: 1
[ ]:
[ ]:
19