Week 3 - Knowing more Python built-in functions

Liaw Bei Le · April 8, 2021

  1. range( )
  2. zip( )
  3. enumerate( )
  4. set( )
  5. dict( ) - when used with range/zip/enumerate( )
    • Iterators: (have next function), iterate, index

What I learnt:

1a. range(number#)

  • Returns numbers from 0 to number#-1
    for number in range(4): 
      print(number)
    

    Output:

    0
    1
    2
    3
    

1b. range(low#, high#)

  • Returns numbers from low# to high#-1
    for number in range(2,5): 
      print(number)
    

    Output:

    2
    3
    4
    

1c. range(low#, high#, step#)

  • Returns numbers in that range, from low to high.
  • Number sequence depends on if there is a step.
  • Range returns an iterator.
    # range(low, high, step)
    for number in range(3, 10, 2): 
      print(number)
    

    Output:

    3
    5
    7
    9
    

Practices

  • Create a dictionary of key value pairs of number : number to the power of 2 using range(0,10)
    d={}
    for number in range(0,10):
      d[number]=number**2
    print(d)
    

    Output:

    {0: 0, 1: 1, 2: 4, 3: 9, 4: 16, 5: 25, 6: 36, 7: 49, 8: 64, 9: 81}
    

2. zip(list1, list2, list3..)

  • Combines two or more iterators together
    list1=[1, 3, 5]
    list2=[2, 4, 6]
    list3=[3, 4, 5]
    for item1, item2, item3 in zip(list1, list2, list3):
      print(item1, item2, item3)
    

    Output:

    1 2 3
    3 4 4
    5 6 5
    

Practices

  • Using 2 lists - subtract values of list2 from list1 & print those values as a list, using zip()
    list1=[10,9,8,7]
    list2=[4,3,2,1]
    #Workings are as below
    list3=[]
    for number1,number2 in zip(list1,list2):
      result=number1-number2
      list3.append(result)
    print(list3)  
    

    Output:

    [6, 6, 6, 6]
    

3. enumerate( )

  • Returns index i and item
    fruits=['apple', 'banana', 'orange']
    # apple is 0, banana is 1
    for i, fruit in enumerate(fruits):
      print(f"{fruit} is {i}")
    

    Output:

    apple is 0
    banana is 1
    orange is 2
    

4. set( )

  • Returns unique items
  • Use list( ) to convert output from { } format to [ ] list format
    sentence="the quick brown fox jumped over the lazy bear"
    list(set(sentence.split(' '))) 
    

    Output:

    ['over', 'brown', 'the', 'quick', 'lazy', 'jumped', 'fox', 'bear']
    

5a. dict(zip(listofkeys, listofvalues))

  • Create a dictionary using a list of keys and list of values
    countries=['India', 'Japan', 'France']
    cities=['Delhi', 'Tokyo', 'Paris']
    dict(zip(countries, cities))
    

    Output:

    {'France': 'Paris', 'India': 'Delhi', 'Japan': 'Tokyo'}
    

5b. dict(zip(range(low#, high#), range(low#, high#)))

  • Create a dictionary using 2 or more sequence of numbers generated using range, by zipping
    dict(zip(range(0, 3), range(3, 6)))
    

    Output:

    {0: 3, 1: 4, 2: 5}
    

5c. dict(enumerate(listofvalues))

  • Create a dictionary using a list of values with their indexes as keys
    fruits=['apple', 'banana', 'orange']
    dict(enumerate(fruits))
    

    Output:

    {0: 'apple', 1: 'banana', 2: 'orange'}
    

More Other Practices

  • Check whether key present in dictionary
    Also, note use of = and ==
    Learn this well for code efficiency:
    capitals={'India':'Delhi', 'Malaysia':'LK', 'France':'Paris', 'Singapore':'Singapore'}
    flag=0
    for capital in capitals.keys():
      if 'Indonesia' == capital:
          flag = 1
    if flag==1:
      print('Yes')
    else:
      print('No')
    
  • Create a dictionary with key and value pair
    d={}
    d['key1']=3
    print(d)
    

    Output:

    {'key1': 3}
    

    Update the value to another value

    d['key1']=4
    print(d)
    

    Output:

    {'key1': 4}
    

Thoughts

I feel that I have been making good progress beyond my expectations in learning Python and I am really quite happy!

Practising more definitely trains me in writing more efficient code.

There were codes that I did using more lines than necessary and thankfully I learnt from my mentor how to better make the codes shorter and more efficient, such as by using ‘flags’. This would definitely help in my future codes.

Twitter, Facebook