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



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

How It Works


Figure 5-4 depicts how the two parameters passed into the function are calculated first and then returned to the original location of the call to be stored into a variable. This variable can now be used later in the program with that value.

Figure 5-4. Returning information and storing into a variable
You can return any data type but may only return a single variable. When you need to return more than one piece of data, you would return a collection of data:
>>> def returnMultiple():
>>> a = 5
>>> b = 10
>>> return [a, b] # one data type holding multiple items

Using Return


The return statement is used to send information back to where the function call occurred. So far, we’ve used the print statement to output information, but this wouldn’t work if we needed access to that value later in the program. Instead, we can return the value and save it into a variable that we can work with later. Let’s check out a couple examples:

# using return keyword to return the sum of two numbers def addNums(num1, num2): return num1 + num2 num = addNums(5.5, 4.5) # saves returned value into num print(num) print( addNums(10, 10) ) # doesn't save returned value

CHapTeR 5 FUnCTIonS
Go ahead and run the cell. We’ll get 10 and 20 for an output. When we call addNums the first time, it runs the function with 5.5 and 4.5 and returns the sum. It then stores that returned value within num. The second time we call the function, we simply print it in place. From here, we could reuse the value stored in num, but not the value returned by the second call.

Ternary Operator


A ternary operator is a shorthand Python branching statement. These operators can be used to assign values into a variable, or in this case, deciding what the return from a function:

# shorthand syntax using a ternary operator def searchList(aList, el):
return True if el in aList else False result = searchList( [ "one", 2, "three" ], 2) # result = True print(result)

Go ahead and run the cell. The ternary operator returns True because the given condition is met. The same code written out normally would look like the following:
>>> if el in aList:
>>> return True >>> else:
>>> return False
It’s generally good practice to write less if you can, but it’s not a necessity.

Download 2,61 Mb.

Do'stlaringiz bilan baham:
1   ...   81   82   83   84   85   86   87   88   ...   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