Walkthrough Workshops

Python Tutorial for "Python + Packet Analysis + Machine Learning"


--help

If you need help, just raise your hand! I promise, we've heard dumber questions.


Getting Started

Python is a popular programming language that is easy to learn! To get started, click on the terminal icon. It looks like this:

After the terminal opens, enter the following command:

$ python hello.py

Note that the dollar sign ($) is not literally typed. It just indicates that you should type a command in the terminal. Hit enter after each command to run it.

This command uses Python to run the script saved at the file hello.py. You should see the text "Hello, world!" printed to your screen. If you open the file, you’ll see some Python code:

print('Hello, world!')

This is a simple Python program that uses the print function to display the string "Hello, world!"

Now back at the terminal, type the python command and hit enter:

$ python

You will be taken to an interactive Python shell. You can use this to enter Python code directly in your terminal without having to create a separate file and run it.

In the Python shell, each prompt starts with the symbol:

>>>

This means that you should enter your Python command, or line of Python code, and hit enter to see the result.

You can do some basic math in Python:

>>> 1 + 1
2
>>> 4 * 5
20
>>> 7/2
3.5

Here, the + (plus sign), * (multiplication sign), and / (division sign) are all called operators. They perform operations – in this case, math operations on numbers.

Another operation we can do is assignment. The = operator assigns a value to a variable:

>>> x = 5

This assigns the value 5 to the variable x. We can also assign strings (text) to variables:

>>> event_name = 'DEFCON 32'

Then we can use the print function to print event_name:

>>> print(event_name)
DEFCON 32

The interactive Python shell is nice, but let’s get out of it and do some serious programming. Use CTRL + D or type quit() to exit:

>>> quit()

Back at the terminal, type the command:

$ cd Desktop/python_training

This uses the cd command to navigate to the training directory which contains Python exercise files. Then type the command:

$ jupyter notebook

This will start a web server that opens up a piece of browser software called Jupyter Notebook. This allows you to run small Python programs in your browser and is handy for displaying both text and runnable code in the same document.

In the left hand menu, double-click on the file tutorial.ipynb and use this file to continue the rest of the training:
MAKE SURE TO CLOSE THE NOTEBOOK SERVER WHEN YOU'RE DONE BEFORE MOVING ON!