# Introductory Python: Functions

*This post is part of a series.*

When programming, we sometimes create chunks of code that we intend to use more than once.

Rather than duplicating such code each time we want to use it, it is more efficient to store the code in a **function**.

For example, suppose we wanted to find how many digits are in a number. One option would be to duplicate the following code each time we needed to use it:

```
n = 26647983324
# turn the number into a string
n_as_str = str(n)
# count how many characters in the string
num_digits = len(n_as_str)
print(num_digits)
OUTPUT
------
11
```

A better option would be to define a function, using a `def function_name(input_names):`

statement followed by indented code that ends with a `return output_value`

statement.

```
def number_of_digits(n):
# turn the number into a string
n_as_str = str(n)
# count how many characters in the string
num_digits = len(n_as_str)
return num_digits
```

This way, we can call the function on any number we want, whenever we want, without having to rewrite any code.

```
num_digits = number_of_digits(26647983324)
print(num_digits)
OUTPUT
------
11
```

```
num_digits = number_of_digits(123456789876543212345678987654321)
print(num_digits)
OUTPUT
------
33
```

Functions are especially useful when we want to perform more complicated operations, such as the one below.

```
def add_digits(n):
# turn the number into a string
n_as_str = str(n)
# this will keep track of the sum of the digits;
# we'll add to it as we loop through the number string
running_total = 0
for character in n_as_str:
# turn digit string into an integer so we can
# add it to the running total
digit = int(character)
# increase the running total as appropriate
running_total += digit
return running_total
```

```
sum_digits = add_digits(123)
print(sum_digits)
OUTPUT
------
6
```

```
sum_digits = add_digits(26647983324)
print(sum_digits)
OUTPUT
------
54
```

## Exercise 1

Create a function `count_zeros`

that counts how many `0`

digits are in a number, and use it to count how many zeros there are in $5^{5^5}$ (you should get a result of 222).

If you have trouble, try first writing code to count the number of zeros in a simple number like $100101$. Then, you can generalize that code to any input number `n`

and paste it into the body of the function.

```
def count_zeros(n):
#
# put your code here!
#
return n # change this to the value you wish to return
```

```
num_zeros = count_zeros(5**5**5)
print(num_zeros)
DESIRED OUTPUT
--------------
222
```

## Exercise 2

Create a function `remove_numbers`

that removes numbers from a string, and use it to remove the letters from the string `'567899876S5748797U89767654C7653C09C8655465765E65435423S979876S976643'`

(you should get the result `SUCCESS`

).

```
def remove_numbers(s):
#
# put your code here!
#
return s # change this to the value you wish to return
```

```
filtered_string = remove_numbers('567899876S5748797U89767654C7653C09C8655465765E65435423S979876S976643')
print(filtered_string)
DESIRED OUTPUT
--------------
SUCCESS
```

## Exercise 3

Create a function `make_id`

that takes generates a random ID string consisting of `n_digits`

randomly chosen digits followed by a dash and then `n_letters`

randomly chosen letters. For example, `make_id(n_digits=3, n_letters=5)`

could output the following ID: `'272-PRXDS'`

.

To do this, you may want to make use of NumPy’s `rand`

function, which returns a random number between 0 and 1. (You may have to manipulate this number a bit to get a random digit between 0 and 9 for the digits portion, or 1 and 26 for the letters portion.)

```
import numpy
numpy.random.rand()
OUTPUT
--------------
(will be a different decimal each time)
```

```
def make_id(n_digits, n_letters):
#
# put your code here!
#
return [n_digits, n_letters] # change this to the value you wish to return
```

```
id = make_id(3,5)
DESIRED OUTPUT
--------------
(will be a different id each time)
```

## Solutions

**Exercise 1**

```
def count_zeros(n):
number_as_string = str(n)
num_zeros = number_as_string.count('0')
return num_zeros
```

**Exercise 2**

```
def remove_numbers(s):
output_string = ''
for char in s:
if not char.isdigit():
output_string += char
return output_string
```

**Exercise 3**

```
import numpy
def make_id(n_digits, n_letters):
output_string = ''
for _ in range(n_digits):
random_decimal_btw_0_and_1 = numpy.random.rand()
random_decimal_btw_1_and_10 = 1+9*random_decimal_btw_0_and_1
random_integer_btw_0_and_9_inclusive = int(random_decimal_btw_1_and_10)
output_string += str(random_integer_btw_0_and_9_inclusive)
output_string += '-'
alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
for _ in range(n_letters):
random_decimal_btw_0_and_1 = numpy.random.rand()
random_decimal_btw_1_and_26 = 1+25*random_decimal_btw_0_and_1
random_integer_btw_0_and_25_inclusive = int(random_decimal_btw_1_and_26)
random_letter = alphabet[random_integer_btw_0_and_25_inclusive]
output_string += str(random_letter)
return output_string
```

*This post is part of a series.*