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¶
๐ 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
โ Back: Syntax & Comments | Next: Variables & Data Types โ