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



Download 2,61 Mb.
bet80/200
Sana20.06.2022
Hajmi2,61 Mb.
#681748
1   ...   76   77   78   79   80   81   82   83   ...   200
Bog'liq
Python Projects for Beginners A Ten Week Bootcamp Approach to Python

Writing Your First Function


Now that we know what the syntactical structure looks like, let’s go ahead and write our own:

# writing your first function
def printInfo( ): # defines what the function does when called print("Name: John Smith") print("Age: 45") printInfo( ) # calls the function to run printInfo( ) # calls the function again

Go ahead and run the cell. We define a function called printInfo, which prints two lines of information each time it’s called. Below that we call the function twice, which outputs the information two times. It may not seem like a more efficient program, but imagine you needed to output that exact information 20 times in a program. It’s concise and efficient.

Function Stages


In Python there are two stages to each function. The first stage is the function definition. This is where you define the name of the function, any parameters it’s supposed to accept, and what it’s supposed to do in the block of code associated with it. See Figure 5- 2.

Figure 5-2. The two steps of a function life cycle (definition and call)
CHapTeR 5 FUnCTIonS
The second stage is known as the function call. Functions will never run until called, so you can define as many functions as you’d like, but if you never call one of them, then nothing will happen. When you call a function, it will run the block of code within the definition.

UDF vs. Built-in


Without even knowing it, you’ve been using functions this whole time. Functions such as range, print, len, etc., are all known as “built-in” functions. They are included in Python because they serve a specific purpose to help build our applications. Now that we’re learning about functions, we can begin to create our own known as UDFs or “user- defined functions.”

Performing a Calculation


Let’s check out one more example of a basic function, but this time do more than just print inside of the block:

# performing a calculation in a function def calc( ):
x, y = 5, 10 print(x + y) calc( ) # will run the block of code within calc and output 15

Go ahead and run the cell. We’ll get an output of 15 every time we call the calc function here.

Download 2,61 Mb.

Do'stlaringiz bilan baham:
1   ...   76   77   78   79   80   81   82   83   ...   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