Learning Python: The Ultimate Guide for Beginners to Coding with Python with Useful Tools



Download 2,02 Mb.
Pdf ko'rish
bet118/126
Sana01.01.2022
Hajmi2,02 Mb.
#285662
1   ...   114   115   116   117   118   119   120   121   ...   126
Bog'liq
Learning Python The Ultimate Guide for Beginners to Coding with

Keyword Arguments


This  is  a  name/value  pair,  which  you  pass  to  a  function.  You  have  to
directly link the value and the variable name inside the argument. With this,
there won’t be any confusion when you pass the argument to the function.
let’s  rewrite  our  previous  code  using  the  keyword  arguments  to  call  our
animal_list()
def animal_list(animal_kind, pet_name):
“““ Display Details About Animal.”””
print(f “\nThis is a {animal_kind}.”)
print (f “My {animal_kind}’s name is {pet_name.title()}.”)
animal_list (animal_kind =“Cat”, pet_name= “Lucy”)
animal_list (pet_name= “Chase”, animal_kind =“Rat”)
Our first four lines haven’t changed. However, there is a difference between
the  last  two  lines.  When  the  interpreter  reads  the  fourth  line,  it  calls  the
function  and  assigns  the  parameter  Cat  to  animal_kind  and  Lucy  to
pet_name respectively. For the next line, it will perform the same operation
because  the  order  doesn’t  matter  when  dealing  with  keyword  arguments.
Therefore, the last two lines of codes are equivalent and produce the same
output. Our program above will display:
This is a Cat.
My Cat’s name is Lucy.
This is a Rat.
My Rat’s name is Chase.
When using keyword arguments, ensure that the right parameter names are
used in the function definition to avoid any error.


Default Value
Besides  using  the  keyword  parameter  and  position  argument,  each
parameter can have a default value in a function. if you provide a parameter
that is providing in the function, Python uses the value. However, if there is
no parameter value, then Python uses a default value for the parameter.
def animal_list(pet_name, animal_kind = “Cat”):
“““ Display Details About Animal.”””
print(f “\nThis is a {animal_kind}.”)
print (f “My {animal_kind}’s name is {pet_name.title()}.”)
animal_list (pet_name= “Lucy”)
Compare  this  program  with  the  previous  code.  Did  you  notice  anything
from the first and last line? Well, animal_list function is used to describe a
Download 2,02 Mb.

Do'stlaringiz bilan baham:
1   ...   114   115   116   117   118   119   120   121   ...   126




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