2. Functions def

1def is_even(x):
2    return x % 2 == 0
3
4
5print(is_even(13))
6print(is_even(12))
line that just executed

next line to execute

Print output (drag lower right corner to resize)
Frames
Objects

Activity: CodeLens 2.1 (cl_l20_2a_en)

2.1. Example

  • Define a function factorial

1def factorial(n):
2    f = 1
3    while n > 0:
4        f = f * n
5        n = n - 1
6    return f
7
8
9for i in range(5):
10    print(factorial(i))
line that just executed

next line to execute

Print output (drag lower right corner to resize)
Frames
Objects

Activity: CodeLens 2.1.1 (cl_l20_2b_en)

You have attempted 1 of 2 activities on this page