Path: blob/master/tutorials-and-examples/training-notebooks/A Python Crash Course - Part 1 - Fundamentals.ipynb
3253 views
Python Crash Course - Part 1 - Fundamentals
Requires
Introduction
This 📔 notebook takes you through the 👨🎓 learning the basic fundamentals of Python. Its an 👩💻 interactive self-paced tutorial within notebook. This course will have you learning variables, data types, loops in no time. 😋
Table of Contents
Getting Started
Variables
Data Types
Collections
Block Indentation
Be sure to click the table of contents under Menu -> View -> TOC to easily navigate the notebook.
[Microsoft Learn - Python learning modules] (https://docs.microsoft.com/learn/browse/?terms=python)
[Python.org - The Python Standard Library] (https://docs.python.org/3/library/index.html)
[Jupyter Notebooks Crash Course] (https://tacc.github.io/CSC2017Institute/docs/day1/jupyter.html)
Getting Started
If you haven't already, you may want to understand how to use Jupyter notebooks and more specifically Azure Notebooks by using this guide "Getting Started with Azure ML Notebooks and Microsoft Sentinel"
The order on when you run the each cells are important. Some cells will have a dependency on another cell in order to work.
Make sure you identify if the cell requires other cells to run, otherwise you may run into errors.
Navigation
| key | description |
|---|---|
| 🔼🔽 | Up/Down keys will navigate cell blocks |
a | Add a cell above highlighted cell |
b | Add a cell below highlighted cell |
d d | Delete a cell |
c | Copy a cell into clipboard |
x | Cut a cell into clipboard |
v | Paste a cell below selected cell |
z | Undo your boo boo |
Enter | Edit a cell |
Escape | Escape current cell in order to navigate |
Ctrl + Enter | Runs current cell and stays on the current cell. |
Shift + Enter | Runs current cell and moves once cell down after running |
m | Convert current cell to markdown cell format |
y | Convert current cell to code cell format |
Use your table contents to quickly browse to certain topics or areas of interest.
Lets get 😎fancy by showing you how to quickly navigate a notebook. You will notice that in whatever cell your working in, it is highlighted indicating its your current or selected cell.
🗺 Navigating - So lets press the
escapekey and navigate up and down by using our arrows 🔼 🔽➕ Adding Cells - Now, lets hit the
aorbkey. This will either add a cell above/below your currently selected cell when navigating.❌ Deleting - Highlight any cell when you are navigating and then press
dd. (Make sure you aren't editing a cellescape)📝 Editing - To edit a cell, just navigate to the cell and press
enterwhich puts you in edit mode.✂ Cutting/Pasting - Highlight a cell and press
corxto copy/cut a cell. Then navigate to where you want to paste it and pressvwhich will paste the cell below your selected/currentEditing - To edit a cell, just navigate to the cell and pressenterwhich puts you in edit mode.Undo - Oops, you made a mistake? 😓 Press z to undo that boo boo 💩.
☝ Above is a helpful reference guide. Try to get familiar with the ⌨ keyboard shortcuts to help you navigate, add, copy, paste, delete, and run cells.
Think you can try navigating the rest of the notebook without using your mouse 🐭 at all.
Remember to navigate using your arrows 🔼 🔽 and the enter/escape key to enter/exit a cell.
👋🏾 Hello World
In the code cell below, enter "Hello World" enclosed in some quotation marks.
This simply just prints/output the input you provided which was enclosed double quotes ".
But sometimes, you may want to print multiple outputs and control/modify the output. So more often you will utilize the print function. print("Hello World")
Can you modify
Hello World and print something else out?
Cell types
📓 Notebook cells can be in two different formats, markdown or code cells.
📃 Markdown - allows you to comment and document your notebook
Markdown Guide - The Markdown Guide is a free and open-source reference guide that explains how to use Markdown to format virtually any document.
👩🏾💻 Code Cells - code cells are the actual runnable code.
Press the
escapekey to ensure you are not editing a code cell and then press eithermoryto convert a cell to either format.How would you add a code cell
belowthis cell and convert it to a markdown cell using what shortcuts you have learned, ?
Comments
You will want to make it a habit to comment your code so you and others can otherstand what its purpose is.
Single line comments is using a # character and any following string after will be ignored
Using Help
The python help🆘 function is used to display the documentation of modules, functions, classes, keywords, etc.
Importing modules
A module is a file containing Python definitions and statements. There will be times you need to do something specific and most likely someone has already created a module you can use. In most cases you can save alot of time if there is a module that already exists you can leverage to help you get to your desired outcome faster 🚀. And who doesn't like to save time ⌚.
👍 Make sure package is installed
If you ever want to use a module, you first must install it. The most common way is to use a native package manager like pip to install the module so you can import it. Remember you can't use what you don't have!
Most of the time to install any module, you can reference the name your trying to import.
There is also a magic
%pip command that will ensure you install the module in the right kernel (rather than the instance of Python that launched the notebook)
Be sure its the first line you have on your cell block.
Sometimes the above command might not work 😫. It could because of mismatch if versions, OS, where notebook is hosted. In that case, you will use the following command to ensure pip installs the module on the python kernel you're running Jupyter Notebooks.
Or open a terminal 🖥 session and run this command so pip will load the module in the proper python kernel.
⏬ Import the modules you need
Once you confirm pip has installed the module ✔, those modules can now be used in the current notebook 📒.
You will import any module by using the import command along with the name of the module you want to import
Sometimes packages have different names than their importable name. E.g. pip install scikit-learn -> import sklearn
If you don't require the whole module library, its always best practice to specify exactly what you want to import
Notice also the line is shortened because you can call directly what you imported datetime instead of the full module path datetime.datetime
Importing module using as
To make it easier to call modules, shorten the module name by using
as to define an alias.
Restart the Kernel
If you are not able to use the module, it may require restarting the kernel. You can do that by importing IPython and restarting it or click on the restart icon 🔃
You will lose all cached variables when you restart the kernel.
Variables
A variable is a value that can change, depending on conditions or on information passed to the program
The Python interpreter automatically picks the most suitable built-in data-type for the variable if no type is provided
You cannot use python keywords as a variable name. You can see a list of keywords by typing
import keyword; print(keyword.kwlist)
Creating variables and assigning values
We first will assign Hello World to the variables s1,s2. We will then print the variables by referencing it in print. Try it below:
How would you print
Microsoft Sentinel using what you know?
Assigning multiple variables
You can assign multiple values to multiple variables in one line.
Note that there must be the same number of arguments on the right and left sides of the = operator:
Exercise: Try assigning an additional variable with value to this list
Remember pretend the
mouse 🖱 has feelings and doesn't want to be touched, Practice using your keyboard ⌨ only.
Temporary variables
It is common to have temporary or dummy variable assignments for a cell. It is conventional to use the underscore for these type of unwanted values.
Exercise: change the unwanted variable
_ to the result of b-a
Data Types
| Data Type | Description | Example |
|---|---|---|
| int | Integers, floating-point, complex numbers | 1,2,3,4,5 |
| bool | an expression that results in a value of either true/false. | True/False |
| str | strings of characters | "John" |
| list | mutable sequence that group various objects together | [1,True,"John"] |
| dict | Dictionary | {'name':'red','age':10} |
| tuple | similar to list but they are immutable. | (123,'hello') |
| set | unordered and mutable collections of arbitrary but hashable Python objects | {"apple","banana","orange"} |
[Documentation - python.org - Data Types] (https://docs.python.org/3/library/datatypes.html)
String
Strings can be declared a number of ways.
Single quotes
Double quotes
Triple double quotes
Why would you want different ways of declaring a string?
Answer: Because this allows you to embed quotes inside a string by declaring it with another way. You cannot use the same quotes you used to declare the string because it would end the string and the output would not be what you wanted.
Press
escape if you need to and navigate ⬇. Press enter to edit the cell and try running the current cell. Can you fix it using what we learned?
Escaping special characters
If your string has special characters or punctuation, you may have problems using it. In order to do that, you will need to use the backslash \ key. This tells python to ignore the character that follows the backslash.
Try removing the backslash and run the cell. What happens?
Inserting values into a string and evaluating expressions
Notice how we prefix f in front the string "You are {age} years old.". This allows us to evaluate any expression within curly braces { }. In this case, we grab the value for variable age and insert it into the string when evaluated.
Numeric data
There are three types of numbers used in Python: integers, floating, and complex numbers.
Run the cell below
(shift-enter). Make a guess what the type() command does?
Here are some arithmetic operations you can perform
| Operator | Name | Description |
|---|---|---|
a + b | Addition | Sum of a and b |
a - b | Subtraction | Difference of a and b |
a * b | Multiplication | Product of a and b |
a / b | True division | Quotient of a and b |
a // b | Floor division | Quotient of a and b, removing fractional parts |
a % b | Modulus | Integer remainder after division of a by b |
a ** b | Exponentiation | a raised to the power of b |
-a | Negation | The negative of a |
Boolean
| Operation | Description | Operation | Description |
|---|---|---|---|
| a == b | a equal to b | a != b | a not equal to b |
| a < b | a less than b | a > b | a greater than b |
| a <= b | a less than or equal to b | a >= b | a greater than or equal to b |
▶ Run the following 👇🏾 cell block and identify why each expression either resolves true or false.
Combining Boolean values
You can combine boolean values by using and,or, and not
Here 5 > 1 evaluates to true, but "apple" == "banana evaluates to false. This then results to false.
Run the following cell 👇🏾, and then see if you can make the result of both conditions evaluate to
True.
Converting Data Types
There may be situations you will need to convert a data type to another in order for the script to work.
Can you try converting a number/string with a decimal to an integer? What happens?
Here are some more data types to practice with
Collections
| Name | sort | indexed | mutable | duplicates |
|---|---|---|---|---|
| List | ✅ ordered | ✅ indexed | ✅changeable | ✅Allows duplicate members |
| Tuple | ✅ ordered | ✅ indexed | ❌unchangeable | ✅Allows duplicate members |
| Set | ❌ unordered | ❌Unindexed | ✅changeable | ❌No duplicate members |
| Dictionary | ✅ ordered | ✅ indexed | ✅changeable | ✅No duplicate keys, but duplicate values are ok |
There will be situations where a certain collection type makes more sense than another. Example is you want a set of distinct items with no duplicates set. Or you want a collection that you can grab the age or name of a person in a collection dictionary.
List
| Name | description | sort | indexed | mutable | duplicates |
|---|---|---|---|---|---|
| List | ordered sequence of values | ✅ ordered | ✅ indexed | ✅changeable | ✅Allows duplicate members |
A list is a sequence, where each element is assigned a position (index)
First position is 0. You can access each position using
Allright, first lets create a list
Notice how a list is enclosed in square brackets [ ].
Run the cell below to create a list, which also will be referenced in the following cells. Any variables created in the session, could be referenced later as long as the kernel was not restarted.
Access items from list
You can access items in a list with the same [ ] square brackets, but inside you put the index location of what items you want returned.
Lets print the third item in the list
Note: The very first position in a list is
0, so the third item in the list is pulled by referencing2
🏃🏾♀️ Run the following cell, but how would you print the last item on the list?
You are ✔ correct if you answered:
But you can also print the last item on the list by using a -1
Using what you know, how would you get the 2nd from the last item on the list
You would reference the index
-2 since any negative value starts from the right. print(my_list[-2])
Adding item to the list
You can add item to the list using the append() method.
Modify a list
You may want to modify values in a list
Removing item from list
Remove the first two items on the list using a range
[<start>:<finish>]
Merging Lists together
When you want to merge two lists together, you can simply use
+Note: There are still duplicates when lists are merged.
Lets create list x,y and another variable myList that is a combination of both.
Can you add more items to the list or even make another list and merge all 3 together using what we learned?
Slicing and Dicing a list
There may be situations where you don't want the first item in a list or only want the first 5. You will need to reference the range you want after the list variable.
Lets slice (extract) the first and second item in the list
Checking if list contains a value
You can check if a list contains a item by using in
Modify the line and search for the number
10in the list.How would you an
andconditional to check for two values
Counting
Count how many items in a list by using len(<list>)
Count how many times an item is in the list list_name.count("<what_to_search_for>")
Count how many times the number 5 shows up in the list.
Getting the highest value in a list
To get the highest number in the list you will use the max() function. You cannot calculate max if the values are not all numeric.
Lists can have various data types
Elements in the lists can be different data types, but be careful of performing any calculation with mixed data types.
Sets
| Name | sort | indexed | mutable | duplicates |
|---|---|---|---|---|
| Set | ❌ unordered | ❌Unindexed | ✅changeable | ❌No duplicate members |
Sets are similar to list but is an unordered collection data type that is iterable, mutable and has no duplicate elements.
Unordered and sets cannot change once the set has been created.
Lets create a set from the list and notice duplicates are removed
Modify the line and add another fruit to the basket.
Create set from string
Run the following cell and do you understand whats happening ? Why is the list shorter 🤔?
Add item to set
Making a set immutable (Cant be modified)
If you ever want to declare a list and ensure the list is not modified, you can freeze the list using frozenset(<set_name>)
Notice we add
Dallas to the set cities. Uncomment # the commented line #frozen_cities.add("Tampa") and run the cell again. What happens?
Dictionary
| Name | sort | indexed | mutable | duplicates |
|---|---|---|---|---|
| Dictionary | ❌ unordered | ✅ indexed | ✅changeable | ✅No duplicate members |
Dictionary consists of key-value pairs {<key> : <value>}. It is enclosed by curly braces {} and values can be assigned and accessed using square brackets [].
Create a dictionary
Lets create a dictionary object with a name and age key
Getting a property
Lets grab the age property from this dictionary object by reference the right key which in this case its the string "age" enclosed in double quotes .
Get list of keys/values
To get the list of keys or values only, you would specify the method
Get list of keys
Get list of values
Adding to dictionary
To add to the list, you simply just reference an unused key name in the dictionary object
Try adding another item to the dictionary object.
Tuple
| Name | sort | indexed | mutable | duplicates |
|---|---|---|---|---|
| Tuple | ✅ ordered | ✅ indexed | ❌unchangeable | ✅Allows duplicate members |
Tuples are like lists except cannot be changed
After running the previous cell, try updating the tuple by running the cell below
What happens when you run the cell and do you know why it happened based on how
tuples work?
Block Indentation
Python uses indentation to define and control function, loops, and conditions. This makes Python easy to read, but the user must play close attention to the whitespace (space/tabs) usage. Always use 4 spaces for indentation and most code editors are able to convert tabs to spaces to reduce any error.
Do not mix tab and spaces. Stay consistent. Indentation Errors will occur or cause the program to do something unexpected
Functions
A function is a block of code which only runs when it is called. This is helpful to run repeatable blocks of code you plan on using often.
You can pass data, known as parameters, into a function.
A function can return data as a result.
Creating a functions
Declare the function name with Syntax
Then any lines you want to belong to the function is associated by using 4 empty spaces
Call a function
Call your custom function you declared by referencing the name of the function ping()
Passing information to functions
Information can be passed into functions as arguments.
How would you pass another value to the greeting function and print it out on the response?
Example: Print a response like, "Hello
<name>, You are<age>years old"
You can also pass multiple arguments by separating each value with a comma
Can you pass third argument and print it in the output when the function is called?
Commenting in functions
Docstrings are not like regular comments. They are stored in the function documentation. You can then use help() to get some helpful guidance on the particular function
Comment your function
Get help details on function
Multi-level indentation and nesting
You can nest conditions, loops, and functions within each other. If a function is not indented to the same level it will not be considers as part of the parent class
This will call the function separateFunction() and provide the list [2,3,5,6,1] as an input argument for the function separateFunction()
Try searching for another
item in the array, or add an additional argument in separateFunction() to search against that argument instead of the static value 1.
Passing values into a function
Default values in a function
You can provide default values if you want to assume certain values. Then if input IS provided, it overrides the static default value.
First lets create another function that has a default value for the greeting. We also will call help() function to get the documentation of the greet() function.
Lets call the function first by providing no input or arguments
This time lets call the function greet() but also provide the arguments name='john' and greeting='sup'
We can pass arguments based on the position in the list
Or we can directly specify what value is for what argument (which now doesn't need to be in the proper list position)
Try passing different arguments in different positions. Or modify the function and add another argument that will be used in the response
Conditional statements
if/else/elif Example
Exercise: Run the cell below shift-enter, How would you modify the if statement to evaluate to false?
Creating a conditional statement
You will nest lines that you want included in each condition evaluated
Run the following command and see if you can make the expression evaluate false?
One-liner if statement (no indentation)
Most style guides and linters (error checking) discourage using this as its not the best practice 👎. Try using lines and spaces as its easier to read for everyone else 💓
You can write the if statement to one line if there isn't a need for nesting multiple lines within the statement.
👍 Even shorter if statement for default values.
You don't have to write a whole conditional statement if all you want is to check if a value exist or use a default value if the condition evaluates to false
The value of result is "A is bigger than B" only if the expression in the following if statement evaluates to True, otherwise the value following else is used.
Try writing another statement that maybe evaluates a string or collection.
Using conditional statement to validate an input provided
Example of searching a list based on the input provided
When this cell is ran, the userInput variable will call the input() function and request input from the user. After the input is provided, the cell will take the input value and check if it it matches any item in the list my_list.
Loops
Loops are simply computer instruction that will keep repeating until a specified condition is reached.
For Loops
Used for interating over a sequence (list, tuple, dictionary, set) and runs a set of statements, once for each item in the list. The loop will end when it reaches the last item on the list its iterating.
Simple For Loop
We will loop starting at 0 and end at 3. On each iteration, we will print the current value of x
How would you make the for loop iterate 10 times instead of 3?
You can process any iterable (string, list, dict, etc) with a for loop
First we create the list of 5 values
[5,10,15,20,25]then we will use the
forloop to iterate through each item in the list and print the string for each item.
Nested for Loop
Can you nest another
condition or loop nested inside this for loop and output the values?
Using For loop to iterate through array of dictionary objects
First we initialize a
list(array)of dictionary objects with key value pairsname, capital, countryThen we use a
for loopto iterate through each state and reference thekeyswithin each dictionary item being iterated to retrieve the values.
Can you add another
key to each dictionary object and print that key value when its been iterated in the for loop?
Iterating through dictionary key/values
Let's use the same list, but iterate it a different way. Lets iterate each state and print the key/value pair for each item.
We first will iterate through each
statein thestateslist.We then grab each
key and valuefromstate.items()We reference directly the
key and valuevariables instead of callingstate[<key>]
Using For loop to enumerate through dictionary keys
Or maybe you want the index location of each key in the list. You can enumerate the dictionary
We first will iterate through each
statein thestateslist.We iterate through each state then grab the
index and keyfromenumerate(state)We now can reference the index location of the
keysand use thekeyto reference properties in thestatevariable.
Using Loop to modify list values
You can use a loop to iterate through a list and modify values or execute lines for each item being looped.
Import the datetime module and create the
years array
Import the datetime module to be used in the years array.
yearsarray is created that will used as the input to theforloopThis example will iterate through each
yearin the years list[1955,1987,1978,2019,1967,1955].Inside this loop we first will calculate the
currentDatevariable by calling the importeddatetimefunction and extracting theyear.We can then calculate the age by subtracting the
currentDatefor eachyearbeing looped.Then we will print a line that outputs these variables being iterated.
The last command to run is
printingout the loop has completed.
While Loop
Most of the time you will be using for loops. This loop will continue to execute the nested statements (indented) in the while loop until a specified condition is true
Example
This
while loop example will run the lines nested in the while loop until i reaches a value that is greater than 10
Finished
You are done 🎈🏁🐱👓 with part 1 of this Python Crash Course and you are well on your way to knowing Python.
Have some cake to celebrate 🍰. If you are still thirsty🧉 for more, you should now be able to step into the other Notebooks 📓 and it may actually make sense!
Remember to check back as the 2nd part of the series will focus on building on top of this knowledge by learning how to manipulate data using:
We will use this knowledge to manipulate and restructure data in order to render charts and visualize output for investigation or sending it.
Keep learning! ❤