5/15/2023 0 Comments List of dictionaries pythonSo, for example, let’s make a very simple list: list_1 = Mutable data types like lists and dictionaries behave differently. a += 1 is run, and this moves the pointer from 5 to 6, it doesn’t actually change the number 5.If we do a += 1, we’re not actually updating 5 to 6. If a data type is immutable, it means it can’t be updated once it’s been created. The variable name a acts like a pointer toward 5, and it helps us retrieve 5 whenever we want.ĥ is an integer, and integers are immutable data types. Let’s start by considering a simple variable assignment: a = 5 To understand the difference between mutable (changable) and immutable (unchangable), it’s helpful to look at how Python actually treats these variables. That means a global list or dictionary can be changed even when it’s used inside of a function, just like we saw in the examples above. And while most of the data types we’ve worked with in introductory Python are immutable (including integers, floats, strings, Booleans, and tuples), lists and dictionaries are mutable. In Python, data types can be either mutable (changeable) or immutable (unchangable). So what’s actually happening here? We’ve bumped up against the difference between mutable and immutable data types. Just as we saw with lists, our global content_ratings variable has been changed, even though it was only modified inside of the make_percentages() function we created. Then we'll to iterate over the dictionary again and do some math to each value to calculate the percentage. We’ll need to start a count at zero and then iterate over each value in the dictionary, adding them to the count so we get the total number of ratings. To do this, we’ll write a function called make_percentages() that will take a dictionary as an argument and convert the counts to percentages. Let’s imagine we want to calculate a percentage for each age rating, so we can get a picture of which age ratings are the most common among apps in the App Store. In the snippet below, we’re starting with a dictionary that contains counts for the number of apps with each age rating in the dataset (so there are 4,433 apps rated “4+”, 987 apps rated “9+”, etc.). To make this a bit more realistic, we’ll be using data from the AppleStore.csv data set that’s used in our Python Fundamentals course (the data is available for download here). Now, let’s write a function that takes a dictionary as an argument to see if a global dictionary variable will be modified when it’s manipulated inside a function as well. New_list = duplicate_last(a_list = initial_list)Īs we can see, here the global value of initial_list was updated, even though its value was only changed inside the function! Dictionaries What happens if we try the same experiment with a different data type, like a list? Below, we’ll create a function called duplicate_last() that will duplicate the final entry in any list we pass it as an argument. But number_1 is an integer, which is a pretty basic data type. ![]() We’ve seen that what we do to a variable like number_1 above inside a function doesn’t affect its global value. (Incidentally, if you’re confused about the difference between parameters and arguments, Python’s documentation on the subject is quite helpful.) What About Lists and Dictionaries? Lists Thus, the variable number_1 can have one value (5) globally, and a different value (50) inside the function, where it is isolated. This is because Python stores variables from a function in a different memory location from global variables. number_1 = 5ĭef multiply_and_add(number_1, number_2):Ī_sum = multiply_and_add(number_1, number_2)Īs we can see above, the function worked correctly, and the values of the global variables number_1 and number_2 did not change, even though we used them as arguments and parameter names in our function. Then, we’ll see whether all of the variable usage inside our function has affected the global value of these variables. We’ll also use the variable names as the function’s parameter names. Then, we’ll use those global variables as the arguments in a function that performs some simple math. We’ll create two global variables, number_1 and number_2, and assign them to the integers 5 and 10. ![]() ![]() To understand how Python handles global variables inside functions, let’s do a little experiment. In this tutorial, we’re going to take a close look at how Python treats different data types when they’re being manipulated inside of functions, and learn how to ensure that our data is being changed only when we want it to be changed. But what they do with our data can be a little confusing, and if we’re not aware of what’s going on, it could cause serious errors in our analysis. Python’s functions (both the built-in ones and custom functions we write ourselves) are crucial tools for working with data.
0 Comments
Leave a Reply. |