---
# System prepended metadata

title: 'Python Functions: Intermediate'
tags: [python]

---

# Python Functions: Intermediate

> * Edit the open_dataset() function and add the name of iOS apps data set ('AppleStore.csv') as a default argument for the file_name parameter.
 
> * Without passing in any argument, use the open_dataset() function to open the AppleStore.csv file. Assign the data set to a variable named apps_data.

> * Inspect the apps_data data set after you run the code to confirm that everything went as expected. You can try to print the first few rows or just use the variable inspector of the code editor.

```python=
# INITIAL CODE
def open_dataset(file_name):
    
    opened_file = open(file_name)
    from csv import reader
    read_file = reader(opened_file)
    data = list(read_file)
    
    return data
# SOLUTION CODE
def open_dataset(file_name='AppleStore.csv'):
    
    opened_file = open(file_name)
    from csv import reader
    read_file = reader(opened_file)
    data = list(read_file)
    
    return data

apps_data = open_dataset() #重要
```

## open built-in.
> https://docs.python.org/3/library/functions.html?highlight=open%20built#open
![](https://i.imgur.com/XqFnfev.png)

We implemented the following logic into the function definition:

If do_sum has a value of True, then we return a + b.
Else (if do_sum is False), we return a - b.
When do_sum is True, the body of the if statement is executed, and the else clause is omitted — this means that return a + b is executed, and return a - b is omitted. When do_sum is False, the opposite happens: return a + b is omitted, and return a - b is executed.

To use an analogy, we can think of the do_sum parameter as a lever or a toggle which we can switch to and fro, depending on whether we want to compute the sum or the difference.

Note that there's more than one way to make the sum_or_difference() function work as we want it to. Below, we redefine the function without using an else clause:

![](https://i.imgur.com/hcAvqYs.png)

The main difference between tuples and lists boils down to whether we can modify the existing values or not. In the case of tuples, we can't modify the existing values, while in the case of lists, we can. Below, we're trying to modify the first value of a list and a tuple.

![](https://i.imgur.com/0gaS31G.png)

Tuples are called immutable data types because we can't change their state after they've been created. Conversely, lists are mutable data types because their state can be changed after they've been created. The only way we could modify tuples, and immutable data types in general, is by recreating them. This is a list of all the mutable and immutable data types we've learned so far.

![](https://i.imgur.com/O5Kbf2b.png)

test

```python=
def open_dataset(file_name='AppleStore.csv', header=True):        
    opened_file = open(file_name)
    from csv import reader
    read_file = reader(opened_file)
    data = list(read_file)
    
    if header:
        return data[1:], data[0]
    else:
        return data
apps_data, header = open_dataset()
```

The part of a program where a variable can be accessed is often called scope. The variables defined in the main program are said to be in the global scope, while the variables defined inside a function are in the local scope.

![](https://i.imgur.com/sGNhxrY.png)

![](https://i.imgur.com/9d9yYmV.png)

###### tags: `python`
