0% found this document useful (0 votes)
4 views5 pages

Python Programming Unit1-2 Notes

The document provides comprehensive notes on Python programming, covering fundamentals in Unit I, including syntax, data types, operators, and control flow statements. Unit II focuses on data structures such as lists, tuples, sets, and dictionaries, detailing their characteristics and common methods. It also includes a comparison table and practice questions to reinforce learning.

Uploaded by

sadhanas7639
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views5 pages

Python Programming Unit1-2 Notes

The document provides comprehensive notes on Python programming, covering fundamentals in Unit I, including syntax, data types, operators, and control flow statements. Unit II focuses on data structures such as lists, tuples, sets, and dictionaries, detailing their characteristics and common methods. It also includes a comparison table and practice questions to reinforce learning.

Uploaded by

sadhanas7639
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Python Programming — Unit I & II

Exam-Ready Notes | II [Link] CS (TSP) | Faculty: Mrs. Selvapriya

UNIT I — Fundamentals of Python


1. Introduction to Python
Python is a high-level, interpreted, general-purpose programming language created by Guido van Rossum
(first released 1991). It emphasizes code readability with significant indentation instead of braces. Python
supports multiple programming paradigms: procedural, object-oriented, and functional programming.

Key Features of Python


• Easy to Learn & Read — simple, English-like syntax
• Interpreted Language — executes line by line, no separate compilation step
• Dynamically Typed — no need to declare variable types explicitly
• Free and Open Source
• Portable — runs on Windows, Linux, Mac without modification
• Extensive Standard Library — large collection of built-in modules
• Object-Oriented — supports classes, objects, inheritance
• Extensible — can be integrated with C, C++, Java

2. Python Syntax Basics


Indentation: Python uses indentation (whitespace) to define blocks of code instead of curly braces {}.
if True:
print("Indented block")
print("Same block")

Comments:
# This is a single-line comment

"""
This is a
multi-line comment / docstring
"""

Variables: No explicit declaration needed; type is inferred at assignment.


x = 10 # int
name = "Sadhana" # str
pi = 3.14 # float
is_valid = True # bool

Identifiers (Naming Rules):

• Must start with a letter (a–z, A–Z) or underscore (_)


• Cannot start with a digit
• Can contain letters, digits, underscores only (no special characters/spaces)
• Case-sensitive (Name and name are different)
• Cannot be a Python keyword (if, for, class, etc.)
3. Data Types
Type Description Example

int Whole numbers x = 10

float Decimal numbers y = 3.14

complex Complex numbers (a+bj) z = 2+3j

str Sequence of characters s = "Hello"

bool True / False values flag = True

list Ordered, mutable collection L = [1,2,3]

tuple Ordered, immutable collection T = (1,2,3)

set Unordered, unique elements S = {1,2,3}

dict Key-value pairs D = {"a":1}

NoneType Represents absence of value x = None

Use type() function to check data type: type(x) → <class 'int'>

4. Operators
a) Arithmetic Operators

Operator Meaning Example (a=10, b=3)

+ Addition a+b = 13

- Subtraction a-b = 7

* Multiplication a*b = 30

/ Division (float result) a/b = 3.333

// Floor Division a//b = 3

% Modulus (remainder) a%b = 1

** Exponentiation a**b = 1000

b) Relational / Comparison Operators: == , != , > , < , >= , <= (return True/False)


c) Logical Operators: and, or, not — used to combine conditional statements
d) Assignment Operators: = , += , -= , *= , /= , //= , %= , **=
e) Bitwise Operators: & (AND), | (OR), ^ (XOR), ~ (NOT), << (left shift), >> (right shift)
f) Membership Operators: in, not in — test membership in a sequence
g) Identity Operators: is, is not — compare memory location/identity of objects

5. Control Flow Statements


a) Conditional Statements
x = 10
if x > 0:
print("Positive")
elif x == 0:
print("Zero")
else:
print("Negative")

b) Looping Statements
for loop — iterates over a sequence (list, string, range, etc.)
for i in range(5):
print(i) # 0 1 2 3 4

while loop — repeats as long as condition is True


i = 0
while i < 5:
print(i)
i += 1

c) Jump / Control Statements

• break — terminates the loop immediately


• continue — skips current iteration, moves to next
• pass — a null statement, does nothing (placeholder)
UNIT II — Data Structures in Python
1. List
An ordered, mutable (changeable) collection of items. Allows duplicate values. Defined using square
brackets [ ].
fruits = ["apple", "mango", "banana"]
[Link]("grape") # add item
[Link]("mango") # remove item
fruits[0] = "orange" # modify item
print(fruits[1:3]) # slicing
print(len(fruits)) # length

Common methods: append(), extend(), insert(), remove(), pop(), sort(), reverse(), index(), count(), clear()

2. Tuple
An ordered, immutable (cannot be changed after creation) collection. Allows duplicates. Defined using
round brackets ( ). Faster than lists, used for fixed data.
point = (3, 4, 5)
print(point[0]) # accessing: 3
# point[0] = 10 -> Error, tuples are immutable
x, y, z = point # tuple unpacking

Common methods: count(), index()

3. Set
An unordered collection of unique items (no duplicates). Mutable, but elements must be immutable.
Defined using curly braces { }.
nums = {1, 2, 3, 3, 2}
print(nums) # {1, 2, 3} - duplicates removed
[Link](4)
[Link](2)

# Set operations
a = {1,2,3}; b = {2,3,4}
print(a | b) # union {1,2,3,4}
print(a & b) # intersection {2,3}
print(a - b) # difference {1}

4. Dictionary
An unordered (insertion-ordered from Python 3.7+) collection of key-value pairs. Keys must be unique and
immutable. Defined using curly braces with key:value pairs.
student = {"name": "Sadhana", "dept": "CS", "year": 2}
print(student["name"]) # accessing value
student["year"] = 3 # updating
student["college"] = "BACAS" # adding new key
del student["dept"] # deleting key

for key, value in [Link]():


print(key, ":", value)

Common methods: keys(), values(), items(), get(), update(), pop(), popitem()


5. Comparison Table: List vs Tuple vs Set vs Dict
Feature List Tuple Set Dict

Syntax [] () {} {key:val}

Ordered Yes Yes No Yes (3.7+)

Mutable Yes No Yes Yes

Duplicates Allowed Allowed Not allowed Keys unique

Indexing Yes Yes No By key

Exam Tip: Common 2-mark question — 'Differentiate List and Tuple.' Answer using mutability, syntax, and speed
(tuples are faster since immutable).

6. Quick Practice Questions


1 What is the difference between a list and a tuple? (2 marks)
2 Explain any 4 features of Python. (5 marks)
3 Write a Python program to check if a number is prime using a for loop and break. (5 marks)
4 Differentiate between break, continue, and pass statements. (3 marks)
5 Write a program to count vowels in a string using a dictionary. (5 marks)
6 What are membership and identity operators? Give examples. (3 marks)

You might also like