My learning journey — Python [Part 2]

Sarah Chua
3 min readApr 30, 2019

Loops

  • Iterables: a collection that you can loop through
  • Item: the current item being processed.
  • range(x,y): goes through items from x to y-1.
  • range(y): goes through items from 0 to y-1
  • range(x,y,z): z indicates the step, z can be negative to do a negative step
  • for x in “string”: iterate through characters in string
  • for item in <list>: iterate through items in the list
  • list(range(…)): creates a list/array out of the range
  • while: do something while a condition applies
  • break: break out of the loop

List

  • collection of things
  • items = [<items>]: standard syntax to create a list
  • list items will be stored in order as they are declared in the list
  • list(range(1,10)): to create a list out of a range of numbers
  • items[<index>]: retrieve 1 item from the using index. 0 is the first index. (backward index starts from -1)
  • <item> in items: checks for existence base on exact match
  • <list>.append(<item>): append 1 item to the end of the list
  • <list>.extend(<list2>): add items in the list2 into the original list
  • <list>.insert(<position>,<item>):insert item at a particular position. Note that to insert at the end using -ve index, last position is -<len of original list>.
  • <list>.clear(): clears the list
  • <list>.pop(): removes the last item on the list and returns it
  • <list>.pop(<index>): remove the item according to index and returns it
  • <list>.remove(<item>): remove first instance of item base on value. If value not found, valueError thrown. item is not returned.
  • <list>.index(<item>, [startingindex], [endingindex]): returns the index of first occurrence of the item. Optionally specify the start and end index to do the search
  • <list>.count(<item>): returns the # of times an item appears in list
  • <list>.reverse(): reverse element order in place. No new list created.
  • <list>.sort(): sort in ascending order
  • <delimiter>.join(<list>): forms a string by joining list elements joined using delimiter.

Slicing

  • <list>[start:end:step]: make new list out of old list indicating the start, stop index and the steps/intervals to pick the elements for the new list
  • start element is included in the list
  • end element is not included in the list
  • a unique copy of the list is created with this
  • items[2:] — returns list with first 2 elements removed
  • items[:] — returns copy of the list
  • items[-4] — returns list sliced with elements from the back
  • items[:4] — returns list with the first 4 elements
  • items[:3:1] — returns list with the first 3 elements
  • items[:3:-1] — note that a negative number can be used for slicing and will return the elements on the opposite side of what you think of a standard start and end

Swapping

  • item[0], item [1] = item [1], item[0]: swaps the items in the array

List comprehension

  • [<operation> for <item> in <list>]: to perform an operation for each element in the list (e.g. x*10 for x in numList)
  • [num*10 for num in range(1,6)]: generate array of 1–6*10
  • [<operation> for <item> in <list> if <condition>]: performs the operation and generate the element in the list base on the if condition (e.g. [num for num in numbers if num%2==0])
  • ** the condition can also be applied in the operation to perform the operation according to the condition

Nested list

  • List which elements are lists
  • list[<index outer>][<index inner>]
  • [[<operation> for <itemx> in <listx>] for <item> in <list>]: generating nested list/operations

Dictionaries

  • key value pair
  • key to be number or string, value can be anything
  • {<key>:<value>, …} / dict(<key> = <value>): making dictionary
  • dictname[<key>]: retrieve value from dict. keyError if key does not exist
  • dictname.get(<key>): retrieve value from dict. Gives None if key does not exist.
  • dictname.values(): get (loopable) iterable of values
  • dictname.keys(): get (loopable) iterable of keys
  • dictname.items(): get (loopable) iterable of keys and values. for key, value in dictname.iterms()
  • <key> in <dictname>: checks whether a key is in the dictionary returning true or false
  • <value> in <dictname>.values(): checks whether a value is in the dictionary returning true or false
  • dictname.clear() : clear the elements in the dictionary
  • dictname.pop(<key>): removes 1 element and returns the value. If key does not exist then key error.
  • dictname.popitem(): remove a random item
  • clonedict = dictname.copy(): makes a copy of the dict. ==:true, is:false
  • {}.fromkeys(<list of keys>,<value>): initialize a dictionary with keys from list of keys with an initial value
  • dict1.update(dict2): updates dict1 with the elements from dict2. Overwrites if the same key exist.
  • {_:_ for _ in _}: iterate over a iterable (list, dictionary) to generate a new dictionary. Example {key:value*2 for key,values in dict.items()}. Logic can be applied to the key or the value during dictionary generation.
  • {list1[i]: list2[i] for i in range(0,3)}: combining 2 lists into key value pair
  • dict(person): converts a list of pairs into a dictionary
  • {k:v for k,v in person}:convert into dictionary where person is a list of pairs

--

--