Python Open Lab, October 5

In the Python Open Lab of this week, we learned list, dictionary, string.
For list, it can store multiple elements and many useful functions about list are introduced.

  • append(x) —   put an element to the tail of a list
  • insert(x) —   insert an element to specific position of a list
  • count(x) —   count number of a element in the list
  • remove(x) —   remove the first x in the list
  • sort()      —   sort a list
  • extend(List b) — put another list to the tail of present list

Slice of list and two-dimension list are also learned.
 
For dictionary, it is different from list because it appears in the form of “key: value” pairs. The keys need to be unique. It is need to be declared by using “{}”. The storage of dictionary is frank, just like “dict[“key”] = value”, so we finish storing one pair into the dictionary. To fetch the value in the dictionary, we just need to use its key, like “print(dict[“key”])”. Dictionary is very flexible and any value of a dictionary can be a list or dictionary. Complex operations can be done by dictionary.
 
For string, the concept string is that anything between double quotes and a single quote is a string. String concatenation is very simple, just using the operator “+”. Element fetch and slice of a string is same as that in a list.
 
Python has great built-in types like list, dictionary and string. They are powerful and time-saving for programmers. A good master of them can enhance coding efficiency and avoid mistakes.
 
Kang