Operators, Expressions and Type Conversion
This is a free course. Log in to run the code exercises and track your progress.
Operators, Expressions and Type Conversion
Besides the arithmetic (+ - * / // % **), relational (== != < > <= >=) and logical (and or not) operators, Class 11 adds:
- Augmented assignment:
+= -= *= /= //= %= **=. - Identity:
isandis notcheck whether two names refer to the same object, while==checks whether the values are equal. - Membership:
inandnot incheck whether a value occurs in a sequence.
a = [1, 2]
b = [1, 2]
print(a == b, a is b) # True False: equal values, two objects
print(3 in [1, 2, 3]) # True
print("y" not in "Python") # False
Python converts types for you in mixed arithmetic, 3 + 2.5 gives the float 5.5 (implicit conversion). You convert explicitly with int(), float(), str(), bool() (explicit conversion, or type casting). int(7.9) truncates to 7, and int("7.9") raises a ValueError.
Read two integers a and b from one line. Print a // b, a % b and a ** b on one line separated by spaces, then on the next line True if a % b == 0 (b divides a) else False.
a, b = map(int, input().split())
print(a / b, a, b) # print a // b, a % b and a ** b instead
print(a == b) # does b divide a?Log in to edit and run this code.