Python is one of the easiest languages to learn so if you’re a beginner, you’re in the right place!
The controls of Jupyter Notebook are pretty straightforward. If you’re using Anaconda your toolbar should look something like this.
On a browser, it will look like this
To start, make sure your notebook is set to the Code mode, not Markdown, Raw, or Heading. It should be on Code by default. Starting on the left, the save button creates checkpoints in your progress. You can also use ctrl or cmd S on your keyboard. Jupyter is constantly autosaving but creating checkpoints allows you to revert back to a stage of your code. The plus button allows you to add a new cell of code, while the scissors delete a cell. Next is copy and paste, followed by the controls to move cells up and down.
The controls you will use most often are run, stop, restart, and restart then run. Code doesn’t output anything until you run it. Most often, you’ll use the shift enter/return keys instead of pressing run. The stop and restart controls are used when your code outputs an error, outputs nothing (keeps running), or outputs the wrong thing.
Now that we understand the controls, let's get coding!
The first thing we will learn is the print
function. A print function is how we output information in Python. Type print (”Hello World”)
in your notebook or copy and paste the code below
print ("Hello World")
<aside> ⚠️ If it didn’t work, make sure you use quotations and brackets as shown above. Syntax is very important when coding.
</aside>
Congrats! You just wrote your first code. We can use variables to store string (text) and then print the variables. In Python, the equal sign = is used to assign things to variables. Let’s try assigning your name to the variable name
. Using the name Jeff as an example, this would look like name = ”Jeff”
. We can then print our name using print (name)
. When you’re print a variable there is no need for quotations. Use your own name and try this out.
name = "Jeff"
print (name)
<aside> ⚠️ Python runs from top to bottom. If you try printing a variable before you define it, the code will run an error.
</aside>
Let’s combine variables and strings to create a sentence. To do this we will use a variable between 2 strings separated by commas
name = "Jeff"
print ("My name is", name,"and I love coding!")
Now it’s your turn. Use the variables age
and flavour
to print how old you are and your favourite flavour of ice cream. Your output should be “I’m age years old **and flavour is the best flavour of ice cream”. Try doing this on your own before checking the answer here.