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


Monday: Creating and Calling Functions



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

Monday: Creating and Calling Functions
Today’s lesson is all about understanding what functions are, the stages of a function, and how to write a function. We’ll find out why they are so important in programs and how they’ll make our lives easier.
To follow along with the content for today, let’s open up Jupyter Notebook from our
python_bootcamp” folder. Once it’s open, create a new file, and rename it to “Week_05.” Next, make the first cell markdown that has a header saying: “Creating & Calling Functions.” We’ll begin working underneath that cell.
What Are Functions?
One of the best reference materials for programming is w3schools.4 They even have Python tutorials. Their official documentation describes functions as the following:
A function is a block of code which only runs when it is called.
You can pass data, known as parameters, into a function. A function can return data as a result.5
Programs will often need to run the same code repeatedly, and although loops help with that, we don’t want to write the same loop many times throughout our program. The solution to the issue is using a function. They essentially store code that will only run when called upon.
CHapTeR 5 FUnCTIonS
All functions are generally associated with a single task or procedure. This makes it easier for us to break down our program into functions. If you build a program that needs to repeatedly print five lines of information, and you need to output it in five different places, you would need to write 25 lines of code. Using a function, you would store the five lines in a block and call the function whenever you need it, resulting in five lines for the information to output and five lines for calling the function, for a grand total of ten lines. This results in a much more efficient program.

Function Syntax


Like loops, functions follow an exact pattern for every functioned created. They all begin with the keyword “def”, followed by the name of the function. This name is arbitrary and can be anything except for Python keywords and previously defined functions. Directly following the name is the parenthesis, and within those are parameters. We won’t cover parameters until tomorrow so just know that parameters are optional, but parenthesis is required. Lastly, we need an ending colon like any other Python statement. See Figure 5- 1 for an example.

Figure 5-1. Function syntax

Download 2,61 Mb.

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