Skip to content
Chris Kimpton edited this page Mar 26, 2019 · 13 revisions

Python language covered per CodeClub project

  • print('') and multi-line using print(''' ... ''')
  • repeat chars - print('&*' * 10)
  • maths - print(3 - 1)
  • variables - myvar = 1
  • input - myvar = input('What...')
  • convert to int - int(myvar)
  • imports - from random import randint, myvar = randint(1,3)
  • conditional logic, like, note the colons and two space (or tab) indent
  • if myvar == 1:
  • othervar = '2'
  • elif myvar == 2:
  • othervar = '3'
  • else:
  • othervar = '4'
  • from turtle import *
  • moves - forward(100)
  • write(0)
  • loops, like so - remember colon and indents like above
  • for step in range(5):
  • write(step)
  • forward(20)
  • find a char in a string - pos = "123".find('2')
  • modulo - % 5 % 2 => 1
  • lookup char pos in string - "123"[2] => "3"
  • loop chars in string
  • for char in mystring:
  • add to end of string - a='1', b='2', a+=b => a is now '12'

What does this do:

1?

a = 1

2?

if x < 5:
   print "The value is OK."

3?

for i in [1,2,3,4,5]:
    print "This is iteration number", i

4?

x = 10
while x >= 0:
   print "x is still not negative."
   x = x-1

5?

x = input("Please enter a number: ")
print "The square of that number is", x*x

6?

colours = ["red","blue","green"]
print colour[0]

7?

person = { 'first name': "Robin", 'last name': "Hood",
           'occupation': "Scoundrel" }

8?

def square(x):
    return x*x

print square(2) # Prints out 4

Astro Pi Challenge - until 20 March 2019

https://trinket.io/mission-zero/classroom-code

https://projects.raspberrypi.org/en/projects/astro-pi-mission-zero/

https://astro-pi.org/missions/zero/faq/

http://esamultimedia.esa.int/docs/edu/European_Astro_Pi_Challenge_Mission_Zero_guidelines.pdf


And some useful links

Clone this wiki locally