![]() 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. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |