示例#1
0
# lambda -
# a single line function that takes 1 or more arguments
# and does a single line expression returning a value
# can be called within another function

from samwise import pb


def my_func(n):
    return lambda a, b: (a + b) * n


def my_func_add(n):
    return lambda a, b: (a + b) + n


f_g = lambda x, y, z: (x + y + z) * 5

a = f_g(2, 3, 4)

pb(a)

my_doubler = my_func(2)
my_tripler = my_func(3)
add_um_plus3 = my_func_add(3)

pb("5 & 100", "input")
pb(my_doubler(5, 100), "+*2")
pb(my_tripler(5, 100), "+*3")
pb(add_um_plus3(5, 100), "++3")
示例#2
0
from samwise import pb
# del values from list

p = ["j", "x", "n", "k", "e", "x"]
pb("", "Original p")
print(p)

pb()

pb("", "del from p: del(p[3:5])")
del (p[3:5])
print(p)
示例#3
0
# Define and use a class
from samwise import pb


class Person:
    def __init__(self, name):
        self.name = name


m = Person('Michael')

pb(f"{m.name}",
   "Define Class, instantiate it & Use instantiated obj.name attribute")
示例#4
0
from samwise import pb
# del values from list

p = [0, 5, 15, 5, 10, 8]
pb("", "Original p")
print(p)

pb()

pb("", "Sorted: sorted(p, reverse=False")

print(sorted(p, reverse=False))
示例#5
0
# Dictionary List Stuff
from samwise import pb

l_fruit = {'apple': 1, 'banana': 2, 'coconut': 3}

pb(f"{l_fruit}", "Original")
l_fruit['pear'] = "x"

pb(f"{l_fruit}", "After adding an additional item")

pb(l_fruit["pear"], "get back value of pear")

l_fruit["pear"] = 40

pb(f"{l_fruit}", "After changing pear value")
示例#6
0
# String Evaluation
from samwise import pb
s_var = "James Bond"
pb(s_var,"Original text")
pb(f"{s_var[2::-1]}","Start at 3rd character and go backwards to the end: var[2::-1]")
示例#7
0
from samwise import pb
import numpy as np
x = np.array([14, 21, 24, 7, 99])
y = np.array([12, 6, 23, 29, 77])  # a list of 5 items
z = np.array(
    [x,
     y])  # each item is a row with a value of list, so 2 dimensional arrays.
pb(z.shape, "Show the shape for the numpy array (rows, cols): z.shape")
pb("", "Array z")
print(z)

pb("", "Array arr")
arr = np.array([[1, 2, 3, 4, 5], [11, 12, 13, 14, 15]])

print(arr)
pb("", "first row [0]")
print(arr[0])
pb("", "first row [0], 2nd & 3rd item [1:3]")
print(arr[0][1:3])

z = np.array([[1, 2, 3, 4, 5, 55], [6, 7, 8, 9, 10, 100],
              [11, 12, 13, 14, 15, 150], [16, 17, 18, 19, 20, 200],
              [21, 22, 23, 24, 25, 250]])

pb("", " numpy array")
print(z)
pb("", " numpy array from row 1:4(-1) get columns 1:5(-1)")
# returns a section of a 2 dim array,
# starting with 2nd row, ending with 4th row [1:4] and
# staring with 3nd column, ending with 5th column [2:5]
zz = z[1:4, 2:5]
示例#8
0
# Help on built-in function dir in module builtins
from samwise import pb

x = None
pb(type(x))





示例#9
0
from samwise import pb
import numpy as np
# calc the means of a column

costs = np.column_stack(([2, 1, 2, 1], [4, 6, 5, 5]))

mean_costs = np.mean(
    costs[:, 1]
)  # Every row, 2nd column get the means - add then all up and divide by all

print(costs)
pb(mean_costs, "mean_costs")
示例#10
0
# if single line
from samwise import pb
s_input = input("please give 2 numbers sepearted by a space: ")
n_space = s_input.index(" ")

n_1 = float(s_input[0:n_space])
n_2 = float(s_input[n_space:])
# print(f"{n_1}:{n_2}")
s_oper = "*"
if n_1 * n_2 > 1000: s_oper = "+"

# value set conditionally
n_val = n_1 * n_2 if n_1 * n_2 <= 1000 else n_1 + n_2

pb(f"{n_1} {s_oper}\n{n_2} =\n----------\n{n_val}",
   "If product of numbers is <= 1000, add them")

l_nums = []
s_input = "1"
while s_input not in ["0", ""]:
    s_input = input("please give numbers to average (0/spc-ends): ")
    if s_input not in ["0", ""]: l_nums.append(float(s_input))

f_tot = 0
if len(l_nums) > 0:
    for f_num in l_nums:
        f_tot = f_tot + f_num

    print(
        f"\n\n{l_nums}\n\nThe Average of {f_tot} / {len(l_nums)} = {f_tot / len(l_nums)}"
    )
示例#11
0
from samwise import pb
# create list from criteria

l_i = [5, 6, 7, 8, 9, 5, 6, 7, 8, 9]
pb("", "Original p")
print(l_i)

pb("", "Evaluate Numbers? and build a new list based on criteria")

l_y = [i for i in range(10) if i > 3]

print(l_y)

pb("", "Evaluate List and build a new list based on criteria")

l_z1 = [j for j in l_i if j >= 7]

print(l_z1)

pb("", "Evaluate List and build a new list by augmenting the list")

l_o = ['hello', 'world']

l_u = [u.upper().replace("O", "?") for u in l_o]
l_z2 = [j + 1 for j in l_i if j >= 7]

print(f"Orig:                           {l_o}")
print(f'upper() and changed "O" to "?": {l_u}')
print(f"Num List: {l_i}")
print(f"Num List Evaluated >= 7:               {l_z1}")
print(f"Num List Evaluated >= 7 & Altered + 1: {l_z2}")
示例#12
0
# create a class with an attribute, instantiate the object initializing the attribute and reference the attribute
from samwise import pb


class Building:

    def __init__(self, number):
        self.number = number


o_b = Building(235)

pb(o_b.number, "Instantiate an object, and then use attribute")
示例#13
0
from samwise import pb
# remove value from list

x = [9, 2, 4, 7, 8]
print(x)
pb(x, "Original x")

pb()

x.remove(9)
print(x)
pb(x, "Removed x")


示例#14
0
from samwise import pb
# reverse values from list

y = [1, 3, 5, 11]
pb("", "Original y")
print(y)

pb()

pb("", "reversed: y.reverse()")
y.reverse()

print(y)