Python Projects for Beginners a ten-Week Bootcamp Approach to Python Programming



Download 2,61 Mb.
bet87/200
Sana20.06.2022
Hajmi2,61 Mb.
#681748
1   ...   83   84   85   86   87   88   89   90   ...   200
Bog'liq
Python Projects for Beginners A Ten Week Bootcamp Approach to Python

Handling Function Scope


When dealing with variables declared in a function, you generally won’t need to access it outside of the function. However, in order to access that value, best practice is to return it:

# accessing variables defined in a function def scopeTest( ): word = "function" return word value = scopeTest( ) print(value)

Go ahead and run the cell. Now we have access to the word defined within the function, we simply assign the returned value to another variable to work with.

In-Place Algorithms


When passing variables into a function, you’re simply passing the value of that variable and not the variable itself. Such that the following will not alter the variable num:
>>> num = 5 >>> def changeNum(n):
>>> n += 5
>>> print(num)
This is different when changing information via index though. Due to how index’s work, via memory location and not by reference, changing an element in a list by the index location will alter the original variable. Let’s check out an example:

# changing list item values by index
sports = [ "baseball", "football", "hockey", "basketball" ] def change(aList):
aList[ 0 ] = "soccer" print("Before Altering: { }".format(sports) ) change(sports) print( "After Altering: { }".format(sports) )

Go ahead and run the cell. Notice how the first item in the sports list changes when the function is called. This is due a change in value by the index itself when the list is passed in. These are known as in-place algorithms because no matter where you alter the information, it will change the values in the memory location directly.

THURSDAY EXERCISES


1. Names: Create a function that will change the list passed in with a parameter of name at a given index. Such that if I were to pass in “Bill” and index 1, it would change “Rich” to “Bill.” Use the list and function definition in the following:
>>> names = ['Bob', 'Rich', 'Amanda'] >>> def changeValue(aList, name, index):

Today was important in understanding how variable accessibility works. Knowing this information will keep our variables secure.

Download 2,61 Mb.

Do'stlaringiz bilan baham:
1   ...   83   84   85   86   87   88   89   90   ...   200




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©hozir.org 2024
ma'muriyatiga murojaat qiling

kiriting | ro'yxatdan o'tish
    Bosh sahifa
юртда тантана
Боғда битган
Бугун юртда
Эшитганлар жилманглар
Эшитмадим деманглар
битган бодомлар
Yangiariq tumani
qitish marakazi
Raqamli texnologiyalar
ilishida muhokamadan
tasdiqqa tavsiya
tavsiya etilgan
iqtisodiyot kafedrasi
steiermarkischen landesregierung
asarlaringizni yuboring
o'zingizning asarlaringizni
Iltimos faqat
faqat o'zingizning
steierm rkischen
landesregierung fachabteilung
rkischen landesregierung
hamshira loyihasi
loyihasi mavsum
faolyatining oqibatlari
asosiy adabiyotlar
fakulteti ahborot
ahborot havfsizligi
havfsizligi kafedrasi
fanidan bo’yicha
fakulteti iqtisodiyot
boshqaruv fakulteti
chiqarishda boshqaruv
ishlab chiqarishda
iqtisodiyot fakultet
multiservis tarmoqlari
fanidan asosiy
Uzbek fanidan
mavzulari potok
asosidagi multiservis
'aliyyil a'ziym
billahil 'aliyyil
illaa billahil
quvvata illaa
falah' deganida
Kompyuter savodxonligi
bo’yicha mustaqil
'alal falah'
Hayya 'alal
'alas soloh
Hayya 'alas
mavsum boyicha


yuklab olish