LearnFrenzy
Sign in Take a free test

Python Programming · Topic 3 of 25

Operators and Expressions Questions and Concepts

  • 4 exercises
  • 50 questions
  • ~60 min
  • Answers explained

Coding Techniques

Operators and Expressions · Study material

Digit and number tricks with // and %

// and % pull numbers apart, divmod() does both at once, and three-argument pow() computes powers modulo m without huge intermediate numbers.

n = 1234
print(n % 10, n // 10 % 10, n // 100)        # last digit, tens digit, drop two digits
print(n % 2 == 0, -7 % 3)                    # even test; Python's % is never negative for a positive divisor
print(pow(2, 10), pow(2, 100, 1_000_007))    # pow with a modulus is fast modular exponentiation
print(max(3, 9, 4), abs(-12), round(7 / 3, 2))

hours, minutes = divmod(135, 60)
print(f"{hours}h {minutes}m")

Output

4 3 12
True 2
1024 698635
9 12 2.33
2h 15m

Defaults with or – and its trap

name = input().strip()
display = name or "Guest"            # "" is falsy → "Guest"

# Trap: 0 is falsy too, so a real 0 is replaced
discount = 0
rate = discount or 10                # 10 – probably not what you wanted
rate = 10 if discount is None else discount   # explicit None check keeps 0

Testing one value against several

grade = "C"

if grade == "A" or "B":          # ✘ always True: "B" is a truthy operand
    pass

if grade == "A" or grade == "B":  # ✔ correct but long
    pass

if grade in ("A", "B"):          # ✔ idiomatic
    pass

Operators checklist

  • Use / for averages, // only for whole-number division.
  • Bracket -x when raising to a power.
  • Use chained comparisons for ranges.
  • Use in with a tuple or set to test several values.
  • Compare to None with is, everything else with ==.
  • Use bitwise operators for flags and powers of two, not for logic.

Ready to test your Programming?

Timed online tests with instant scores and explanations.

About this section

About Python Programming

Why it matters

  • Python is the most popular first language and the default for data science, AI, automation and scripting
  • Campus coding rounds and technical interviews increasingly allow – or prefer – Python
  • "Predict the output" questions on lists, strings, dictionaries and mutable defaults are a favourite in written tests
  • Clean Python with comprehensions, generators and OOP is what product companies expect from freshers

Who it is for

  • School students (Class 11–12 computer science with Python) preparing for board exams and practicals
  • College students (B.Tech, BCA, MCA, B.Sc CS) preparing for semester exams, lab work and viva
  • Freshers preparing for campus placements, coding tests and Python interviews
  • Anyone moving into data science, AI or automation who needs strong Python fundamentals

What every topic gives you

  • Study material in five tabs: concepts with runnable examples, coding techniques, programs, interview questions and a cheat sheet
  • Topic-wise MCQs that check your understanding of every concept
  • "Predict the output" puzzles – the trickiest part of written tests
  • "Find the error" programs: syntax errors, runtime exceptions and logical bugs
  • Coding problems with solutions and real output, every one run on Python 3 – open any example in the online Python compiler

Asked in

  • Campus placements
  • TCS, Infosys, Wipro, Accenture tests
  • Python interviews for freshers
  • Data science interviews
  • CBSE / university practicals
  • GATE CS

How to use this section

  1. Read the study material of a topic and run the examples in the browser.
  2. Practise the four exercises – MCQs, output, errors, coding problems – each marked Easy, Medium or Hard, and read every explanation.
  3. Revise with the interview questions and cheat sheet, then move to the next topic.

Search LearnFrenzy

Esc

Quick links