Intro to Coding 👩💻
Coding is a fundamental skill that has made its way into every industry: car washing, financial accounting, spaceships - you name it! Beyond the impact that code makes on our daily lives, learning how to code sharpens your analytical thinking and problem-solving skills.
In this course, we’ll look at how you can get started with Python, a beginner-friendly coding language that’s easy to learn and simple to use. Not sure what a “coding language” means? No worries - we have you covered!
Python
When we talk with others, we speak with them in a language such as English.
Similarly, when we want to “code” something, we express that in a programming language such as Python.
These programming languages often consist of a list of instructions. For example, an example of code for a robot could be:
move forward 2 steps
move backward 3 steps
turn right
move forward 2 steps
turn left
stop
Although there isn’t a “formal” definition of the language, we can see that it makes sense - every line (in English) tells the robot to do something.
Let’s start with some Python.
print("Hello World!")
What will happen when we run the above code?
Python has a straightforward syntax, and in many cases, you can infer the meaning of some code without understanding how the code actually works!
Setup
Before we can write any code, we’ll first have to set up our Python environment. To run your code, you’ll need to install Python - the Python program will read a file and run the code inside the file.
To install Python, see the installation instructions here: https://www.python.org/downloads/
Now, we need a place to write our code. In theory, any text editor will do, such as Notepad for Windows or TextEdit on MacOS.
However, more advanced code editors can make your life far easier by providing auto-completion, syntax highlighting, and descriptive error messages when your code is failing.
One of the most popular code editors is Visual Studio Code (vscode for short). You can follow the installation instructions here: https://code.visualstudio.com/
To run a Python program, you can type
python filename.py
replacing filename.py
with the name of your Python file. Note: Python files typically end in .py
Now, onto some actual coding!
Hello World
To make sure your program environment is working properly, let’s run our Hello World program!
print("Hello World!")
If you run this program, you should see the words “Hello World!” printed across your screen.
Note: Python is whitespace sensitive. You should always indent your code consistently - you can either use tabs, or 4 spaces, but never both.
Variables
Variables are things that store data. You might be familiar with something like x=5. This means that the variable, x, stores a value of 5.
A similar idea occurs in programming. We can create variables with names, that store specific values.
x = 5
y = 0.3
z = "hello"
What is the value of y?
The most common, “basic” types are numbers and strings. Within numbers, we have integers (e.g. -1234, 9824, 3) and floats (e.g. -0.53, 98.34).
In computer science, a “string” means a sequence of characters. In the example above, z is represented by the string “hello”. We can create a string by wrapping the text in either double quotes (“”) or single quotes (‘’).
Once we define these variables, we can use them anywhere in our code! Let’s see how we might print out “hello”:
Be careful about print(‘z’)
- this will print out “z” to the screen because the single quotes refer to a string, not a variable.
Types
As you saw in the variables section, Python offers a multitude of “types”; different types are used to represent different kinds of data.
Let’s say we have five apples. How can we represent this in code using an integer?
How can we represent the five apples in code using a string?
As you can see, there is no “correct” way to represent the number of apples. Both variable declarations get across the same message that there are five apples.
However, in this case, programmers will likely prefer to use the integer 5 over the string “five”. Do you see why?
The integer type allows flexibility! For example, let’s say you buy 4 more apples. To express this in code with integers, we can simply write:
num_apples = 5
num_apples = num_apples + 4
The last line might be confusing: wouldn’t that be saying x = x + 4
? That equation is clearly wrong!
Instead, the last line means that we’re updating the value in num_apples
– the new value of num_apples
(left side) is the old value of num_values
plus 4 (right side).
Notice that we can’t do this if we’re using strings!
For now, the key types you’ll need are integers, floats, and strings.
Lists
Another useful type in Python is the list. These lists allow you to store data of any type together, and there is no limit on the size of these lists. Lists are typically represented in square brackets. For example,
list1 = [1, 2, 3, 4]
list2 = ["a", "b", 3]
list3 = [0.234, "hello", 9, ["another list", "b"]]
In the list3
, we even have a list inside of a list! Which of the following is a list?
Only the first option is a list. The second option is a string, and the third option uses parentheses instead of square brackets.
Once we have a list, how can we add more elements? Luckily, there is a append
function. Functions are sections of code that are run when they are called. Let’s see them in action:
x = [1, 2, 3]
x.append(4)
x.append(5)
This code snippet creates a list called x
, then adds the values 4
and 5
to the end. What will be the resulting list?
To get the value of a list at a specific position, we can use the square bracket operator:
first_element = x[0]
second_element = x[1]
Important: the numbering of positions starts from 0 – we call this zero-indexing.
What is the value of x[3]
?
Since the array is zero-indexed, x[3]
refers to the fourth element in the array – 4.
In future sections, we’ll look at how we can go through a list and obtain all of the elements in that list.
Summary
In summary, Python is a simple yet incredibly powerful coding language that can easily automate many aspects of our lives. We’ve learned the basics in this course - variables, lists, loops, and functions - but there’s a lot more potential that Python offers! If you’re interested, check out the resources below:
Resources:
Python’s official tutorial: https://www.python.org/about/gettingstarted/
Real Python: https://realpython.com/
More tutorials: https://www.pythontutorial.net/