Day: February 8, 2021

Python Example – Get OS name, platform and release information

  (Python Example for Citizen Data Scientist & Business Analyst)   Write a Python program to get OS name, platform and release information. Sample Solution Python Code: import platform import os print(os.name) print(platform.system()) print(platform.release()) Sample Output: posix Linux 4.4.0-47-generic   Python Example – Get OS name, platform and release information Free Machine Learning & Data …

Python Example – Check whether Python shell is executing in 32bit or 64bit mode on OS

  (Python Example for Citizen Data Scientist & Business Analyst)   Write a Python program to determine whether a Python shell is executing in 32bit or 64bit mode on OS?   Sample Solution Python Code: # For 32 bit it will return 32 and for 64 bit it will return 64 import struct print(struct.calcsize(“P”) * …

Python Example – Check whether a file exists in current path

  (Python Example for Citizen Data Scientist & Business Analyst)   Write a Python program to check whether a file exists. Sample Solution Python Code: import os.path open(‘abc.txt’, ‘w’) print(os.path.isfile(‘abc.txt’)) Sample Output: True   Python Example – Check whether a file exists in current path Free Machine Learning & Data Science Coding Tutorials in Python …

Python Example – Compute the distance between two points

  (Python Example for Citizen Data Scientist & Business Analyst)   Write a Python program to compute the distance between the points (x1, y1) and (x2, y2).   Sample Solution Python Code: import math p1 = [4, 0] p2 = [6, 6] distance = math.sqrt( ((p1[0]-p2[0])**2)+((p1[1]-p2[1])**2) ) print(distance) Sample Output: 6.324555320336759   Compute the distance …

Python Exercise – Future value of a specified principal amount, rate of interest, and a number of years

  (Python Example for Citizen Data Scientist & Business Analyst)   Write a Python program to compute the future value of a specified principal amount, rate of interest, and a number of years. Sample Solution Python Code: amt = 10000 int = 3.5 years = 7 future_value = amt*((1+(0.01*int)) ** years) print(round(future_value,2)) Sample Output: 12722.79 …

Python Exercise – Add two objects if both objects are an integer type

  (Python Example for Citizen Data Scientist & Business Analyst)   Write a Python program to add two objects if both objects are an integer type.   Sample Solution Python Code: def add_numbers(a, b): if not (isinstance(a, int) and isinstance(b, int)): raise TypeError(“Inputs must be integers”) return a + b print(add_numbers(10, 20)) Sample Output: 30 …

Python Exercise – Return true if the two given int values are equal or their sum or difference is 5

  (Python Example for Citizen Data Scientist & Business Analyst)   Write a Python program which will return true if the two given integer values are equal or their sum or difference is 5.   Sample Solution Python Code: def test_number5(x, y): if x == y or abs(x-y) == 5 or (x+y) == 5: return …

Python Exercise – Sum of two given integers

  (Python Example for Citizen Data Scientist & Business Analyst)   Write a Python program to sum of two given integers. However, if the sum is between 15 to 20 it will return 20.   Sample Solution: Python Code: def sum(x, y): sum = x + y if sum in range(15, 20): return 20 else: …