- 1. Wprowadzanie, drukowanie i liczby
- 2. Liczba całkowita i liczba zmiennoprzecinkowa
- 3. Warunki: if-then-else
- 4. Dla pętli z zakresem
- 5. Smyczki
- 6. Podczas gdy pętla
- 7. Listy
- 8. Funkcje i rekursja
- 9. Dwuwymiarowe listy (tablice)
- 10. Zestawy
- 11. Słowniki
- 12. JavaScript
- 13. HTML5 and CSS
- 14. Responsive Design with Bootstrap
- 15. jQuery
Ad place
Lesson 3. Conditions: if, then, else
1/15. The syntax
Let's write a program which, given an integer x, prints |x| — its absolute value.By definition of absolute value, if x > 0, such program should just print x. Otherwise, it should print -x. Naturally we are about to check if some condition is met. The behaviour is different when it is met and when it is not.
The keywords
if
and else
are used in such situations. Look at the code.Given -273 as the input data, it outputs 273. Why? Check the tick "step by step" and look at the visualizer.
-273
x = int(input()) if x > 0: print(x) else: print(-x)