Standard functions

The table below contains the most important standard functions for this course. Check the Python-documentation for a complete list and for detailed descriptions.
Function Explanation Example
abs Absolute value. The argument should be a number.
float Returns a float-value of a number or a string that can interpreted as a number. float('1.3') gives 1.3
float('1.3\n') gives 1.3
format Format conversion. format(1/3,'.2f') gives '0.33'
format(1/3,'5.1f') gives ' 0.3'
help Gives help. help(help)
help(input)
input Read a string from standard input. s = input()
x = eval(input('Give a number: ))
int Converts a number or a string to an integer. int(3.9) gives 3
int(-3.9) gives -3
int('34') gives 34
len Returns the length of a list, a tuple ocr a string. len((1,2,3)) gives 3
len('"Hej!"') gives 6
list Constructs a list from a string or a tuple. list('abc') gives [a, b, c]
max Returns the maximum value in a list, tuple or string. max(3, 7, 5) gives 7
max('axel') gives 'x'
min Returns the minimum value in a list, tuple or string. min([4, 3.5, 4, 9]) gives 3.5
open Opens a file. ifil = open('data.txt','r')
ofil = open('result.txt', 'w')
pow Returns x raised to y.
print Prints in console window. print(x + 4)
print('Ciao!', end='') No line brake 
range Generates a sequence of integers.
Typically used in for-statements.
range(5) generates 0, 1, 2, 3, 4
range(3, 6) generates 3, 4, 5
range(0, 6, 2) generates 0, 2, 4
round Rounds floating point numbers. round(5/3) gives 2
round(5/3, 1) gives 1.7
round(5/3, 2) gives 1.67
sorted Returns a list with elements sorted. sorted('yxa') gives ['y', 'x', 'a']
sorted((3,1,5,2)) gives [1, 2, 3, 5]
str Returns the argument as a string. str([1, 2]) gives '[1, 2]'
sum Returns the sum. sum((1, 2, 3)) gives 6
type Returns the data type. x = 2
type(x) == int gives True
type(x) == str gives False
zip Returns an object for merging things. list(zip('ab', '12')) gives
[('a', '1'), ('b', '2')]

list(zip([1, 3, 5],[2, 4, 6, 8]) gives
[(1, 2), (3, 4), (5, 6)]

Valid CSS!