## convert from Centimeters to Yards, Feet and Inches

inCm = float(input(“How many centimeters do you want to convert? “)) yards = inCm // 91.44 left = (inCm / 91.44) – yards inCm = left * 91.44 feet = inCm // 30.48 left = (inCm / 30.48) – feet inCm = left * 30.48 inches = inCm / 2.54 print(“This is”,int(yards),”yards,”,int(feet),”feet,”,inches,”inches”)

## convert from lbs to gallons and ounces

lbs = float(input(“Plase enter the lbs of water treated: “)) ounces = lbs * 16 gallons = int(ounces / 128) ounces = ounces – gallons * 128 print(“That’s”,gallons,”gallons and”,ounces,”ounces of treated waste water.”)

## Total File Size

import os def main(): path = input(“Enter a directory or a file: “).strip() try: print(getSize(path), “bytes”) except: print(“Directory or file does not exist”) def getSize(path): size = 0 if not os.path.isfile(path): lst = os.listdir(path) for subdirectory in lst: size += getSize(path + “\\” + subdirectory) else: size += os.path.getsize(path) return size main()

## Recursive Binary Search

import os def main(): path = input(“Enter a directory or a file: “).strip() try: print(getSize(path), “bytes”) except: print(“Directory or file does not exist”) def getSize(path): size = 0 if not os.path.isfile(path): lst = os.listdir(path) for subdirectory in lst: size += getSize(path + “\\” + subdirectory) else: size += os.path.getsize(path) return size main()

## Fibonacci index

def main(): index = eval(input(“Enter an index for a fibonacci number: “)) print(“The Fibonacci number at index”, index, “is”, fib(index)) def fib(index): if index == 0: # Base case return 0 elif index == 1: # Base case return 1 else: # Reduction and recursive calls return fib(index – 1) + fib(index – 2) main() … Read moreFibonacci index

## Factorial Of

def main(): n = eval(input(“Enter a nonnegative integer: “)) print(“Factorial of “, n, “is”, factorial(n)) # Return the factorial for the specified number def factorial(n): if n == 0: return 1 else: return n * factorial(n – 1) # recursive call main() # Call the main function

## Dumping and Loading Objects Using Pickle

import pickle def main(): outfile = open(“pickle.dat”, “wb”) pickle.dump(45, outfile) pickle.dump(56.6, outfile) pickle.dump(“Programming is fun”, outfile) pickle.dump([1,2,3,4], outfile) outfile.close() infile = open(“pickle.dat”, “rb”) print(pickle.load(infile)) print(pickle.load(infile)) print(pickle.load(infile)) print(pickle.load(infile)) infile.close() main() detect end of file import pickle def main(): outfile = open(“numbers.dat”, “wb”) data = eval(input(“Enter an integer (the input exits of the input is 0):”)) while … Read moreDumping and Loading Objects Using Pickle

## Exception Example

def main(): try: number1, number2 = eval(input(“Enter two numbers, seperate by a comma: “)) result = number1 / number2 print(“Result is”, result) except ZeroDivisionError: print(“Division by zero!”) except SyntaxError: print(“A comma may be missing in the input”) except: print(“Something wrong in the input”) else: print(“No exceptions”) finally: print(“The finally clause is executed”) main()

## Write Read Numbers

from random import randint def main(): outfile = open(“Numbers.txt”, “w”) for i in range(10): outfile.write(str(randint(0, 9))+ ” “) outfile.close() infile = open(“Numbers.txt”, “r”) s = infile.read() numbers = [eval(x) for x in s.split()] for number in numbers: print(number, end= ” “) infile.close() main()