0% found this document useful (0 votes)
7 views14 pages

Python Programming

The document covers control structures in Python, including sequential, selection, and iteration controls, along with their syntax and examples. It also discusses lists and tuples, detailing their characteristics, operations, and methods for accessing and modifying elements. Additionally, it provides example programs and assignment topics for practical application of the concepts.

Uploaded by

pjbalakumaran
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)
7 views14 pages

Python Programming

The document covers control structures in Python, including sequential, selection, and iteration controls, along with their syntax and examples. It also discusses lists and tuples, detailing their characteristics, operations, and methods for accessing and modifying elements. Additionally, it provides example programs and assignment topics for practical application of the concepts.

Uploaded by

pjbalakumaran
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

2025-2026 II SEM

UNIT II

Selection Control: If Statement- Indentation in Python- Multi-Way Selection - Iterative


Control: While Statement- Infinite loops- Definite vs. Indefinite Loops.

Lists: List Structures - Lists (Sequences) in Python - Python List Type - Tuples -
Sequences - Nested Lists.

2. CONTROL STRUCTURE

2.1 INTRODUCTION
In programming, the control structure determines the order in which statements are
executed.

Python supports three major types of control structures:

1. Sequential Control
2. Selection (Decision-making) Control
3. Iteration (Looping) Control

1. Sequential Control Structure

 This is the default mode of execution in Python.


 Statements are executed line by line in the same order as they appear.

Example:

print("Start")
x = 10 + 5
print("Sum =", x)
print("End")

Output:

Start
Sum = 15
End

1
25BAS2C10 PYTHON FOR ANALYTICS
2025-2026 II SEM

2. Selection / Decision-Making Control

 Decision-making structures allow the program to make choices and execute different
code blocks based on conditions.
 Python uses Boolean expressions to evaluate conditions (True or False).

The if Statement

 Used to execute a block of code only if a condition is true.

Syntax:

if condition:
statement(s)

Example:

age = 20
if age >= 18:
print("Eligible to vote")

Output:

Eligible to vote

The if–else Statement

 Used when you need to execute one block if true and another block if false.

Syntax:

if condition:
statement(s)
else:
statement(s)

Example:

num = int(input("Enter a number: "))


if num % 2 == 0:
print("Even number")

2
25BAS2C10 PYTHON FOR ANALYTICS
2025-2026 II SEM
else:
print("Odd number")

The if–elif–else Ladder

 Used to test multiple conditions.

Syntax:

if condition1:
statement(s)
elif condition2:
statement(s)
else:
statement(s)

Example:

marks = 85
if marks >= 90:
print("Grade A")
elif marks >= 75:
print("Grade B")
elif marks >= 60:
print("Grade C")
else:
print("Grade D")

Nested if Statement

 When one if statement is placed inside another.

Example:

num = int(input("Enter a number: "))


if num >= 0:
if num == 0:
print("Zero")
else:
print("Positive number")
else:
print("Negative number")

3
25BAS2C10 PYTHON FOR ANALYTICS
2025-2026 II SEM
4. Iteration / Looping Control

 Loops are used to execute a block of code repeatedly as long as a condition is true.
 Python supports two main types of loops:

1. while Loop
2. for Loop

and provides two loop control statements:

 break
 continue

1. The while Loop

 Used when the number of iterations is not known in advance.

Syntax:

while condition:
statement(s)

Example:

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

Output:

1
2
3
4
5

2. The for Loop

 Used when we know exactly how many times to repeat.

Syntax:

4
25BAS2C10 PYTHON FOR ANALYTICS
2025-2026 II SEM
for variable in sequence:
statement(s)

Example:

for i in range(1, 6):


print(i)

Output:

1
2
3
4
5

The range() Function

 Used to generate a sequence of numbers.

Common forms:

 range(stop) → starts from 0


 range(start, stop)
 range(start, stop, step)

Example:

for i in range(2, 11, 2):


print(i)

Output:

2
4
6
8
10

Loop Control Statements


a) BREAK STATEMENT

 Used to terminate the loop prematurely.

5
25BAS2C10 PYTHON FOR ANALYTICS
2025-2026 II SEM
Example:

for i in range(1, 10):


if i == 6:
break
print(i)

Output:

1
2
3
4
5

CONTINUE STATEMENT

Used to skip the rest of the statements in the current iteration and move to the next.

Example:

for i in range(1, 6):


if i == 3:
continue
print(i)

Output:

1
2
4
5

PASS STATEMENT

Used as a placeholder when no action is required.

Example:

for i in range(5):
if i == 2:
pass # Placeholder
print(i)

6
25BAS2C10 PYTHON FOR ANALYTICS
2025-2026 II SEM
Nested Loops

Loops inside another loop.

Example:

for i in range(1, 4):


for j in range(1, 3):
print(i, j)

Output:

11
12
21
22
31
32

Example Programs

1. Sum of first N numbers

n = int(input("Enter N: "))
sum = 0
for i in range(1, n+1):
sum += i
print("Sum =", sum)

2. Factorial of a number

n = int(input("Enter number: "))


fact = 1
for i in range(1, n+1):
fact *= i
print("Factorial =", fact)

3. Display numbers using while loop

i=1
while i <= 10:
print(i)
i += 1

3. List

7
25BAS2C10 PYTHON FOR ANALYTICS
2025-2026 II SEM
3.1 List Structure

 A list is a mutable sequence used to store an ordered collection of items.


 Lists can hold elements of different data types — integers, strings, floats, or even other
