Tuesday 24 May 2011

Built-in Functions

The Python interpreter has a number of functions that are always available for use, meaning you do not have to import any libraries. This lesson will cover a few of the built-in functions.



A function consists of three components: Name, Input Argument(s) and Output. 
An example function would be a = abs(x), which is shown (exploded) below.

Output          Name           Input Argument
a             =    abs        (                 x               )

The input argument is placed inside of the parentheses, and the output is the value that is returned by the function. There are functions that will require multiple inputs to work, such as the pow function which returns x to the power of y. Eg:

pow(x, y)
pow(5, 2
5 to the power of 2 = 25

Using the example above, you can assign a variable (name) to the output:
result = power(5, 2)

Below is a list of some of the Built-in functions that you may find useful when starting out:
Function Description Example
abs(x) Returns the absolute value of x. ans = abs(-3) ans = 3
pow(x, y) Returns x to the power of y. ans = pow(5, 2) ans = 25
round(x[, n]) Returns the floating point value x rounded to n digits after the decimal point. If n is omitted, it defaults to zero. ans = round(1.6) ans = 2
int([number|string[, base]]) Converts a number or string to an integer. If no arguments are given, returns 0. ans = int('7')   ans = 7
float([x]) Converts a string or number to a floating point. ans = float('1.5') ans = 1.5
range([start], stop[, step]) Constructs progression of integer values. The arguments must be integers. The step value defaults to 1 if it is omitted and if the start value is omitted it defaults to 0. range(0, 5)
str(x) Converts object x to a string representation str(ans)
print([object, ...], sep=' ', end='\n', file=sys.stdout) Print object(s) to the stream file, separated by sep and followed by end. sep, end and file, if present must be given as keyword arguments. See below
input([prompt]) If the prompt argument is present, it is written to the standard output without a trailing newline. The function then reads a line from input, converts it to a string and returns that. See below

Print() Example
ans = 15
print("This is an example!")
print("This is another example:", 777)
print(ans)
print("The answer is:", ans)
print("I", "like", "pizza!")
print("I", "like", "pizza!", sep="-")
print("There are", ans, "pizza slices left...")

Output from above:
This is an example!
This is another example: 777
15
The answer is: 15
I like pizza!
I-like-pizza!
There are 15 pizza slices left...

Input() Example
guess = input('Please enter your guess: ')
name = input('Please enter your name: ')
number = int(input('Enter number: '))
print("Hello", name, "you guessed", guess, "and your number is", number)

Output from above:
Please enter your guess: 10
Please enter your name: nathan
Enter number: 20
Hello nathan you guessed 10 and your number is 20


Python has an extensive library of functions and as such, I can't possibly teach all of them. There is a help feature available to learn more about Pythons functions which can be accessed from the Python Shell.

Help -> Python Docs

You can also view the documentation on the Python website. Two great libraries to look at are math and random, which will be upcoming in later exercises and examples.

Once you have a grasp on this lesson, continue on to User-Controlled Input and Output.

-Nathan (everythingPython)

No comments:

Post a Comment