Python Basics
General information on using Python for Data Science and Machine Learning
Updated: 03 September 2023
Python Basics
Based on this Cognitive Class Course
Labs
Jupyter Notebooks with Examples on these can be found in the labs
folder
The Labs are from this Cognitive Class Course and are under the MIT License
Types
Hello World
We can simply print out a string in Python as follows
Python Version
We can check our version as follows
The sys
module is a built-in module that has many system specific parameters and functions
Comments
Comments can be done by using the #
Docstrings
Python also allows for use of docstrings which can appear immediately after a function, class definition, or at the top of a module, these are done as follows
Also note that Python uses '
and "
to mean the same thing
Types of Objects
Python is object oriented, and dynamically typed. We can get the type of a value in python with the type
function
We can get information about a type using the sys
object properties, for example
Type Conversion
We can use the following to convert between types
Expressions
Expressions in python can include integers, floats, and strings, depending on the operation
We can do the following
Integer division will round off to the nearest integer
It is also helpful to note that Python will obey BODMAS
Variables
Variables can simply be assigned without being defined first, and are dynamically types
In a notebook we can simply evaluate the value of a variable or expression by placing it as the last line of a cell
Strings
Defining Strings
Strings can be defined with either '
or "
, and can be a combination of any characters
Indexing
Strings are simply an ordered sequence of characters, we can index these as any other array with []
as follows
We can also index negatively as follows
Length
We can get the length of a string with len()
Slicing
We can slice strings as follows
Or generally as
Stride
We can also input the stride, which will select every nth value within a certain range
For example
Concatenation
We can concatenate strings as follows
Escape Characters
At times we may need to escape some characters in a Python string, these are as follows
Character | Escape |
---|---|
newline | <NEW LINE> |
\ | \ |
’ | \’ |
” | \” |
ASCII Bell | \a |
ASCII Backspace | \b |
ASCII FF | \f |
ASCII LF | \n |
ASCII CR | \r |
ASCII Tab | \t |
ASCII VT | \v |
Octal Character | \ooo |
Hex Character | \xhh |
We can also do multi line strings with the """
or '''
If we have a string that would otherwise need escaping, we can use a string literal as follows
String Operations
We have a variety of string operations such as
Tuples
Define
A tuple is a way for us to store data of different types, this can be done simply as follows
A key thing about tuples is that they are immutable. We can reassign the entire tuple, but not change its values
Indexing
We can index a tuple the same way as a string or list using positive or negative indexing
Concatenation
We can also concatenate tuples
Slice and Stride
We can slice and stride as usual with
Sorting
We can sort a tuple with the sorted
function
The sorted
function will return a list
Nesting
Since tuples can hold anything, they can also hold tuples
We can access elements of tuples with double indexing as follows
Lists
Defining
A list is an easy way for us to store data of any form, such as numbers, strings, tuples, and lists
Lists are mutable and have many operations that enable us to work with them more easily
Indexing
Lists can also be indexed using the usual method both negatively and positively
Operations
Slice and Stride
Extend
Extend will add each object to the end of the list
Append
Append will add the input as a single object to the last value of the list
Modify an element
List elements can be modified by referencing the index
Delete an Element
We can delete elements by index as well
String Splitting
We can split a string into a list as follows
Cloning
Lists are stored by reference in Python, if we want to clone a list we can do it as follows
Sets
A set is a unique collection of objets in Python, sets will automatically remove duplicate items
Defining a Set
Set Operations
Set from a List
We can create a set from a list with the set
function
Add Element
We can add elements to a set with
If the element already exists nothing will happen
Remove Element
We can remove an element from a set with
Check if Element is in Set
We can check if an element is in a set by using in
which will return a bool
Set Logic
When using sets we can compare them with one another
Intersection
We can find the intersection between sets with &
or with the intersection function
Difference
We can fin d the difference in a specific set relative to another set with
Which will give us the elements that set_1
has that set_2
does not
Union
We can get the union of two sets with
Superset
We can check if one set is a superset of another with
Subset
We can check if one set is a subset of another with
Dictionaries
Dictionaries are like lists, but store data by a key instead of an index
Keys can be strings, numbers, or any immutable object such as a tuple
Defining
We can define a dictionary as a set of key-value pairs
Accessing a Value
We can access a value by using its key, such as
Get All Keys
We can get all the keys in a dictionary as follows
Append a Key
Key-value pairs can be added to a dictionary as follows
Delete an Entry
We can delete an entry by key using
Verify that Key is in Dictionary
We can use the in
operator to check if a key exists in a dictionary
Conditions and Branching
Comparison Operators
We have a few different comparison operators which will produce a boolean based on their condition
Operation | Operator | i = 1 |
---|---|---|
equal | == | i == 1 |
not equal | != | i != 0 |
greater than | > | i > 0 |
less than | < | i < 2 |
greater than or equal | >= | i >= 0 and i >= 1 |
less than or equal | <= | i <= 2 and i <= 1 |
Logical Operators
Python has the following logical operators
Operation | Operator | i = 1 |
---|---|---|
and | and | i == 1 and i < 2 |
or | or | i == 1 or i == 2 |
not | not | not(i != 0) |
String Comparison
When checking for equality Python will check if the strings are the same
Comparing strings is based on the ASCII Code for the string, for example 'B' > 'A'
because the ASCII Code for B is 102 and A is 101
When comparing strings like this the comparison will be done in order of the characters in the string
Branching
Branching allows us to run different statements depending on a condition
If
The if statement will only run the code that forms part of its block if the condition is true
If-Else
An if-else can be done as follows
Elif
If we want to have multiple if conditions, but only have the first one that is true be executed we can do
Loops
For Loops
A for loop in Python iterates through a list and executes its internal code block
Range
If we want to iterate through the values without using a predefined list, we can use the range function to generate a list of values for us to to iterate through
The range
function works as follows
The range function only requires the stop value, the other two are optional,the stop value is not inclusive
Using this we can iterate through the values of our array as follows
While Loops
While loops will continue until the stop condition is no longer true
Functions
Defining
Functions in Python are defined and called as follows
We can have arguments in our function
Functions can also return values
A function can also have a variable number of arguments such as
The vals object will be taken in as a tuple
Function input arguments can also have default values as follows
Or with multiple arguments
Help
We can get help about a function by calling the help function
Will give us help about the print function
Scope
Functions have access to variables that are globally defined, as well as their own local scope. Locally defined variables are not accessible from outside the function unless we declare it as global as follows
Note that the global_var
will not be defined until our function is at least called once
Objects and Classes
Defining a Class
We can define a class Circle
which has a constructor, a radius and a colour as well as a function to increase its radius and to plot the Circle
We make use of matplotlib
to plot our circle here
Instantiating an Object
We can create a new Circle
object by using the classes constructor
Interacting with our Object
We can use the dir
function to get a list of all the methods on an object, many of which are defined by Python already
We can get our object’s property values by simply referring to them
We can also manually change the object’s properties with
We can call our object’s functions the same way
The red_circle
can be plotted by calling the draw_circle
function
Reading Files
Note that the preferred method for reading files is using with
Open
We can use the built-in open
function to read a file which will provide us with a File
object
The 'r'
sets open to read mode, for write mode we can use 'w'
, and 'a'
for append mode
Properties
File
objects have some properties such as
Read
We can read the file contents to a string with the following
Close
Lastly we need to close our File
object with
We can verify that the file is closed with
With
A better way to read files is by using using the with
statement which will automatically close the file, even if we encounter an exception
We can also read the file in by pieces either based on characters or on lines
Read File by Characters
We can read the first four characters with
Note that this will still continue to parse the file, and not start over each time we call read()
, so we can read the first seven characters is like so
Read File by Lines
Our File
object looks a lot like a list with each line a new element in the list
We can read our file by lines as follows
We can read each line of our file into a list with the readline
function like so
Or with the readlines
function like so
Writing Files
We can also make use of open to write content to a file as follows
The write
function works the same as the read
function in that each time we call it, it will just write a single line to the file, if we want to write multiple lines to our file w need to do this as follows
Copy a File
We can copy data from one file to another by simultaneously reading and writing between the files
Pandas
Pandas is a library that is useful for working with data as a DataFrame in Python
Importing Pandas
The Pandas library will need to be installed and then imported into our notebook as
Creating a DataFrame
We can create a new DataFrame in Pandas as follows
Read CSV as DataFrame
We can read a csv as a DataFrame with Pandas by doing the following
Read XLSX as DataFrame
We need to install an additional dependency to do this firstm and then read it with the pd.read_excel
function
View DataFrame
We can view the first few lines of our DataFrame as follows
Assume our data looks like the following
Name | Age | Height | |
---|---|---|---|
0 | John | 23 | 1.2 |
1 | Jack | 12 | 2.3 |
2 | Smith | 34 | 1.1 |
3 | Jenny | 13 | 1.6 |
4 | Maria | 42 | 0.5 |
Working with DataFrame
Assigning Columns
We can read the data from a specific column as follows
Age | |
---|---|
0 | 23 |
1 | 12 |
2 | 34 |
3 | 13 |
4 | 42 |
We can also assign multiple columns
Age | Height | |
---|---|---|
0 | 23 | 1.2 |
1 | 12 | 2.3 |
2 | 34 | 1.1 |
3 | 13 | 1.6 |
4 | 42 | 0.5 |
Reading Cells
We can read a specific cell in one of two ways. The iloc
fnction allows us to access a cell with the row and column index, and the loc
function lets us do this with the row index and column name
Slicing
We can also do slicing using loc
and iloc
as follows
Name | Age | |
---|---|---|
1 | Jack | 12 |
2 | Smith | 34 |
Age | Height | |
---|---|---|
0 | 23 | 1.2 |
1 | 12 | 2.3 |
2 | 34 | 1.1 |
Saving Data to CSV
Using Pandas, we can save our DataFrame to a CSV with
Arrays
The Numpy Library allows us to work with arrays the same as we would mathematically, in order to use Numpy we need to import it as follows
Arrays are similar to lists but are fixed size, and each element is of the same type
1D Arrays
Defining an Array
We can simply define an array as follows
Types
An array can only store data of a single type, we can find the type of the data in an array with
Manipulating Values
We can easily manipulate values in an array by changing them as we would in a list. The same can be done with splicing and striding operations
We can also use a list to select a specific indexes and even assign values to those indexes
Attributes
An array has various properties and functions such as
Array Operations
We have a few different operations on arrays such as
Linspace
The linspace
function can be used to generate an array with values over a specific interval
Plotting Values
We can apply a function to these values by using array operations, such as those mentioned above as well as others like
2D Arrays
Defining a 2D Array
Two dimensional Arrays can be defined by a list that contains nested lists of the same size as follows
We can similarly make use of the previously defined array operations
Accessing Values
Values in a 2D array can be indexed in either one of two ways
Slicing
We can perform slicing as follows
Mathematical Operations
We can perform the usual mathematical operations with 2D arrays as with 1D
Dancing Man
The following Script will make a dancing man if run in Jupyter > because why not