Built in functions

Python has many built in functions to make programming easier.  You have already encountered some them like int(),float() or list() when converting data types, range() for making a list of integers, along with print() in a lot of the examples.

Here are some more details for some of them along with a few of the more common ones that you will use.

print

The print function will take any number of values separated by commas and prints them to the screen as a continuous line.

# print using a single value
print("This is to be shown to the user")

# print using multiple values
name = "John"
age = "20"
print("Hello", name, "you are", age)

You can use triple quotes to do multiple lines of text including new lines. You can also include single and double quotes within a triple quote print.

# print using triple quotes
print("""This ia a multi line
quote block, you can include quotes like '
and " within it.
""")

The f print method allows you to include variables within your print statement. This is useful if you are needing a lot of data printed out.

# print substituting values
user = "John Doe"
location = "New York"
print(f"The user {user} is currently in {location}")

Here the print statement is replacing any curly bracket value with the matching variable contents.


range

The range function generates a list of integers. This is particularly useful in loops.
If provided with only one parameter the range function counts from 0 until it hits that value e.g.
range(5) generates:

0 1 2 3 4

If two parameters are provides however the function assigns them as the beginning and ending values. So range(0, 5) is the same as just range(5)

So range(4,9) would generate:

4 5 6 7 8

If a third parameter is provided then it is used as the step value. So range(5, 20, 5) would generate:

5 10 15

As such you can never give the step argument a value of 0.

The code:

test_list = range(5)
for value in test_list:
    print(value)
print("")
test_list2 = range(4, 9)
for value in test_list2:
    print(value)
print("")
test_list3 = range(0, 20,  5)
for value in test_list3:
    print(value)
print("")

Gives an output:

0
1
2
3
4

4
5
6
7
8

0
5
10
15

input

The input function allows the program to receive a keyboard input. When it is called the program halts until an input is submitted. This function then outputs a string containing the data that was entered on the keyboard.

This function has an optional parameter which accepts a string that is shown as a prompt to the user. This saves the developer from having to use a separate print function to explain what the input is asking for.

name = input("Please enter your name? ")

len

The len function returns the length of an object that you pass into the function. If the object is a list it will tell you how many entries there are in it. If it is a string then it will show how many characters are in it.

The code:

test_list = [34, 91, 2, 920]
print(len(test_list))
print("")
test_string = "Hello World!"
print(len(test_string))

Gives an output:

4

12

type

The type function returns the data type of the argument. This is useful when you are unsure of the data type or dealing with user created data types such as classes.

The code:

test = "Hello World!"
print(type(test))
print("")
test_2 = 9043.01
print(type(test_2))
print("")
test_3 = ["cat", "dog", "rabbit"]
print(type(test_3))

Gives an output:

<class 'str'> 

<class 'float'> 

<class 'list'>
Report a Glow concern
Cookie policy  Privacy policy

Glow Blogs uses cookies to enhance your experience on our service. By using this service or closing this message you consent to our use of those cookies. Please read our Cookie Policy.