Show Menu

Python for Raspberry Pi Cheat Sheet (DRAFT) by

Memory for often used code

This is a draft cheat sheet. It is a work in progress and is not finished yet.

datetime strftime selectors

Weekday name (Sat)
Weekday name (Saturday)
Month name (Oct)
Month name (October)
Day of month (01...31)
Hour (24h)
Hour (12h)
Day of year (001...366)
Month (01...12)
Minute (00...59)
Second (00...61)
Week number (00...53) (Sun)
Weekday number (0(Sun­day­)...6)
Week number (00...53) (Mon)
Year (00...99)
Year (1990...2099)
Time zone name


from datetime import datetime
i =
print i.strftime("%H:%M:%S")

new object

#!/usr/bin/env python
# -- coding: utf-8 --

class SampleObject(object):
    def __init__(self, a):
      self.a = a

   def staticMethod():
      # This is a static method

   def objectMethod(self, b):
      # needs to be instance of class
      b += 1
      return b

if __name__ == "__main__":
   myObject = SampleObject(42)

if clauses

if a > 5 and b < 3:
   # condition 1
elif a < 5 and b < 3:
   # condition 2
   # condition 3

for loop

for i in range(4):
   # do something


myList = []
myList.append("Another entry")
entry = myList[0]

JSON parsing

import requests
r = requests.get("")
data = r.json()
entry = data['day1']['room2']['temp3']