Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lære Functions | Python Basics
Introduction to Finance with Python

bookFunctions

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/value v, which passed as argument;
  • map(f, c) - returns iterable collection, in which function f applied to each element of iterable collection c;
  • filter(f, c)- returns iterable collection, which contains those elements of iterable collection c, for which value of f is True;
  • zip(c1,c2) - returns list of tuples with an item from each iterable collection c1 and c2, by iterating them in parallel;
  • len(s) - return number of items in object s(either can be sequence, like string, or collection);
  • max(c) - returns maximum value in iterable collection c;
  • max(arg1,arg2) - returns maximum value of elements arg1 and arg2(can be used with larger number of arguments);
  • min() - have two ways of usage, just like max(), however - finds minimum value;
  • sum() - returns sum of elements into collection;
  • pow(base, power) - returns number base raised to power power;
  • 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:

question mark

What is the value of the variable d?

f = lambda x: x ** 2 + 2 * x
a = [1, -1, 3]
b = ['a', 'c', 'd', 'g', 'b']
c = list(map(f, a))
d = dict(zip(b, c))

Select the correct answer

Var alt klart?

Hvordan kan vi forbedre det?

Tak for dine kommentarer!

Sektion 1. Kapitel 3

Spørg AI

expand

Spørg AI

ChatGPT

Spørg om hvad som helst eller prøv et af de foreslåede spørgsmål for at starte vores chat

Suggested prompts:

Spørg mig spørgsmål om dette emne

Opsummér dette kapitel

Vis virkelige eksempler

Awesome!

Completion rate improved to 7.14

bookFunctions

Stryg for at vise menuen

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/value v, which passed as argument;
  • map(f, c) - returns iterable collection, in which function f applied to each element of iterable collection c;
  • filter(f, c)- returns iterable collection, which contains those elements of iterable collection c, for which value of f is True;
  • zip(c1,c2) - returns list of tuples with an item from each iterable collection c1 and c2, by iterating them in parallel;
  • len(s) - return number of items in object s(either can be sequence, like string, or collection);
  • max(c) - returns maximum value in iterable collection c;
  • max(arg1,arg2) - returns maximum value of elements arg1 and arg2(can be used with larger number of arguments);
  • min() - have two ways of usage, just like max(), however - finds minimum value;
  • sum() - returns sum of elements into collection;
  • pow(base, power) - returns number base raised to power power;
  • 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:

question mark

What is the value of the variable d?

f = lambda x: x ** 2 + 2 * x
a = [1, -1, 3]
b = ['a', 'c', 'd', 'g', 'b']
c = list(map(f, a))
d = dict(zip(b, c))

Select the correct answer

Var alt klart?

Hvordan kan vi forbedre det?

Tak for dine kommentarer!

Sektion 1. Kapitel 3
some-alt