Common Built-in Functions

Overview

The functions and methods that are in this section are at the core of the python langauge.

Print

So far we have only provided the print statement one term at a time. This has automatically concatenated the new line character ("\n"). If we want to print multiple varibles on one line,we can sperate them with a comma.
print("x")
print("x","y")
x = "Hello"
y = "World"

print (x)
print (x,y)
Returns:
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 sequence

Range

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)
Returns:
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 list
x = "welcome"
y = []
for i in x:
    y.append(i)
y.sort()
print (y)
Returns:
['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)))
Returns:
3.45
3
3.45
<class 'float'>
['3', '.', '4', '5']

Minimum, Maximum and Abolute Values

max (x) - This returns the maximum number from a list
min(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))
Returns:
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))
Returns:
529
This can be a very dangerous function to use, if you are evaluating strings from the user then you are giving them complete control over the code and the system that runs the code.