0% found this document useful (0 votes)
6 views9 pages

Python Control Statements & Arrays

The document covers key concepts in Python related to algorithm design and problem-solving strategies, focusing on control flow statements like break, continue, and pass. It also explains the use of else statements with while and for loops, and provides an overview of arrays, including their creation, dynamic sizing, and built-in methods for processing elements. Additionally, it details how to import the array module and gives examples of using arrays in Python programming.

Uploaded by

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

Python Control Statements & Arrays

The document covers key concepts in Python related to algorithm design and problem-solving strategies, focusing on control flow statements like break, continue, and pass. It also explains the use of else statements with while and for loops, and provides an overview of arrays, including their creation, dynamic sizing, and built-in methods for processing elements. Additionally, it details how to import the array module and gives examples of using arrays in Python programming.

Uploaded by

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

Computational Thinking Using

Python
CSE1500
[L-T-P-C: 2-0-2-3]

MODULE – 2
Algorithm Design & Problem-Solving
Strategies

1
break, continue, pass statements

break: Python break statement is used to terminate the current loop and resumes execution at the next statement.
The break statement can be used in both Python while and for loops. Its syntax is
break
continue : The continue keyword is used to end the current iteration in a for loop (or
a while loop), and continues to the next iteration. Its syntax is
continue
pass :
 The pass statement in Python is a null operation.
 When Python executes it, nothing happens.
 It is mainly used as a placeholder in code, where you haven’t written logic yet, but the syntax requires a
statement.
Its syntax is
pass

2
while else
This else statement is executed only when the while loops are executed completely and the condition becomes false. If
we break the while loop using the "break" statement then the else statement will not be executed.

While (condition):
# Code to execute while the condition is true n=1
else: sum = 0
# Code to execute after the loop ends naturally # While loop to calculate the sum
while n <= 5:
sum += n
n += 1
else:
print("Loop completed. Sum =", sum)
print("end of the program")
O/p:
Loop completed. Sum = 15
end of the program

3
for else

For else :
How for...else works
 Loop completes normally: The else block executes after the for loop has gone through all the items in the iterable.
 Loop terminated by break: The else block is completely skipped if a break statement is executed inside the loop.
for i in range(5):
Student Activity: use break statement in while, for print(i)
and analyse the output. else:
print("Finally finished!")
O/p:
0
1
2
3
4
Finally finished!

4
Arrays :

• An array in Python is a collection of elements of the same type, stored in a contiguous memory locations, that
allows efficient storage and access using an index.
• In Python, an array is a data structure provided by the array module that can store multiple items of the same data
type (e.g., only integers, only floats, etc.) in a single variable.
• Arrays can increase or decrease their size dynamically. It means, we need not declare the size of the array. When
the elements are added, it will increase its size and when the elements are removed, it will automatically decrease
its size in memory.
• To create an array in Python, we need to use the built-in array module or NumPy module, as shown in the below
code.
• Methods that are useful to process the elements of any array are available in ‘array’ module.

import array as arr


or
import numpy as np

5
Arrays :
Creating an Array:
arrayname = array(type code, [elements])

Example: marks = array(‘i’, [8, 6,7, 9,10])

Here ‘i’ is a type code that represents integer type array where we can store integer numbers
The important type codes are given in Table

6
Arrays :
Importing the Array Module
There are three ways to import the array module into our program

1) import array
a=[Link]('i',[4,6,2,9])
Here, the first ‘array’ represents the module name and the next ‘array’ represents the class name for which the object
is created. We should understand that we are creating our array as an object of array class.

2) import array as ar
Here, the array is imported with an alternate / alias name ‘ar’. Hence we can refer to the array class of ‘ar’ module as:
a=[Link]('i', [4,6,2,9])

3) from array import *


This is the third way of importing the array module, ‘*’ means import all (classes, objects, variables etc) from the array
module into our program
Example : a=array('i',[4,6,2,9])

7
Arrays :
Example : A Python program to create an integer type array.
# store student marks in array
import array
marks = [Link]('i', [11, 12, 14, 15,10])
# Accessing elements of array.
print("array contains")
for i in marks:
print(i)
11 12 14 15 10
array contains
11 marks[0] marks[1] marks[2] marks[3] marks[4]
12
14 Arrangement of Elements in an Array
15
10

8
Array Methods :

Python has some built in methods / functions to process the array (available is array class of array module)
append(x) → Adds an element x at the end of the array.
insert(i, x) → Inserts element x at position i.
remove(x) → Removes the first occurrence of element x.
pop([i]) → Removes and returns the element at index i (default last).
index(x) → Returns the index of the first occurrence of x.
count(x) → Returns the number of times x appears in the array.
reverse() → Reverses the order of array elements in place.
extend(iterable) → Appends elements from another iterable/array to the end.
buffer_info() → Returns a tuple (address, length) giving memory details.
typecode → Attribute that gives the type code of array elements.
tolist() → Converts the array into a Python list.

You might also like