📖
Introduction to programming with Python
  • Introduction to programming with Python 3
  • Preface
    • Common misconceptions about learning to program
    • The Hard truth about learning to program
    • Personal qualities for success
  • 1 - The way of the program
    • Python Programming Language
    • The first program
    • What is debugging?
    • Glossary
    • Exercises
  • 2 - Variables, expressions and statements
    • Values, types and variables
    • Common Built-in data types
    • Expressions
    • Code Format
    • Debugging
    • Glossary
    • Exercises
  • 3 - Functions
    • Python "built-in" Functions
    • Math Functions
    • Composition
    • User Defined Functions
    • PEP 8 Recommendations
    • Debugging
    • Glossary
    • Exercises
  • 4 - Conditionals
    • Boolean expressions
    • Conditional statements
    • PEP 8 Recommendations
  • 5 - Iteration
    • The while statement
    • The for statement
    • break and continue statements
    • Nested Loops
    • PEP 8 Recommendations
    • Debugging
    • Glossary
    • Exercises
  • 6 - A short introduction to testing: Building Reliable Software
  • 7 - A deeper dive into Strings, Lists and Tuples
    • More on Strings
    • More on Lists
    • More on Tuples
    • Debugging
    • Glossary
    • Exercises
  • 8 - A deeper look at Functions in Python
    • Function Preconditions and Postconditions
    • Positional and Keywords arguments
    • Nested Functions
    • Scope of a variable
    • Recursion
    • Functions' side effects
    • Glossary
    • Exercises
  • 9 - Code Documentation
    • Basics of Commenting Code
    • Documenting Code via Python Docstring
  • 10 - Sets and dictionaries
    • Sets
    • Dictionaries
    • Which data structure should I use?
    • Debugging
    • Glossary
    • Exercises
  • 11 - File I/O
    • Read/Write to a file
    • File management
    • Debugging
    • Glossary
    • Exercises
  • 12 - Handling Errors and Exceptions
  • 13 - Writing modules
  • 14 - Classes
    • Classes and Objects
    • Classes and Functions
    • Classes and Methods
    • Pythonic Magic: Understanding and Implementing Dunder Methods
    • Glossary
    • Exercises
  • 15 - Python's Type Hinting
  • Acknowledgements
Powered by GitBook
On this page
  • Function calls
  • Type conversion functions
  • Common built-in functions
  • Keyboard input
Edit on GitHub
  1. 3 - Functions

Python "built-in" Functions

Function calls

In the context of programming, a function is a named sequence of statements that performs a computation. When you define a function, you specify the name and the sequence of statements. Later, you can "call" the function by name. We have already seen one example of a function call:

>>>type(32) 
 <type 'int'>
>>>

The name of the function is type. The expression in parentheses is called the argument of the function. The result, for this function, is the type of the argument.

It is common to say that a function "takes'' an argument and "returns'' a result. The result is called the return value.

Type conversion functions

Python provides built-in functions that convert values from one type to another. The int function takes any value and converts it to an integer, if it can, or complains otherwise:

>>> int('32')
 32 
>>> int('Hello') 
 Traceback (most recent call last): 
     File "<pyshell#40>", line 1, 
         in int('hello')
 ValueError: invalid literal for int() with base 10: 'Hello' 

int can convert floating-point values to integers, but it doesn't round off; it chops off the fraction part:

>>> int(3.99999)
 3
>>> int(-2.3)
 -2 
>>>

float converts integers and strings to floating-point numbers:

>>> float(32) 
 32.0 
>>> float('3.14159') 
 3.14159 
>>>

Finally, str converts its argument to a string:

>>> str(32)
 '32' 
>>> str(3.14159)
 '3.14159'
>>>

We know that the values returned are strings due to the single quotes around the numbers.

Common built-in functions

Python provides many useful function for common programming tasks. We have already seen one, the print} function. A subset of the built-in function is given in the table below:

Function
Description
Example

abs(x)

Returns the absolute value for x.

abs(-2) is 2.

max(x1, x2,...)

Returns the largest among x1, x2, ...

max(1, 5, 2) is 5.

min(x1, x2,...)

Returns the smallest among x1, x2, ...

min(1, 5, -2) is -2.

pow(a, b)

pow(2, 3) is 8.

round(x)

Returns an integer nearest to x. If x is equally close to two integers, the even one is returned.

round(5.4) is 5, round(3.5) is 4, round(4.5) is 4.

Keyboard input

The programs we have written so far are a bit rude in the sense that they accept no input from the user. They just do the same thing every time.

Python provides a built-in function called input that gets input from the keyboard. When this function is called, the program stops and waits for the user to type something. When the user presses the "Return" or "Enter" key, the program resumes and input returns what the user typed as a string.

>>> user_input = input()
 What are you waiting for? 
>>> print(user_input) 
 What are you waiting for? 
>>>

Before getting input from the user, it is a good idea to print a prompt telling the user what to input. The function input can take a prompt as an argument:

>>> name = input('What...is your name?\n') 
 What...is your name? 
 Arthur, King of the Britons! 
>>> print(name)
 Arthur, King of the Britons! 
>>>

The sequence '\n' at the end of the prompt represents a newline, which is a special character that causes a line break. That's why the user's input appears below the prompt.

If you expect the user to type an integer, you can try to convert the return value to am int:

>>> prompt = 'What is the velocity of an unladen swallow?\n' 
>>> speed = input(prompt) 
 What is the velocity of an unladen swallow?
 17 
>>> int(speed) 
 17 
>>>

But if the user types something other than a string of digits, you get an error:

>>> speed = input(prompt) 
 What is the velocity of an unladen swallow? 
 What do you mean, an African or a European swallow? 
>>> int(speed) 
 Traceback (most recent call last): 
     File "<pyshell#4>", line 1, 
         in int(speed) 
 ValueError: invalid literal for int() with base 10: 'What do you mean, an 
             African or a European swallow?' 
>>>

We will see how to handle this kind of error later.

Previous3 - FunctionsNextMath Functions

Last updated 1 year ago

Returns . Same as a**b.

aba^bab