VS Code examples



examples/functions/fibonacci.py
def fib(n):
    if int(n) != n or n <= 0:
        raise ValueError("Bad parameter")

    if n == 1:
        return 1
    if n == 2:
        return 1
    return fib(n-1) + fib(n-2)

print(3, fib(3))    # 2
print(30, fib(30))  # 832040

fib(0.5)

examples/basics/rectangle_argv.py
import sys

def main():
    if len(sys.argv) != 3:
        exit("Needs 2 arguments:  width length")

    width  = int( sys.argv[1] )
    length = int( sys.argv[2] )

    

    if length <= 0:
        exit("length is not positive")

    if width <= 0:
        exit("width is not positive")

    area = length * width
    print("The area is ",  area)

main()

examples/vscode/mylib.py
import random

def count():
    for x in range(1000):
        v = random.choice("abcd")
        print(x)
        print(v)

def add(x, y):
    return x + y

def multiply(x, y):
    return x * y


def calc(x, y, z):
    if z == "+":
        return x + y
    if z == "*":
        return x * y
    if z == "-":
        return x - y
    if z == "/":
        return x / y
    raise Exception(f"Unknown operator {z}")

examples/vscode/use_mylib.py
import mylib

mylib.count()
print(mylib.calc( 2, 3, "+"))
print(mylib.calc( 2, 3, "*"))