Python Cheatsheet

This cheatsheet has some of the tips and tricks for using the Python programming language. As I learn more about Python, I will post more useful code snippets here in order to help you.

Submissions or issues identified can be submitted via issue on this project's repository.

Table of Contents

Basics

Math and Numbers

Examples


Running Python Scripts

python <script name>

Replace <script name> with the file name of the Python script that you want to run.


Declare Variables

Variables hold the values that are used in your application or script. In Python, variables are not strong-typed. This means that the variables you declare, are not limited to a particular data type. However, the variable does have a value, any future changes to that value must be of the same type. So you cannot set the value of a variable to a string and then change it to a integer.

myVariable = "red" 
myOtherVariable = false
myOtherOtherVariable = 5

Functions

To write a function in Python, you start the function out with the "def" keyword.

Example

def myFunction (param1, param2, param3): 
   sum = param1 + param2 + param3
   return sum

If you notice, there are no braces or brackets around the contents of the function. Python relies on indentation for the grouping of lines together within a given section.


Random Number

Random nubmers can be used to do countdowns or other things that are needed. To get a random number, you have to import the randomint class from the random library by adding

from random import randint

to the top of your Python script.

Then you can set the random integer to a variable by doing

myNumber = randint(5,60)

In the example above, 5 is the lower limit of the range and 60 is the upper limit of the range. If your range needs to be different, then changes either or both of these values.


Basic Mathematics

Addition

To do addition in Python, just have to code out two numbers and add them together like you would normally do for a math problem that you were writing on paper. Just keep in mind that the calculations always have to be done on the right side of the equals sign and will be set to the variable on the left of the equals sign.

sum = 5 + 3
print sum

Output:

8

Subtraction

Similar in fashion, subtraction is written just like addition, except it uses the minus sign instead of the plus sign.

difference = 10 - 3
print difference 

Output:

7

Multiplication

Multiplication is done using the asterisk.

product = 4 * 3
print product

Output:

12

Division

quotient = 20 / 5
print quotient

Output:

4

Hello World

Below is a sample of how to print out "Hello world" in Python.

print ("Hello world")

Output:

Hello world

More Resources

The Raspberry Pi traffic light project is written in Python code. You can review the code at https://github.com/almostengr/raspitraffic-stem.