Python Research
Python Research
Python https://www.edx.org/course/basics-of-comp
uting-and-programming
28/05/20
Driving question: How can I program a small video game presenting newfound skills and
techniques for constructing and programming games?
Driving question:
How can I program a video game using python, and what skills and techniques are required
to constructing a successful game?
Notes:
Green in python is the python string - They store text data in programs (Letters, words and
sentenced)
Red in python is the syntax error, meaning that you got something wrong with the script; if
you type a function that doesn’t exist, it will display a syntax error in red
Orange in python is a statement that controls the execution of the script. They control a
certain part of the script called a block.
- Python interpreter is the program that reads and runs your code
- Variable is a mailbox, where you can put something in and check the contents later
- The variable script can be executed through creating a variable, eg: msg = ‘Hello
world’ and then printing the msg, print(msg)
- You can ask the user questions or tell them to answer something using the input
function
- You can add two strings or more together by using a ‘+’ between them; however, this
will add them together so you have to manually add a space between each string like
this, “ “
- If adding a quotation mark or apostrophe, make sure that the outside ones are
different from the inside one, ‘“‘ and “‘“
- Python can also work as a powerful calculator using all the maths symbols and being
able to work
- Addition = ‘+’
Subtraction = ‘-’
Multiplication = ‘*’
Division = ‘/’
Power of = ‘* *’
- Len function counts the number of characters that are in the string
- You can make the input an integer by adding int at the beginning of the string - This
converts the string to a number
- The str function is the complete opposite of the int function, changing the integer
back to a string
- You can use ‘’’ or “”” if the string goes for multiple lines
- The if statement allows certain inputs to have different results, with the else making it
that if certain inputs aren’t in place, you will get a different result
- If the ‘if’ statement has another ‘if’ statement in it, it is called nesting. That means if
the first statement is true, then the second one will be tested.
- Elif is an abbreviation for else and if together.
- In a range, the first two numbers are what the outcome going to be, with the third
number being the gap between each number
- You can go backwards in range by having the 3rd number being a negative
- rstring can get rid of whitespaces at the right of the string
- Data structures store and organise data so it can be accessed and modified
efficiently. Lists are the simplest data structures in Python; this can be used with the
list function
- List = [ ]
- Splitting a list will make you able to count each unit in the list
- Loop initialisation is the set up for a loop
- To stop a loop we use a termination condition
- The instructions for the repetition is the body of the loop control structure
- To change the instruction, we can keep track of how many times we repeat the body,
which is the number of iterations
- while command != → This will keep repeating the script until the specific thing is
done
- while line: → Leaving it blank will make it so that you need to leave the response
blank to stop the command
- # accessing a list element - Getting specific elements in a list
# slicing from the start - Slice the list from the start, stopping at the specified number
# slicing to the end - Starts from the number and end at the final list element
# slicing in the middle - Slices between the elements
If statement:
Operation Operator
Equal to ==
Not equal to !=
Scripts:
- print(‘Hello world’) - Print script
print(name) - Input
- s = input('Enter a number: ')
number = int(s)
new_number = number - 1
print(new_number) - Integer
- print(len('Hello World!')) - len function
- for i in "Hello":
print(i, end=" ")
print('Next line') - For loop with end function
- output = ''
for i in "Hello":
output += i
print(output)
print('Next line') - Output with loop
- for i in range(10):
print(i) - Range with loop
- x = "hello world"
print(x[:5])
print(x[6:]) - Slice script
- print(10/3)
= 3.3333333333333… - Division
- name = input('Students: ')
print('Class Roll')
roll = sorted(name.split())
for (name) in roll:
print(name) - Sorting
- i=0
while i < 10:
print(i)
i = i + 1 - While
- books = []
book = input('What book are you returning? ')
while book != '':
books.append(book)
book = input('What book are you returning? ')
books.sort()
print(books) - Looping with append
- sep = ':'
values = ['a', 'b', 'c', 'd', 'e']
print(sep.join(values)) - Separator and Join functions
Evidence:
Python ^_^:
Repeaterbot:
In the black:
Method of transportation:
Broken keyboard:
Short/long names:
Give me a G!:
Count to n:
Speaking backwards:
Secret agent:
Part 6 - Storing lists of values:
Halve this:
Weather report:
Sorted!:
Lift Off!:
Guessing game:
Up the lift:
Up the downstair:
● The Python print function can take multiple arguments, and the joining (sep)
and ending (end) strings can be specified. For example:
● print("Hello", "there!")
print('Hello', 'world', sep='-')
print('There is no new line here', end='*')
● print('at all!')
print(2 + 2)
print(20 - 7)
print(5*6)
print(15/2)
print(100/2 - 3*8)
f = open('output.txt', 'w')
f.write("Hello, world!")
f.write("Now we're working with files!")
f.close()
● In Python 3, the input function reads in a string provided by the user. If you
want to convert the input to a different type, such as a float or integer, you need
to cast to the appropriate type. For example:
● The year variable needs to be converted to an integer so that we can perform
the subtraction.
● When you run examples on the website that require user input, the program will
print out the prompt and wait for input from you. Simply type into the text box
and hit enter.
● name = input('What is your name? ')
year = int(input('What year were you born in? '))
nyears = year - 1879
print(name, 'was born', nyears, 'years after Einstein.')
● There are two types of numbers in Python. Whole numbers (or integers) have
the type int, and fractional or decimal numbers have the type float. You can
convert between types by casting using the int and float functions:
● ‘w’ when opening a file will write it, it will be ‘r’ on default, read mode
● /n makes a newline character
Review of Introduction to Programming:
Maths mix:
Robots in a line!:
Up the lift:
Guessing game:
Word2SMS:
Favourite colour:
Car colours:
Word counter:
SHOUT your orders!: