Python Devlopment






Practice Programs-2



1. Define a Python function remdup(l) that takes a nonempty list of integers l and removes all duplicates in l, keeping only the first occurrence of each number.

 

def remdup(t):
    s=[]
    for i in t:
       if i not in s:
          s.append(i)
    return(s)

 

2. Write a Python function sumsquare(l) that takes a nonempty list of integers and returns a list [odd,even], where odd is the sum of squares all the odd numbers in l and even is the sum of squares of all the even numbers in l.

 

def sumsquare(l):
    numlist=[]
    evnsumsq=0
    oddsumsq=0
  
    # iterating each number in list 
    for num in l: 
        # checking condition 
        if num % 2 == 0: 
            evnsumsq =evnsumsq+(num*num)
        else: 
            oddsumsq =oddsumsq+(num*num)
            
    numlist=[oddsumsq,evnsumsq]
    return(numlist)

 

3. A two dimensional matrix can be represented in Python row-wise, as a list of lists: each inner list represents one row of the matrix.

 

def transpose(M):
    n = len(M[0])
    L = sum(M, [])
    return [L[i::n] for i in range(n)]