Python Revision Tour Notes
Python Revision Tour Notes
1.1 Introduction
simple syntax.
Example:
# Basic program
print("Hello World!") # print -> built-in function, "Hello World!" -> string literal
Tokens are the smallest building blocks in Python programs: keywords, identifiers, literals,
operators, punctuators.
Example:
1.2.1 Keywords
Example:
print("Condition is True")
Example:
student_name = "Alice" # student_name -> identifier
print(student_name)
1.2.3 Literals/Values
Example:
1.2.4 Operators
Example:
a=5
b=3
print(sum)
1.2.5 Punctuators
Example:
print("A is greater")
Example:
greet()
Example:
age = 25
Example:
x = 10 # x is int
Example:
a, b, c = 1, 2, 3 # multiple assignment
Example:
Example:
x = 5 # int
y = 3.14 # float
z = "Hello" # str
Example:
list1.append(4)
s = "hello" # s is immutable
1.8 Expressions
Example:
a=5
b = 10
c = a + b # expression: a + b
print(c)
Example:
print(result)
1.8.2 Evaluating Relational Expressions
Example:
Example:
a=5
b = 10
Example:
x = "123"
Example:
import math
Example:
if True:
print("Executed")
Example:
x=5
if x > 0:
print("Positive")
Example:
if 10 > 5:
print("10 is greater")
Example:
x = -1
if x >= 0:
print("Positive")
else:
print("Negative")
x=0
if x > 0:
print("Positive")
elif x == 0:
print("Zero")
else:
print("Negative")
Example:
x = 10
if x > 0:
if x < 20:
Example:
a = 10
b=5
print(result)
for i in range(3):
print(i)
Example:
print(num)
Example:
i=1
while i <= 3:
print(i)
i += 1
Example:
for i in range(5):
if i == 3:
print(i)
Example:
for i in range(3):
print(i)
else:
Example:
for i in range(2):
for j in range(2):
print(i, j)