Day: February 9, 2021

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

Hits: 14 (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 …

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

Hits: 19 (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 …

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

Hits: 6 (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 …

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

Hits: 1 (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 …

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

Hits: 2 (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 = …

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

Hits: 4 (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 …

Python Exercise: Write a python program to access environment variables

Hits: 2 (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 …

Python Exercise: Write a Python program to print to stderr

Hits: 7 (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 …

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

Hits: 2 (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. …

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

Hits: 3   (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 …

Python Example – Write a Python program to list all files in a directory in Python

Hits: 6  (Python Example for Citizen Data Scientist & Business Analyst)   Write a Python program to list all files in a directory in Python. Sample Solution Python Code: from os import listdir from os.path import isfile, join files_list = [f for f in listdir(‘/home/students’) if isfile(join(‘/home/students’, f))] print(files_list); Sample Output: [‘3ff44d80-2423-11e7-807b-bd9de91b1602.py’, ‘f5272c90-2423-11e7-807b-bd9de91b1602.py’, ‘test.txt’, ‘f34e26d0-2 …

Python Example – Write a Python program to parse a string to Float or Integer

Hits: 2  (Python Example for Citizen Data Scientist & Business Analyst)   Write a Python program to parse a string to Float or Integer. Sample Solution Python Code: n = “246.2458” print(float(n)) print(int(float(n))) Sample Output: 246.2458 246   Python Example – Write a Python program to parse a string to Float or Integer Free Machine …

Python Example – Write a Python program to find out the number of CPUs using

Hits: 2  (Python Example for Citizen Data Scientist & Business Analyst)   Write a Python program to find out the number of CPUs using. Sample Solution Python Code: import multiprocessing print(multiprocessing.cpu_count()) Sample Output: 4   Python Example – Write a Python program to find out the number of CPUs using Free Machine Learning & Data …

Python Example – Get the path and name of the file that is currently executing

Hits: 2  (Python Example for Citizen Data Scientist & Business Analyst)   Write a python program to get the path and name of the file that is currently executing. Sample Solution Python Code: import os print(“Current File Name : “,os.path.realpath(__file__)) Sample Output: Current File Name : /home/students/fb6e28e0-2425-11e7-807b-bd9de91b1602.py   Get the path and name of the …

Python Example – How to Locate Python site-packages

Hits: 0  (Python Example for Citizen Data Scientist & Business Analyst)   Write a Python program to locate Python site-packages. Sample Solution Python Code: import site; print(site.getsitepackages()) Sample Output: [‘/usr/local/lib/python3.5/dist-packages’, ‘/usr/lib/python3/dist-packages’, ‘/usr/lib/python3.5/dist-packages ‘]   Python Example – How to Locate Python site-packages Free Machine Learning & Data Science Coding Tutorials in Python & R for …