Tag Archives: Python tutorials

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

Python Exercise – Sum of three given integers if two values are equal sum will be zero

  (Python Example for Citizen Data Scientist & Business Analyst)   Write a Python program to sum of three given integers. However, if two values are equal sum will be zero.   Sample Solution Python Code: def sum(x, y, z): if x == y or y == z or x==z: sum = 0 else: sum …

Python Exercise – Get the least common multiple (LCM) of two positive integers

  (Python Example for Citizen Data Scientist & Business Analyst)   Write a Python program to get the least common multiple (LCM) of two positive integers.   Sample Solution:- Python Code: def lcm(x, y): if x > y: z = x else: z = y while(True): if((z % x == 0) and (z % y …

Python Exercise – Compute the greatest common divisor (GCD) of two positive integers

  (Python Example for Citizen Data Scientist & Business Analyst)   Write a Python program to compute the greatest common divisor (GCD) of two positive integers.   Sample Solution Python Code: def gcd(x, y): gcd = 1 if x % y == 0: return y for k in range(int(y / 2), 0, -1): if x …