Python Revision Tour Notes
1.1 Introduction
Python is an easy-to-learn, powerful programming language. It emphasizes code readability with
simple syntax.
Example:
# Basic program
print("Hello World!") # print -> built-in function, "Hello World!" -> string literal
1.2 Tokens in Python
Tokens are the smallest building blocks in Python programs: keywords, identifiers, literals,
operators, punctuators.
Example:
age = 18 # age -> identifier, 18 -> literal
if age >= 18: # if -> keyword, >= -> operator
print("Adult") # print -> built-in function
1.2.1 Keywords
Reserved words with special meaning. Cannot be used as identifiers.
Example:
if True: # if, True -> keywords
print("Condition is True")
1.2.2 Identifiers (Names)
User-defined names for variables, functions, etc.
Example:
student_name = "Alice" # student_name -> identifier
print(student_name)
1.2.3 Literals/Values
Fixed values like numbers, text, boolean.
Example:
x = 42 # 42 -> integer literal
y = "Hello" # "Hello" -> string literal
1.2.4 Operators
Symbols that perform operations.
Example:
a=5
b=3
sum = a + b # + -> addition operator
print(sum)
1.2.5 Punctuators
Symbols used to organize code.
Example:
if (a > b): # () -> parentheses, : -> colon punctuator
print("A is greater")
1.3 Barebones of a Python Program
A simple structure includes statements, functions, and expressions.
Example:
def greet(): # greet -> function identifier
print("Hi there!") # print -> function
greet()
1.4 Variables and Assignments
Variables store data. Assignment links a variable to a value.
Example:
name = "John" # name -> variable, "John" -> string literal
age = 25
1.4.1 Dynamic Typing
Variable types are assigned at runtime.
Example:
x = 10 # x is int
x = "Python" # x becomes str
1.4.2 Multiple Assignments
Assigning multiple variables at once.
Example:
a, b, c = 1, 2, 3 # multiple assignment
1.5 Simple Input and Output
input() to take user input, print() to display output.
Example:
name = input("Enter your name: ") # input -> function
print("Hello", name) # print -> output
1.6 Data Types
Types of data: int, float, str, bool, list, etc.
Example:
x = 5 # int
y = 3.14 # float
z = "Hello" # str
1.7 Mutable and Immutable Types
Mutable types can change (list), immutable cannot (str, int).
Example:
list1 = [1, 2, 3] # list1 is mutable
list1.append(4)
s = "hello" # s is immutable
1.8 Expressions
Expressions combine variables and operators to produce results.
Example:
a=5
b = 10
c = a + b # expression: a + b
print(c)
1.8.1 Evaluating Arithmetic Operations
Arithmetic operators perform math operations.
Example:
result = (5 + 3) * 2 # +, * -> arithmetic operators
print(result)
1.8.2 Evaluating Relational Expressions
Relational operators compare two values.
Example:
print(5 > 3) # > -> relational operator
1.8.3 Evaluating Logical Expressions
Logical operators combine conditions.
Example:
a=5
b = 10
print(a > 3 and b < 20) # and -> logical operator
1.8.4 Type Casting (Explicit Type Conversion)
Manually converting one type to another.
Example:
x = "123"
y = int(x) # type casting from str to int
1.8.5 Math Library Functions
Use built-in math functions for calculations.
Example:
import math
print(math.sqrt(16)) # sqrt -> math function
1.9 Statement Flow Control
Controls execution flow (conditional, looping).
Example:
if True:
print("Executed")
1.10 The if Conditionals
Execute blocks based on conditions.
Example:
x=5
if x > 0:
print("Positive")
1.10.1 Plain if Conditional Statement
Single condition checking.
Example:
if 10 > 5:
print("10 is greater")
1.10.2 The if-else Conditional Statement
Provides alternate block if condition fails.
Example:
x = -1
if x >= 0:
print("Positive")
else:
print("Negative")
1.10.3 The if-elif Conditional Statement
Checks multiple conditions.
Example:
x=0
if x > 0:
print("Positive")
elif x == 0:
print("Zero")
else:
print("Negative")
1.10.4 Nested if Statements
if inside another if.
Example:
x = 10
if x > 0:
if x < 20:
print("Between 0 and 20")
1.10.5 Storing Conditions
Store condition results in variables.
Example:
a = 10
b=5
result = a > b # storing condition
print(result)
1.11 Looping Statements
Repeat execution based on conditions.
Example:
for i in range(3):
print(i)
1.11.1 The for Loop
Iterates over a sequence.
Example:
for num in [1, 2, 3]:
print(num)
1.11.2 The while Loop
Repeats while a condition is true.
Example:
i=1
while i <= 3:
print(i)
i += 1
1.12 Jump Statements break and continue
Control loop execution using break and continue.
Example:
for i in range(5):
if i == 3:
break # break -> exit loop
print(i)
1.13 More on Loops
Enhancing loop control.
1.13.1 Loop else Statement
else executes after the loop finishes normally.
Example:
for i in range(3):
print(i)
else:
print("Done!") # executed after loop
1.13.2 Nested Loops
Loop inside another loop.
Example:
for i in range(2):
for j in range(2):
print(i, j)