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
- cmp(list2,list3) – compares all the elements of both the lists.
- Len(list) – it tells the total length of a particular list.
- Max(list) – it returns the item from the list with the maximum value.
- Min(list) – returns item which has the minimum value.
- List(seq) – converts a given tuple to a list.
- append(object1) – appends the object objec1 to the list.
- count(objec1) – returns the count of how many times that object has occurred on the list.
- extend(seq) – it appends the contents of seq to the list.
- index(objec1) – returns the lowest index in the list that objec1 appears.
- insert(index, objec1) – this inserts the object objec1 in the list at the offset index.
- pop(objec1=list[-1]) – removes and returns the last object from the list.
- remove(objec1) – it removes the object objec1 from the list.
- reverse() – it reverses the objects of a list in place.
- 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
- capitalize() – capitalizes the first letter of a string.
- center(width, fillchar) – this method returns a space-padded string with an original string centered to a total width of the columns.
- count(str1, beg= 0,end=len(string)) – it counts how many times do str1 occurs in a string.
- decode(encoding=’UTF-8′,errors=’strict’) – it decodes the given string using codec that is registered for encoding.
- 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
- cmp(dict2, dict4) – compares the elements of both dictionaries.
- len(dict1) – gives the total length of a dictionary.
- str(dict1) – it produces a printable string representation of a particular dictionary.
- type(variable) – the type of passed variable is returned with this.
- clear() – removes all the elements.
- copy() – returns a copy of the dictionary.
- fromkeys() – creates a new dictionary with the keys.
- get(key, default=None) – returns value or default if the key is not present in the dictionary.
- has_key(key) – returns true if the key is present, else false.
- items() – returns a whole list of tuple pairs of the dictionary.
- keys() – returns the list of all the keys.
- 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.
- update(dict4) – it adds dictionary dict4’s key-value pairs to the dict.
- values() – returns the list of dictionary dict’s values.