Categories:Viewed: 6 - Published at: a few seconds ago

Write a Python Program to Remove the nth Index Character from a Non-Empty String

def remove(string, n):  
      first = string[:n]   
      last = string[n+1:]  
      return first + last

Write a Python Program to Detect if Two Strings are Anagrams

def anagram_check(s1, s2):
    if(sorted(s1)==sorted(s2)):
        return True
    else:
        return False

Write a Python Program to Form a New String where the First Character and the Last Character have been Exchanged

def change(string):
      return string[-1:] + string[1:-1] + string[:1]

Write a Python Program to Remove the Characters of Odd Index Values in a String

def modify(string):  
    final = ""   
    for i in range(len(string)):  
        if i % 2 == 0:  
            final = final + string[i]  
    return final

Write a Python Program to Take in Two Strings and Print the Larger String

string1='python'
string2='theschoolofai'
count1=0
count2=0
for i in string1:
      count1=count1+1
for j in string2:
      count2=count2+1
if(count1<count2):
      print("Larger string is:")
      print(string2)
elif(count1==count2):
      print("Both strings are equal.")
else:
      print("Larger string is:")
      print(string1)

Write a Python Program to Count and print Number of Lowercase Characters in a String

string='This is an Assignment'
count=0
for i in string:
      if(i.islower()):
            count=count+1
print("The number of lowercase characters is:")
print(count)

Write a Python Program to Put Even and Odd elements in a List into Two Different Lists

a=[2, 3, 8, 9, 2, 4, 6]
even=[]
odd=[]
for j in a:
    if(j%2==0):
        even.append(j)
    else:
        odd.append(j)
print("The even list",even)
print("The odd list",odd)

Write a Python Program to Sort the List According to the Second Element in Sublist

a=[['A',34],['B',21],['C',26]]
for i in range(0,len(a)):
    for j in range(0,len(a)-i-1):
        if(a[j][1]>a[j+1][1]):
            temp=a[j]
            a[j]=a[j+1]
            a[j+1]=temp

Write a Python Program to Find the Second Largest Number in a List Using Bubble Sort

a=[2, 3, 8, 9, 2, 4, 6]
for i in range(0,len(a)):
    for j in range(0,len(a)-i-1):
        if(a[j]>a[j+1]):
            temp=a[j]
            a[j]=a[j+1]
            a[j+1]=temp 

Write a Python Program to Find the Intersection of Two Lists

def main(alist, blist):
    def intersection(a, b):
        return list(set(a) & set(b))
    return intersection(alist, blist)

Write a Python Program to Create a List of Tuples with the First Element as the Number and Second Element as the Square of the Number using list comprehension

l_range=2
u_range=5
a=[(x,x**2) for x in range(l_range,u_range+1)]

Write a Python Program to print all Numbers in a Range which are Perfect Squares and Sum of all Digits in the Number is Less than 10

l=6
u=9
a=[x for x in range(l,u+1) if (int(x**0.5))**2==x and sum(list(map(int,str(x))))<10]
print(a)

Write a Python Program to Swap the First and Last Value of a List

a=[2, 3, 8, 9, 2, 4, 6]
n = len(a)
temp=a[0]
a[0]=a[n-1]
a[n-1]=temp
print("New list is:")
print(a)

Write a Python Program to Remove and print the Duplicate Items from a List

a=[2, 3, 8, 9, 2, 4, 6]
b = set()
unique = []
for x in a:
    if x not in b:
        unique.append(x)
        b.add(x)
print("Non-duplicate items:")
print(unique)

Write a Python Program to Read a List of Words and Return the Length of the Longest One

a=['the', 'tsai', 'python']
max1=len(a[0])
temp=a[0]
for i in a:
    if(len(i)>max1):
       max1=len(i)
       temp=i
print("The word with the longest length is:")
print(temp)

Write a Python Program to Remove the ith Occurrence of the Given Word in a List where Words can Repeat

a=['the', 'tsai', 'python' ,'a' ,'the', 'a']
c=[]
count=0
b='a'
n=3
for i in a:
    if(i==b):
        count=count+1
        if(count!=n):
            c.append(i)
    else:
        c.append(i)
if(count==0):
    print("Item not found ")
else: 
    print("The number of repetitions is: ",count)
    print("Updated list is: ",c)
    print("The distinct elements are: ",set(a))

Write a Python function to Find Element Occurring Odd Number of Times in a List

def find_odd_occurring(alist):
    """Return the element that occurs odd number of times in alist.

    alist is a list in which all elements except one element occurs an even
    number of times.
    """
    ans = 0

    for element in alist:
        ans ^= element

    return ans

Write a Python Program to Check if a Date is Valid and Print the Incremented Date if it is

date="20/04/2021"
dd,mm,yy=date.split('/')
dd=int(dd)
mm=int(mm)
yy=int(yy)
if(mm==1 or mm==3 or mm==5 or mm==7 or mm==8 or mm==10 or mm==12):
    max1=31
elif(mm==4 or mm==6 or mm==9 or mm==11):
    max1=30
elif(yy%4==0 and yy%100!=0 or yy%400==0):
    max1=29
else:
    max1=28
if(mm<1 or mm>12):
    print("Date is invalid.")
elif(dd<1 or dd>max1):
    print("Date is invalid.")
elif(dd==max1 and mm!=12):
    dd=1
    mm=mm+1
    print("The incremented date is: ",dd,mm,yy)
elif(dd==31 and mm==12):
    dd=1
    mm=1
    yy=yy+1
    print("The incremented date is: ",dd,mm,yy)
else:
    dd=dd+1
    print("The incremented date is: ",dd,mm,yy)