Skip to content

Keywords & Literals ๐Ÿš€

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 โ†’