Lists, Loops, strings, and dictionaries in python

Dating back to 1991, python computer programming language is considered a gap-filler and a way out to write a script that automates the boring stuff. It is also considered as a language that rapidly prototypes the applications that will be implemented in other languages available. However, over a few years, this language has emerged as a first-class citizen in data analysis, software development, and infrastructure management. It is not a back-room utility language anymore. Python is a major part of web application development and system management. Also, it is a key driver of the increase in machine intelligence and big data analytics. In this article, we will learn about python’s lists, loops, strings, and dictionaries.

Lists

The list is the most flexible data type that is available in python. It can be easily written as a list of values or items between square brackets. We need to separate them with a comma. However, one of the important things in a list is that the values in it need not be of the same type. For example – list1 = [‘physics’, ‘biology’, 2020]. List indices always start at 0 and we can slice or concatenate them. We can access the values in a list and can update or delete them accordingly.

Built-in functions and methods

  1. cmp(list2,list3) – compares all the elements of both the lists.
  2. Len(list) – it tells the total length of a particular list.
  3. Max(list) – it returns the item from the list with the maximum value.
  4. Min(list) – returns item which has the minimum value.
  5. List(seq) – converts a given tuple to a list.
  6. append(object1) – appends the object objec1 to the list.
  7. count(objec1) – returns the count of how many times that object has occurred on the list.
  8. extend(seq) – it appends the contents of seq to the list.
  9. index(objec1) – returns the lowest index in the list that objec1 appears.
  10. insert(index, objec1) – this inserts the object objec1 in the list at the offset index.
  11. pop(objec1=list[-1]) – removes and returns the last object from the list.
  12. remove(objec1) – it removes the object objec1 from the list.
  13. reverse() – it reverses the objects of a list in place.
  14. sort([func]) – it sorts all the objects of the list.

Loops

Usually, we execute the statements sequentially. In a function, the first statement is executed, then the second statement is executed and the same goes on. But then, there may be a situation when you need to execute a certain block of code more than once. For this purpose, programming languages provide you with various control structures that allow for more complicated execution paths. A loop statement is the one that allows the programmers to execute a statement or a group of statements multiple times. Python provides us with the following types of loops for handling our looping requirements.

  • While loop – This loop statement repeats a particular statement or a group of statements while the given condition is true. It tests the given condition before actually executing the loop body.
  • For loop – For loop executes a sequence of statements several times and also abbreviates the code that manages the variable of the loop.
  • Nested loop – The programmers can use one or more than one loop inside any other for, while, or do-while loop.

Strings

The string is the most popular data type in the python programming language. We can easily create them by enclosing all the characters in quotes. One important thing to remember is that python treats both the single and double quotes equally. Creating strings in a program is as simple as assigning a particular value to a variable. We can always access the values in the strings and can update the existing string by reassigning a given variable to another string. The newly assigned value can be related to the previous value or a very different string altogether. Python is a language that doesn’t support the character type string. We treat them as strings of length one. Thus, often we can also consider as a substring.

Main built-in string methods

  1. capitalize() – capitalizes the first letter of a string.
  2. center(width, fillchar) – this method returns a space-padded string with an original string centered to a total width of the columns.
  3. count(str1, beg= 0,end=len(string)) – it counts how many times do str1 occurs in a string.
  4. decode(encoding=’UTF-8′,errors=’strict’) – it decodes the given string using codec that is registered for encoding.
  5. encode(encoding=’UTF-8′,errors=’strict’) – this method returns encoded version of string.

Dictionaries

In a dictionary, the keys are separated from their values by a colon. The items or values are separated by commas and the whole thing is enclosed by curly braces. We can write an empty dictionary without any items in it with just curly braces like {}. The keys are always unique in a dictionary whereas it is not the case with the values. The values can be of any type in the dictionary but the keys should always be of an immutable data type like tuples, strings, or numbers. We can access the values in a dictionary and can update and delete the elements just like a list.

Built-in dictionary functions and methods

  1. cmp(dict2, dict4) – compares the elements of both dictionaries.
  2. len(dict1) – gives the total length of a dictionary.
  3. str(dict1) – it produces a printable string representation of a particular dictionary.
  4. type(variable) – the type of passed variable is returned with this.
  5. clear() – removes all the elements.
  6. copy() – returns a copy of the dictionary.
  7. fromkeys() – creates a new dictionary with the keys.
  8. get(key, default=None) – returns value or default if the key is not present in the dictionary.
  9. has_key(key) – returns true if the key is present, else false.
  10. items() – returns a whole list of tuple pairs of the dictionary.
  11. keys() – returns the list of all the keys.
  12. setdefault(key, default=None) – it is similar to get(). But, it will set dict[key] to default if the key is not present in dict.
  13. update(dict4) – it adds dictionary dict4’s key-value pairs to the dict.
  14. values() – returns the list of dictionary dict’s values.

Leave a Comment