📋 Contents
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()andimportfunctions effectively. - Identify and apply all seven categories of Python operators.
- Understand operator precedence and associativity.
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).
| Feature | Description |
|---|---|
| Simple & Easy-to-Learn | Few keywords, simple structure, clearly defined syntax — ideal for beginners. |
| Interpreted & Interactive | Processed at runtime; no pre-compilation needed. Interactive mode allows line-by-line testing. |
| Object-Oriented | Supports data hiding, operator overloading, inheritance. Also supports functional and structured programming. |
| Portable | Same interface on all platforms: Windows, Unix, Linux and Macintosh. |
| Scalable | Suitable for both small scripts and large applications; can compile to platform-independent bytecode. |
| Extendable | Low-level modules can be added; easily integrates with C, C++, COM, ActiveX, CORBA and Java. |
| Dynamic | High-level dynamic data types, dynamic type checking and automatic garbage collection. |
| GUI & Databases | Supports GUI applications (Windows MFC, X Window) and interfaces to all major commercial databases. |
| Broad Standard Library | Portable, cross-platform library supporting text processing, browsers and more. |
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:
myVarandmyvarare different identifiers. - Special characters (@, #, $ etc.) are not allowed.
- Reserved keywords cannot be used as identifiers.
Reserved Keywords
The following words are reserved and cannot be used as identifiers:
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
= is always the variable name; the right side is the value to store.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")
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."""
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)
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
from math import pi to import specific names directly into the namespace.An operator manipulates operands. In a = b + c, a, b, c are operands and =, + are operators. Python supports seven categories:
10.1 Arithmetic Operators
| Op | Operation | Description | Example (a=10, b=5) |
|---|---|---|---|
+ | Addition | Adds values on either side | a+b = 15 |
- | Subtraction | Subtracts right from left | a-b = 5 |
* | Multiplication | Multiplies values on either side | a*b = 50 |
/ | Division | Divides left by right | a/b = 2.0 |
% | Modulus | Returns remainder | b%2 = 1 |
** | Exponent | Raises left to power of right | b**2 = 25 |
// | Floor Division | Quotient with decimal removed | b//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
| Op | Description |
|---|---|
== | 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
| Op | Equivalent To | Description |
|---|---|---|
+= | c = c+a | Add and assign |
-= | c = c-a | Subtract and assign |
*= | c = c*a | Multiply and assign |
/= | c = c/a | Divide and assign |
%= | c = c%a | Modulus and assign |
**= | c = c**a | Exponent and assign |
//= | c = c//a | Floor divide and assign |
10.4 Logical Operators
| Operator | Operation | Description |
|---|---|---|
and | Logical AND | True if both operands are True |
or | Logical OR | True if any operand is True |
not | Logical NOT | Reverses 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
| Op | Name | Description |
|---|---|---|
& | AND | Bits set in both operands |
| | OR | Bits set in either operand |
^ | XOR | Bits set in one but not both |
~ | NOT | Inverts all bits |
<< | Left Shift | Shift left by pushing zeros from right |
>> | Right Shift | Shift right; leftmost copies fill from left |
10.6 Membership Operators
| Operator | Description |
|---|---|
in | True if value is found in the sequence |
not in | True 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
| Operator | Description |
|---|---|
is | True if both variables refer to the same object in memory |
is not | True if variables do NOT refer to the same object |
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 not | Identity operators |
in, not in | Membership operators |
not, or, and | Logical operators (lowest precedence) |
= (assignment) and ** (exponent) are right-associative; all other operators are left-associative.🔍 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.
- List any five features of Python with brief explanations.
- Is Python a case-sensitive language? Give an example.
- How can you run Python programs? Name at least two modes.
- State the rules for naming an identifier in Python.
- How are single-line and multi-line comments written in Python?
- What is the difference between
=(assignment) and==(comparison)? - Differentiate between
isand==with an example. - What is the purpose of the
importstatement? Give an example. - Evaluate:
5 + 3 * 2 ** 2 // 4 - 1— show step-by-step working. - Write a Python program to demonstrate all assignment operators.
=.#; multi-line: triple quotes. Interpreter ignores comments.print() for output; input() returns a string; import loads modules.** is highest; logical operators lowest; = and ** are right-associative.Gafoor I
Assistant Professor | Department of Mathematics | NAM College Kallikkandy
Teaching Note · Chapter 1 · Introduction to Python · Academic Year 2025–26
Comments describe what the code does and are for programmer readability. Python ignores comments during execution.
#symbol.#on each line, or enclose in triple quotes ('''or""").#inside a string literal does not start a comment.