Day: February 9, 2021

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()) …

Python Exercise: Write a python program to access environment variables

(Python Example for Citizen Data Scientist & Business Analyst)   Write a python program to access environment variables. Sample Solution:- Python Code: import os # Access all environment variables print(‘*———————————-*’) print(os.environ) print(‘*———————————-*’) # Access a particular environment variable print(os.environ[‘HOME’]) print(‘*———————————-*’) print(os.environ[‘PATH’]) print(‘*———————————-*’) Sample Output: *———————————-* environ({‘LESSOPEN’: ‘| /usr/bin/lesspipe %s’, ‘_’: ‘/usr/bin/timeout’, ‘LIBVIRT_DEFAULT_URI’: ‘qemu:///system ‘, ‘HOME’: …

Python Exercise: Write a Python program to print to stderr

(Python Example for Citizen Data Scientist & Business Analyst)   Write a Python program to print to stderr. Sample Solution Python Code: from __future__ import print_function import sys def eprint(*args, **kwargs): print(*args, file=sys.stderr, **kwargs) eprint(“abc”, “efg”, “xyz”, sep=”–“) Sample Output: abc–efg–xyz   Python Exercise: Write a Python program to print to stderr Free Machine Learning …

Python Exercise: Write a Python program to determine profiling of Python programs

(Python Example for Citizen Data Scientist & Business Analyst)   Write a Python program to determine profiling of Python programs. Note: A profile is a set of statistics that describes how often and for how long various parts of the program executed. These statistics can be formatted into reports via the pstats module. Sample Solution …

Python Exercise: Write a Python program to print without newline or space

  (Python Example for Citizen Data Scientist & Business Analyst)   Write a Python program to print without newline or space?   Sample Solution Python Code: for i in range(0, 10): print(‘*’, end=””) print(“n”) Sample Output: **********   Python Exercise: Write a Python program to print without newline or space Free Machine Learning & Data …