#to accept a string split it into small batches
a=input('enter the value of string ')
l=int(input('enter the size of the batch '))
k=''
for i in range(len(a)):
if a[i:i+l] not in k:
k=k+a[i:i+l]
print(a[i:i+l])
#To accept a string and split them into batches
#To accept a string and split them into batches
n=input('enter the value of n ')
b=int(input('enter the batch size '))
c=len(n)
for i in range(0,c,b):
print(n[i:i+b])
#To accept a string split them into batches without same letter combinations
n=input('enter the value of n ')#iglobal
b=int(input('enter the value of b '))#2
for i in range(len(n)):#0,1,2,3,4,5,6
c=n[i:i+b]#n[0:0+2]->ig,n[1:1+2]->gl,n[6:8]-l
if len(c)==b:
if(n[i]!=n[i+1]):
print(c)
else:
print(c)
#For accepting the input for UID and password and display a message using
while loop
while True:
UID=input('enter the value of user ID')
if(UID=='iglobal'):
while True:
pwd=int(input('enter the value of password '))
if(pwd==123):
print('Good Morning')
break
else:
continue
break
else:
continue
# Fibonacci series:
... # the sum of two elements defines the next
a, b = 0, 1
print(a)
while b < 10:
print(b, end=',')
a, b = b, a+b
Python Functions
A function is like a mini-program within a program.
❖ Previously discussed print(), input(), and len() functions in which Python
provides several built in functionalities.
❖ A major purpose of functions is to group code that gets executed
multiple times
❖ Without a function defined, you would have to copy and paste this code
each time
❖ Want to avoid duplicating code, have to remember to change the code
everywhere you copied it
❖ A parameter is a variable that an argument is stored in, when a function
is called.
❖ Value stored in a parameter is forgotten when the function returns
❖ similar to how a program’s variables are forgotten when the
program terminates.
Return Values and return Statements
When creating a function using the def statement, you can specify what the
return value should be with a return statement. A return statement consists of
the following:
● The return keyword
● The value or expression that the function should return
#sample program for user defined functions
def f1():
print(' Welcome to')
print(' Ameerpet ')
print(' Hyderbad ')
print('---------')
f1()
f1()
#Passing a Parameter to a function
def f1(n):
print(n+' Welcome to')
print(' Ameerpet ')
print(' Hyderbad ')
print('---------')
f1(name)
f1(name)
#Calculating the PF for an employee based on age and returning values
def pf(age):
if(age>15 and age<30):
b=float(input('enter the basic salary '))
pf=0.1*b
return pf
elif(age>30 and age<=60):
b=float(input('enter the basic salary '))
pf=0.15*b
return pf
else:
print('retired people are not paid pf ')
age=int(input('enter the age of the employee '))
k=pf(age)
if(k!=None):
print('the pf of the employee is',k)
#Print factorial of a number until user press zero
def fact(n):
p =1
for i in range(1,n+1):
p=p*i
return p
while True:
n = int(input('Enter value for n '))
if( n == 0):
break
else:
print(' Factorial of ', n , ' = ', fact(n))
#Program returns a QUARTER string depending
#on what MONTH is passed as an argument
#IF given error input ask for correct month
def quarter(n):
if(n=='jan' or n=='feb' or n=='mar'):
print('quarter 1')
elif(n=='apr' or n=='may' or n=='jun'):
print('quarter 2')
elif(n=='july' or n=='aug' or n=='sep'):
print('quarter 3')
elif(n=='oct' or n=='nov' or n=='dec'):
print('quarter 4')
else:
print('wrong input')
n=input('enter the value of n ')
quarter(n)
Collection Data Types:
The List Data Type
❖ A list is a value that contains multiple values in an ordered sequence.
❖ A list value looks like this: ['cat', 'bat', 'rat', 'elephant', 123,
‘456’]. Just as string values are typed with quote characters to mark
where the string begins and ends.
❖ A list begins with an opening square bracket and ends with a closing square
bracket, [ ]. Values inside the list are also called items. Items are separated
by commas (that is, they are comma-delimited).
❖ The value [] is an empty list that contains no values, as the empty string.
Negative Indexes
❖ While indexes start at 0 and go up, you can also use negative integers for the
index.
❖ The integer value -1 refers to the last index in a list, the value -2 refers to
the second-to-last index in a list, and so on
>>> ls=['iglobal',123,'sai','venkat']
>>> ls
['iglobal', 123, 'sai', 'venkat']
>>> ls[0]
'iglobal'
>>> ls[5]
Traceback (most recent call last):
File "<pyshell#3>", line 1, in <module>
ls[5]
IndexError: list index out of range
>>> ls[-1]
'venkat'
>>> ls[-1:-5]
[]
>>> ls[-1:-3]
[]
>>> ls[-3:-1]
[123, 'sai']
>>> ls[:3]
['iglobal', 123, 'sai']
>>> ls[2:]
['sai', 'venkat']
>>> ls=['sri',[1,2,3],156,'venkat','mani']
>>> ls[1]
[1, 2, 3]
\
>>> ls[1][2]
3
>>> ls[0][2]
'i'
>>> ls.index('venkat')
3
>>> ls.index('v')
Traceback (most recent call last):
File "<pyshell#15>", line 1, in <module>
ls.index('v')
ValueError: 'v' is not in list
>>> ls[1].index(2)
1
ls[1][1][2] (sub of sub lists)
Length with len()
>>> len(ls)
5
>>> ls.index(123)
>>> len(ls[2])
>>> ls
[12, 'sai', 'sri', 45.6]
***to slice 5.6 from 45.6 use
>>>float(str(ls[3])[1:])
>>> import sys
>>> sys.getsizeof(ls) # to find the size of given data type
56
*****Maximum size of a list in python is 536,870,912 elements
Changing values in list:
>>> ls
['sri', 123, [1, 'iglobal', 3], 'sai123', 56.23]
>>> ls[1]='venkat'
>>> ls
['sri', 'venkat', [1, 'iglobal', 3], 'sai123', 56.23]
>>> ls[1]+'hi'
'venkathi'
>>> ls
['sri', 'venkat', [1, 'iglobal', 3], 'sai123', 56.23]
>>> ls[1]=ls[1]+'hi'
>>> ls
['sri', 'venkathi', [1, 'iglobal', 3], 'sai123', 56.23]
List Concatenation and List Replication
>>> ls1=['india','china']
>>> ls1+ls #Concatenation
['india', 'china', 'sri', 'venkathi', [1, 'iglobal', 3], 'sai123', 56.23]
>>> ls1*10
['india', 'china', 'india', 'china', 'india', 'china', 'india', 'china', 'india', 'china', 'india',
'china', 'india', 'china', 'india', 'china', 'india', 'china', 'india', 'china']
Removing Values from Lists with remove()
❖ The remove() method is passed the value to be removed from the list it is
called on.
❖ If the value appears multiple times in the list, only the first instance of the
value will be removed
>>> ls
['sri', 'venkat', [1, 'iglobal', 3], 'sai123', 56.23]
>>> ls.remove('sri')
>>> ls
['venkat', [1, 'iglobal', 3], 'sai123', 56.23]
>>> ls.remove(ls[0])
>>> ls
[[1, 'iglobal', 3], 'sai123', 56.23]
>>> ls=[1,2,3,4,4,4,4]
>>> ls.remove(4)
>>> ls
[1, 2, 3, 4, 4, 4]
>>> newls
['india', 'china', 'india', 'china', 'india', 'china', 'india', 'china', 'india', 'china', 'india',
'china', 'india', 'china', 'india', 'china', 'india', 'china', 'india', 'china']
>>> newls.remove('india')
>>> newls
['china', 'india', 'china', 'india', 'china', 'india', 'china', 'india', 'china', 'india', 'china',
'india', 'china', 'india', 'china', 'india', 'china', 'india', 'china']
Deleting values in a list
❖ The del statement will delete values at an index in a list.
❖ All of the values in the list after the deleted value will be moved up one
index.
ls=[[1, 'iglobal', 3], 'sai123', 56.23]
>>> del ls[1:]
>>> ls
[[1, 'iglobal', 3]]
Adding values to the List
Append Method
>>> ls=['sri', 'venkat', [1, 'iglobal', 3], 'sai123', 56.23]
>>> ls.append('yamuna')
>>> ls
['sri', 'venkat', [1, 'iglobal', 3], 'sai123', 56.23, 'yamuna']
Insert Method
ls
['sri', 'venkat', 'manjeet', [1, 'iglobal', 3], 'sai123', 56.23, 'yamuna']
>>> ls.insert(2,ls[3:5])
>>> ls
['sri', 'venkat', [[1, 'iglobal', 3], 'sai123'], 'manjeet', [1, 'iglobal', 3],
'sai123', 56.23, 'yamuna']
The in and not in Operators
>>> 'python' in ['sri', 'venkat', [1, 'iglobal', 3], 'sai123', 56.23]
False
>>> 'python' not in ['sri', 'venkat', [1, 'iglobal', 3], 'sai123', 56.23]
True
Use of for loop in list