Posts

Showing posts from April, 2021

To count number of digits in an integer (or) To count number of digits/letters using inbuilt method

 #To count number of digits in an integer x = int(input("Enter a string")) count = 0 while x  != 0:     x //= 10     count += 1 print("Number of digits are "+str(count)) ''' #To count number of digits/letters using inbuilt method x = input("Enter a string") print("Number of digits/letters are ",len(str(x))) '''

To check whether given input is palindrome or not in python

 string = input("Enter a string") string = string.casefold() revofstring = reversed(string) if list(string) == list(revofstring):     print("The string is palindrome") else:      print("The string is not palindrome")

Program to convert fahrenheit to celsius in python

 fahrenheit=float(input("Enter temperature in fahrenheit")) celsius = (fahrenheit - 32)*(5/9) print("%0.2f degree fahrenheit is %0.2f degree celsius"%(fahrenheit,celsius))

Program to convert celsius to fahrenheit in python

 celsius=float(input("Enter temperature in celsius")) fahrenheit = (celsius*(9/5))+32 print("%0.2f degree celsius is %0.2f degree fahrenheit"%(celsius,fahrenheit))

Simple calculator program in python

 def add(x,y):     return x+y def subtract(x,y):     return x-y def multiply(x,y):     return x*y def divide(x,y):     return x/y print("___Simple Calculator___") print("1. Addition") print("2. Subtraction") print("3. Multiplication") print("4. Division") choice = input("Enter your choice 1 or 2 or  3 or 4 ") if choice in ('1','2','3','4'):         a=float(input("Enter first number "))         b=float(input("Enter second number "))         if choice == '1':             print(a,"+",b,"=",add(a,b))         elif choice == '2':             print(a,"-",b,"=",subtract(a,b))         elif choice == '3':             print(a,"*",b,"=",multiply(a,b))         elif choice == '4':             print(a,"/",b,"=",divid...

To draw a triangle using turtle graphics in python

Image
 import turtle turtle.pensize(5) turtle.speed(2) turtle.pencolor("green") turtle.bgcolor("cyan") for i in range(3):     turtle.forward(100)     turtle.left(120)

To draw a square using turtle graphics in python

Image
 import turtle turtle.speed(0) turtle.bgcolor("black") for colors in ["red","magenta","blue","yellow"]:         turtle.color(colors)         turtle.pensize(3)         turtle.left(90)         turtle.forward(200)

To check a given number is even or odd in Python

 n=int(input("Enter a number")) if(n%2==0):     print("{0} is even".format(n)) else:     print("{0} is odd".format(n))

Program to print area of a triangle in Python

 a=float(input("Enter first side of triangle")) b=float(input("Enter second side of triangle")) c=float(input("Enter third side of triangle")) s=(a+b+c)/2 area=(s*(s-a)*(s-b)*(s-c)) ** 0.5 print("Area of triangle with sides {0},{1},{2} is {3}".format(a,b,c,area))

Program to swap two numbers in Python

 a=input("Enter a value") b=input("Enter b value") temp=a a=b b=temp print("After swapping") print("Value of a = {}".format(a)) print("Value of b = {}".format(b))

Program to copy a file in Python

 from shutil import copyfile copyfile("d:/example1.txt","d:/example2.txt") #Copies the file data in example1 to example2

Program to print a calendar in a year in Python

Image
 import calendar yr = int(input("Enter year : ")) print(calendar.calendar(yr)) #OUTPUT

Program to print a month in a year in Python

 import calendar #yr=2005 #mm=5 yr=int(input("Enter year you want: ")) mm=int(input("Enter month you want: ")) print(calendar.month(yr,mm))

Program to print a random number between two numbers from user input in Python

 import random a=int(input("Enter first number")) b=int(input("Enter second number")) print("Some random number between {0} and {1} is".format(a,b)) print(random.randint(a,b))

Program to print a random number in Python

 import random print("Some random number between 0 and 1000 is") print(random.randint(0,1000))

Program to print addition of two numbers in Python

 a=input("Enter first number") b=input("Enter second number") c=float(a) + float(b) print("Addition of {0} and {1} is {2}".format(a,b,c))

Program to check whether a given number is prime or not in Python

 n=5 flag=False if (n>1):     for i in range(2,n):         if(n%i)==0:             flag=True             break if flag:     print(n,"is not prime") else:     print(n,"is prime")

To print Hello World in Python

 print('Hello World!') #This program prints Hello World!