6. Operators in python
Operator Syntax Description
Sum + Adds two numbers (or) strings
Subtraction - Only numbers
Product * Product of numbers, repetition of strings
Division / Division between numbers
Modulo % Remainder of division between two
numbers
Power ** Power of one number over another
7. Conditionals
• The if statement
Conditional Description
== Equal
!= Not Equal
> Greater than
< Less than
>= Greater than or equal to
<= Less than or equal to
8. Loops
• The for loop is of the following form.
• The range indicates the number of times a statement will be implemented.
• There is a colon value to indicate the start of for loop.
• The code below is executed many times. The number of times it gets executed
depends on the values specified in range.
• Note that the code below the for statement is indented.
9. Lists in python
• A list holds ordered collection of items.
• And item can be a string or a number
10. Dictionaries - Recap
• A dictionary consists of two things (a) keys (b) values
• Use strings to represent keys
• Values can be anything
11. Dictionaries - Recap
• Print a value in a dictionary
• Delete a value in a dictionary
• Print all keys of a dictionary
• Add values to a dictionary
12. Functions can accept and return multiple
values
• How would you call this function?
16. Exercise
Programming exercise 1:
a) Ask the user to input an integer N. Create a list with the length N, containing
random numbers between 0 and N*2. Make sure that there are no duplicates
in the list created.
b) Create dictionary N keys, where the keys are the values in the list generated
in the previous step. Values are random numbers between 0 and 10.
c) Create a list that contains the values of the dictionary created in the previous
step as its entries.
17. Pandas - Dataframe
• Pandas is useful and important for reading CSV files, the datasets
used for training models
18. Pandas - Dataframe
• Pandas is useful and important for reading CSV files, the datasets
used for training models
19. Pandas - Dataframe
• Pandas is useful and important for reading CSV files, the datasets
used for training models
23. Interpreting CSV Data - Properties
• len() - Returns the total amount of rows
• shape - Returns an object which contains the total number of rows and
columns
• head(n) - Retrieves the top n (Integer) rows
• info() - Displays all columns and their data types
• dtypes() - Retrieves the column title and its respective data type
• columns – Retrieves the column names
24. Pandas- Methods
• Creating a dataframe from scratch
Note: This is useful when you want to create a
dataframe and add data to it later
26. Adding Elements to CSV File
• Create new data and append (add) to current CSV File
• Data is added to the end (tail) of the DataFrame
• We can use lists!
• If no value is given for a column, it is empty
27. Adding Elements to CSV File
• Creating a new DataFrame, without reading a new CSV File
dataFrame = pd.DataFrame([[Data]], columns=[Columns])
• Data and Columns are just lists!
28. Constructing our new Data Frame
• We want to add a new player (new data) to our NBA CSV file
(existing data)
Ex:
new_player_columns = ['Name', 'Team', 'Number', 'Position', 'Age',
'Height', 'Weight', 'College', 'Salary']
new_player_data = ['Ray Allen', 'Boston Celtics', 10, "C", 24, "6-
6", 190, "Boston College", 800000]
29. Adding Elements to CSV File - String Concept
firstName = "Ray"
lastName = "Allen"
fullName = firstName + " " + lastName
print(fullName)
#Output:
# Ray Allen
• We can now think about this in terms of DataFrames!
30. Creating our new Data Frame
• Now we can make our new DataFrame using the data we made
newPlayerDataFrame = pd.DataFrame([new_player_data], columns= new_player_columns)
31. Combining DataFrames Together
• concat(parameters) - Takes a list of DataFrames and combines them,
we can pass in various parameters
combinedDataFrame = pd.concat([nbaDataFrame, new_player_dataframe])
print(combinedDataFrame.tail())
*Note - We print the tail as data is added to the end.
32. Pandas- Exercise
• Create an empty dataframe with the following columns
• [`num_1`, `num_2`, `num_3`]
• Generate random numbers and add 10 rows to the dataframe
34. Statistics of columns/features – how does it
help?
• Describes the range of different features
• Understand the range of feature the machine learning algorithm trained
with
• Variation within features
• Standard deviation indicates the variation of data within the range