<jupyter_start><jupyter_text>MethodsWe've already seen a few example of methods when learning about Object and Data Structure Types in Python. Methods are essentially functions built into objects.Methods perform specific actions on an object and can also take arguments, just like a function.Methods are in the form:object.method(arg1,arg2,etc...<jupyter_code>L=[1,2,3,4,8,9]
L.append(2)
L
L.count(2)
help(L.count)
help(L.remove)
L.remove(2)
L
L.remove(10)<jupyter_output><empty_output><jupyter_text>FunctionsFunctions will be one of our main building blocks when we construct larger and larger amounts of code to solve problems.A function is a useful device that groups together a set of statements so they can be run more than once. They can also let us specify parameters that can serve as inputs to the functions.**Parameter**A parameter is the variable listed inside the parentheses into the function definition.**Argument**An argument is value that are sent to the function when it is called. **Type 01**<jupyter_code>def show(L):
  print(L)
a=100
show(a)<jupyter_output>100<jupyter_text>Type 02<jupyter_code>def say():
  print("Hello World\n")
say()<jupyter_output>Hello World<jupyter_text>**Type 03**<jupyter_code>def sum(n1,n2):
  return n1+n2
b=300
c=120
sum(b,c)<jupyter_output><empty_output><jupyter_text>**Type 04**<jupyter_code>def divisor(a):
  for i in range(1,a+1):
    if(a%i==0):
      print(i)
  else:
    print("All divisors are printed\n")
q=30
divisor(q)<jupyter_output>1
2
3
5
6
10
15
30
All divisors are printed<jupyter_text>**Type 05**<jupyter_code>def check(s):
  p=s.split()
  return p[0][0]==p[1][0]
z="Cook chicken"
check(z)
def check_01(s):
  p=s.split()
  return p[0][0].upper()==p[1][0].upper()
check_01(z)<jupyter_output><empty_output><jupyter_text>**Type 06**<jupyter_code>def Three_Times(L):
  sum=0
  for i in L:
    print(i*3)
    sum=sum+i
  return sum
l=[1,2,3,4]
Three_Times(l)
print("SUM: ",Three_Times(l))<jupyter_output>3
6
9
12
SUM:  10<jupyter_text>**Type 07**<jupyter_code>def sum_of_values(d):
  sum=0
  for i in d.values():
    sum=sum+i
  return sum
D={1:1,2:2,3:3}
sum_of_values(D)<jupyter_output><empty_output><jupyter_text>**Type 08**<jupyter_code>def print_big(letter):
    patterns = {1:'  *  ',2:' * * ',3:'*   *',4:'*****',5:'**** ',6:'   * ',7:' *   ',8:'*   * ',9:'*    '}
    alphabet = {'A':[1,2,4,3,3],'B':[5,3,5,3,5],'C':[4,9,9,9,4],'D':[5,3,3,3,5],'E':[4,9,4,9,4]}
    for pattern in alphabet[letter.upper()]:
        print(patterns[pattern])
print_big('E')<jupyter_output>*****
*    
*****
*    
*****<jupyter_text>**Type 09**<jupyter_code>def ave(a,b,c,d):
  return (a+b+c+d)/4
a=12
b=2
c=6
d=10
ave(a,b,c,d)
ave(a,b,c)
def ave(a,b,c,d=20):
  return (a+b+c+d)/4
ave(a,b,c)<jupyter_output><empty_output><jupyter_text>**Type 10**<jupyter_code>def person_info(name,age,profession):
  print("My name is {}.I am {} years old.I am a {}.".format(name,age,profession))
N="<NAME>"
a=53
p="Music Director"
person_info(N,a,p)
person_info(a,p,N)
person_info(name=N,profession=p,age=a)<jupyter_output>My name is <NAME>.I am 53 years old.I am a Music Director.<jupyter_text>**Type 11**<jupyter_code>def s(a,b):
  return a+b
def m(a,b):
  return a*b
def both_s_m(a,b):
  sum=s(a,b)
  mul=m(a,b)
  return sum,mul

both_s_m(2,3)<jupyter_output><empty_output><jupyter_text>*argsThe special syntax *args in function definitions in python is used to pass a variable number of arguments to a function. It is used to pass a non-keyworded, variable-length argument list.The syntax is to use the symbol * to take in a variable number of arguments; by convention, it is often used with the word args.What *args allows you to do is take in more arguments than the number of formal arguments that you previously defined. With *args, any number of extra arguments can be tacked on to your current formal parameters. **Example 01**<jupyter_code>def average(*args):
  sum=0
  for i in args:
    sum=sum+i
  return sum/len(args)
average(1,2,3,4,5,6,8)<jupyter_output><empty_output><jupyter_text>**Example 02**<jupyter_code>def print_statement(*t):
  for i in t:
    print(i)
print_statement("Hi Everyone","How are you all?","I hope everyone is alright.")<jupyter_output>Hi Everyone
How are you all?
I hope everyone is alright.<jupyter_text>**Example 03**<jupyter_code>def args_with_one_argument(a1,*args):
  print("The argument:",a1)
  for i in args:
    print(i)
args_with_one_argument(200,2,3,4,90)<jupyter_output>The argument: 200
2
3
4
90<jupyter_text>**kwargsThe special syntax **kwargs in function definitions in python is used to pass a keyworded, variable-length argument list.We use the name kwargs with the double star. The reason is because the double star allows us to pass through keyword arguments (and any number of them).A keyword argument is where you provide a name to the variable as you pass it into the function.One can think of the kwargs as being a dictionary that maps each keyword to the value that we pass alongside it. That is why when we iterate over the kwargs there doesn’t seem to be any order in which they were printed out. **Example 01**<jupyter_code>def print_with_key(**kwargs):
  for value in kwargs.values():
    print(value)


print_with_key(d1="1",d2=9,d3=9999,d4="HI EVERYONE")<jupyter_output>1
9
9999
HI EVERYONE<jupyter_text>**Example 02**<jupyter_code>def print_with_key_01(**kwargs):
  for key in kwargs:
    print(kwargs[key])
    
print_with_key_01(d1="1",d2=9,d3=9999,d4="HI EVERYONE")<jupyter_output>1
9
9999
HI EVERYONE<jupyter_text>**Example 03**<jupyter_code>def kwargs_sum(**kwargs):
  sum=0
  for i in kwargs:
    sum=sum+kwargs[i]
  return sum/len(kwargs)
kwargs_sum(a=22,b=88,c=90)<jupyter_output><empty_output><jupyter_text>**Example 04**<jupyter_code>def Week_Dairy(name,*savings,**extra_activities):
  sum=0
  print("My name is ",name)
  for i in savings:
    sum=sum+i;
  print("My one week saving : ",sum)
  for key,value in extra_activities.items():
    print(key," : ",value)

Week_Dairy("Mr.R",100,50,60,30,80,60,20,Saturday="Music",Sunday="Poetry",Monday="Debate",Tuesday="Calliography",Wednesday="Dancing",Thrusday="Movie",Friday="Cooking")<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.14584346135148274, 'fraction_numerical': 0.04018797601685302, 'mean_word_length': 6.069873997709049, 'pattern_counts': {'":': 0, '<': 69, '<?xml version=': 0, '>': 69, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '19', 'dir': 'jupyter-structured-clean-dedup', 'id': '453577', 'license': 'Unlicense', 'n_tokens_mistral': 2486, 'n_tokens_neox': 2348, 'n_words': 614, 'path': 'Resources/Class Lectures/Class_06(Functions).ipynb', 'repo_name': 'JU-Computer-Club/Python-AnybodyCanCode', 'size': '6179'}