Python Cheat Sheet - Week 1

Hello! Here's a recap of the concepts that were covered in Week 1. There was a lot to take in, so this may help you remember. Code is in blue, outputs are green.

Print

When you use the print() function, it prints the output to the console:

print('Hello') Hello

Comments

You can put a # in front of a line to make the interpreter ignore it. This is a Good Thing, because it lets you put notes in your code.

Import

Python has lots of 'modules'. These are libraries of code that enable it to do extra things. They may be huge, radically changing how Python can be used. Or they may be small, adding common functions to save you the trouble of writing them yourself. You can import whole modules

import random

Or individual functions from them:

from random import randint

Variables

A variable is like a container. It stores something. It could be a number, or some text (referred to as a string). You can call it whatever you like, but there are some rules:

You can join words together, to create a name that makes sense. Python convention is to use snake_case (lowercase words joined with underscores), but some people prefer camelCase because they think it's cool.

You can change variables - that's what makes them variable:

current_score = 5 player_name = 'Bingo ' current_score = current_score + 10 print (player_name + ' scored ' + str(current_score) + ' points!') Bingo scored 15 points

You can also print variables by putting an ‘f’ in the print parameters. This is called “formatted string literals”. No, me neither. But it looks like this:

print (f'{player_name} scored {str(current_score)} points!') Bingo scored 15 points

The entire output is enclosed in quotes, and variables are denoted by curly brackets. You might find it easier to read than the concatenated version.

Note the str() function around current score. This converts it from a number to a string. Without that, Python would attempt the sum 5 + “points”, and nobody’s got the maths for that.

Operators

You can use +, -, * and / (add, subtract, multiply and divide) just as you would on your calculator:

print (12+13) 25

+ is also used to join strings together, as in the previous example. This is called ‘concatenation’.

Functions

A function is a chunk of code that you can use as often as you like. It has a name, and the name is followed by brackets. The brackets are so you can send variables to the function. First, you must define the function. In Python, that looks like this:

def my_function(score): new_score = score + 5 print(new_score)

‘def’ tells Python that I’m defining a function, my_function is what I’m calling it, and (score) means it should expect a variable, and whatever that variable is called, within the function it’s going to be called ‘score’. The colon says, “Right, here it is, this is my function!”, and everything after that has to be indented with 4 spaces (weird, huh?) to be a part of it.

As you can see, it’s a pretty simple function. It’s part of a game, and every time you shoot an asteroid, it adds 5 points to the score. To ‘call’ the function, you use its name, like this:

my_function(current_score) 10

There’s a slight problem with this, because while the function has printed the new score, our variable current_score is still set to 5. So next time we shoot an asteroid, it’ll be 5 + 5 = 10 again. How do we update current score? This is where ‘return’ comes in. A function can return a variable to the place it was called. So if we use return(), we can call the function as often as we like, and provided we’re good at shooting asteroids, the score will keep on going up:

current_score = 5 def my_function(score): new_score = score + 5 return(new_score ) current_score = my_function(current_score) print(current_score) current_score = my_function(current_score) print(current_score) 10 15