Functions
What is a function?
Creating function
Function <function name>
can be defined using following syntax:
def <function name>(arg1,arg2,...,argN):
<body of function>
return <output of function>
Where arg1
,arg2
,...,argN
- arguments of function, <body of function>
- actual block of code, <output of function>
- return value, in fact - not necessary for some functions.
Built-in functions
Also, additionally to function, that we could create, there are a variety of built-in functions, which we can use without importing any libraries beforehand.
Among them we can highlight next:
type(v)
- returns type of a variable/valuev
, which passed as argument;map(f, c)
- returns iterable collection, in which functionf
applied to each element of iterable collectionc
;filter(f, c)
- returns iterable collection, which contains those elements of iterable collectionc
, for which value off
isTrue
;zip(c1,c2)
- returns list of tuples with an item from each iterable collectionc1
andc2
, by iterating them in parallel;len(s)
- return number of items in objects
(either can be sequence, like string, or collection);max(c)
- returns maximum value in iterable collectionc
;max(arg1,arg2)
- returns maximum value of elementsarg1
andarg2
(can be used with larger number of arguments);min()
- have two ways of usage, just likemax()
, however - finds minimum value;sum()
- returns sum of elements into collection;pow(base, power)
- returns numberbase
raised to powerpower
;int()
,float()
,complex()
,bool
,list()
,dict()
,set()
,str()
,tuple()
, etc - convert variable/value to the corresponding datatype.
Addition
For more detailed explanation and examples - dive into video:
Tack för dina kommentarer!
Fråga AI
Fråga AI
Fråga vad du vill eller prova någon av de föreslagna frågorna för att starta vårt samtal
Ställ mig frågor om detta ämne
Sammanfatta detta kapitel
Visa verkliga exempel
Awesome!
Completion rate improved to 7.14
Functions
Svep för att visa menyn
What is a function?
Creating function
Function <function name>
can be defined using following syntax:
def <function name>(arg1,arg2,...,argN):
<body of function>
return <output of function>
Where arg1
,arg2
,...,argN
- arguments of function, <body of function>
- actual block of code, <output of function>
- return value, in fact - not necessary for some functions.
Built-in functions
Also, additionally to function, that we could create, there are a variety of built-in functions, which we can use without importing any libraries beforehand.
Among them we can highlight next:
type(v)
- returns type of a variable/valuev
, which passed as argument;map(f, c)
- returns iterable collection, in which functionf
applied to each element of iterable collectionc
;filter(f, c)
- returns iterable collection, which contains those elements of iterable collectionc
, for which value off
isTrue
;zip(c1,c2)
- returns list of tuples with an item from each iterable collectionc1
andc2
, by iterating them in parallel;len(s)
- return number of items in objects
(either can be sequence, like string, or collection);max(c)
- returns maximum value in iterable collectionc
;max(arg1,arg2)
- returns maximum value of elementsarg1
andarg2
(can be used with larger number of arguments);min()
- have two ways of usage, just likemax()
, however - finds minimum value;sum()
- returns sum of elements into collection;pow(base, power)
- returns numberbase
raised to powerpower
;int()
,float()
,complex()
,bool
,list()
,dict()
,set()
,str()
,tuple()
, etc - convert variable/value to the corresponding datatype.
Addition
For more detailed explanation and examples - dive into video:
Tack för dina kommentarer!