IG

IG Math Space

Mathematics Teaching and Research

📚 Classroom Space · Teaching Note

Introduction to Python

Chapter 1 — A comprehensive classroom teaching note covering features, variables, operators and I/O functions.

👨‍🏫 Gafoor I 🏛️ NAM College Kallikkandy 📐 Department of Mathematics 📘 Taming Python by Programming

📋 Contents

  1. Learning Objectives
  2. History & Background
  3. Features of Python
  4. Identifiers & Keywords
  5. Variables
  6. Comments
  7. Indentation
  8. Multi-line Statements & Quotes
  9. Input, Output & Import
  10. Operators
  11. Classroom Activities
  12. Review Questions
  13. Summary
01 Learning Objectives

By the end of this class session, students will be able to:

  • Describe the history and key features of the Python programming language.
  • Understand identifiers, reserved keywords, variables and comments.
  • Apply indentation rules and use multi-line / multiple-statement constructs.
  • Use input(), print() and import functions effectively.
  • Identify and apply all seven categories of Python operators.
  • Understand operator precedence and associativity.
02 History & Background

Python was developed by Guido van Rossum at the National Research Institute for Mathematics and Computer Science, Netherlands during 1985–1990. It is derived from languages such as ABC, Modula-3, C, C++, Algol-68, SmallTalk and Unix shell scripts.

The name Python was inspired by the BBC comedy series Monty Python's Flying Circus. Rossum wanted a short, unique and mysterious name.

Python is a general-purpose, interpreted, interactive, object-oriented and high-level programming language. Its source code is available under the GNU General Public License (GPL).

03 Features of Python
FeatureDescription
Simple & Easy-to-LearnFew keywords, simple structure, clearly defined syntax — ideal for beginners.
Interpreted & InteractiveProcessed at runtime; no pre-compilation needed. Interactive mode allows line-by-line testing.
Object-OrientedSupports data hiding, operator overloading, inheritance. Also supports functional and structured programming.
PortableSame interface on all platforms: Windows, Unix, Linux and Macintosh.
ScalableSuitable for both small scripts and large applications; can compile to platform-independent bytecode.
ExtendableLow-level modules can be added; easily integrates with C, C++, COM, ActiveX, CORBA and Java.
DynamicHigh-level dynamic data types, dynamic type checking and automatic garbage collection.
GUI & DatabasesSupports GUI applications (Windows MFC, X Window) and interfaces to all major commercial databases.
Broad Standard LibraryPortable, cross-platform library supporting text processing, browsers and more.
04 Identifiers & Reserved Keywords

Identifiers

