Apuntes
Python
Contenido del apunte (4)
Página 1
Variables in python:
there are 4 data types float,int,str,boolean
to convert str(),bool(),int(),float()
Python List: created using [], a collection of python types
to get data of lists we use the index like list[1]
to get a subset of the list we use slicing list[start index: end index] where end index is exclusive
List manipulations: Add data to list -> list + [values]
delete data from list -> del(list[index])
change value at a particular index -> list[index] = new value
a list variable just refers to the memory where the collection is stored not to the exact values so if we will do something like this:
list _a = list_b so we are creating a new variable which also refers to the memory where this collection is stored. So any changes made to the list_b will also reflect in list_a, If that is what we don't want then we can use keyword list or we can use slicing
list_b = list(list_a) or list_a[:]
FUNCTIONS: built-in functions to help out
Python objects come up with certain methods corresponding to python data types
methods in python are functions corresponding o data types in python
Packages are simply the directory
e.g-> like numpy, matplotlib,skilit
Matplotlib.pyplot: Provides a MATLAB-like plotting framework
we use this framework to plot data by importing the framework (import matplotlib.pyplot as plt) and then calling the function plot
Plot: this function tells python to what to and how to plot, but to actually display the plot we need to the display function.
scatter: In place of plot we can use scatter function to show data by data points only
Página 2
Packages: directory of python scripts, comprises of methods functions and types for solving problems coming in a particular scope
Numpy package: stands for numeric python
numpy array contains values of single type only alike lists
ndarray states n-dimensional array
2d numpy arrays: to select values use the index like index[row, column]
to create an array using a list use np.array(list) in the same way we can create 2d arrays and to see how many rows and how many columns are contained in the array use the shape attribute.
Página 3
"MatplotLib: Used to create different plots in python
We need to import matplotlib.pyplot to use basic plotting functions:
to create a line plot we use plt.plot(x,y)
Scatter plot: It used to represent data using data points instead of lines
Histograms: these are used to draw bar graphs which are simply a visual representation of data showing how many data points fall in a particular range.
in histogram the range is decided by specifying bins, to create a histogram we use plt.hist(value,bins)
CUSTOMIZATION: Are used to annotate the plots
plt.xlabel(xlab)
plt.ylabel(ylab)
# Add title
plt.title(title)
Página 4
DICTIONARIES IN PYTHON:
it is set of key-value pairs where one can get the value by using the corresponding key e.g
dict = {"abc":1,"def":2,"ghi":3}
where abc, def and ghi are keys and 1,2,3 are values to get the value one should use the following syntax
dict["abc"] this will give 1, and to get all the keys of a particular list we can use keys method, i.e dict.keys()
-> To add a new value to a pre-existing list use following syntax
dist["xyz"] = 5
we can also check whether a particular key presents in a dictionary or not, by using ("def" in dict) this will return true
to update a value in dictionary use dict["abc"] = 8
to del ->de( dict["abc"])
Note: A dictionary can only contain immutable objects
we can also create nested dictionaries
Información
- Descripción
- Apunte sobre Python , creado por virat yogi el 01/04/2018.
- Creado por
- virat yogi
- Visitas
- 1
- Valoración