Python Programs

1.PROGRAM :

a=int(input("Enter the first number:"))
b=int(input("Enter the second number:"))
if(a>b):
    m=a
else:
    m=b
while(1):
    if(m%a==0 and m%b==0):
        print("LCM is:",m)
        break    m=m+1
 
 
 
OUTPUT : 
 
Enter the first number:5
Enter the second number:6
LCM is: 30


 
2.PROGRAM:

num = int(input("Enter the number : "))
if num < 0:
   print("Enter a positive number")
else:
   sum = 0   while(num > 0):
       sum += num
       num -= 1   print("The sum is", sum)
 
 
 
OUTPUT:
 
Enter the number : 3
The sum is 6
 
 
1.Branching:
password = "qwerty"attempt = input("Enter password: ")

if attempt == password:
    print("Welcome")
 
OUTPUT :
 
Enter password: qwerty
Welcome
 
2.Looping :
 
num=int(input("Enter a number:"))
temp=num
rev=0while(num>0):
    dig=num%10    rev=rev*10+dig
    num=num//10if(temp==rev):
    print("The number is palindrome!")
else:
    print("Not a palindrome!")

 
Output :
 
Enter a number:121
The number is palindrome!
 
3.Functions :
 
def sum( arg1, arg2 ):
   total = arg1 + arg2
   print ("Total : ", total)

def sub( arg1, arg2 ):
   total = arg1 + arg2
   print ("Difference : ", total)

def send():
    print ("Sorry, You have entered the wrong number.")

num1 = int(input("Enter first number : "))
num2 = int(input("Enter second number : "))

choice = int(input("Enter your choice : 1.Add 2.Subtract : "))

if choice==1:
    sum( num1,num2 )
elif choice==2:
    sub(num1,num2)
else :
    send()

Output :
 
Enter first number : 12
Enter second number : 23
Enter your choice : 1.Add 2.Subtract : 1
Total :  35
 
4.Strings :
 
string=input(("Enter a string:"))
if(string==string[::-1]):
      print("The string is a palindrome")
else:
      print("Not a palindrome")
 
Output :
 
Enter a string:madam
The string is a palindrome
 
5.Lists :
 
list1 = ['physics', 'chemistry', 1997, 2000];

#Accessing Elements in the list :print ("list1[1:2]: ", list1[1:2])

#Updating Values in the list :list1[2] = "python";
print ("New value available at index 2 : ")
print (list1[2])

#Repitition :print ("After doing Repitition : ")
print (list1[2]*3)

#Length of the List :print ("Length of the List : ")
print (len(list1))

#Deletion :del list1[2];
print ("After deleting value at index 2 : ")
print (list1)
 
Output :
 
list1[1:2]:  ['chemistry']
New value available at index 2 : 
python
After doing Repitition : 
pythonpythonpython
Length of the List : 
4
After deleting value at index 2 : 
['physics', 'chemistry', 2000]
 
6.Tuples :
 
tup1 = ('physics', 'chemistry', 1997, 2000);
tup2 = (1, 2, 3, 4, 5, 6, 7 );

#Accessing Elements in Tupleprint ("tup2[1:5]: ", tup2[1:5])

#Concatenationtup3 = tup1 + tup2;
print (tup3)

#Length of the Tuple :print ("Length of the List : ")
print (len(tup1))

#Indexing , Slicing and MatricesL = ('spam', 'Spam', 'SPAM!')
print (L[2])
print (L[-2])
print (L[1:])
 
Output :
 
tup2[1:5]:  (2, 3, 4, 5)
('physics', 'chemistry', 1997, 2000, 1, 2, 3, 4, 5, 6, 7)
Length of the List : 
4
SPAM!
Spam
('Spam', 'SPAM!') 
 

Comments