lists.

Syntax:
list_name = [element1, element2, element3, ...]

Example:
fruits = ["apple", "banana", "cherry"]
numbers = [10, 20, 30, 40]
mixed = [10, "python", 3.14, True]

3.2 Characteristics of Lists

Property Description

Ordered Elements have a definite index order

Mutable Can be modified after creation

Heterogeneous Can contain different data types

Indexing & Slicing Supports both positive and negative indices

Dynamic Size can be changed at runtime

Example:

data = [1, 2, 3, 4]
data[2] = 10
print(data)
# Output: [1, 2, 10, 4]

3.3 Accessing List Elements

1. Indexing

List elements can be accessed using indexes.

8
25BAS2C10 PYTHON FOR ANALYTICS
2025-2026 II SEM
fruits = ["apple", "banana", "cherry"]
print(fruits[0]) # apple
print(fruits[-1]) # cherry

2. Slicing

Slicing returns a subsection of a [Link] = [10, 20, 30, 40, 50]

print(numbers[1:4]) # [20, 30, 40]


print(numbers[:3]) # [10, 20, 30]
print(numbers[-3:]) # [30, 40, 50]

3.5 Updating and Modifying Lists

Since lists are mutable, their elements can be added, removed, or changed.

Updating an Element
nums = [5, 10, 15]
nums[1] = 20
print(nums) # [5, 20, 15]

Adding Elements
Method Example Description

append() [Link]("mango") Adds an item at the end

insert() [Link](1, "orange") Adds an item at a specific index

extend() [Link](["kiwi", "melon"]) Adds multiple elements

Removing Elements
Method Example Description

remove() [Link]("apple") Removes first matching element

pop() [Link](1) Removes element at index

clear() [Link]() Removes all elements

9
25BAS2C10 PYTHON FOR ANALYTICS
2025-2026 II SEM
Common List Operations

Operation Example Result

Concatenation [1,2] + [3,4] [1,2,3,4]

Repetition [1,2]*2 [1,2,1,2]

Membership 3 in [1,2,3] True

Length len([10,20,30]) 3

Maximum max([10,20,30]) 30

Minimum min([10,20,30]) 10

Traversing a List

Using a For Loop


fruits = ["apple", "banana", "cherry"]
for fruit in fruits:
print(fruit)
Using Range and Index
for i in range(len(fruits)):
print(i, fruits[i])

List Comprehension

A compact way to create lists using a single line of code.

Syntax:

[expression for item in iterable if condition]

Example:

squares = [x*x for x in range(1,6)]


print(squares) # [1, 4, 9, 16, 25]

Nested Lists

10
25BAS2C10 PYTHON FOR ANALYTICS
2025-2026 II SEM
A nested list is a list that contains other lists as its elements.

Example:

matrix = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
]

print(matrix[0]) # [1, 2, 3]
print(matrix[1][2]) # 6

Iterating a Nested List:

for row in matrix:


for val in row:
print(val, end=" ")

Tuples in Python

Definition:

A tuple is an immutable sequence of ordered elements.


Once created, its elements cannot be modified.

Syntax:
tuple_name = (element1, element2, element3)
Example:
colors = ("red", "green", "blue")
print(colors[1]) # green

Characteristics of Tuples

Property Description

Ordered Maintains the order of elements

Immutable Cannot be changed once created

Heterogeneous Can contain different data types

11
25BAS2C10 PYTHON FOR ANALYTICS
2025-2026 II SEM
Property Description

Indexing & Slicing Supported like lists

Example:

t = (10, 20, 30, 40)


print(t[0]) # 10
print(t[1:3]) # (20, 30)

Tuple Operations

Operation Example Result

Concatenation (1,2)+(3,4) (1,2,3,4)

Repetition (1,2)*2 (1,2,1,2)

Membership 3 in (1,2,3) True

Length len((10,20,30)) 3

Difference Between List and Tuple

Feature List Tuple

Syntax [] ()

Mutability Mutable Immutable

Performance Slower Faster

Use Case When data may change When data is fixed

Methods Available Many (append, remove, etc.) Limited (count, index)

Example Programs

12
25BAS2C10 PYTHON FOR ANALYTICS
2025-2026 II SEM
1. Sum of List Elements

nums = [10, 20, 30, 40]


print("Sum =", sum(nums))

2. Largest Element in a List

nums = [3, 8, 1, 9, 5]
print("Max =", max(nums))

3. Convert Tuple to List

t = (1, 2, 3)
l = list(t)
print(l)

4. Nested List Traversal

matrix = [[1,2],[3,4],[5,6]]
for row in matrix:
print(row)

Assignment Topics

1. Write programs using if, if-else, and elif conditions.


2. Create menu-driven programs using multi-way selection.
3. Program using while loop with counters.
4. Create an example for an infinite loop & correct it.
5. Compare definite and indefinite loops using examples.
6. Write a program using lists & list methods.

13
25BAS2C10 PYTHON FOR ANALYTICS
2025-2026 II SEM
7. Create nested lists for marks of students and process them.
8. Write programs using tuple operations.
9. Write sequence-based programs (searching, slicing operations).

YouTube Video Links

 If–Else in Python: [Link]


 While Loops: [Link]
 Lists & Sequences: [Link]

14
25BAS2C10 PYTHON FOR ANALYTICS

You might also like