Author Dusty Phillips Reviewers


>>> stock = "FB", 75.00, 75.03, 74.90



Download 2,95 Mb.
Pdf ko'rish
bet152/183
Sana20.07.2022
Hajmi2,95 Mb.
#831085
1   ...   148   149   150   151   152   153   154   155   ...   183
Bog'liq
python3-oop

>>> stock = "FB", 75.00, 75.03, 74.90
>>> stock2 = ("FB", 75.00, 75.03, 74.90)
If we're grouping a tuple inside of some other object, such as a function call, list 
comprehension, or generator, the parentheses are required. Otherwise, it would 
be impossible for the interpreter to know whether it is a tuple or the next function 
parameter. For example, the following function accepts a tuple and a date, and returns 
a tuple of the date and the middle value between the stock's high and low value:
import datetime
def middle(stock, date):
symbol, current, high, low = stock
return (((high + low) / 2), date)
mid_value, date = middle(("FB", 75.00, 75.03, 74.90),
datetime.date(2014, 10, 31))
The tuple is created directly inside the function call by separating the values with 
commas and enclosing the entire tuple in parenthesis. This tuple is then followed
by a comma to separate it from the second argument.
www.it-ebooks.info


Python Data Structures
[
 158 
]
This example also illustrates tuple unpacking. The first line inside the function 
unpacks the 
stock
parameter into four different variables. The tuple has to be 
exactly the same length as the number of variables, or it will raise an exception. We 
can also see an example of tuple unpacking on the last line, where the tuple returned 
inside the function is unpacked into two values, 
mid_value
and 
date
. Granted, this 
is a strange thing to do, since we supplied the date to the function in the first place
but it gave us a chance to see unpacking at work.
Unpacking is a very useful feature in Python. We can group variables together to 
make storing and passing them around simpler, but the moment we need to access 
all of them, we can unpack them into separate variables. Of course, sometimes we 
only need access to one of the variables in the tuple. We can use the same syntax 
that we use for other sequence types (lists and strings, for example) to access an 
individual value:

Download 2,95 Mb.

Do'stlaringiz bilan baham:
1   ...   148   149   150   151   152   153   154   155   ...   183




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