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


Declaring a List of Mixed Data Types



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

Declaring a List of Mixed Data Types


Lists can hold any data type, even other lists. Let’s check out an example of several data types:

# declaring a list of mixed data types num = 4.3 data = [num, "word", True] # the power of data collection print(data)

Go ahead and run that cell. This will output [4.3, ‘word’, True]. It outputs 4.3 as the first item because when the list is defined, it stores the value of num, not the variable
itself.

Lists Within Lists


Let’s get a little more complex and see how lists can be stored within another list:

# understanding lists within lists
data = [5, "book", [ 34, "hello" ], True] # lists can hold any type print(data) print( data[2] )

Go ahead and run that cell. This will output [5, ‘book’, [34, ‘hello’], True] and [34, ‘hello’]. The first output is the entire data variable’s value, which stores an integer, a string, a list, and a boolean data type. The second output is the list stored inside of our data variable, which is located at index 2 and includes an integer and string data type.

Accessing Lists Within Lists


In the last cell, we saw how to output the list stored within the data variable. Now, we’ll see how we can access the items within the inner list. To access items within a list normally, we simply use bracket notation and the index location. When that item is another list, you simply add a second set of brackets after the first set. Let’s check out an example and come back to it:

# using double bracket notation to access lists within lists print( data[2][0] ) # will output 34 inner_list = data[2] # inner list will equal [34, 'hello'] print( inner_list[1] ) # will output 'hello'

Go ahead and run that cell. The first output will be 34. This is because our first index location is accessing the second index in data, which is a list. Then the second index location specified is accessing the value in that list at location zero, which results in the integer of 34. The second output is “hello”. We get this result because we declared a
ChApter 4 LIsts And Loops
variable to store the value at index 2 of our data variable, which happens to be a list. Our inner_list variable is now equal to [34, ‘hello’] and we access the value at index 1, which is the string “hello”. To get a little bit more understanding of how multi-indexing works, check out Table 4-1.
Table 4-1. Multi-indexing values


Download 2,61 Mb.

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