Posts

Showing posts from December, 2023

Functions in Python:

 def calculateGmean(a,b):  mean = (a*b)/(a+b)  print(mean) def isGreater(a,b):   if(a>b):     print("First number is greater")   else:     print("Second number is greater or equal") def isLesser(a,b):   pass a = 9 b = 8 isGreater(a,b) calculateGmean(a,b) # gmean1 = (a*b)/(a+b) # print(gmean1) c = 8 d = 74 isGreater(c,d) calculateGmean(c,d) # gmean2 = (c*d)/(c+d) # print(gmean2) Result: First number is greater 4.235294117647059 Second number is greater or equal 7.219512195121951

Continue in Python:

 for i in range(12):   if(i==10):     print("Skip the iteration")     continue   print("5 X", i, "=", 5 * i) Result:5 X 0 = 0 5 X 1 = 5 5 X 2 = 10 5 X 3 = 15 5 X 4 = 20 5 X 5 = 25 5 X 6 = 30 5 X 7 = 35 5 X 8 = 40 5 X 9 = 45 Skip the iteration 5 X 11 = 55 In continue  iteration is exit out.

Break in Python:

 for i in range(12):   print("6 X", i+1, "=", 6 * i+1)   if(i == 10):     break print("Skip the loop") Result: 6 X 1 = 1 6 X 2 = 7 6 X 3 = 13 6 X 4 = 19 6 X 5 = 25 6 X 6 = 31 6 X 7 = 37 6 X 8 = 43 6 X 9 = 49 6 X 10 = 55 6 X 11 = 61 Skip the loop In break only loops are exit.

while loop and Do while loop example

 # i = 0    For loop # while(i<=10): #   print(i) #   i = i + 1 # print("Done with the loop") i = int(input("Enter the number: ")) print(i) while(i<=38):   i = int(input("Enter the number: "))   print(i) print("Done with the loop") # count = 5 # while(count>0): #   print(count) #   count = count -1 # else: #   print("I am inside else") # do { #   loop body; # }while(condition); Do while loop example: while True:   number = int(input("Enter a positive number: "))   print(number)   if not number > 0:     break   

For loops programming:

 # name = 'Abhishek' # for i in name: #   print(i) #   if(i =="b"): #     print("This is something special!")      # colors = ["Red", "Green", "Blue", "Yellow"] # for color in colors: #   print(color) #   for i in color: #     print(i) # for k in range(5): #   print(k + 1)    # for k in range(1, 20001): #   print(k)    for k in range(1, 12, 3):   print(k) Result:(for k in range(1,2,3): 1 4 7 10

Match case statement in python

 x = int(input("Enter the value of x:"))  # x is the variable to match match x:   # if x is o    case 0:        print("x is zero")   # case with if-condition    case 4:     print("case is 4")    case _ if x!=90:     print(x, "is not 90")    case _ if x!=80:      print(x, "is not 80")    case _ if x!=90:      print(x, "is not 90")    case _ if x!=80:      print(x, "is not 80")    case _:       print(x) Result: Enter the value of x:56 56 is not 90 Enter the value of x:0 x is zero

time,minute and seconds program examples:

 import time timestamp = time.strftime('%H:%M:%S') print(timestamp) timestamp = time.strftime('%H') print(timestamp) timestamp = time.strftime('%M') print(timestamp) timestamp = time.strftime('%S') print(timestamp) # https://docs.python.org/3/library/time.html#time.strftime(time related document) Result: 18:18:26 18 18 26

time program examples

 import time second=time.time() local_time = time.ctime(second) print(local_time,second) Result: Sun Dec 24 18:14:58 2023 1703441698.0444565 >

If else statement python program

 num = int(input("Enter the value of num:")) if (num < 0):     print("Number is  negative") elif (num==0):     print("Number is zero") elif (num==9999):     print("Number is special") else:     print("Number is positive")      print("I am happy now") result: Enter the value of num:60 Number is positive I am happy now >