➗ Python Basics

Operators & Expressions

ऑपरेटर और एक्सप्रेशन

⏱ 2 hr3 topicsInteractive
🎯 By the end: You can use every category of Python operator correctly, predict the result of an expression, and tell apart / , // and %.

Operators are the verbs of programming — they do things to values: add them, compare them, combine conditions. Python has several families of operators, and this chapter runs through all of them with a playground so you can test every result yourself.

1Arithmetic operators

These do maths. Three of them surprise beginners, so look closely:

OperatorMeaningExampleResult
+ - *add, subtract, multiply7 * 214
/division — always gives a float7 / 23.5
//floor division — drops the fraction7 // 23
%modulo — the remainder7 % 21
**exponent (power)2 ** 38
The % (modulo) operator is a workhorse: n % 2 == 0 tests if n is even, and it's everywhere in real programs. Run the playground and change the numbers to feel how /, // and % differ.
Arithmetic operators▶ runs real Python
main.py
Output
Click Run to execute.
Key points
  • / always gives a float (7/2 = 3.5); // is floor division (7//2 = 3).
  • % (modulo) gives the remainder; n % 2 == 0 tests for even numbers.
  • ** is exponent (2**3 = 8).

2Comparison and logical operators

Comparison (relational) operators compare two values and always produce a BooleanTrue or False:

OperatorMeaning
==equal to
!=not equal to
<   >less than, greater than
<=   >=less than or equal, greater than or equal
Don't confuse = (assignment — put a value into a variable) with == (comparison — ask if two values are equal). Mixing them up is a classic bug.

Logical operators combine Boolean values:

  • and — True only if both sides are True.
  • or — True if either side is True.
  • not — flips True to False and vice versa.
Comparison & logical operators▶ runs real Python
main.py
Output
Click Run to execute.
Key points
  • Comparison operators (==, !=, <, >, <=, >=) always return a Boolean (True/False).
  • = assigns a value; == checks equality — don't confuse them.
  • Logical: and needs both True, or needs either True, not flips the Boolean.

3Assignment, identity and membership

Three more handy families:

  • Assignment: = stores a value. The shortcuts +=, -=, *=, /= update a variable in place — x += 5 means x = x + 5.
  • Membership: in and not in test whether a value appears inside a string, list, etc. — 'a' in 'cat' is True.
  • Identity: is and is not test whether two names refer to the same object in memory (not just equal values). It's most commonly used as x is None.
Operator precedence decides the order: ** first, then * / // %, then + -, then comparisons, then not, and, or. When in doubt, use parentheses to make the order explicit and readable.
Assignment & membership▶ runs real Python
main.py
Output
Click Run to execute.
Key points
  • = assigns; shortcuts like += update in place (x += 5 means x = x + 5).
  • Membership in / not in test if a value is inside a string/list.
  • Identity is / is not test if two names are the same object (often x is None).
  • Precedence: ** then * / // % then + - then comparisons then not/and/or; use parentheses to be clear.

★ Practical: predict, then run

First predict each result on paper, then check it in the playground:

  1. What do 17 / 5, 17 // 5 and 17 % 5 each give?
  2. Is 4 == 4.0 True or False? What about 4 is 4.0?
  3. Evaluate: (3 > 2) and (5 != 5).
  4. Start with x = 8, then do x *= 2 twice. What is x?

Ready for the chapter test?

Answer 5 questions. Score 60% or more to mark this chapter complete.

Start the test →

💡 Log in to save your progress and earn the certificate.