Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
10 views

Format Method, Lists, 5list Operations, List Built in Functions, Creating A Tuple in Python (1) - 1625595544124

Uploaded by

keed wild
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

Format Method, Lists, 5list Operations, List Built in Functions, Creating A Tuple in Python (1) - 1625595544124

Uploaded by

keed wild
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

The format() method:

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 [].

A list can be defined as below:

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

The list has the following characteristics:

 The lists are ordered.

 The element of the list can access by index.

 The lists are mutable types.

 A list can store the number of various elements.

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:

List Indexing and Splitting:

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]

 The start denotes the starting index position of the list.


 The stop denotes the last index position of the list.

 The step is used to skip the nth element within a start:stop

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:

Updating list Values:

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.

Let's see how the list responds to various operators.

Consider a Lists l1 = [1, 2, 3, 4] and l2 = [5, 6, 7, 8] to perform operation.

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:

Adding elements to the list:

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:

Removing elements from the list:


Python provides the remove() function which is used to remove the element from
the list. Consider the following example to understand this concept.

Output:

Python list built-in functions:

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:

A tuple can be written as the collection of comma-separated (,) values enclosed


with the small () brackets. A tuple can be defined as follows.

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))

#Creating a tuple with single element

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:

You might also like