Introduction to Python
IPython shell: place where you can type Python code and immediately see the results
Python script: text files with the extension .py -> list of Python commands that are executed ->
you need print() to see the answer
General
- Text files: -.py
- Use print() to generate output from script
- Multiply: *
- Square: **
Data types
- type(): to check what kind of type is the variable
- Float: represent a real number (can be an integer part and a fractional part, use a
point) -> float
- Integer: represents a whole number -> int
- String: represent text (you can use double and single quotes)-> str
- Boolean: True or False (capitalization is important) -> bool
- Different type = different behavior!
Python list
- [a, b, c ]: list, a collection of values (with a komma) -> contain any type and different
types
- You can use sublists
Subsetting lists
- First element is 0, second element is 1 etc.
- You can also call the last element -1 etc.
- Slicing: [start : end] -> the first element is included, the last is excluded ([3:5] means the
fourth and the fifth)
- You can also leave the start or end, then python takes it from the beginning or the end
Manipulating lists
- Change, add and remove list elements
- Fam[7] = 1.86 -> change the 8th element to another number
- Fam[0:2] = [“Lisa”, 1.74] -> change the two first elements
- List + list = pastes together in a single list
- del fam[2] -> remove the third element
,Functions
- Type()
- Function: piece of reusable code, solves particular task, call function instead of writing
code yourself
- max() -> to calculate maximum value in the list
- round() -> inputs are which number you want to round and how many digits, you
separate them with a comma, if you don’t specify the number of digits python rounds to
the closest integer
- len() -> to get the length of the list
Methods
- Methods: functions that belong to objects
- Index of a list: fam.index(“mom”) #calling the index of mom in the list fam
- fam.count(1.73) -> counting the times 1.73 is in the list
- sister.capitalize(“z”, “sa”) -> replaces the z with sa in a string
- Objects have methods associated, depending on type
- fam.append(“me”) -> adds me to the list
- place.upper() -> gives you capital letters
-
Packages
- Each script = module
- You need to install the packages:
- Download get-pip.py
- Terminal: python3 get-pip.py
- Pip3 install numpy
- Import package
- Import numpy
- nump.array([1, 2, 3])
- Import numpy as np
- np.array([1, 2, 3])
- From numpy import array -> only import the array function from the numpy
package
NumPy
- Calculations over entire arrays
- Easy and fast
- Installation: pip3 install numpy
- Import numpy as np
- NumPy arrays: only one type
- Als je nu np.array + np.array doet gaat ie wel elk getal optellen, bij array + array gaat hij
de getallen aan de lijst toevoegen
, Type of NumPy arrays
- numpy.ndarray : n dimensional package
- Np_2d -> 2d array
- Np_2d.shape -> gives the shape of the array
- Np_2d[0][2] -> gives the index of the first row, third column -> also an option: np_2d[0, 2]
- Np_2d[:, 1:3] -> gives you every row and the second to third column
- Np_2d[1, :] -> gives the full second row
- print(np_baseball.shape) -> geeft aantal rijen en kolommen
NumPy: basic statistics
- np.corrcoef() -> to check if things are correlated
- np. std() -> for standard deviation
- np.mean() -> for mean
- np.median() -> for median
- Np enforce single data type -> speed
Intermediate python
Data visualization:
- Very important in data analysis
- Explore data
- Report insights
Matplotlib
- Import matplotlib.pyplot as plt
- Year = [1950, 1970, 1990, 2010]
- Pop = [2.519, 3.692, 5.263, 6.972]
- plt.plot(year, pop)
- plt.show() -> shows the plot
Scatter plot
- plt.scatter(year, pop) -> makes a scatterplot instead of a line plot
- Plt.show
- plt.xscale(‘log’) -> makes it on a logarithmic scale
Histogram
- Explore dataset
- Get idea about distribution
- Import matplotlib.pyplot as plt
- help(plt.hist) -> x = list of values, second argument = bins
- Plt.hist(values, bins = 3) -> 3 bins
- Plt.show
- plt.clf() -> cleans a little bit
Customization
IPython shell: place where you can type Python code and immediately see the results
Python script: text files with the extension .py -> list of Python commands that are executed ->
you need print() to see the answer
General
- Text files: -.py
- Use print() to generate output from script
- Multiply: *
- Square: **
Data types
- type(): to check what kind of type is the variable
- Float: represent a real number (can be an integer part and a fractional part, use a
point) -> float
- Integer: represents a whole number -> int
- String: represent text (you can use double and single quotes)-> str
- Boolean: True or False (capitalization is important) -> bool
- Different type = different behavior!
Python list
- [a, b, c ]: list, a collection of values (with a komma) -> contain any type and different
types
- You can use sublists
Subsetting lists
- First element is 0, second element is 1 etc.
- You can also call the last element -1 etc.
- Slicing: [start : end] -> the first element is included, the last is excluded ([3:5] means the
fourth and the fifth)
- You can also leave the start or end, then python takes it from the beginning or the end
Manipulating lists
- Change, add and remove list elements
- Fam[7] = 1.86 -> change the 8th element to another number
- Fam[0:2] = [“Lisa”, 1.74] -> change the two first elements
- List + list = pastes together in a single list
- del fam[2] -> remove the third element
,Functions
- Type()
- Function: piece of reusable code, solves particular task, call function instead of writing
code yourself
- max() -> to calculate maximum value in the list
- round() -> inputs are which number you want to round and how many digits, you
separate them with a comma, if you don’t specify the number of digits python rounds to
the closest integer
- len() -> to get the length of the list
Methods
- Methods: functions that belong to objects
- Index of a list: fam.index(“mom”) #calling the index of mom in the list fam
- fam.count(1.73) -> counting the times 1.73 is in the list
- sister.capitalize(“z”, “sa”) -> replaces the z with sa in a string
- Objects have methods associated, depending on type
- fam.append(“me”) -> adds me to the list
- place.upper() -> gives you capital letters
-
Packages
- Each script = module
- You need to install the packages:
- Download get-pip.py
- Terminal: python3 get-pip.py
- Pip3 install numpy
- Import package
- Import numpy
- nump.array([1, 2, 3])
- Import numpy as np
- np.array([1, 2, 3])
- From numpy import array -> only import the array function from the numpy
package
NumPy
- Calculations over entire arrays
- Easy and fast
- Installation: pip3 install numpy
- Import numpy as np
- NumPy arrays: only one type
- Als je nu np.array + np.array doet gaat ie wel elk getal optellen, bij array + array gaat hij
de getallen aan de lijst toevoegen
, Type of NumPy arrays
- numpy.ndarray : n dimensional package
- Np_2d -> 2d array
- Np_2d.shape -> gives the shape of the array
- Np_2d[0][2] -> gives the index of the first row, third column -> also an option: np_2d[0, 2]
- Np_2d[:, 1:3] -> gives you every row and the second to third column
- Np_2d[1, :] -> gives the full second row
- print(np_baseball.shape) -> geeft aantal rijen en kolommen
NumPy: basic statistics
- np.corrcoef() -> to check if things are correlated
- np. std() -> for standard deviation
- np.mean() -> for mean
- np.median() -> for median
- Np enforce single data type -> speed
Intermediate python
Data visualization:
- Very important in data analysis
- Explore data
- Report insights
Matplotlib
- Import matplotlib.pyplot as plt
- Year = [1950, 1970, 1990, 2010]
- Pop = [2.519, 3.692, 5.263, 6.972]
- plt.plot(year, pop)
- plt.show() -> shows the plot
Scatter plot
- plt.scatter(year, pop) -> makes a scatterplot instead of a line plot
- Plt.show
- plt.xscale(‘log’) -> makes it on a logarithmic scale
Histogram
- Explore dataset
- Get idea about distribution
- Import matplotlib.pyplot as plt
- help(plt.hist) -> x = list of values, second argument = bins
- Plt.hist(values, bins = 3) -> 3 bins
- Plt.show
- plt.clf() -> cleans a little bit
Customization