05. Function and Method Calls

Function Calls

Calls are a type of expression that call on some lines of code. A function call works by invoking a function, which is simply a code block with several lines of code.

Take, for example, the print() function. As the help manual page says, this prints the values to a stream, or to sys.stdout (standard out) by default. Our standard out here, is the terminal window (or IDLE GUI on Windows). Thus, when we call the print command, it'll print out to our terminal window.

>>> print("Hello world!")
Hello world!

Try out the len() function as well, which is used to count the number of characters in some argument.

>>> len("Hello world!")

Additionally, there are functions that allow for user interaction, which we'll see soon.

>>> userName = input("What is your name? ")
What is your name? JohnDoe

Now if you print out the userName variable, you'll get the user's inputted name.

>>> print(userName)

Method Calls

Furthermore, there exists method calls, which are functions that come with objects, or data types. To use a particular object's method, use the dot notation such as objectInstance.methodCall().

For example, we can use method calls from the String object to run functions on any string we created.


This will look for how many times the G letter appears in our string. Note that this method is specific to the primitive data type string. If you try the same method call with an int, for instance, you'll get an error

>>> x = 42
>>> x.count("C")
Traceback (most recent call last):
   File "<stdin>", line 1, in <module>
AttributeError: 'int' object has no attribute 'count'

We'll see many data types other than the string and int, and along with the variety of methods that come along with them.

Aching back from coding all day?

Self-Massage Tool

Aching back from coding all day? Try Back Problems

Relieve spasms, tight muscles, trigger points and pressure points with the Body Back Buddy! This trigger point massage is designed to help you self-message any area of your body - especially those that are hard to reach. Keeping your muscles relaxes and out of contraction is importan in helping to reduce pain and prevent muscle injury.

$ Check price
29.9529.95Amazon 4.5 logo(3,443+ reviews)

More Back Problems resources

Learn to be a Pythonista!

Programming Python

Learn to be a Pythonista! Try Python

Programming Python shows in-depth tutorials on the language's number of application domains including: system administration, GUIs, the Web, networking, front-end scripting layers, and more. This book focuses on commonly used tools and libraries to give you a comprehensive understanding of Python’s many roles in practical, real-world programming.

$ Check price
64.9964.99Amazon 4 logo(56+ reviews)

More Python resources