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



Download 2,61 Mb.
bet59/200
Sana20.06.2022
Hajmi2,61 Mb.
#681748
1   ...   55   56   57   58   59   60   61   62   ...   200
Bog'liq
Python Projects for Beginners A Ten Week Bootcamp Approach to Python

Index Location

Value at Location

Data Type

Can Be Indexed Again

0

5

Integer

no

1

‘book’

string

Yes

2

[34, ‘hello’]

List

Yes

3

true

Boolean

no

Notice that strings can also be index further. If you wanted to only print out the “b” in “book,” you would simply write the following:
>>> print( data[ 1 ][ 0 ] ) # will output 'b'

Changing Values in a List


When you work with lists you need to be able to alter the value of the items within the list. It’s like re-declaring a normal variable to a different value, except you access the index first:

# changing values in a list through index data = [5, 10, 15, 20] print(data) data[0] = 100 # change the value at index 0 - (5 to 100) print(data)

Go ahead and run that cell. Before we altered the value at index 0, it outputs [5, 10, 15, 20]. Once we accessed the zero index and changed its value to 100, however, the list ended up changing to [100, 10, 15, 20].

Variable Storage


When variables are declared, the value assigned is put into a location in memory. These locations have a specific reference ID. It’s not often you’ll need to check the ID of a variable, but for educational purposes, it’s good to know how storage works. We would use the id() function to check the storage location in memory for a variable:
>>> a = [ 5, 10 ]
>>> print( id(a) ) # large number represents location in memory
When a list is stored in memory, each item is given its own location. Changing the value using index notation will change the value stored within that memory block. Now, if a variable’s value is another variable, like so:
>>> a = [5, 10]
>>> b = a
Changing the value at a specific index will change the value for both lists. Let’s see an example:

# understanding how lists are stored a = [5, 10] b = a print( "a: { }\t b: { }".format(a, b) )
print( "Location a[0]: { }\t Location b[0]: { }".format( id(a[0]), id(b[0]) ) ) a[0] = 20 # re-declaring the value of a[0] also changes b[0] print( "a: { }\t b: { }".format(a, b) )

Go ahead and run that cell. We’re going to get several outputs here. The first is printing out the values of both list variables to show that they have the same values. The second print statement will output the location in memory for each list’s first item. Then lastly, after we change the value of the first item within our “a” list, the value in our “b” list also changes. This is because they share the same memory location.
ChApter 4 LIsts And Loops

Download 2,61 Mb.

Do'stlaringiz bilan baham:
1   ...   55   56   57   58   59   60   61   62   ...   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