Python Hints and Tips - Navigators
Python Hints and Tips - Navigators
Input
Getting input
Use the input function to get input data. In the coding challenge you must not include any
user prompts; just use a simple input statement as shown below.
Python
quantity = input()
Some questions will require two lines of input. In this case, use two input prompts.
Python
num1 = input()
num2 = input()
Python
1
UK Bebras Coding Challenge - Navigators Python hint and tips
Output
Use the print function to produce output. Ensure the format is exactly as required.
# example 1
print('This is a string')
# example 2
amount = 5
print(amount)
>> 5
# example 1
first_name = 'Sofia'
last_name = 'Petra'
print(first_name + ' ' + last_name)
2
UK Bebras Coding Challenge - Navigators Python hint and tips
# example 1
num1 = 23
num2 = 5
num3 = -7
print(str(num1) + ',' + str(num2) + ',' + str(num3))
>> 23,5,-7
>> 23,5,-7
3
UK Bebras Coding Challenge - Navigators Python hint and tips
Strings
Getting the length of a string
Python
my_string = 'crocodile'
my_string_length = len(my_string)
print(my_string_length)
>> 9
# example 1
my_string = 'crocodile'
new_string = 'ccc'+'crocodile'
print(new_string)
>> ccccrocodile
# example 2
my_string = 'crocodile'
new_string = 'crocodile' + 'ccc'
print(new_string)
>> crocodileccc
4
UK Bebras Coding Challenge - Navigators Python hint and tips
Indexing starts at 0.
Python
my_string = 'crocodile'
first_letter = my_string[0]
print(first_letter)
>> c
my_string = 'crocodile'
sixth_letter = my_string[5]
print(sixth_letter)
>> d
>> CROCODILE
>> desk
5
UK Bebras Coding Challenge - Navigators Python hint and tips
Mathematical operators
Examples are shown with the following variables:
a=7
b=2
+ addition a+b 9
- subtraction a-b 5
* multiplication a*b 14
Rounding
Use the built in function round to round to a given number of decimal places.
Python
>> 3.142
6
UK Bebras Coding Challenge - Navigators Python hint and tips
Relational operators
Examples are shown with the following variables:
a=7
b=2
== Equal to a == b False
7
UK Bebras Coding Challenge - Navigators Python hint and tips
Selection
Selection is used to run a block of statements if a condition evaluates as True.
Python
temp = 20
if temp < 16:
print('It is chilly out today')
>>
This program will produce no output as the temperature (temp) is >= 16. The condition
evaluates as False so the indented statement is skipped.
An else statement provides one or more statements to be executed if the initial condition
evaluates as False.
Python
temp = 20
if temp < 16:
print('It is chilly out today')
else:
print('It is warm out today')
Python
temp = 20
if temp < 16:
print('It is chilly out today')
elif temp > 27:
print('It is hot out today')
else:
print('It is warm out today')
8
UK Bebras Coding Challenge - Navigators Python hint and tips
Python
a = 7
b = 2
while a > b:
print (f'{a} is greater than {b}')
b = b + 1
Sometimes you might make a mistake in your code and the while loop condition always
evaluates as True. This is an infinite loop. You can stop your code running in the Python
IDLE by pressing ESC. If you use a different IDE make sure you know how to halt your code.
9
UK Bebras Coding Challenge - Navigators Python hint and tips
Python
for i in range(3):
print ('Hello')
>> Hello
>> Hello
>> Hello
You can keep the output on a single line by using an end of line character:
Python
You can use the value of the iterator variable if you need. In the following example the
iterator variable is named i:
10
UK Bebras Coding Challenge - Navigators Python hint and tips
Python
for i in range(3):
print(i)
>> 0
>> 1
>> 2
Notice that the sequence of values start at 0 and end at 2. The range function generates
a sequence of values starting at 0 and up to, but not including, the value specified. You
can also specify a specific start value and a step value. For example:
Python
>> 2
>> 3
>> 4
Python
>> 3
>> 5
>> 7
>> 9
11
UK Bebras Coding Challenge - Navigators Python hint and tips
Python
>> 10
>> 8
>> 6
>> 4
>> 2
Notice that the program stops outputting values after 2 because the range excludes the
specified end value.
Python
>> B
>> e
>>
>> k
>> i
>> n
>> d
In this example character is a variable. On each iteration of the loop its value is the next
character in the string. You can name this variable whatever you wish.
12