Skip to content

Keywords & Literals πŸš€ΒΆ

Prerequisites: Python Syntax

Mentor's Note: Think of Keywords as the "Forbidden Words"β€”you can use them to tell the computer what to do, but you can never use them as names for your variables! πŸ’‘


🌟 The Scenario: The Magic Spells πŸ§™β€β™‚οΈΒΆ

Imagine you are at a wizard school.

  • Keywords: There are specific "Spell Words" like Fire or Fly. These words have magic powers. You cannot name your pet cat "Fire" because every time you call your cat, you might accidentally burn the house down! πŸ“¦
  • Literals: The actual "Ingredients" you use in your spells, like 3 frog legs or "Purple Powder". They are the raw data. βœ…

πŸ“– Concept ExplanationΒΆ

1. Python KeywordsΒΆ

Keywords are the Reserved Words in Python. They have a predefined meaning and cannot be used as variable names.

The "Magic" List (Common ones): - False, None, True (Note the Uppercase!) - and, as, assert, break, class, continue - def, del, elif, else, except - for, from, if, import, in, is - lambda, not, or, pass, return, while, with

2. Python LiteralsΒΆ

A Literal is data whose value is exactly as it appears.

Type Example
Numeric 100, 3.14, 2+3j
String "Hello", 'Python'
Boolean True, False
Special None (Represents "nothing")

🎨 Visual Logic: Categorizing Keywords¢

graph TD
    A[Keywords] --> B[Flow Control: if, for, while]
    A --> C[Boolean Logic: and, or, not]
    A --> D[Structures: def, class, return]
    A --> E[Error Handling: try, except, raise]

πŸ’» Implementation: Seeing Literals in ActionΒΆ

# πŸ›’ Scenario: Brewing a potion
# πŸš€ Action: Using various literals

# String Literal πŸ§ͺ
ingredient = "Magic Dust"

# Numeric Literal πŸ”’
quantity = 5

# Boolean Literal βœ…
is_ready = True

# Special Literal 🚫
error_msg = None 

print(f"Brewing {quantity} bags of {ingredient}...")

πŸ“Š Sample Dry Run (Check)ΒΆ

Name Type Valid as Variable?
score Literal (None) Yes βœ…
while Keyword No ❌ (It's a reserved spell!)
100 Literal (Numeric) No ❌ (Names can't be raw numbers)

πŸ’‘ Interview & Board Focus πŸ‘”ΒΆ

  • MCQ: "Which of the following is NOT a keyword in Python?" -> Key Point: pass is a keyword, but Pass (capital P) is not.
  • MCQ: "What does the None literal represent?" -> Answer: The absence of a value.

πŸ’‘ Pro Tip: "Simplicity is the soul of efficiency." - Austin Freeman


← Back: Syntax & Comments | Next: Variables & Data Types β†’