This course covers the key Python skills you’ll need so you can start using Python for data science. The course is ideal for someone with some previous coding experience who wants to add Python to their repertoire or level up their basic Python skills. (If you’re a first-time coder, you may want to check out these “Python for Non-Programmers” learning resources.)
We’ll start with a brief overview of Python syntax, variable assignment, and arithmetic operators. If you have previous Python experience, you can skip straight to the hands-on exercise.
spam_amount = 0 print(spam_amount) # Ordering Spam, egg, Spam, Spam, bacon and Spam (4 more servings of Spam) spam_amount = spam_amount + 4 if spam_amount > 0: print("But I don't want ANY spam!") viking_song = "Spam " * spam_amount print(viking_song)
There’s a lot to unpack here! This silly program demonstrates many important aspects of what Python code looks like and how it works. Let’s review the code from top to bottom.
spam_amount = 0
Variable assignment: Here we create a variable called
spam_amount and assign it the value of 0 using
=, which is called the assignment operator.
Aside: If you’ve programmed in certain other languages (like Java or C++), you might be noticing some things Python doesn’t require us to do here:
- we don’t need to “declare”
spam_amountbefore assigning to it
- we don’t need to tell Python what type of value
spam_amountis going to refer to. In fact, we can even go on to reassign
spam_amountto refer to a different sort of thing like a string or a boolean.
Function calls:. print is a Python function that displays the value passed to it on the screen. We call functions by putting parentheses after their name and putting the inputs (or arguments) to the function in those parentheses.
# Ordering Spam, egg, Spam, Spam, bacon and Spam (4 more servings of Spam) spam_amount = spam_amount + 4
The first line above is a comment. In Python, comments begin with the # symbol.
Next, we see an example of reassignment. Reassigning the value of an existing variable looks just the same as creating a variable – it still uses the = assignment operator.
In this case, the value we’re assigning to spam_amount involves some simple arithmetic on its previous value. When it encounters this line, Python evaluates the expression on the right-hand-side of the = (0 + 4 = 4), and then assigns that value to the variable on the left-hand side.
But I don't want ANY spam! Spam Spam Spam
We won’t talk much about “conditionals” until later, but, even if you’ve never coded before, you can probably guess what this does. Python is prized for its readability and the simplicity.
Note how we indicated which code belongs to the
"But I don't want ANY spam!" is only supposed to be printed if
spam_amount is positive. But the later code (like
print(viking_song)) should be executed no matter what. How do we (and Python) know that?
The colon (
:) at the end of the
if line indicates that a new “code block” is starting. Subsequent lines which are indented are part of that code block. Some other languages use
} to mark the beginning and end of code blocks. Python’s use of meaningful whitespace can be surprising to programmers who are accustomed to other languages, but in practice it can lead to more consistent and readable code than languages that do not enforce indentation of code blocks.
The later lines dealing with
viking_song are not indented with an extra 4 spaces, so they’re not a part of the
if‘s code block. We’ll see more examples of indented code blocks later when we define functions and using loops.
This code snippet is also our first sighting of a string in Python:
"But I don't want ANY spam!"
Strings can be marked either by double or single quotation marks. (But because this particular string contains a single-quote character, we might confuse Python by trying to surround it with single-quotes, unless we’re careful.)
* operator can be used to multiply two numbers (
3 * 3 evaluates to 9), but amusingly enough, we can also multiply a string by a number, to get a version that’s been repeated that many times. Python offers a number of cheeky little time-saving tricks like this where operators like
+ have a different meaning depending on what kind of thing they’re applied to. (The technical term for this is operator overloading)
Numbers and arithmetic in Python
We’ve already seen an example of a variable containing a number above:
“Number” is a fine informal name for the kind of thing, but if we wanted to be more technical, we could ask Python how it would describe the type of thing that
int – short for integer. There’s another sort of number we commonly encounter in Python:
float is a number with a decimal place – very useful for representing things like weights or proportions.
type() is the second built-in function we’ve seen (after
print()), and it’s another good one to remember. It’s very useful to be able to ask Python “what kind of thing is this?”.
A natural thing to want to do with numbers is perform arithmetic. We’ve seen the
+ operator for addition, and the
* operator for multiplication (of a sort). Python also has us covered for the rest of the basic buttons on your calculator:
||True division||Quotient of
||Floor division||Quotient of
||Modulus||Integer remainder after division of
||Negation||The negative of
One interesting observation here is that, whereas your calculator probably just has one button for division, Python can do two kinds. “True division” is basically what your calculator does:
It always gives us a
// operator gives us a result that’s rounded down to the next integer.
Can you think of where this would be useful? You’ll see an example soon in the coding challenges.
Order of operations
The arithmetic we learned in primary school has conventions about the order in which operations are evaluated. Some remember these by a mnemonic such as PEMDAS – Parentheses, Exponents, Multiplication/Division, Addition/Subtraction.
Python follows similar rules about which calculations to perform first. They’re mostly pretty intuitive.
8 - 3 + 2
-3 + 4 * 2
Sometimes the default order of operations isn’t what we want:
Parentheses are your useful here. You can add them to force Python to evaluate sub-expressions in whatever order you want.
Builtin functions for working with numbers
max return the minimum and maximum of their arguments, respectively…
abs returns the absolute value of it argument:
In addition to being the names of Python’s two main numerical types,
float can also be called as functions which convert their arguments to the corresponding type: