Format Method, Lists, 5list Operations, List Built in Functions, Creating A Tuple in Python (1) - 1625595544124
Format Method, Lists, 5list Operations, List Built in Functions, Creating A Tuple in Python (1) - 1625595544124
The format() method is the most flexible and useful method in formatting strings.
The curly braces {} are used as the placeholder in the string and replaced by the
format() method argument. Let's have a look at the given an example:
Output:
List:
A list in Python is used to store the sequence of various types of data. Python lists
are mutable type its mean we can modify its element after it created. A list can be
defined as a collection of values or items of different types. The items in the list
are separated with the comma (,) and enclosed with the square brackets [].
If we try to print the type of L1 and L2 using type() function then it will come out
to be a list.
Output:
Characteristics of Lists
Let's check the first statement that lists are the ordered.
Output:
Both lists have consisted of the same elements, but the second list changed the
index position of the 5th element that violates the order of lists. When compare
both lists it returns the false.
Lists maintain the order of the element for the lifetime. That's why it is the
ordered collection of objects.
Output:
The indexing is processed in the same way as it happens with the strings. The
elements of the list can be accessed by using the slice operator [].
The index starts from 0 and goes to length - 1. The first element of the list is
stored at the 0th index, the second element of the list is stored at the 1st index,
and so on.
list_variable[start:stop:step]
For Example:
Output:
Unlike other languages, Python provides the flexibility to use the negative
indexing also. The negative indices are counted from the right. The last element
(rightmost) of the list has the index -1; its adjacent left element is present at the
index -2 and so on until the left-most elements are encountered.
For Example:
Output:
Lists are the most versatile data structures in Python since they are mutable, and
their values can be updated by using the slice and assignment operator.
For Example:
Output:
Python list Operations:
The concatenation (+) and repetition (*) operators work in the same way as they
were working with the strings.
Iterating a list:
A list can be iterated by using for - in loop. A simple list containing four strings,
which can be iterated as follows.
Output:
Python provides append() function which is used to add an element to the list.
However, the append() function can only add value to the end of the list.
Consider the following example in which, we are taking the elements of the list
from the user and printing the list on the console.
Output:
Output:
Python provides the following built-in functions, which can be used with the lists.
For Example:
Output:
Another Example:
Output:
Tuple:
Python Tuple is used to store the sequence of immutable Python objects. The
tuple is similar to lists since the value of the items stored in the list can be
changed, whereas the tuple is immutable, and the value of the items stored in the
tuple cannot be changed.
Creating a tuple:
Output:
An empty tuple can be created as follows.
T4 = ()
Creating a tuple with single element is slightly different. We will need to put
comma after the element to declare the tuple.
tup1 = ("Softwarica")
print(type(tup1))
tup2 = ("Softwarica",)
print(type(tup2))
Output:
<class 'str'>
<class 'tuple'>
A tuple is indexed in the same way as the lists. The items in the tuple can be
accessed by using their specific index value.
For Example:
Output: