Learn · Reference
Many boxes take a value: a number, some words, a name, or a sum that works one out. Here's how to write them.
7, 3.5, -2."hello". Single quotes don't work.score, firstName.["red", "blue"], or [] for an empty one. colours[1] is the first item.ab"The sum is " + (a + b)45It showsThe sum is 9a = int(input("First number? "))b = int(input("Second number? "))print("The sum is " + str(a + b))| Symbol | Means | Example |
|---|---|---|
+ | add numbers, or join text | 3 + 4 is 7, "Hi " + name |
- * / | take away, times, divide | 10 / 4 is 2.5 |
% | remainder after dividing | 10 % 3 is 1 |
== != | is equal to, is not equal to | answer == 42, colour != "red" |
> < >= <= | bigger, smaller, at least, at most | age >= 13 |
&& | and: both must be true | age >= 13 && age <= 19 |
|| | or: at least one is true | day == "Sat" || day == "Sun" |
! | not: flips true and false | !contains(word, "a") |
( ) | work this part out first | "Sum: " + (a + b) |
sweets = 17friends = 5"Each gets " + floor(sweets / friends)"Left over: " + sweets % friendsEach gets 3Left over: 2import math
def remainder_of(a, b): # Like % in B4Coder: the remainder has the sign of a, so -7 % 3 is -1 (Python's % gives 2). r = abs(a) % abs(b) return -r if a < 0 else r
sweets = 17friends = 5print("Each gets " + str(math.floor(sweets / friends)))print("Left over: " + str(remainder_of(sweets, friends)))Use them in any box, like round(price, 2). Each does a small job and gives back the answer.
round(number) or round(number, digits)floor(number)ceil(number)abs(number)sqrt(number)power(number, times)min(a, b, …) or min(list)max(a, b, …) or max(list)random(lowest, highest)length(list or text)upper(text)lower(text)contains(list or text, thing)join(list) or join(list, between)split(text) or split(text, between)word = "banana"upper(word)length(word)round(3.7)BANANA64import math
def round_half_up(x, digits=0): # Like round() in B4Coder: a half goes up, so 2.5 is 3 (Python's round() gives 2). scale = 10 ** digits whole = math.floor(x * scale) if x * scale - whole >= 0.5: whole += 1 return whole if digits == 0 else whole / scale
word = "banana"print(word.upper())print(len(word))print(round_half_up(3.7))