The first datatype you are going to meet is the string, this how you can use text within programming.
You have already met the string, when you wrote the line:
print("Hello, World!")
Strings can be identified by the speech marks around them, anything between two speech marks is a string.
Within our code, we call the string type str
, simply because this is less to type than string
Here are some simple examples of strings:
"Hello, World!"
"Lorem Ipsum"
"The quick brown fox jumped over the lazy dog."
There are also some other strings, for example ""
is the empty string, because there is nothing between the speech marks.
Your task is to remove any lines of code which print something that isn't a string.
When you are ready, continue on to the next task.
Close this message and have another go!
This guide is intended to help with syntax. See the prelude section for pre-defined functions and variables. See the tutorials page for tutorials on how to write code.
int
Integer values, such as 2
, -4
and 254
.
float
Floating point values, such as 2.0
, -4.72
and 254.2681
.
string
String values, such as "hello, world!"
, "haha"
and "33"
.
boolean
Boolean values True
and False
.
colour
Colour values for drawing on the canvase, See the prelude for more.
list[element_type]
List values. Note the element type must be correctly specified. Examples are [1,2,3]
, [["hello"], ["world"]]
and []
.
name: type
A variable must first be declared with a type before it can be used
name = value
name += value
Only applicable to integer values. Equivalent to name = name + value
.
name -= value
Only applicable to integer values. Equivalent to name = name - value
.
Applicable to boolean values. and
, or
and not
.
Applicable to numerical values. +
, -
, *
, **
, /
, //
and %
.
Applicable to comparable values. >
, <
, ==
, !=
, <=
and >=
.
if condition:
statements
elif condition:
statements
else:
statements
while condition:
statements
for ident in list:
statements
def name(parameter: parameter_type) -> return_type:
statements
Values can be returned with return
def name(parameter: parameter_type):
statements
Equivalent to a function which returns a None
type
Check the tutorials page for more!
The prelude is all the pre-defined functions and variables available to use. See the tutorials page for tutorials on how to write code.
print(expr)
Prints the value of the expression to the terminal.
print_str(expr)
Prints the value of the expression as a string to the terminal.
clear_terminal()
Clears the terminal.
input(message: str) -> str
Displays message to the user, and returns their input as a string.
str(expr) -> str
Where possible returns the string representation of expr.
int(expr) -> int
Where possible parses the integer representation of expr.
float(expr) -> float
Where possible parses the float representation of expr.
len(expr) -> int
Where possible returns the length of expr.
type(expr) -> str
Returns the name of the type of expr.
round(n: float) -> int
Rounds the value of n using bankers rounding.
random_int(from: int, to: int) -> int
Returns a random integer between from and to.
random_float(from: float, to: float) -> float
Returns a random float between from and to.
pi: int
The value of π.
str.upper()
Returns the uppercase of str.
str.lower()
Returns the lowercase of str.
str.replace(s1: str, s2: str)
Returns a string will all occurances of s1 replaced with s2.
str.split(d: str)
Returns a list of strings of str split on all occurances of d.
str.join(ss: list[str])
Returns a string from joining all strings in ss using d.
str.isnumeric() -> bool
Returns true if all characters in str are numbers.
l.append(elem)
Appends elem to the list l.
l.remove(elem)
Removes the first instance of elem from the list l.
l.count(elem)
Returns the total of elems present in the list l.
range(f: int, t: int) -> list[int]
Returns the list of values from f to t.
line(x1: int, y1: int, x2: int, y2: int)
Draws a line from (x1,y1) to (x2,y2).
rect(x: int, y: int, w: int, h: int)
Draws a rectangle of dimensions w by h on the canvas at position (x,y).
circle(x: int, y: int, r: int)
Draws a circle with radius r on the canvas at position (x,y).
ellipse(x: int, y: int, r1: int, r2: int)
Draws an ellispe with horizontal radius r1 and vertical radius r2 at position (x,y).
arc(x: int, y: int, r: int, f: int, t: int)
Draws an arc with radius r on the canvas at position (x,y) from f degrees to t degrees.
sect(x: int, y: int, r: int, f: int, t: int)
Draws a sector with radius r on the canvas at position (x,y) from f degrees to t degrees.
triangle(x1: int, y1: int, x2: int, y2: int, x3: int, y3: int)
Draws a triangle with corners at positions (x1,y1), (x2,y2) and (x3,y3).
polygon(xs: list[int], ys: list[int])
Draws a polygon with corners at positions (xi,yi) from xs and ys.
img(x: int, y: int, w: int, h: int url: string)
Draws the image loaded from the url at position (x,y) with width w and height h.
text(x: int, y: int, t: string)
Draws the text t at position (x,y).
rgb(r: int, g: int, b: int) -> colour
Returns the colour for the respecive red, green and blue values.
rgba(r: int, g: int, b: int, a: float) -> colour
Returns the colour for the respecive red, green and blue and alpha values. Alpha is opacity, in the range 0.0 to 1.0.
red: colour
The built in colour red, equivalent to rgb(240, 51, 51)
.
green: colour
The built in colour green, equivalent to rgb(98, 203, 166)
.
blue: colour
The built in colour blue, equivalent to rgb(117, 117, 232)
.
pink: colour
The built in colour pink, equivalent to rgb(197, 89, 146)
.
yellow: colour
The built in colour yellow, equivalent to rgb(221, 192, 107)
.
white: colour
The built in colour white, equivalent to rgb(255, 255, 255)
.
black: colour
The built in colour black, equivalent to rgb(0, 0, 0)
.
transparent: colour
The built in colour transparent, equivalent to rgba(0, 0, 0, 0.0)
.
width: int
The width of the canvas in pixels.
height: int
The height of the canvas in pixels.
line_colour(c: colour)
Sets the canvas line colour to c.
fill_colour(c: colour)
Sets the canvas fill colour to c.
line_width(w: int)
Sets the canvas line width to w.
font_size(s: int)
Sets the canvas font size to s.
translate(x: int, y: int)
Translates the canvas by (x,y).
rotate(a: int)
Rotates the canvas by angle a in degrees.
scale(x: float, y: float)
Scales the canvas size by (x,y).
frame_rate(s: int)
Sets the frame rate of the definable draw function.
stop_draw(s: int)
Stops the execution of the definable draw function.
def draw():
Is called at frame_rate, intended for animations and games on the canvas.
def left_pressed():
Is called whenever the left arrow key is pressed.
def right_pressed():
Is called whenever the right arrow key is pressed.
def up_pressed():
Is called whenever the up arrow key is pressed.
def down_pressed():
Is called whenever the down arrow key is pressed.
def space_pressed():
Is called whenever the space key is pressed.
Check the tutorials page for more!