The following page contains a basic introduction to coding with Python. If you feel confident with core Python concepts, feel free to have a quick look through and then move to the next page on Python on the Raspberry Pi.
Python - On the Raspberry Pi
Python - What is it?
“What's a coder's favourite animal? The Python”
- Tell me, what do your coffee machine, the rover on Mars, the computer you are reading this on, and a house security system have in common? Code. In this day and age of technological advancements, code has become the lifeblood of every computer, every device and, of course every smart robot you see
- Python is a worldwide favourite for both new and advanced coders due to how frictionless it is to understand and how powerful its system is when used properly. This page will give you a basic introduction to coding with Python, from the basics of variables and loops to importing libraries and declaring functions.
- It is suggested you download Thonny for your Python coding; however, you can write Python code online using programwiz or any standard IDE (Visual Studio code, Jupiter, ETC). It is also suggested you play around with these concepts yourself as the best way to learn is to do.
Variables
- Variables are one of the most important concepts to understand in Python. In coding, a variable is simply a metaphorical box to store a value; we can place a different value in this box or use the value however we please. We can also name these variables whatever we want.
- We can store many “types” of things in these variables. The main ones we will use are:
- Integers - a number with no decimal points like 1 or 2
- Floats - numbers with decimal points such as 1.1
- String - words such as ”Hello World”
- Boolean - a true or false variable that is either True or False
- While other languages require you to say if a variable is a number or a word, Python can just figure it out as your code runs.
var1 = 1 # Store the integer one in var1
var2 = 1.1 # Store the float value in var2
var3 = "Hello World" # Store the phrase "Hello World" in var3
var4 = True
Operators
“When they are a (54 - 2)/(2^2) - 3 😍”
- Operators are mathematic operations that can be applied to variables.
- Most symbols are consistent with basic mathematics; however, there are some unique ones you should know for programming (listed on the right).
- The plus symbol can also be used to join strings together as shown to the right
var = 1 + 2 # Addition
var = "Hello " + "world" # Creates "Hello world"
var = 20 - 10 # Subtraction
var = 5 * 6 # Multiplication
var = 10 / 2 # Division
var = 7 % 3 # Division Remainder (e.g. 3 % 2 = 1)
var = 4 ** 2 # Expoent (e.g 4 to the power of 2 = 16)