Posts about Home


Python Crash Course


Introduction

This page details the absolute essentials that you need to know to get up an running with Python. This short list of concepts is deceptively powerful. If you get these ideas really clear in your mind and spend some time experimenting with IDLE, you will be well on your way to getting your coding chops.

Comments & Indents

Any line that starts with # is called a comment and is ignored by Python. These are where you can document your code


# This is a comment, it will be ignored by python

Python uses space or tab indenting to group code. On the screen you cannot see spaces or tabs but they do have a unique code inside your text file, so they are known as ‘whitespace’ and they really matter in Python!


flag = True

# Logic Test
if flag is True:
    # Indented code is treated as a group or block
    # It 'belongs' to the test above
    # It is run if the test succeeds
    print("Only print this if flag is true!")

# No indenting means this code is NOT part
# of the test and will run regardless of flag
print("Always print this")

EITHER indent using spaces OR tabs. Don’t use a mixture, it will cause an error. Be careful of this problem if you cut and paste someone else’s code into your own – they may have used different indenting.

Strings & Numbers

Python represents the whole number 2 differently from the text symbol for it, which is ‘2’.

Letters and other text symbols are called characters. Groups of characters are called Strings. Characters and strings are always represented inside inverted commas, or quotation marks.


this_is_a_string = Two
so_is_this = 2
and_this_is_a_single_text_character = 2
but_this_is_a_number = 2

Whole numbers are called Integers


this_is_an_integer = 2

Python can only combine or compare like types so you will often need to convert one form to the other.

Integer to String


str(2) # returns '2'

will return the string equivalent of 2, which is ‘2’

String to Integer


int(‘2’) # returns 2

will return the integer equivalent of ‘2’, which is 2

Strings & Integers are just two of the types of data that you can create, store, modify and retrieve. When they are created we give them a name label. Because the data associated with the label can change, we often call these name labels variables. Below are the more of the common types of data you can create and reference using named variables.

Tuples & Lists

It is often useful to keep groups of numbers or strings together under a single variable name if they represent related information. The width and height of your display window, for example are often kept together.

Tuples

If the numbers will not need to be changed during your program - for example, the width and height of your graphics window will be set once when the window is created but not changed while your program runs – then you can use a tuple. Tuples are created using a comma ‘,’ separated list of data between two normal brackets ‘(‘ ‘)’


my_screen_res_tuple = (640, 480)

You can access an individual value within a tuple by specifying the index of the item in the tuple


my_screen_res_tuple[0] # will return 640

All indexes start from 0. The first item in a tuple, list or dictionary is at index 0, the second at index 1 etc.

Lists

List are very like tuples, but you can modify them as your program is running. You create a list using a comma ‘,’ separated list of data between two square braces‘[‘ ‘]’


my_flexible_list = [2, 4, e’, 25, 2’, a string’, 5, 2’]
my_flexible_list[2] # will return ‘e’
my_flexible_list[2] = f # will replace ‘e’ with ‘f’
my_flexible_list[1:3] # will return a new list - [4, ‘e’]
len(my_flexible_list) # returns the length of the list – 8
my_flexible_list.append(42) # adds 42 to the end of the list - [2, 4, ‘e’, 25, ‘2’, ‘a string’, 5, ‘2’, 42]

Strings are a List of characters. You can manipulate strings in the same way as a list.

Dictionaries

Dictionaries are a bit like lists, but with labels for each item. Instead of finding items by their index, you can find an item by its label. You create a dictionary using a comma ‘,’ separated list of labels & data between two curly braces‘{‘ ‘}’. Label and data pairs are separated by a colon ‘:’


my_dictionary = {“Item 1 Label”:101, Second Label”:23}
my_dictionary[“Second Label”] # returns 23
Item 1 Label in my_dictionary # returns True

Logic

Logic allows you to test the state of variables and make your code ‘branch’ in different directions depending on the outcome of the test. The logical test can have several parts to it, but always ends with a colon ':'. The code block to be run if the test succeeds must be indented below it.


if test:
	# Do indented code here, if test is True
elif other_test:
	# Do indented code here, if other_test is True
else:
	# Do indented code here, only if test and other_test are False

You can use the if block on its own without an else (or any elif's).

You can also have as many elif blocks as you need, but there should never be more than one else

Loops

Loops allow you to repeated indented code blocks. You can repeat until a logical test becomes False by using a While loop. The Loop declaration, like logic, ends with a colon ':' and the block of code to be looped must be indented below it.


flag = True

while flag:
	# Indented code here is repeated
	# until flag becomes False	

You can repeat a fixed number of times using a For loop.


for thing in my_list:
	# Indented code here is repeated
	# for each item in my_list
	# You can access the item 
	# using the 'thing' variable 
    # each iteration	
	
for x in range(10):
	# Indented code repeated 10 times
	# x will be 0 through 9
	# as the loop repeats
	print(x)
	
for x in range(5,10):
	# Indented code repeated 5 times
	# x will be 5 through 9
	# as the loop repeats
	print(x)

Welcome to RaspberryPilot.com!


Welcome to the website of the Kenilworth Games Creators Club (GCC). The club runs weekly in term-time at Kenilworth School. All ages are welcome - we currently have members from years 7 to 11.

We base most of discussions and coding around python and pygame. But if you have the interest and aptitude we have also dabbled in other things - including C++.

The club has some Raspberry Pi computers, which are always available, but some members bring their own computers and we can help with setting them up for game development and programming in between club meetings.

Use the links under Main Menu to find find details of the tools and technology we use in the club – including the Raspberry Pi – plus blog articles and reports from our weekly meetings.