Posts

FIND THE DAY OF YEAR

Image
FIND THE DAY OF YEAR FIND THE DAY OF YEAR Learn Python Programming Home FIND THE DAY OF YEAR Published on September 23, 2024 by MSK Technologies The day_of_year function takes a date as input and calculates the day of the year for that date. Program: from datetime import datetime def day_of_year(date): return date.timetuple().tm_yday if __name__=="__main__": print(day_of_year(datetime(2024, 10, 1))) Output: 275 My A...

CHECK IF DATE IS VALID

Image
 CHECK IF DATE IS VALID The is_date_valid function checks if a date is valid. from datetime import datetime def is_date_valid(val):     return datetime.strptime(val, "%B %d, %Y %H:%M:%S")      if __name__=="__main__":     print(is_date_valid("December 17, 1995 03:24:00")) Output: 1995-12-17 03:24:00

TRANSPOSE OF A MATRIX

Image
 TRANSPOSE OF A MATRIX The transpose_matrix function computes the transpose of a given matrix. def transpose_matrix(matrix):     return [list(row) for row in zip(*matrix)]   if __name__=="__main__":     matrix = [ [1, 2, 3], [4, 5, 6], [7, 8, 9] ]     print("\n".join(", ".join(map(str, row)) for row in transpose_matrix(matrix))) Output: 1, 4, 7 2, 5, 8 3, 6, 9

CONVERT RGB TO HEX

Image
 CONVERT RGB TO HEX The rgb_to_hex function combines the red, green, and blue (RGB) values into a single hexadecimal color code. def rgb_to_hex(r, g, b):     return f"#{((r << 16) + (g << 8) + b):06X}"   if __name__=="__main__":     print(rgb_to_hex(0, 51, 255)) Output: #0033FF

SWAP TWO VARIABLES

Image
 SWAP TWO VARIABLES The swap_without_temp function swaps the values of two variables a and b without using a temporary variable. def swap_without_temp(a, b):     return b, a   if __name__=="__main__":     result = swap_without_temp(5, 10)     print(f"After swapping: a = {result[0]}, b = {result[1]}") Output: After swapping: a = 10, b = 5

CONVERT CELSIUS TO FAHRENHEIT

Image
 CONVERT CELSIUS TO FAHRENHEIT The celsius_to_fahrenheit function converts Celsius to Fahrenheit. Program: def celsius_to_fahrenheit(celsius):     return (celsius * 9/5)+32 if __name__=="__main__":     print(celsius_to_fahrenheit(25)) Output: 77.0