word = 'banana talks'
for i in word: #Here the i is the letters in the string word
print 'Character from string:',i
for h in range(len(word)): #Here the h is index created by range function with value created by length
print 'Index:',h,'charater:' ,word[h]
n1 = word.upper()
print 'String in the upper case:', n1
n2 = word.lower()
print 'String in the lower case:', n2
print 'Display a part of string :',word[1:5] # It display string from index 1 to 4
print 'Display a part of string :',word[4:8] # It display string from index 4 to 7
Solve Problems by Coding Solutions - A Complete solution for python programming
Factorial of a Number
# Python program to find the factorial of a number provided by the user.
num = int(input("Enter a number: "))
factorial = 1
# check if the number is negative, positive or zero
if num < 0:
print("Sorry, factorial does not exist for negative numbers")
elif num == 0:
print("The factorial of 0 is 1")
else:
for i in range(1,num + 1):
factorial = factorial*i
print("The factorial of",num,"is",factorial)
num = int(input("Enter a number: "))
factorial = 1
# check if the number is negative, positive or zero
if num < 0:
print("Sorry, factorial does not exist for negative numbers")
elif num == 0:
print("The factorial of 0 is 1")
else:
for i in range(1,num + 1):
factorial = factorial*i
print("The factorial of",num,"is",factorial)
Lisiting particular type of files in a directory
import os
Path = os.getcwd()
Names= os.listdir(Path)
h=raw_input('Enter the file type extension: ')
for n in Names:
if h in n:
print n
Path = os.getcwd()
Names= os.listdir(Path)
h=raw_input('Enter the file type extension: ')
for n in Names:
if h in n:
print n
Enter the sum of n numbers using file
n=int(raw_input('Enter the n for sum of n numbers: '))
f=open('integers.txt','w')
for count in range(n+1):
f.write(str(count)+"\n")
f.close()
f=open('integers.txt','r')
sum=0
for l in f:
l=l.strip()
number=int(l)
sum+=number
print 'The sum is',sum
f=open('integers.txt','w')
for count in range(n+1):
f.write(str(count)+"\n")
f.close()
f=open('integers.txt','r')
sum=0
for l in f:
l=l.strip()
number=int(l)
sum+=number
print 'The sum is',sum
Writing and reading integers from a file
f=open('myfile.txt','w')
for count in range(5):
f.write(str(count))
f.close()
f=open('myfile.txt','r')
t=f.read()
print t
for count in range(5):
f.write(str(count))
f.close()
f=open('myfile.txt','r')
t=f.read()
print t
Writing and reading string from a file
f=open('myfile.txt','w')
f.write('\n Image Processing Research Group \n www.iprg.co.in \n')
f.close()
f=open('myfile.txt','r')
text=f.read()
print text
f.write('\n Image Processing Research Group \n www.iprg.co.in \n')
f.close()
f=open('myfile.txt','r')
text=f.read()
print text
Basic operations in Python List
l=[]
n=int(raw_input('Enter the no of elements added to list:'))
for i in range(n):
element=int(raw_input('Enter the elements:'))
l.append(element)
print '\nNew List is ',
m=int(raw_input('\nEnter the no of elements added to list using insert:'))
for i in range(m):
u=int(raw_input('Enter the index to be added'))
u1=int(raw_input('Enter the element to be added'))
l.insert(u,u1)
print '\nNew List after insert operation is: ',l
ch=int(raw_input('\nPress 1 to pop the last element\nPress 2 to pop the element in a particular index\nEnter the choice: '))
if ch==1:
l.pop()
print '\nNew List after deletion is: ',l
elif ch==2:
p=int(raw_input('Enter the element index to be deleted'))
l.pop(p)
print '\nNew List after deletion is: ',l
else:
print '\nInvalid choice'
print ' \nList elements are sorted: ',l
n=int(raw_input('Enter the no of elements added to list:'))
for i in range(n):
element=int(raw_input('Enter the elements:'))
l.append(element)
print '\nNew List is ',
m=int(raw_input('\nEnter the no of elements added to list using insert:'))
for i in range(m):
u=int(raw_input('Enter the index to be added'))
u1=int(raw_input('Enter the element to be added'))
l.insert(u,u1)
print '\nNew List after insert operation is: ',l
ch=int(raw_input('\nPress 1 to pop the last element\nPress 2 to pop the element in a particular index\nEnter the choice: '))
if ch==1:
l.pop()
print '\nNew List after deletion is: ',l
elif ch==2:
p=int(raw_input('Enter the element index to be deleted'))
l.pop(p)
print '\nNew List after deletion is: ',l
else:
print '\nInvalid choice'
print ' \nList elements are sorted: ',l
To add elements in list using insert
y=[]
n=int(raw_input('Enter the no of elements added to list:'))
index=0
for i in range(n):
element=int(raw_input('Enter the elements:'))
y.insert(index, element)
index=index+1
print y
n=int(raw_input('Enter the no of elements added to list:'))
index=0
for i in range(n):
element=int(raw_input('Enter the elements:'))
y.insert(index, element)
index=index+1
print y
To add elements inside a list using append
l=[]
n=int(raw_input('Enter the no of elements added to list:'))
for i in range(n):
element=int(raw_input('Enter the elements:'))
l.append(element)
print l
n=int(raw_input('Enter the no of elements added to list:'))
for i in range(n):
element=int(raw_input('Enter the elements:'))
l.append(element)
print l
To count the elements in a nested list with all elements are list
a=[[3, 4, 5,8, 8 ], [5, 6, 7], [7, 8, 9]]
def count(a):
j=0
n=0
for b in a:
j=len(b)
n=n+j
return n
t= count(a)
print "No elements in the nested list are:",t
def count(a):
j=0
n=0
for b in a:
j=len(b)
n=n+j
return n
t= count(a)
print "No elements in the nested list are:",t
Subscribe to:
Posts (Atom)