Programs topratice

 1) program to add natural numbers till 100 :

a=0
for i in range(1,101):
    a=a+i

print(a)

 

2) Using while loop and an if statement write a function named name_adder which appends all the elements in a list to a new list unless the element is an empty string " ":

l=["tom","shelby","johnny","arthur","","thomas","chris"]
nl=[]

def name_adder(list):
    j=0
    while j < len(list):
        if list[j] != "":
            nl.append(list[j])
        j+=1
    return nl

print(name_adder(l))

 

3) Using a for loop and .append() method append each item with a Dr. prefix to the list.

lst1=["Phil", "Oz", "Seuss", "Dre"]
lst2=[]    
for i in lst1:
    lst2.append("dr."+i)

print(lst2)

 

4) Write a python program to check given number is prime or not

print("enter the number : ")
n=int(input())

for i in range(2,n):
    if (n % i)==0 :
        print("it is not a prime number")
        break
    else:
        print("it is a prime")
        break

 

Comments

Popular posts from this blog

Queue in Java

Using lists , tuples, sets, dictionaries

Blog on Pytest