CHAPTER 6 · CHEAT SHEET
Python Fundamentals
Character Set · Tokens · Literals · Operators · Statements · I/O
CORE CONCEPTS
6.1 INTRODUCTION – IPO CYCLE
IPO = Input Process Output
Program = A set of instructions that governs processing (makes IPO happen)
Almost all computer actions follow the IPO cycle
Python basics: character set, tokens, expressions, statements, I/O
6.2 PYTHON CHARACTER SET
Character set = valid characters Python can recognise
TYP E E X A M P LE S
Letters A–Z, a–z
Digits 0–9
Special symbols # @ = + ( ) { } [ ] ; : , .
Whitespace space, tab, newline, carriage return
Other All ASCII + Unicode characters
6.3 TOKENS (LEXICAL UNITS)
Token = smallest individual unit in a program (also called lexical unit)
Keywords Identifiers Literals Operators Punctuators
TOKENS IN DETAIL
KEYWORDS
Keywords = reserved words with special meaning; cannot be used as identifiers
if else elif while for def return True False None and or
not in is import from class break continue pass try except
global lambda del assert
IDENTIFIERS (NAMES)
Identifier = user-defined name for variable, function, class, object etc.
Must start with a letter or underscore (_)
Can contain letters, digits, underscore
Case-sensitive Age ≠ age
Cannot be a keyword
No special characters allowed
✓ VALID
myFile chk _name file13
✗ INVALID
29CLT DATA-REC My file
LITERALS / VALUES
Literal = fixed/constant value directly used in code
TYP E EXAMPLE
Integer 10 , -8 , 0o14 (oct), 0xA (hex)
Float 3.14 , 2.5E4 , 0.58E01
Complex 3+4j , -1j
String 'hello' , "world" , """multi"""
Boolean True , False
None None (absence of value)
STRINGS & ESCAPE SEQUENCES
STRING LITERALS
String = sequence of characters in quotes
Single-line: 'hello' or "hello"
Multiline: use """...""" or '''...'''
Backslash (\) at line end continues single-line string
str1 = "Hello\nWorld"
str2 = """Hello
World"""
len(str1) # 11
ESCAPE SEQUENCES
Escape sequence = special characters starting with \
SEQ UENCE M EA NI NG
\n New line
\t Horizontal tab
\r Carriage return
\\ Backslash
\' Single quote
\" Double quote
\a Bell (BEL)
\b Backspace (BS)
\0 Null character
\uxxxx Unicode (16-bit)
NUMERIC LITERALS DETAIL
Integer forms:
Decimal: 10 , -17
Octal: starts with 0o 0o14
Hexadecimal: starts with 0x 0xA = 10
Float forms:
Fractional: 3.14 , .5
Exponent: 2.5E4 = 25000, 0.58E01
INVALID FLOATS
17/2 (comma not allowed) · .E2 (no digit before E)
OPERATORS
ALL OPERATORS AT A GLANCE
CAT EGORY OPERATORS E XA MP LE
Arithmetic + - * / // % ** 2**3 → 8
Assignment = += -= *= /= //= %= **= x += 5
Relational == != > < >= <= x >= 10
Logical and or not x>0 and y>0
Bitwise & | ^ ~ << >> x << 2
Membership in not in 'a' in 'cat'
Identity is is not x is None
Unary + - not -x
6.4 BAREBONES OF A PYTHON PROGRAM
STATEMENTS
Statement = a programming instruction that causes some action
May or may not produce a value
Example: a = 15 print("Hi")
EXPRESSIONS
Expression = combination of symbols that represents a value
Always evaluates to a value
a + 5 # expression
15 # expression
(3 + 5) / 4 # expression
a > 10 # expression (bool)
COMMENTS
Comment = readable info for programmers, ignored by Python
# This is a single-line comment
"""
This is a
multi-line comment
"""
Single-line: start with #
Multi-line: enclose in """...""" or '''...'''
FUNCTIONS
Function = named block of code that can be reused by calling its name
def SeeYou():
print("Time to say Bye!")
SeeYou() # calling the function
Defined with def keyword
Statements inside are indented at same level
BLOCKS & INDENTATION
Block / Suite = group of statements at the same indentation level
Python uses 4 spaces for indentation (no curly braces)
All statements in a block must be equally indented
if b < a:
# this block runs if b < a
print("b is less")
tmp = a
a = b
b = tmp
PUNCTUATORS
Punctuators = symbols that organise structure, rhythm & emphasis
' " # \ ( ) [ ] { } @ , : . = ;
6.4–6.6 STYLE RULES, VARIABLES & I/O
PYTHON STYLE RULES
Use 4 spaces per indentation level
Max 79 characters per line
Two blank lines between top-level definitions
One blank line between methods
Whitespace around operators and after punctuation
Python is case-sensitive
Statements end by pressing Enter (no semicolon needed)
VARIABLES & ASSIGNMENTS
Variable = named storage that holds a value (can change)
name = "Priya" # string
age = 15 # integer
contribution = 718.75 # float
# Multiple assignment
a = b = c = 10
x, y = 5, 10
6.6 INPUT & OUTPUT
print() = displays output on screen
input() = takes input from keyboard (returns string)
# Output
print("Hello World")
print("Value:", x)
# Input
name = input("Enter name: ")
age = int(input("Enter age: "))
NOTE
input() always returns a string. Use int() or float() to convert.
QUICK REFERENCE SUMMARY
CHAPTER 6 – ONE-PAGE SUMMARY
TER M DEFINITION E XA MP LE
IPO Input Process Output cycle Calculator app
Token Smallest unit in a program if , x , 10
Keyword Reserved word, cannot rename if else for while
Identifier User-defined name myAge , _count
Literal Fixed value in code 42 , "Hi" , True
Operator Token triggering computation + - * / %
Expression Evaluates to a value a + 5
Statement Instruction causing action x = 10
Comment Ignored by Python, for humans # note
Function Named reusable code block def foo():
Block/Suite Group of statements at same indent body of if
Indentation 4 spaces mark code blocks inside loops, functions
print() Output to screen print("Hi")
input() Read keyboard input ( str) input("Name: ")
Python Fundamentals · Chapter 6 · Computer Science with Python