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

Conteúdo do Curso

Introduction to Finance with Python

Introduction to Finance with Python

1. Python Basics
2. Options Trading
3. Time Series Forecasting

book
Functions

What is a function?

Creating function

Function <function name> can be defined using following syntax:

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:

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))
```

What is the value of the variable d?

Selecione a resposta correta

Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

Seção 1. Capítulo 3
We're sorry to hear that something went wrong. What happened?
some-alt