This website includes Education Information like a programming language, job interview question, general knowledge.mathematics

Education log

PageNavi Results No.

Ads

Sunday, October 6, 2019

python programming examples


                 python programming examples



In this article, today learn basic python programming list.




1. Python hello word program :


print("This line will be printed.")
save script as  name of hello.py




2. Add Two Numbers:


# Python3 program to add two numbers
 
number1 = input("First number: ")
number2 = input("\nSecond number: ")
 
# Adding two numbers
# User might also enter float numbers
sum = float(number1) + float(number2)
 
# Display the sum
# will print value in float
print("The sum of {0} and {1} is {2}" .format(number1, number2, sum))






3. check whether a number is Prime or not:


num = 11
 
# If given number is greater than 1
if num > 1:
     
   # Iterate from 2 to n / 2 
   for i in range(2, num//2):
        
       # If num is divisible by any number between 
       # 2 and n / 2, it is not prime 
       if (num % i) == 0:
           print(num, "is not a prime number")
           break
   else:
       print(num, "is a prime number")
 
else:
   print(num, "is not a prime number")





4. Check Even Odd Number:


num = int(input("Enter any number: "))
flag = num%2
if flag == 0:
    print(num, "is an even number")
elif flag == 1:
    print(num, "is an odd number")
else:
    print("Error, Invalid input")




5. Convert Celsius To Fahrenheit:


celsius = 37.5

fahrenheit = (celsius * 1.8) + 32
print('%0.1f degree Celsius is equal to %0.1f degree Fahrenheit' %(celsius,fahrenheit))





6. Convert Kilometers to Miles:


kilometers = 5.5

conv_fac = 0.621371

miles = kilometers * conv_fac
print('%0.3f kilometers is equal to %0.3f miles' %(kilometers,miles))



7. Display Calendar:


import calendar

yy = 2019
mm = 11

print(calendar.month(yy, mm))




8. Display the multiplication Table:


num = 12

for i in range(1, 11):
   print(num,'x',i,'=',num*i)





9. Do While loop:


Syntax:


do { 
     //statement 
} while (condition); 
              
              
              

Example:


i = 1 
 
while True: 
    print(i) 
    i = i + 1 
    if(i > 5): 
        break 




10. Fibonacci number:


def Fibonacci(n):
    if n<0:
        print("Incorrect input")
    # First Fibonacci number is 0
    elif n==1:
        return 0
    # Second Fibonacci number is 1
    elif n==2:
        return 1
    else:
        return Fibonacci(n-1)+Fibonacci(n-2)
 
# Driver Program
 
print(Fibonacci(9))


11. Factorial Number:


factorial = 1

# check if the number is negative, positive or zero
if num < 0:
   print("Sorry, factorial does not exist for negative numbers")
elif num == 0:
   print("The factorial of 0 is 1")
else:
   for i in range(1,num + 1):
       factorial = factorial*i
   print("The factorial of",num,"is",factorial)







12. find area of a circle:

def findArea(r):
    PI = 3.142
    return PI * (r*r);
 
# Driver method
print("Area is %.6f" % findArea(5));




13. Find ASCII Value of Character:


c = 'p'

# Uncomment to take character from user
#c = input("Enter a character: ")

print("The ASCII value of '" + c + "' is",ord(c))



14. find sum of array:


def _sum(arr,n):
     
    # return sum using sum 
    # inbuilt sum() function
    return(sum(arr))
 
# driver function
arr=[]
# input values to list
arr = [12, 3, 4, 15]
 
# calculating length of array
n = len(arr)
 
ans = _sum(arr,n)
 
# display sum
print (\'Sum of the array is \',ans)





15. Find the Size (Resolution) of a Image:



def jpeg_res(filename):
   """"This function prints the resolution of the jpeg image file passed into it"""

   # open image for reading in binary mode
   with open(filename,'rb') as img_file:

       # height of image (in 2 bytes) is at 164th position
       img_file.seek(163)

       # read the 2 bytes
       a = img_file.read(2)

       # calculate height
       height = (a[0] << 8) + a[1]

       # next 2 bytes is width
       a = img_file.read(2)

       # calculate width
       width = (a[0] << 8) + a[1]

   print("The resolution of the image is",width,"x",height)

jpeg_res("img1.jpg")




16. Leap Year:


year = 2000

# To get year (integer input) from the user
# year = int(input("Enter a year: "))

if (year % 4) == 0:
   if (year % 100) == 0:
       if (year % 400) == 0:
           print("{0} is a leap year".format(year))
       else:
           print("{0} is not a leap year".format(year))
   else:
       print("{0} is a leap year".format(year))
else:
   print("{0} is not a leap year".format(year))




17. Nested If Statment:



Syntax:


if (condition1):
   # Executes when condition1 is true
   if (condition2):
      # Executes when condition2 is true
   # if Block is end here
# if Block is end here



Example:


i = 10
if (i == 10):
    #  First if statement
    if (i < 15):
        print ("i is smaller than 15")
    # Nested - if statement
    # Will only be executed if statement above
    # it is true
    if (i < 12):
        print ("i is smaller than 12 too")
    else:
        print ("i is greater than 15")




3 comments:

  1. This post is really awesome. Genuinely i like this blog. It gives me more useful information. I hope you share lots of things with us .Free Multiplication Games

    ReplyDelete
  2. I read the above article and I got some different kind of information from your article about a mattress. It is a helpful article to enhance our knowledge for us. Thankful to you for sharing an article like this.mental math classes

    ReplyDelete