An identifier is a name used to identify a variable, function, class or module. Rules:

  • Must begin with a letter (A–Z or a–z) or an underscore (_).
  • Can be followed by letters, digits (0–9) or underscores.
  • Case-sensitive: myVar and myvar are different identifiers.
  • Special characters (@, #, $ etc.) are not allowed.
  • Reserved keywords cannot be used as identifiers.
💡 Note: Python is a case-sensitive language.

Reserved Keywords

The following words are reserved and cannot be used as identifiers:

FalseNoneTrue andasassert asyncawaitbreak classcontinuedef delelifelse exceptfinallyfor fromglobalif importinis lambdanonlocalnot orpassraise returntrywhile withyield
05 Variables

Variables are reserved memory locations to store values. The interpreter allocates memory based on the data type and decides what can be stored.

Python variables do not need explicit declaration — memory is reserved automatically when a value is assigned using the = operator.

Assignment Examples

a    = 100        # Integer
b    = 1000.0     # Float
name = "John"     # String

print(a)          # 100
print(b)          # 1000.0
print(name)       # John

Multiple Assignment

a = b = c = 1              # Same value to multiple variables
a, b, c = 1, 2, "Tom"     # Different values to multiple variables
Tip: The left side of = is always the variable name; the right side is the value to store.
06 Comments in Python

Comments describe what the code does and are for programmer readability. Python ignores comments during execution.

  • Single-line comment — use the # symbol.
  • Multi-line comment — use # on each line, or enclose in triple quotes (''' or """).
# This is a single-line comment

# This is a multi-line comment
# that spans several lines

"""
This is another
multi-line comment
using triple quotes
"""
💡 Note: A # inside a string literal does not start a comment.
07 Indentation in Python

Unlike C, C++ or Java which use braces {}, Python uses indentation to define blocks of code. A block starts with indentation and ends with the first un-indented line.

Four whitespaces are the recommended convention (preferred over a tab character).

if True:
    print("Correct")   # indented — inside the if block
else:
    print("Wrong")
💡 Note: Inconsistent indentation raises an IndentationError at runtime.
08 Multi-line Statements & Quotes

Multi-Line Statements

Use the line continuation character \ or enclose in brackets to span a statement across lines:

total = 1 + 2 + \
        3 + 4

# Or using parentheses
total = (1 + 2 +
         3 + 4)

Multiple Statements on One Line

A group of individual statements making a code block is called a suite. Use a semicolon to write multiple statements on one line:

x = 10; y = 20; print(x + y)

Quotes in Python

Python accepts single ('), double (") and triple (''' / """) quotes to denote string literals. Triple quotes can span multiple lines.

s1 = 'Hello'
s2 = "World"
s3 = """This string
spans multiple
lines."""
09 Input, Output & Import Functions

print() — Displaying Output

The print() function converts expressions to strings and writes to standard output.

print(*objects, sep=' ', end='\n', file=sys.stdout, flush=False)

sep — separator between values (default: space)  |  end — character after last value (default: newline)

print(1, 2, 3, 4)                   # 1 2 3 4
print(1, 2, 3, 4, sep='+')          # 1+2+3+4
print(1, 2, 3, 4, sep='+', end='%') # 1+2+3+4%

input() — Reading Input

Reads a line from the keyboard and always returns a string.

name = input("Enter your name: ")
print("Your name is:", name)

n = int(input("Enter a number: "))   # convert to int
print("The number is:", n)
💡 Note: raw_input() is Python 2 only and is not supported in Python 3. Always use input().

import — Using Modules

When code grows large, it is organised into modules (.py files). Use the import keyword:

import math
print(math.pi)       # 3.141592653589793
print(math.sqrt(16)) # 4.0
Tip: Use from math import pi to import specific names directly into the namespace.
10 Operators in Python

An operator manipulates operands. In a = b + c, a, b, c are operands and =, + are operators. Python supports seven categories:

10.1 Arithmetic Operators

OpOperationDescriptionExample (a=10, b=5)
+AdditionAdds values on either sidea+b = 15
-SubtractionSubtracts right from lefta-b = 5
*MultiplicationMultiplies values on either sidea*b = 50
/DivisionDivides left by righta/b = 2.0
%ModulusReturns remainderb%2 = 1
**ExponentRaises left to power of rightb**2 = 25
//Floor DivisionQuotient with decimal removedb//2 = 2
a, b, c = 10, 5, 2
print("Sum=",        a+b)   # 15
print("Difference=", a-b)   # 5
print("Product=",    a*b)   # 50
print("Quotient=",   a/b)   # 2.0
print("Remainder=",  b%c)   # 1
print("Exponent=",   b**c)  # 25
print("Floor Div=",  b//c)  # 2

10.2 Comparison (Relational) Operators

OpDescription
==True if both operands are equal
!=True if operands are not equal
>True if left is greater than right
<True if left is less than right
>=True if left is greater than or equal to right
<=True if left is less than or equal to right

10.3 Assignment Operators

OpEquivalent ToDescription
+=c = c+aAdd and assign
-=c = c-aSubtract and assign
*=c = c*aMultiply and assign
/=c = c/aDivide and assign
%=c = c%aModulus and assign
**=c = c**aExponent and assign
//=c = c//aFloor divide and assign

10.4 Logical Operators

OperatorOperationDescription
andLogical ANDTrue if both operands are True
orLogical ORTrue if any operand is True
notLogical NOTReverses the logical state of its operand
a, b, c, d = 10, 5, 2, 1
print((a>b) and (c>d))   # True
print((a>b) or  (d>c))   # True
print(not(a>b))           # False

10.5 Bitwise Operators

OpNameDescription
&ANDBits set in both operands
|ORBits set in either operand
^XORBits set in one but not both
~NOTInverts all bits
<<Left ShiftShift left by pushing zeros from right
>>Right ShiftShift right; leftmost copies fill from left

10.6 Membership Operators

OperatorDescription
inTrue if value is found in the sequence
not inTrue if value is NOT found in the sequence
s = 'abcde'
print('a' in s)       # True
print('f' in s)       # False
print('f' not in s)   # True

10.7 Identity Operators

OperatorDescription
isTrue if both variables refer to the same object in memory
is notTrue if variables do NOT refer to the same object
💡 Note: is checks identity (memory address), while == checks equality (value).

10.8 Operator Precedence (Highest → Lowest)

Operator(s)Description
**Exponentiation (right-associative)
~, +, -Complement, unary plus and minus
*, /, %, //Multiply, divide, modulo, floor division
+, -Addition and subtraction
>>, <<Right and left bitwise shift
&Bitwise AND
^, |Bitwise XOR and OR
<=, <, >, >=Comparison operators
<>, ==, !=Equality operators
=, %=, /=, //=, -=, +=, *=, **=Assignment operators
is, is notIdentity operators
in, not inMembership operators
not, or, andLogical operators (lowest precedence)
Tip: In Python, = (assignment) and ** (exponent) are right-associative; all other operators are left-associative.
11 Classroom Activities

🔍 Activity 1 — Predict the Output

What will the following code print? Work it out on paper first, then verify.

a, b = 7, 3
print(a ** b)
print(a // b)
print(a % b)
print(a > b and b > 0)

🐛 Activity 2 — Debug the Code

Find and fix the error in the code below:

x = input("Enter a number:")
y = x + 10
print(y)

✏️ Activity 3 — Write the Program

Write a Python program that:

  • Accepts two numbers from the user.
  • Prints their sum, difference, product, quotient and remainder.
  • Checks whether the first number is greater than the second.
12 Review Questions
  1. List any five features of Python with brief explanations.
  2. Is Python a case-sensitive language? Give an example.
  3. How can you run Python programs? Name at least two modes.
  4. State the rules for naming an identifier in Python.
  5. How are single-line and multi-line comments written in Python?
  6. What is the difference between = (assignment) and == (comparison)?
  7. Differentiate between is and == with an example.
  8. What is the purpose of the import statement? Give an example.
  9. Evaluate: 5 + 3 * 2 ** 2 // 4 - 1 — show step-by-step working.
  10. Write a Python program to demonstrate all assignment operators.
13 Summary
🐍
HistoryCreated by Guido van Rossum (1985–1990); GPL licensed; general-purpose and high-level.
FeaturesSimple, interpreted, OOP, portable, scalable, extendable, dynamic, GUI-capable, broad library.
🏷️
IdentifiersUser-defined names; case-sensitive; cannot be reserved keywords; no special characters.
📦
VariablesNo explicit declaration needed; type decided at assignment time using =.
💬
CommentsSingle-line: #; multi-line: triple quotes. Interpreter ignores comments.
IndentationDefines code blocks; 4 spaces recommended; inconsistency causes IndentationError.
⌨️
I/Oprint() for output; input() returns a string; import loads modules.
Operators7 types: Arithmetic, Comparison, Assignment, Logical, Bitwise, Membership, Identity.
📊
Precedence** is highest; logical operators lowest; = and ** are right-associative.
G

Gafoor I

Assistant Professor  |  Department of Mathematics  |  NAM College Kallikkandy

Teaching Note · Chapter 1 · Introduction to Python · Academic Year 2025–26