Built-in functions
Python programs can use what we refer to as functions. A function in mathematics has a name, a set of parameters, and an output. For example, f(x) = 2x
is a function that its output is twice as its input. In Python, there are built-in functions that look similar to mathematical functions but work differently. For example, we can call a function called abs(x)
:
x = -1
y = abs(x)
In this program, y
will have the value 1
since abs(x)
produces the absolute value of its input x
. Thus, some functions like abs
work as expressions, and they can produce values.
Here are some built-in functions to try: int(x)
, float(x)
, abs(x)
. More functions will be covered later.
A note on data and types
Computer programs always work with data. But how can we give data to our program? There are two simple ways to do this. One is to give data using a keyboard and one is a file. We will not get into the details of the files at this moment. Let us only focus on receiving data from the user on your computer's keyboard. Recall that two persons are involved in a computer program. One is the programmer who writes the program, and one is the user that uses the program. The user interacts with the program, mostly through a keyboard. The user's device with the program is called an I/O device (e.g., keyboard). We say that the user provides input to the program when entering data on the keyboard. How can the programmer let the program receive input from the user? This is done using a function called input
, which we will study below.
Data is first kept in (short memory) RAM. To know where the data is and work with the data, we define names in a program that points to the RAM data. These names refer to variables, and variables refer to data in memory. Each data object in a variable has a type. You can think of a type as a category or mathematically as set membership. There are several types that we will study during the course of these notes that are important for useful programs. Integers and floats are numeric types that are sets of integer and real numbers, respectively.
x = 1
y = 1.0
In the program above, x
is the type integer, and y
is the type float (real number). To ask Python about the type of a variable, you can use the type
function. Open the Python shell and write the following.
x = 1
type(x)
When executing the two lines above, the shell outputs <class int>
, which means x
belongs to integers.
Another important type is the string type. When the user enters a value on a string, that value is first stored in the form of a string.
x = input('Enter a value: ')
type(x)
When executing the two lines above on the Python shell, you will see that the shell outputs <class str>
, which means the type of x
is a str
ing. We will study many more times in later chapters and examine their relationships and possible operations on these types.
A Simple I/O Program
I/O refers to input and output. Programs generally receive input and produce output. Here, we will build a program that receives two numbers n
and k
and outputs the result of n รท k
. This program must use the input()
function to receive the user's value of n
and k
. If we do not use input()
, we will have to use a fixed value for n
and one for k
. We can do that, but it will not be a useful program. When using input()
, we should always remember that the type of values returned by input()
is always string
. So, if we want to deal with numbers, we will have to convert the value we receive on input()
to one of a numeric (int
or float
) type. Here's how we should write the program. First, receive the two values:
x = float(input())
y = input()
y = float(y)
This program uses the input()
function and immediately gives the value of the input()
function to the float()
function. This is to convert thevalue type ( string
) to float
.
Note that x
and y
are both variables that receive their input from the keyboard and the type of input is converted to float
. There is no difference between y = float(input())
and y = input()
followed by y = input(y)
. The input
function always produced a string value that the user enters on the keyboard. The float
function converts a value of type string to a value of type float. To see the difference between a float and a string, try these lines on the Python shell:
x = input()
type(x)
x = float(x)
type(x)
You will see that after executing the second line type(x)
, the Python shell says <class 'str'>
, and after executing the fourth line, the Python shell says <class 'float'>
. Also, note that after executing the first line x = input()
, you are required to enter something on the keyboard. Try to enter something that is not a number and see what will happens after executing the third line.
Next, we will do the operation and print the results:
print(x/y)
Remember that the type of division is always float
. You cannot divide anything unless it is of a numeric type.
We can improve this program to also round the results up to some decimal places. Luckily, there is a built-in function in Python to round real numbers to the nearest integer or to some d decimal places:
z = round(x/y)
print(z)
Here, the round function takes only a single argument, which is a value of type float. In this case, round(x/y)
will round the result of x/y
to the nearest integer with three cases.
- If the decimal part is less than 5, then it will round down.
- If the decimal part is more than 5, then it will round up.
- If the decimal is 5, then it will round to the nearest even number. Example:
round(1.5)
andround(2.5)
will evaluate to 2.
Another way to use the round function is to round the given value up to some decimal places:
z = round(x/y, 2)
print(z)
The program above will round x/y
to only two decimal places. For example, if x/y
was 1.456, then round(x/y,2)
is 1.46.