Assumption University



Worksheet 6 ???????????????????????????????????????????? Python ?????????????????????????????????????????????????? ????????????????????????????????????????????????????????????? Python???????? (Function) ?????????????????????????????????????????????????????????? ?????? Python ?????????????????????????????????????????????????????? ??????????????????????????????????????????????????? ? ????????????????????? ?????????????????????????? ? ???????????????????? reuse ???? ??????????????????????????????????????? Python ??????????????????????????def function_name(args...): # statementsdef function_name(args...): # statements return value?????????????????????????????????? Python ????????????????def????????????????function_name???????????????????? ????????????( )??????????????????????????????????? ?????????????????????????????????????????????????????????????????? ???????????????????????? ????????????????????????????????????? ????????????????????????????????????????????????? Pythondef mypersonalinfo(): print('My name is Thanachai T.') print('Address: Neverland')def hello(name): print('Hello %s' % name)def count_vowel(str): vowel = 0 for c in str: if c in ('A', 'E', 'I', 'O', 'U', 'a', 'e', 'i', 'o' 'u'): vowel = vowel + 1 return voweldef area(width, height): c = width * height return c???????????????????????????? Python???????????????????????????????????????????????? ???????????????????????????????????????? ?????????????????????????????????????????????????????????????????? (arguments) ????????????????????????? (parameters) ????????????????????? ???????????????????????????????????????????????????????? ?????????????????????????????????????????????????????????????????????????? ???????????????????????????????????????? Python (?????????????????????)# calling functionsmypersonalinfo()hello('Danny')hello('Mateo')print('Vowel in string = %d' % count_vowel(''))print('Vowel in string = %d' % count_vowel('Python'))print('Area = %d' % area(8, 4))???????????My name is Thanachai T.Address: NeverlandHello DannyHello MateoVowel in string = 5Vowel in string = 1Area = 32def area(width, height): c = width * height return cresult = area(4, 5)print(‘The area is’, result)??????????????????? area( ) ?????????? (return) ????????????????????? result ??????????????????????????def area(width, height): c = width * height print(‘The area is’,c)area(4, 5)???????????????????????????????? ????????????????????????? 1) ?????????????????????????????????????? Body Mass Index (BMI) ???????????????????????????? height ??? weight ??????????????????????? calculate_BMI(height, weight) ??????????????????????????????????????? BMI ????????????? ??????????????????????? return ??? BMI ??????????????? result ???????????????? BMI ??????????? ?????????????def add(a, b): c = a + b return ca = int(input(‘Enter value a: ’))b = int(input(‘Enter value b: ’))result = add(a, b)print(‘The addition is’, result)???????????????????????????????????????????????????? ????????????? ???????????? Python code ???????????????????????????? ???????????????? add(a, b) ????????????????????????????????? result ????????????????????2) 2) ?????????????????????????????????????????????????????? ???????????????????????????? name ??? id ??????????????????????? show_info(name, id) ????????????????????????????????? Python code ??????????????????????????????????????? ???????????????? show_info(name, id) ????????????? ???????????????? split_num( ) ????????????????????????????? list ???????????????????????????????????????????????? ?????????????????????????????? list ???????????????????????????????????? ?????????for i in list_num: ?????? i ?????????????????????????? list ??????????????? ???????????????????????????????????????? ???????????????????????????? list ????????????????????? .append(i) ???????????????????????????????????????3) ????????????????? ??????????????????????????????????????????????????????? list ????????????????????????????????? 0 – 50 ???????????????? 50 ???????????????????????? ??????????????????????????????????????? ???? split_50( ) ??????????????????? Python code ???????????????????????? ? ?????????????????????????? ????????????????????????????????????? ???????????????????? list ????????????4) ??????????????????????????????????????????????????????? list ????????????????????????????????? 0 – 25, 26 – 50, 51 - 75 ??? 75 - 100 ???????????????????????? ??????????????????????????????????????? ???? split_quarter( ) ??????????????????? Python code ???????????????????????? ? ?????????????? (???????????????? 100) ?????????????????????????? ???????? inputs/outputs5) ?????????????????????????????? ??? ?? ??? ??? (??????? 4 ????????) ???????? Python code ??????????????????????????? (???????????????????????) ??????????????????????? ???????????????????? option ???????? ?????????????????????? input/output????????????????????? ??????????????????????????????????????????????? ????????????????????????????????????????????????????????? a1 ??? a2 ????????????????????????????????? list ????????????????????? a1 ??? a2 ???? 1000000 ????? list ????? 1000002 ???????????????????????????????????????? ?????????????????????????????? ???????????????????????????????????????????????????? infinity6) ???????????????????????? second_largest( ) ????????????????????????????? (??? float(‘-inf’) ????????????????????????????? – infinity?????????????????????????????????????????????????????????? list y ?????? list x7) ????????????????????????????? remove ?????????????????? list y ??????????? ?????? list x???????? inputs/outputs???????????????????????????????? list y ?????? 2 ??? 6 ?????? remove ?????? list x ................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download