def employee(emp_num, name, emprole):
print(emp_num, name, emprole)
employee(1, 'John', 'Manager')
employee(2, 'Tim', 'CFO')
print('')
employee(1, 'Phil', 'Manager')
print('')
def student(firstname, lastname='Mark', standard='Fifth'):
print(firstname, lastname, 'studies in', standard, 'Standard')
# 1 positional argument
student('John')
# 3 positional arguments
student('Bill', 'Gates', 'Seventh')
student('Tim', 'Cook', 'Seventh')
# 2 positional arguments
student('Bill', 'Gates')
student('Bill', 'Cook')
print('')
# A required argument is required
def greeting(phrase):
print(phrase)
return;
print('Not passing required argument')
greeting('hello')
print('')
1 John Manager
2 Tim CFO
1 Phil Manager
John Mark studies in Fifth Standard
Bill Gates studies in Seventh Standard
Tim Cook studies in Seventh Standard
Bill Gates studies in Fifth Standard
Bill Cook studies in Fifth Standard
Not passing required argument
hello
Postional.py
def log(sequence, message, *values):
alist = list(values)
for element in alist:
try:
element = int(element)
print('this is not a color, this is a number')
print('group', sequence, 'number', element)
print('')
except ValueError:
print('this is a color')
print('group', sequence, message, element)
print('')
pass
def my_generator():
for i in range(10):
yield i
def my_func(*args):
print('')
print('my_generator counts 0 to 10')
print(args)
log(5)
log(0, 'Favorite colors')
log(1, 'Favorite colors', 7, 33, 16)
log(2, 'Favorite colors', 'red', 'yellow', 'blue')
log(3, 'Favorite colors', 'red', -1, 'blue')
log(4, 'Favorite colors', 'red')
# log(5) will return a Typeerror because it is missing argument message
seq_num = 5
retry = 1
while retry == 1:
message_input = input('what is the message? ')
if message_input == '':
message_input = input('you need to provide the message? ')
values_input = input(' what are the values separated by ,? ')
values_list = values_input.split(",")
for element in values_list:
if not element:
print('you didn\'t give any values')
print('')
else:
try:
element = int(element)
print('this is not a color, this is a numbers')
print('group', seq_num, 'number', element)
print('')
except ValueError:
print('this is a color')
print('group', seq_num, message_input, element)
print('')
pass
try_again = input('Do you wish to try again? Y or y for Yes or any key for no')
if try_again == 'Y' or try_again == 'y':
seq_num += 1
print('')
else:
retry = 0
print('')
# my generator count 0 to 10 and my_func prints it
it = my_generator()
my_func(*it)
Arguments.py
#!/usr/bin/env python3 from my_functions import * math_func() math_func(5) math_func(5, '*', 4) math_func(6, '+', 4) math_func(10, '/', 2) math_func(7, '/', 3) math_func(8, '-', 4) math_func(400, 'sqroot') # even adding the secondnum will not break it because it isn't used math_func(5, 'sqroot', 1) math_func(200, 'powerof', 2) math_func(200, 'powerof', 3) # raising a number to .5 is same as square root math_func(16, 'powerof', 0.5) # cubed root of a number math_func(27, 'powerof', (1 / 3)) # fourth root of a number math_func(81, 'powerof', (1 / 4)) math_func(339019928, 'powerof', .05) math_func(10, 'fact') math_func(4, 'circarea') math_func(4, 'circcircum')
Argument_relops.py
#!/usr/bin/env python3 from my_functions import * list = [] list = [[17, '+', 9], [17, '-', 9], [17, '*', 9], [17, '/', 9], [17, '//', 9], [17, 'operator.add', 9], [17, 'operator.sub', 9], [17, 'operator.mul', 9], [17, 'operator.truediv', 9], [17, 'operator.floordiv', 9], [17, '>', 9], [17, '<', 9], [17, '>=', 9], [17, '<=', 9], [17, '==', 9], [17, '!=', 9], [17, 'operator.gt', 9], [17, 'operator.lt', 9], [17, 'operator.ge', 9], [17, 'operator.le', 9], [17, 'operator.eq', 9], [17, 'operator.ne', 9] ] length = len(list) print('Testing MDAS') x = 0 while x < length: if list[x][1] == 'operator.add' or list[x][1] == 'operator.add': print('') if list[x][1] == '>': print('') print('Testing truth or false') print(list[x][0], list[x][1], list[x][2], '=', get_truth(list[x][0], list[x][1], list[x][2])) x += 1
ArgumentsWithInputs.py
#!/usr/bin/env python3 from my_functions import * print('') print('testing input get_truth(numberA, operatorC, numberB ') numberA = float(input('First Number: ')) numberB = float(input('second number: ')) operatorC = input('Operator: ') if numberA - int(numberA) == 0: numberA = int(numberA) if numberB - int(numberB) == 0: numberB = int(numberB) print(numberA, operatorC, numberB, '=', round(get_truth(numberA, operatorC, numberB), 4))
ArgumentsWithoutQuotes.py
#!/usr/bin/env python3 from my_functions import * print('') print('Operator without quotes around it') print('17.1 operator.add 9.1 is', round(get_truthWQ(17.1, operator.add, 9.1), 4)) print('17.1 operator.sub 9.1 is', round(get_truthWQ(17.1, operator.sub, 9.1), 4)) print('17.1 operator.mul 9.1 is', round(get_truthWQ(17.1, operator.mul, 9.1), 4)) print('17.1 operator.truediv 9.1 is', round(get_truthWQ(17.1, operator.truediv, 9.1), 4)) print('17.1 operator.floordiv 9.1 is', round(get_truthWQ(17.1, operator.floordiv, 9.1), 4)) print('') print('Fractions') print('17+1/3 operator.add 9+2/3 is', round(get_truthWQ(17+1/3, operator.add, 9+2/3), 4)) print('17+1/3 operator.sub 9+2/3 is', round(get_truthWQ(17+1/3, operator.sub, 9+2/3), 4)) print('17+1/3 operator.mul 9+2/3 is', round(get_truthWQ(17+1/3, operator.mul, 9+2/3), 4)) print('17+1/3 operator.truediv 9+2/3 is', round(get_truthWQ(17+1/3, operator.truediv, 9+2/3), 4)) print('17+1/3 operator.floordiv 9+2/3 is', round(get_truthWQ(17+1/3, operator.floordiv, 9+2/3), 4)) print('') print('power of') print('17.1 operator.gt 0.0 is', round(get_truthWQ(17.1, pow, 0.0), 4)) print('17.1 operator.gt 2 is', round(get_truthWQ(17.1, pow, 2), 4)) print('17.1 operator.gt 3 is', round(get_truthWQ(17.1, pow, 3), 4)) print('square root') print('17.1 operator.gt 1/2 is', round(get_truthWQ(17.1, pow, 1/2), 4)) print('cube root') print('17.1 operator.gt 1/3 is', round(get_truthWQ(17.1, pow, 1/3), 4)) print('') print('true of false') print('1.0 operator.gt 0.0 is', get_truthWQ(1.0, operator.gt, 0.0)) print('1.0 operator.lt 0.0 is', get_truthWQ(1.0, operator.lt, 0.0)) print('1.0 operator.ge 0.0 is', get_truthWQ(1.0, operator.ge, 0.0)) print('1.0 operator.le 0.0 is', get_truthWQ(1.0, operator.le, 0.0)) print('1.0 operator.eq 0.0 is', get_truthWQ(1.0, operator.eq, 0.0)) print('1.0 operator.ne 0.0 is', get_truthWQ(1.0, operator.ne, 0.0)) print('') print('Fractions: true of false') print('1/2 operator.gt 1/3 is', get_truthWQ(1/2, operator.gt, 1/3)) print('1/2 operator.lt 1/3 is', get_truthWQ(1/2, operator.lt,1/3)) print('1/2 operator.ge 1/3 is', get_truthWQ(1/2, operator.ge, 1/3)) print('1/2 operator.le 1/3 is', get_truthWQ(1/2, operator.le, 1/3)) print('1/2 operator.eq 1/3 is', get_truthWQ(1/2, operator.eq, 1/3)) print('1/2 operator.ne 1/3 is', get_truthWQ(1/2, operator.ne, 1/3))
MathClassPrint.py
#! /usr/bin/env python3 import math import operator class mathforms: a = 0 result = b = c = 0 def choice(self, op: object = 0, first: object = 0, second: object = 0) -> object: rel_ops = { '+': operator.add, '-': operator.sub, '*': operator.mul, '/': operator.truediv, '//': operator.floordiv, 'operator.add': operator.add, 'operator.sub': operator.sub, 'operator.mul': operator.mul, 'operator.truediv': operator.truediv, 'operator.floordiv': operator.floordiv, '>': operator.gt, '<': operator.lt, '>=': operator.ge, '<=': operator.le, '==': operator.eq, '!=': operator.ne, 'operator.gt': operator.gt, 'operator.lt': operator.lt, 'operator.ge': operator.ge, 'operator.le': operator.le, 'operator.eq': operator.eq, 'operator.ne': operator.ne } return rel_ops[op](first, second); class print_out(): def __init__(self): print('print out', choice(op, first, second)) def main(): self = 0 opermain = 'operator.add' firstnum = 17 secondnum = 9 print('main', opermain, firstnum, secondnum) choice('operator.add', 17, 9) print_out() if __name__ == "__main__": main()
MyFunctions.py
#! /usr/bin/env python3
import math
import operator
class mathforms:
a = 0
result = b = c = 0
def choice(self, op: object = 0, first: object = 0, second: object = 0) -> object:
rel_ops = {
'+': operator.add,
'-': operator.sub,
'*': operator.mul,
'/': operator.truediv,
'//': operator.floordiv,
'operator.add': operator.add,
'operator.sub': operator.sub,
'operator.mul': operator.mul,
'operator.truediv': operator.truediv,
'operator.floordiv': operator.floordiv,
'>': operator.gt,
'<': operator.lt,
'>=': operator.ge,
'<=': operator.le,
'==': operator.eq,
'!=': operator.ne,
'operator.gt': operator.gt,
'operator.lt': operator.lt,
'operator.ge': operator.ge,
'operator.le': operator.le,
'operator.eq': operator.eq,
'operator.ne': operator.ne
}
return rel_ops[op](first, second);
class print_out():
def __init__(self):
print('print out', choice(op, first, second))
def main():
self = 0
opermain = 'operator.add'
firstnum = 17
secondnum = 9
print('main', opermain, firstnum, secondnum)
choice('operator.add', 17, 9)
print_out()
if __name__ == "__main__":
main()michael@wpmm22:/var/www/wp.scsiraidguru.com/public_html/Python/Arguments$
michael@wpmm22:/var/www/wp.scsiraidguru.com/public_html/Python/Arguments$ ls
arguments.py argumentsWithoutQuotes.py positional.py
arguments_relops.py math_class_print.py
argumentsWithInputs.py my_functions.py
michael@wpmm22:/var/www/wp.scsiraidguru.com/public_html/Python/Arguments$ sudo cat my_functions.py
#!/usr/bin/python3
import math
import operator
def get_truth(firstnum, op, secondnum):
rel_ops = {
'+': operator.add,
'-': operator.sub,
'*': operator.mul,
'/': operator.truediv,
'//': operator.floordiv,
'operator.add': operator.add,
'operator.sub': operator.sub,
'operator.mul': operator.mul,
'operator.truediv': operator.truediv,
'operator.floordiv': operator.floordiv,
'>': operator.gt,
'<': operator.lt,
'>=': operator.ge,
'<=': operator.le,
'==': operator.eq,
'!=': operator.ne,
'operator.gt': operator.gt,
'operator.lt': operator.lt,
'operator.ge': operator.ge,
'operator.le': operator.le,
'operator.eq': operator.eq,
'operator.ne': operator.ne
}
return rel_ops[op](firstnum, secondnum);
def get_truthWQ(firstnumWQ=0, op=0, secondnumWQ=0):
return op(firstnumWQ, secondnumWQ);
def math_func(firstnum=0, operand=0, secondnum=0):
if operand == '*':
print(firstnum, '*', secondnum, '=', firstnum * secondnum)
return;
elif operand == '/':
result = firstnum / secondnum
if result - int(result) == 0:
print(firstnum, '/', secondnum, '=', int(result))
else:
print(firstnum, '/', secondnum, '=', "{:.4f}".format(result))
return;
elif operand == '+':
result = firstnum + secondnum
print(firstnum, '+', secondnum, '=', result)
return;
elif operand == '-':
result = firstnum - secondnum
print(firstnum, '-', secondnum, '=', result)
return;
elif operand == 'sqroot':
result = math.sqrt(firstnum)
if result - int(result) == 0:
print('square root of', firstnum, '=', int(result))
else:
print('square root of', firstnum, '=', "{:.4f}".format(result))
return;
elif operand == 'powerof':
from fractions import Fraction
result = math.pow(firstnum, secondnum)
# if the result returns an integer, it will only print the integer, else it will print 2 decimal places
# This also prints the fractional value not the decimal
if result - int(result) == 0:
print(firstnum, 'raised to the power of', Fraction(secondnum).limit_denominator(100), '=', int(result)
)
else:
print(firstnum, 'raised to the power of', Fraction(secondnum).limit_denominator(100),
'=', "{:.2f}".format(result))
return;
elif operand == 'fact':
result = math.factorial(firstnum)
print(firstnum, '! is =', result)
return;
elif operand == 'circarea':
result = math.pi * firstnum ** 2
print('area of circle with radius of', firstnum, 'is', "{:.4f}".format(result))
return;
elif operand == 'circcircum':
result = math.pi * 2 + firstnum
print('circumference of circle with radius of', firstnum, 'is', "{:.4f}".format(result))
return;
elif firstnum == operand == secondnum == 0:
print('you forgot the all the inputs')
return;
elif operand == secondnum == 0:
print('you forgot the operand')
return;