Lecture 8
Lecture 8
𝐋𝐞𝐜𝐭𝐮𝐫𝐞 − 𝟖
Input Statement
Accept more than one input in the same line
• Use input() function with for loop
• Syntax is
a, b = [ i for i in input(‘Enter two numbers : ’). split()]
• enter data using spaces
• [ ] represents a list / we can use () also
• data are strings
• a, b = [ i for i in input(‘ Enter two numbers : ’). split(‘ , ’)]
• enter data using comma
• a, b = [ int(i) for i in input(‘ Enter two numbers : ’). split()]
• data are integers
a, b = (i for i in input('Enter two numbers : '). split(','))
print('entered numbers are', a,b)
• eval() function takes a string and evaluates it
a, b = 2, 3
c = eval('a+b-5+a')
print(c)
• when dealing with a huge number of elements, arrays use less memory
compared to lists and arrays offer faster execution than lists
• In Python, there is a standard module by the name array that helps us to
create and use arrays
Creating an array object
• Syntax is
arrayname = array(type code, [elements])
10 20 30 40 50
a[0] a[1] a[2] a[3] a[4]
• len(a) returns the number of elements in the array a
from array import*
a = array('i', [10, 20, 30, 40, 50]) write this program
n = len(a) using while loop
for i in range(n):
print(a[i], end = ' ')
Slicing on Arrays
• The syntax is
arrayname[start : stop : stride]
Basic Idea
• The insertion sort scans input array A[n]
from A[0] to A[n-1]
• It inserts each element A[k] into its proper position in
the previously sorted sub array A[0], A[1], …., A[k-1].
Algorithm
Input: A[n]
Basic idea: