Python Revision Tour Quick Revision Notes
Python Revision Tour Quick Revision Notes
● Definition: A token is the smallest individual unit in a Python program that is meaningful
to the interpreter. It is the fundamental building block of the language's syntax.
● In-Depth Explanation: Think of a Python script as an English sentence. The sentence is
made up of words, punctuation, and spaces. Similarly, a Python script is made up of
tokens. The interpreter first performs lexical analysis, which is the process of breaking
the raw text of your code into a stream of these meaningful tokens. Understanding tokens
is like learning the alphabet and punctuation before you can form words and sentences.
● Example: In the line print("Hello"), the tokens are:
1. print (an Identifier, which happens to be a built-in function name)
2. ( (a Punctuator)
3. "Hello" (a String Literal)
4. ) (a Punctuator)
Topic 2: Keywords
● Definition: A keyword is a word that is reserved by the Python language and has a
special, predefined meaning and function.
● In-Depth Explanation: Keywords form the structural foundation of Python. They define
the rules and logic of the language, such as how to create a loop (for, while), make a
decision (if, else), or define a function (def). You cannot use these words for any other
purpose, such as naming a variable or function, because the interpreter would be
confused.
● Example:
Python
# Using the 'if' and 'else' keywords to create a conditional statement.
age = 18
if age >= 18: # 'if' is a keyword
print("You are eligible to vote.")
else: # 'else' is a keyword
print("You are not eligible to vote.")
# for = 10 # This would cause a SyntaxError because 'for' is a keyword.
Topic 3: Identifiers
● Definition: An identifier is a custom name given by a programmer to an entity like a
variable, function, class, or module.
● In-Depth Explanation: Identifiers are the labels you create to keep track of things in
your program. Choosing good, descriptive identifiers is one of the most important
aspects of writing readable and maintainable code. student_name is a much better
identifier than sn.
● Syntax & Rules:
1. Must start with a letter (a-z, A-Z) or an underscore (_).
2. Can be followed by any combination of letters, numbers (0-9), and underscores.
3. Cannot be a keyword.
4. Are case-sensitive (score and Score are different).
● Illustrative Example:
Python
# A good, descriptive identifier following conventions.
player_score = 100
# Also valid, often used for "private" or internal variables by convention.
_internal_counter = 0
# A valid, but poor, identifier. It's confusing.
__l_ = "Some value" # 'l' looks like '1'
# An invalid identifier.
# 2nd_player_score = 50 # Will cause a SyntaxError: cannot start with a number.
Topic 4: Literals
● Definition: A literal is a fixed data value that appears directly in the source code. It
represents itself.
● In-Depth Explanation: When you write x = 10, x is the identifier, but 10 is the literal. It's
the raw, hard-coded data. Python supports various kinds of literals to represent different
types of data.
● Illustrative Examples:
Collection list, tuple, dict, set Literals that define scores = [98, 95,
data structures. 100], point = (10,
20)
Dynamic Typing
● Definition: Dynamic Typing means the data type of a variable is determined at runtime
and can change during the program's execution.
● Explanation: You don't pre-declare a variable's type (like int x; in other languages).
Python automatically detects the type from the value you assign. A variable is just a name
pointing to an object, and that name can be made to point to a different type of object
later.
Immutable Data Types
● Definition: An immutable object is one whose internal state cannot be changed after it
has been created.
● Analogy: Think of an immutable object as an engraved stone tablet. To change the
text, you cannot erase it; you must carve a new tablet.
● Types: int, float, bool, str, tuple.
● Illustrative Example:
Python
name = "Amit"
print(f"Initial name: {name}, Memory ID: {id(name)}")
# It looks like we are changing the string, but we are not.
# We are creating a NEW string object "Amit Sharma" and making the 'name'
# label point to this new object. The original "Amit" object may be garbage collected.
name = name + " Sharma"
print(f"Updated name: {name}, Memory ID: {id(name)}") # The ID will be different!
Operator Precedence
● Definition: Operator Precedence is the set of rules that dictates the order in which
operators are evaluated in a complex expression.
● Explanation: The acronym PEMDAS is a useful guide: Parentheses, Exponents,
Multiplication/Division, Addition/Subtraction. Operators on the same level (like * and /)
are evaluated from left to right.
● Illustrative Example:
Python
# Without parentheses, precedence rules apply:
# 1. 3 * 5 = 15
# 2. 10 + 15 = 25
result_1 = 10 + 3 * 5
print(f"Result without parentheses: {result_1}") # 25
# With parentheses, we override the default order:
# 1. (10 + 3) = 13
# 2. 13 * 5 = 65
result_2 = (10 + 3) * 5
print(f"Result with parentheses: {result_2}") # 65
Loops
● Definition: A loop (for, while) repeatedly executes a block of code.
● Explanation: Use a for loop when you want to iterate over a known sequence (definite
iteration). Use a while loop when you want to repeat as long as a condition is true
(indefinite iteration).
s.split(del) Splits the string at del and "a-b-c".split('-') -> ['a', 'b',
returns a list. 'c']
L.append(item) Adds item to the end of the L=[1]; L.append(2) -> [1, 2]
list.
D[key] = val Sets the value for key. D={'a':1}; D['b']=2 -> {'a':1,
'b':2}