Week 4 - Using assignment operators, custom functions within functions, return values

Liaw Bei Le · April 16, 2021

  1. Assignment operators: count=count+1 can be written as count+=1 and so on. Same for *= and -= etc.
  2. Using custom functions within custom functions
  3. Returning more than 1 value - returns the values as items in a tuple.
    Need to create multiple variables (not just 1) for the returned values.
    Can create more than 1 variable for items in list, tuple, strings etc.

& dry run codes when there are bugs (to figure out what went wrong).


What I learnt:

1. Using += , *= , -= etc. to increment and decrement

2. Using custom functions within custom functions

  • Find length of list & multiple and sum of items in list without using len( ) or sum( ) inbuilt functions.
    Use f strings.
    a_list=[2,5,7,1]
    def length_of_list(list1):
      count=0
      for number in list1:
          count+=1
      return count
    def multiples(list1):
      times=1
      for number in list1:
          times*=number
      return times
    def sum_all(list1):
      summed=0
      for number in list1:
          summed+=number
      return summed
    length=length_of_list(a_list)
    multiple=multiples(a_list)
    summed=sum_all(a_list)
    print(f"The length, sum and product are {length}, {summed} and {multiple}.")
    

    - Continued: -
    For another list of numbers, use the above 3 functions created previously into one custom function and get the length, sum and product of the list.

    diff_list=[1, 2, 3, 4]
    def list_info(onelist):
    # don't need to put: "for numbers in onelist:"
      added=sum_all(onelist)
      multiplied=multiples(onelist)
      length=length_of_list(onelist)
      return added, multiplied, length
    list_information=list_info(diff_list)
    print(f"The sum, multiple and length of list is {list_information}.")
    
  • Find the length of a string without using len( ) function.
    Use f strings.
    string_1="This is a cat"
    def find_length(a_string):
      count=0
      for character in a_string:
          count+=1
      return count
    string_length=find_length(string_1)
    print(f"Length of string is {string_length}.")
    

    - Continued: -
    For another string, use the above function created previously into one custom function and get the length of the longest word in the string.
    Use max( ) inbuilt function.

    '''split the sentence into many strings of separate words'''
    '''find length of each word and append the length to a list'''
    '''use max( ) on the list to find max word length'''
    string_2="This is a dog"
    def get_longest_wordlength(onestring):
      newlist=onestring.split(' ')
      listoflengths=[]
      for word in newlist:
          wordlength=find_length(word)
          listoflengths.append(wordlength)
      return max(listoflengths)
    get_longest_wordlength(string_2)
    

3. Returning more than 1 value - returns the values as items in a tuple

We can create more than 1 variable for items in list, tuple, strings etc.

  • Find the length of longest word, length of the whole string & frequency of each character in the string in a dictionary.
    All above using 1 custom function
    mystring="the quick brown fox jumped over the lazy dog in singapore"
    # practise this! ***
    def mydictionary(onestring):
      d={}
      for character in onestring:
          if character in d:
              d[character]=d[character]+1
          else:
              d[character]=1
      return d
    def string_info(thestring):
      '''using custom fxn in above prev example'''
      length_of_word=get_longest_wordlength(thestring)
      '''using custom fxn in above prev example'''
      string_length=find_length(thestring)
      '''using custom fxn in code above here'''
      thedictionary=mydictionary(thestring)
      '''return 3 values at once'''
      return length_of_word, string_length, thedictionary
    '''create 3 variables to store the 3 returned values from the custom fxn'''
    length_of_word, string_length, thedictionary=string_info(mystring)
    print(f"The length of word is {length_of_word}, string length is {string_length}, and frequency of character is {thedictionary}.")
    

When you return more than 1 value, it gets gets returned as a tuple:

  • Practice example 1:
    countries=['India', 'Singapore']
    country1, country2=countries
    print(country1, country2)
    

    Output:

    India 
    Singapore
    
  • Practice example 2:
    letter1, letter2='12'
    print(letter1, letter2)
    

    Output:

    1 
    2
    

Thoughts

Custom functions are a bit tricky for me at the moment, especially when we have to insert them into other custom functions for specific tasks/ outputs. The difficult part is getting familiar with where to insert the custom function & figuring out the arguments needed for the custom functions.

In my next post, I will be doing more practices on custom functions and using them, so that I can familiarise and be more confident when using these custom functions.

Twitter, Facebook