Python Lists


Python Lists

Sequence is the most basic information construction inwards Python, Each chemical part of a sequence is assigned a let on - its seat or index. The get-go index is zero, the second index is one, as well as and then forth.

Python has 6 built-in types of sequences, simply the most mutual ones are Lists as well as Tuples.
-----------------------------
The listing is a most versatile datatype available inwards Python which tin locomote written as 
a listing of comma-separated values (items) betwixt foursquare brackets. Important thing about a listing is that items inwards a listing need non locomote of the same type.

Examples:

list1 = ['Java', 'Visual Basic', 1997, 2000];
list2 = [10, 20, 30, 40, 50];
list3 = ["a", "b", "c", "d"];
-----------------------------
1) Accessing Values inwards Lists

To access values inwards lists, purpose the foursquare brackets for slicing along amongst the index.

Examples:

abc = ["Selenium", "UFT", "SilkTest", 100, 200, 300];
print (abc);
print (abc[1]);
-----------------------------
2) Updating Lists

You tin update unmarried or multiple elements of lists yesteryear giving the while on the 
left-hand side of the assignment operator, as well as you lot tin add together to elements inwards a list 
with the append() method.

Examples:

list = ['physics', 'chemistry', 1997, 2000];

print (list);
print (list[2]);
list[2] = 9999;
print (list);
print (list[2]);
-----------------------------
is the most basic information construction inwards Python Python Lists
3) Delete List Elements

To take a listing element, you lot tin purpose either the del disputation if you lot know exactly which element(s) you lot are deleting or the remove() method if you lot create non know. 

Example:

list1 = ['physics', 'chemistry', 1997, 2000];

print (list1);
del (list1[2]);
print (list1);
-----------------------------
4) Built-in List Functions & Methods / Operations on Python Lists

i) len()
Gives the sum length of the list.

Example:
list = [10, 20, 30, 40, 50];

print (len(list));

ii) max()
Returns especial from the listing amongst max value.

Example:

list = [10, 20, 30, 40, 50];

print (max(list));

iii) min()

Returns especial from the listing amongst 2nd value.

Example:

list = [10, 20, 30, 40, 50];

print (min(list));
-------------------------------
Methods amongst Description

i) append() method

Appends object obj to list

Example: 
list = [10, 20, 30, 40, 50];

print(list);
list.append(70);
print(list);

ii) remove() method
Removes chemical part from listing yesteryear chemical part value.

Example:

list = [10, 20, 30, 40, 50];

print(list);
list.remove(50);
print(list);

iii) reverse() method
Reverses elements of listing inwards place

Example:

list = [10, 20, 30, 40, 50];

print(list);
list.reverse();
print(list);
-------------------------------------------------------------
Also Read:

Python Step yesteryear Step Tutorial

1) In Which sequence should We larn Python?

2) Introduction to Python

3) Download & Install Python

4) Variables as well as Data Types inwards Python

5) Python Operators

6) Python Conditional Statements

7) Python Loops...

8) String Handling inwards Python

Sumber http://www.gcreddy.com/
Post a Comment (0)
Previous Post Next Post