Keywords & Literals 🚀
Python Keywords & Literals is a core Python concept covering learn about Python's reserved keywords and various types of literals (numeric, string, boolean). Essential reference for board exams and interviews. This topic is essential for academic learning, board exam preparation, and developing optimized real-world code.
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,continuedef,del,elif,else,exceptfor,from,if,import,in,islambda,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
💻 Implementation: Seeing Literals in Action
- Python (3.10+)
# 🛒 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:
passis a keyword, butPass(capital P) is not. - MCQ: "What does the
Noneliteral represent?" -> Answer: The absence of a value.
💡 Pro Tip: "Simplicity is the soul of efficiency." - Austin Freeman