PGoCareerGoCareer prep tools
Home
LoginSign up
  • Java
  • Python
  • AI
  • React
  • Angular
  • PHP
  • Node.js
  • SQL
  • DSA
  • HTML
  • CSS
  • JS
  • Spring
  • ML
  • MongoDB

Python · Theory

Assert

← All stacks

Theory

91/268

Assert

assert condition, "msg" checks a belief while developing. If False, AssertionError. Good for internal checks, not for user input validation in production (can be disabled with python -O).

Example — assert marks >= 0. If marks is -5, the script stops with a clear message.

Output — ok 72. If marks were -5, AssertionError. python -O can skip asserts.

Exam tip

What assert does + python -O warning.

Example

# Assert
marks = 72
assert marks >= 0, "marks cannot be negative"
print("ok", marks)

Output: ok 72. If marks were -5, AssertionError. python -O can skip asserts.

Short notes

  • Defassert checks a condition.
  • RuleFalse → AssertionError.
  • TrapDon’t use assert as the only user-input check in production.

Questions

1

Explain Assert as if you are teaching a junior — definition, then one tiny script.

2

What does the example print, and why?

3

What mistake do freshers make with Assert?

Previous← DateNextIDEs →
P

GoCareerGo

Utilities · Preparation Hub · Resume · CV · Tools — one workspace.

Workspace

DashboardProfilePreparation HubResume builderCV builderCareer planning

PDF Tools

Merge PDFSplit PDFCompress PDFImage to PDFAll toolsJobs

Image & QR

Compress ImageResize ImageQR ScannerQR GeneratorBlogIT interview prep

Company

FAQFeedbackContactPrivacyTermsSitemap

© 2026 GoCareerGo. Keep moving forward.