Associate Data Scientist (DataCamp)
1. Basic Python:
-Original list x also get altered in below: Original list x don’t get altered in below:
-You can also remove elements from your list. You can do this with the del statement:
x = ["a", "b", "c", "d"]
del(x[1])
-The ; sign is used to place commands on the same line.
-Help(function_name) will tell everything about a function or method
-Lists to Dictionary:
l1 = ['Moin', 'Rafi', 'Sid', 'Aneeq']
l2 = [24, 29, 26, 20]
d = dict(zip(l1, l2))
print(d)
>>>{'Moin': 24, 'Rafi': 29, 'Sid': 26, 'Aneeq': 20}
-List and dicts. have almost same methods
- Always intitialise variables to be used in While loop else, it will destroy you.
-Preparing List by loop:
L=[]
For i in range(10):
L.append(i)
Print(L)
>>> [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
2. Numpy:
-The major difference between lists and numpy arrays is that we can directly perform mathematical
functions on numpy arrays while not on lists, also speed is fast.
-np.shape is attribute of np array, that's why we use np before shape.
-we can enter various dtypes in an array but can't perform mathematical operations.
-Numpy arrays Filtering:
Associate Data Scientist (DataCamp)
-Example of Numpy Filtering:
import numpy as np
np_positions=np.array(positions)
np_heights=np.array(heights)
# Heights of the goalkeepers: gk_heights
gk_heights=np_heights[np_positions=='GK']
# Heights of the other players: other_heights
other_heights=np_heights[np_positions!='GK']
# Print out the median height of goalkeepers. Replace 'None'
print("Median height of goalkeepers: " + str(np.median(gk_heights)))
# Print out the median height of other players. Replace 'None'
print("Median height of other players: " + str(np.median(other_heights)))
-Max Fucntion:
x=100
y=min(x-1,x)
print(y)
>>>99
Associate Data Scientist (DataCamp)
3. Pandas
4. Developing Python Pakages
-For python to consider a directory as package, the directory must contain __init__.py file.
Importing patterns:
-from package.subpackage import function1 as f1
-To add documentation in your function add it in triple quotation after def line, while for
package add the same in __init__.py file. To access doc of a func, use help(func_name)
-‘pymet –w –o (style name i.e. Google, Numpydoc, Javadoc etc.) func/script name’ is used at top
of file/func to generate sample documentation in specified format which can be customized
accordingly.
-suppose we have a package ‘badobadi’. To make it installable we have to create a new
directory ‘badobadi’ i.e. outer folder with same name which shouldd contain ‘badobadi’ and a
script named ‘setup.py’.
Setup.py should include following code:
from setuptools import setup, find_packages
setup(author=’Chahat Fateh’,description=’Aye hye oye hoye’,
name=’badobadi’,versio=’0.1.0’,packages=find_packages(include=[‘badobadi’,’badobadi.*’]
#package & subpackages) #if our package uses other packages inside then add parameter
install_requires=[‘pandas>=1.0’,’matplotlib==2.2.1’] etc. similarly for python versions use
python_requires=’>=2.7,!=3.0.*’ etc. Aternatively after completion of package development
write: pip freeze>requirements.txt’. This will add a requirements file in you package
automaticaly
Associate Data Scientist (DataCamp)
-To install package in a py script i.e. example.py that is in dir_name directory (in same dir. Where
is our package badobdai) write below line in terminal:
‘pip install –e .’
-pip freeze shows all installed pacakges and versions.
-Similarly LICENSE,README.md (includes package details and example app.) and MANIFEST.in
(which condatins code: include LICENSE \n include README.md) files should be added in
badobadi (Outerfolder) folder of our package
-Now our package is ready for Distributions and Upload: For Distribution use following code in
terminal:
‘python setup.py sdist bdist_wheel’
We can upload on Github or PyPI (most common for pythonistas). For PyPI create a free account
and use code in terminal:
‘twine upload dist/*’