Tag Archives: Python tutorials

Python Exercise: Write a Python program to convert pressure in kilopascals to pounds per square inch, a millimeter of mercury (mmHg) and atmosphere pressure

(Python Example for Citizen Data Scientist & Business Analyst)   Write a Python program to convert pressure in kilopascals to pounds per square inch, a millimeter of mercury (mmHg) and atmosphere pressure.   Sample Solution Python Code: kpa = float(input(“Input pressure in in kilopascals> “)) psi = kpa / 6.89475729 mmhg = kpa * 760 …

Python Exercise: Write a Python program to calculate body mass index

(Python Example for Citizen Data Scientist & Business Analyst)   Write a Python program to calculate body mass index.   Sample Solution Python Code: height = float(input(“Input your height in meters: “)) weight = float(input(“Input your weight in kilogram: “)) print(“Your body mass index is: “, round(weight / (height * height), 2)) Sample Output: Input …

Python Exercise: Write a Python program to convert seconds to day, hour, minutes and seconds

(Python Example for Citizen Data Scientist & Business Analyst)   Write a Python program to convert seconds to day, hour, minutes and seconds.   Sample Solution Python Code: time = float(input(“Input time in seconds: “)) day = time // (24 * 3600) time = time % (24 * 3600) hour = time // 3600 time …

Python Exercise: Write a Python program to convert all units of time into seconds

(Python Example for Citizen Data Scientist & Business Analyst)   Write a Python program to convert all units of time into seconds.   Sample Solution Python Code: days = int(input(“Input days: “)) * 3600 * 24 hours = int(input(“Input hours: “)) * 3600 minutes = int(input(“Input minutes: “)) * 60 seconds = int(input(“Input seconds: “)) …

Python Exercise: Write a Python program to convert the distance (in feet) to inches, yards, and miles

(Python Example for Citizen Data Scientist & Business Analyst)   Write a Python program to convert the distance (in feet) to inches, yards, and miles.   Sample Solution Python Code : d_ft = int(input(“Input distance in feet: “)) d_inches = d_ft * 12 d_yards = d_ft / 3.0 d_miles = d_ft / 5280.0 print(“The distance …

Python Exercise: Write a Python program to calculate the hypotenuse of a right angled triangle

(Python Example for Citizen Data Scientist & Business Analyst)   Write a Python program to calculate the hypotenuse of a right angled triangle.   Sample Solution Python Code : from math import sqrt print(“Input lengths of shorter triangle sides:”) a = float(input(“a: “)) b = float(input(“b: “)) c = sqrt(a**2 + b**2) print(“The length of …

Python Exercise: Write a Python program to convert height (in feet and inches) to centimeters

(Python Example for Citizen Data Scientist & Business Analyst)   Write a Python program to convert height (in feet and inches) to centimeters Sample Solution Python Code: print(“Input your height: “) h_ft = int(input(“Feet: “)) h_inch = int(input(“Inches: “)) h_inch += h_ft * 12 h_cm = round(h_inch * 2.54, 1) print(“Your height is : %d …

Python Exercise: Write a python program to find the sum of the first n positive integers

(Python Example for Citizen Data Scientist & Business Analyst)   Write a python program to find the sum of the first n positive integers. Sample Solution Python Code: n = int(input(“Input a number: “)) sum_num = (n * (n + 1)) / 2 print(sum_num) Sample Output: Input a number: 2 3.0   Python Exercise: Write …

Python Exercise: Write a program to get execution time (in seconds) for a Python method

(Python Example for Citizen Data Scientist & Business Analyst)   Write a program to get execution time (in seconds) for a Python method. Sample Solution Python Code: import time def sum_of_n_numbers(n): start_time = time.time() s = 0 for i in range(1,n+1): s = s + i end_time = time.time() return s,end_time-start_time n = 5 print(“nTime …

Python Exercise: Write a Python program to get height and the width of console window

(Python Example for Citizen Data Scientist & Business Analyst)   Write a Python program to get height and the width of console window. Sample Solution Python Code: def terminal_size(): import fcntl, termios, struct th, tw, hp, wp = struct.unpack(‘HHHH’, fcntl.ioctl(0, termios.TIOCGWINSZ, struct.pack(‘HHHH’, 0, 0, 0, 0))) return tw, th print(‘Number of columns and Rows: ‘,terminal_size()) …