20 page Code Snippets (1)
20 page Code Snippets (1)
This title page gives reminders on input. The following pages are mostly
extracted from the excellent CodingClub cards which can also be purchased
in pocket book format here: http://codingclub.co.uk/codecards/
For this competition, your program must produce no output other than the
requested answer. For inputs then, you must use them without any
prompts e.g.
num1 = int(input())
num2 = int(input())
nums = input().split()
num1 = int(nums[0])
num2 = int(nums[1])
num3 = int(nums[2])
Example:
You can also use them to filter items into a new list:
>>> nums = [1,2,3,4,5,6,7,8]
>>> odds = [int(x) for x in nums if x % 2 == 1]
>>> print(odds)
[1,3,5,7]
APPENDIX 2: More useful…
string functions
Reversing a string or extracting parts of it…
list functions
Summarising or sorting a list…
>>> A = [3,1,5,2,4]
>>> print(sum(A)) >>> A = [3,1,5,2,4]
>>> B = sorted(A)
15 >>> print(B)
>>> print(max(A) + min(A)) [1,2,3,4,5]
6
PROBLEM A: “Input a single line containing 5 whole numbers which are each
separated by a space. Output the range of the numbers which is defined to be
the smallest number subtracted from the largest number.”
numText = input().split()
nums = [int(i) for i in numText]
sortedNums = sorted(nums)
range = sortedNums[4] - sortedNums[0]
print(range)
num = int(input())
start = 1
for i in range(num-1):
start = start * 2 + 3
print(start)