Loops in Python: CH - Srilakshmi
Loops in Python: CH - Srilakshmi
Ch.Srilakshmi
WHAT ARE LOOPS?
Loop
Q. Write a program for sum of integer upto ‘n’ by using while loop.
condition
Conditional code
AFTER PROGRAM EXECUTION
FLOWCHART of ‘for LOOP’
/condition is
false
E X A M P L E O F “ FO R LO O P ”
Q.Write a program for sum of integer upto ‘n’ by using for loop.
Increment factor
(in-built RANGE)
Final value
Initial value
variable
AFTER PROGRAM EXECUTION
fruits = ["apple", "banana", "cherry"]
for x in fruits:
print(x)
Output:
apple
banana
cherry
Looping Through a String
for x in "banana":
print(x)
Output:
b
a
n
a
n
a
The break Statement
fruits = ["apple", "banana", "cherry"]
for x in fruits:
print(x)
if x == "banana":
break
Output:
apple
banana
The range() Function
for x in range(6):
print(x)
else:
print("Finally finished!")
Output:
0
1
2
3
4
5
Finally finished!
Nested Loops
Array is basically a data structure which can hold more than one value at a time.
Typecode Value
b Represents signed integer of size 1
byte/td>
import array
-ive index value can be used as well, -ive indexing starts from reverse order
0 1 2 3 4 5
Example
Output
First element is: 2
Second element: 4
last element: 8
Basic Array Operations
Array concatenation
Slicing
sorting
Finding the length of an array
The len() function returns an integer value that is equal to the number of elements
present in that array
Finding the length of an array
Syntax
len(array_name)
Example
from array import *
a = array('i', [2, 4, 6, 8])
print(len(a)) output
4
Adding elements to an array
extend() is used to add a more than one element at the end of an array
numbers.append(4)
print(numbers)
numbers.extend([5, 6, 7])
print(numbers)
numbers.insert(3,8)
print(numbers)
Output
numbers.remove(12)
print(numbers) # Output: array('i', [10, 11, 12, 13])
numbers.pop(2) # Output: 12
print(numbers)
Arrays Concatenation
For loop used to iterates over the items of an array specified number of
times
While loop iterates the elements until a certain condition is met
Example
from array import *
numbers_list = [2, 5, 62, 5, 42, 52, 48, ]
a = array('i', numbers_list)
for x in a:
print(x)
Sorting of an array
Format: sorted(array_name)
Example
6
Sorting of array in descending order
Example:
from array import * output:
a = array('i',[4,3,6,2,1]) 6
b=sorted(a, reverse=true) 4
for x in b: 3
print(x) 2
1
Array Methods
Python has a set of built-in methods that you can use on lists/arrays.
Method Description
extend() Add the elements of a list (or any iterable), to the end of the
current list
index() Returns the index of the first element with the specified value
import numpy as np
from numpy import *
Example 1 D Array
# 1D Array
Import numpy as np
a = np.array([10,20,30,40,50])
print (a)
Output
array([10,20,30,40,50])
Example 2D Array
Import numpy as np
#2D Array
#3x3 Array
B=np.array([
[10,20,30,],
[40,50,60],
[70,80,90]])
print (b)
Example 3D Array
Import numpy as np
# 3D Array
# 3x3x3
C=np.array([
[[1,2,3],[4,5,6],[7,8,9]],
[[9,8,7],[6,5,4],[3,2,1]],
[[1,2,3],[4,5,6],[7,8,9]]
])
Print (c)
Thank you