Зміст курсу
Learn Python from Scratch
Learn Python from Scratch
Writing your own functions (2/5)
All we have learned till now can be used inside the function. For example, you can easily put conditional statements inside the function body.
For example, we can define a function that will check if the number is odd or even.
# define a function def is_odd(n): if n % 2 == 0: return "even" else: return "odd" # testing function print('2 is', is_odd(2)) print('3 is', is_odd(3))
Завдання
Define a function is_positive
checking if the number is positive (in that case return positive), negative (return negative), or equals zero (return zero).
Завдання
Define a function is_positive
checking if the number is positive (in that case return positive), negative (return negative), or equals zero (return zero).
Все було зрозуміло?
Writing your own functions (2/5)
All we have learned till now can be used inside the function. For example, you can easily put conditional statements inside the function body.
For example, we can define a function that will check if the number is odd or even.
# define a function def is_odd(n): if n % 2 == 0: return "even" else: return "odd" # testing function print('2 is', is_odd(2)) print('3 is', is_odd(3))
Завдання
Define a function is_positive
checking if the number is positive (in that case return positive), negative (return negative), or equals zero (return zero).
Завдання
Define a function is_positive
checking if the number is positive (in that case return positive), negative (return negative), or equals zero (return zero).
Все було зрозуміло?
Writing your own functions (2/5)
All we have learned till now can be used inside the function. For example, you can easily put conditional statements inside the function body.
For example, we can define a function that will check if the number is odd or even.
# define a function def is_odd(n): if n % 2 == 0: return "even" else: return "odd" # testing function print('2 is', is_odd(2)) print('3 is', is_odd(3))
Завдання
Define a function is_positive
checking if the number is positive (in that case return positive), negative (return negative), or equals zero (return zero).
Завдання
Define a function is_positive
checking if the number is positive (in that case return positive), negative (return negative), or equals zero (return zero).
Все було зрозуміло?
All we have learned till now can be used inside the function. For example, you can easily put conditional statements inside the function body.
For example, we can define a function that will check if the number is odd or even.
# define a function def is_odd(n): if n % 2 == 0: return "even" else: return "odd" # testing function print('2 is', is_odd(2)) print('3 is', is_odd(3))
Завдання
Define a function is_positive
checking if the number is positive (in that case return positive), negative (return negative), or equals zero (return zero).