In this series, you’ll learn the key Python concepts you need to know to get started with using Python for data science. The course will take you from a complete beginner all the way through taking on your own machine learning projects!
This course doesn’t just put a bunch of words on a page. It guides you through the material in a hands-on way, providing you the opportunity to validate your learning with inline quizzes. You’ll also be able to write your code directly in the browser (!) or following along in a notebook or in your favourite code editor.
The course is split into four different sections:
- Python Basics: learn what Python is, how to work with Python for data science, and how to automate boring, repetitive tasks
- Python Data Analysis with Pandas: learn how to use the popular data analysis library, Pandas, to work with tabular data
- Python Data Visualization: Learn how to visualize data with Python’s Matplotlib and Seaborn to gain insights into your data
- Introduction to Machine Learning: dive into the exciting realm of machine learning with hands-on projects, taking you through a classification project and a regression project in an approachable way
Let’s get started!
What is Python?
Let’s start off with the technical definition of what Python is:
Python is an interpreted, high-level, general-purpose programming language. It is dynamically typed and garbage collected.Wikipedia – Python
But, wait, what does this mean? Let’s break this down a little bit:
interpreted: meaning that it is directly executed, without ending to be compiled first
high-level: meaning that it is abstracted from the details of the computer and uses many natural language elements to make code easier to read
general-purpose: meaning it can be used in many different domains (such as data science!)
dynamically typed: meaning that the data types are checked at runtime but are not explicitly enforced (meaning you could accidentally pass in text instead of a number)
garbage collected: meaning that the program tries to clear up memory as items are no longer used
Where does the name Python come from? It’s actually named after the British comedy troupe Monty Python. It’s a perfect homage – the troupe was an experimental comedy series covering many different topics. This, too, is true for Python – it’s multi-purpose and offers a playground for people to develop their own solutions with ease.
Do you need to install Python?
To answer whether you need to install Python or not is a bit complicated. You may already have it bundled with your operating system. However, let’s not worry about it for now (we’ll cover off installing in a later section).
I want to let you dive into the world of Python as quickly as possible. Writing code is, after all, the best way to learn. Because of this, the following sections will include simple quizzes that allow you to better test your learning. Python interpreters have also been built into the tutorial in order to actually let you play with code without having to worry about installing Python.
Printing in Python
Let’s get started by writing our first program! Let’s be a little conventional and print out a
"hello". What does it mean to print something in Python? Whenever we want to show some output to the screen or some output device, we can use the
Let’s give this a shot! Take a moment to look at the code below. What do you think it does? Once you’ve reflected on this for a moment, click the play button and see what’s returned.
Cool, right? Now that you’ve done this, let’s explore the interactivity. Click the pencil icon to enter edit mode. From there, try printing something else.
Try printing some of the following:
print('Welcome to datagy.io!')
print("Let's learn some Python!")
When you enter the above code into the code editor, you get the following returned:
Welcome to datagy.io! Let's learn some Python!
We can see that each of these statements is printed on a new line and that the code doesn’t include include the single quotes you had placed around the inputs.
What do those quotes actually mean? Characters placed between quotes (whether single or double) represent a string data type.
Python strings are sequences of unicode characters, which is an encoding format used to represent consistency in characters across multiple languages. We can create strings using single quotes (this
'this'), double quotes (like
"this"), or triple quotes (like
"""this"""), though the last version is used generally for multi-line strings.
Now let’s dive into the wonderful world of Python variables and take our understanding of strings even further.
Variables in Python
In this section, you’ll learn about Python variables, starting off with what variables are. You can think of variables as
containers that store information and reference different objects. It can often be helpful to think of them as boxes, where the items inside that box may change, but the container stays consistent.
We can create a variable in Python by assigning an object using an equal sign,
Let’s create a variable
message and store a welcome message. We can then use the
print() function to display our message.
message = 'Welcome to datagy.io!' print(message)
Let’s take a look at a couple of particular notes here:
- We didn’t need to declare a variable
type, like you would need to in other programming languages. Recall that Python is dynamically-typed, meaning that the type of the variable isn’t set at the beginning.
- We were able to pass in
messagewithout quotes. Because the variable refers to our string, Python was able to interpret that the variable inside was a string.
There are a couple of rules and best practices to help you name your variables:
|Type||Rule / Best Practice||Example|
|Rule||Variables must start with a letter||num_apples = 3, not 1num_apples = 3|
|Variable names can only contain letters, numbers, or underscores||num_apples = 3, not num_apples? = 3|
|Best Practice||Variable names should be descriptive||num_apples = 3, not n = 3|
|Use a convention (like snake-case) to name and style your variables and be consistent||num_apples = 3, not numapples = 3|
|Write constants (values that don’t change) in all upper cases||PI = 3.14, not pi = 3.14|
Now that you have a good, preliminary understanding of Python variables, let’s take a little bit of time to dive into using Python to do math!
Python Integers and Floats
Now let’s take a look at how to actually do some math in Python, by learning a bit about Python integers,
ints, and floating point decimals,
Python comes with two main ways of defining numbers:
- integers are whole numbers, such as 0, 1, and -1.
- floating point numbers are decimal numbers, such as 0.0, 1.0, -1.0
There are also
complex numbers, which sit outside the scope of this course.
When we’re working with numbers, we may be interested in doing math with them. Python comes with operators that allow us to perform mathematical operations on different numbers.
For example, we could write the following arithmetic operation:
print(1 + 2)
This returns the value of
The table below breaks down the different operators that are available in Python:
|x + y||Addition||The sum of x and y|
|x – y||Subtraction||The difference between x and y|
|x * y||Multiplication||The product of x and y|
|x ** y||Exponentiation||The value of x raised to the power y|
|x / y||Division||The quotient of x and y|
|x // y||Floored Division||The quotient of x and y, rounded down to the nearest integer|
|x % y||Modulus||The remainder of the quotient between x and y|
|-x||Negation||The negative value of x|
Let’s take a look at a few examples. The section below shows the
print() function call with a different operation in it.
Take a look at the code and try and guess what the result will be! Toggle the tab by selecting the value on the right to see if the results match what you expected!
Let’s start with some exponents:
print(2 ** 3)
Now let’s try some floored division:
print(10 // 3)
Finally, let’s take a look at the modulus operator,
print(10 % 3)
Is the modulus operator a bit confusing? The way that it works is by dividing the two numbers and returning the remainder of the division. In the example above, 3 goes into 10 three times, leaving the value of 1.
Try writing some code into the editor below and see if the results match what you’d expect. Remember to wrap your operation in a
print() function in order to have the results displayed.
Congratulations on making it through the first day of learning Python! You learned a lot of information today and it’s ok to feel a little bit overwhelmed.
To recap, you learned that Python is an interpreted language that can be used for many different domains. You also learned how to create variables in Python and you worked with three data types: strings, integers, and floats. Finally, you learned how to do some early math using Python.
In Day 2, you’ll learn more about how to do math in Python and you’ll dive into creating your own functions! See you there.