Sign up or log in to check your solutions
First time here? Enter your email and password, and we'll create your account.
Returning user? Enter your email and password, and you'll be logged in.
- 1. Input, print and numbers
- 2. Integer and float numbers
3. Conditions: if, then, else
- 4. For loop with range
- 5. Strings
- 6. While loop
- 7. Lists
- 8. Functions and recursion
- 9. Two-dimensional lists (arrays)
- 10. Sets
- 11. Dictionaries
- 13. HTML5 and CSS
- 14. Responsive Design with Bootstrap
- 15. jQuery
Lesson 3. Conditions: if, then, else
1/15. The syntaxLet'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.
elseare 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.
x = int(input()) if x > 0: print(x) else: print(-x)