Overview
The functions and methods that are in this section are at the core of the python langauge.print("x")
print("x","y")
x = "Hello" y = "World" print (x) print (x,y)
Hello Hello World
Input
We have already featured the input statement, but here is a breif recap.y = input(x) - This returns a string that is input (Python 3). The user will be prompted with the string x.
y = raw_input(x) - This returns a string that is input (Python 2). The user will be prompted with the string x.
Length
len(x) - This returns the length of a sequenceRange
range(start, end, step) -Creates a list from start upto but not including end, in steps of step.for i in range(15): print (i) for i in range(2,15): print (i) for i in range(2,15,3): print (i)
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 2 3 4 5 6 7 8 9 10 11 12 13 14 2 5 8 11 14
Sort
sort(x) - This returns a sorts a listx = "welcome" y = [] for i in x: y.append(i) y.sort() print (y)
['c', 'e', 'e', 'l', 'm', 'o', 'w']
Varaible Types
Converting varibles is required to get your program to treat the data correctly.str(x) - Converts a variable x to a string
int(x) - Converts a variable x to int
float(x) - Converts a variable x to a float
list(x) - Coverts an interable variable x into a list
type(x) - Returns the types of a variable
x = 3.45 print (str(x)) print (int(x)) print (float(x)) print (type(x)) print (list(str(x)))
3.45 3 3.45 <class 'float'> ['3', '.', '4', '5']
Minimum, Maximum and Abolute Values
max (x) - This returns the maximum number from a listmin(x) - This returns the minimum number from a list
abs(x) - This returns the absolute value of a number, thi is he distance from zero
x = [2,4,8,1,56] print (max(x)) print (min(x)) x = -54 print (abs(x))
56 1 54
Evaluate
eval(x) - This takes a string that will be treated like python code and the result of running this code will be returned.x = "23**2" print (eval(x))
529