Tuesday, 4 September 2018

Lists


Python Lists

Python Collections (Arrays)

There are four collection data types in the Python programming language:
  • List is a collection which is ordered and changeable. Allows duplicate members.
  • Tuple is a collection which is ordered and unchangeable. Allows duplicate members.
  • Set is a collection which is unordered and unindexed. No duplicate members.
  • Dictionary is a collection which is unordered, changeable and indexed. No duplicate members.
When choosing a collection type, it is useful to understand the properties of that type. Choosing the right type for a particular data set could mean retention of meaning, and, it could mean an increase in efficiency or security.

List

A list is a collection which is ordered and changeable. In Python lists are written with square brackets.

Example

Create a List:
thislist = ["apple""banana""cherry"]
print(thislist)

Access Items

You access the list items by referring to the index number:

Example

Print the second item of the list:
thislist = ["apple""banana""cherry"]
print(thislist[1])

Change Item Value

To change the value of a specific item, refer to the index number:

Example

Change the second item:
thislist = ["apple""banana""cherry"]
thislist[1
] = "blackcurrant"
print(thislist)

Loop Through a List

You can loop through the list items by using a for loop:

Example

Print all items in the list, one by one:
thislist = ["apple""banana""cherry"]
for x in thislist:
  print
(x)

List Length

To determine how many items a list have, use the len() method:

Example

Print the number of items in the list:
thislist = ["apple""banana""cherry"]
print(len(thislist))

Add Items

To add an item to the end of the list, use the append() method:

Example

Using the append() method to append an item:
thislist = ["apple""banana""cherry"]
thislist.append("orange"
)
print(thislist)
To add an item at the specified index, use the insert() method:

Example

Insert an item as the second position:
thislist = ["apple""banana""cherry"]
thislist.insert(1
"orange")
print(thislist)

Remove Item

There are several methods to remove items from a list:

Example

The remove() method removes the specified item:
thislist = ["apple""banana""cherry"]
thislist.remove("banana"
)
print(thislist)

Example

The pop() method removes the specified index, (or the last item if index is not specified):
thislist = ["apple""banana""cherry"]
thislist.pop()
print(thislist)

Example

The del keyword removes the specified index:
thislist = ["apple""banana""cherry"]
del thislist[0]
print(thislist)

Example

The del keyword can also delete the list completely:
thislist = ["apple""banana""cherry"]
del thislist
print(thislist) #this will cause an error because "thislist" no longer exists.

Example

The clear() method empties the list:
thislist = ["apple""banana""cherry"]
thislist.clear()
print(thislist)

The list() Constructor

It is also possible to use the list() constructor to make a list.

Example

Using the list() constructor to make a List:
thislist = list(("apple""banana""cherry")) # note the double round-brackets
print
(thislist)

List Methods

Python has a set of built-in methods that you can use on lists.
Method
Description
Adds an element at the end of the list
Removes all the elements from the list
Returns a copy of the list
Returns the number of elements with the specified value
Add the elements of a list (or any iterable), to the end of the current list
Returns the index of the first element with the specified value
Adds an element at the specified position
Removes the element at the specified position
Removes the item with the specified value
Reverses the order of the list
Sorts the list


Operators


Python Operators
Operators are used to perform operations on variables and values.
Python divides the operators in the following groups:
  • Arithmetic operators
  • Assignment operators
  • Comparison operators
  • Logical operators
  • Identity operators
  • Membership operators
  • Bitwise operators
·         Python Arithmetic Operators
·         Arithmetic operators are used with numeric values to perform common mathematical operations:
Operator
Name
Example
+
Addition
x + y
-
Subtraction
x - y
*
Multiplication
x * y
/
Division
x / y
%
Modulus
x % y
**
Exponentiation
x ** y
//
Floor division
x // y
·        
·         Python Assignment Operators
·         Assignment operators are used to assign values to variables:
Operator
Example
Same As
=
x = 5
x = 5
+=
x += 3
x = x + 3
-=
x -= 3
x = x - 3
*=
x *= 3
x = x * 3
/=
x /= 3
x = x / 3
%=
x %= 3
x = x % 3
//=
x //= 3
x = x // 3
**=
x **= 3
x = x ** 3
&=
x &= 3
x = x & 3
|=
x |= 3
x = x | 3
^=
x ^= 3
x = x ^ 3
>>=
x >>= 3
x = x >> 3
<<=
x <<= 3
x = x << 3
Python Comparison Operators
Comparison operators are used to compare two values:
Operator
Name
Example
==
Equal
x == y
!=
Not equal
x != y
> 
Greater than
x > y
< 
Less than
x < y
>=
Greater than or equal to
x >= y
<=
Less than or equal to
x <= y

Python Logical Operators
Logical operators are used to combine conditional statements:
Operator
Description
Example
and 
Returns True if both statements are true
x < 5 and  x < 10
or
Returns True if one of the statements is true
x < 5 or x < 4
not
Reverse the result, returns False if the result is true
not(x < 5 and x < 10)

Python Identity Operators
Identity operators are used to compare the objects, not if they are equal, but if they are actually the same object, with the same memory location:
Operator
Description
Example
is 
Returns true if both variables are the same object
x is y
is not
Returns true if both variables are not the same object
x is not y

Python Membership Operators
Membership operators are used to test if a sequence is presented in an object:
Operator
Description
Example
in 
Returns True if a sequence with the specified value is present in the object
x in y
not in
Returns True if a sequence with the specified value is not present in the object
x not in y

Python Bitwise Operators
Logical operators are used to combine conditional statements:
Operator
Name
Description
AND
Sets each bit to 1 if both bits are 1
|
OR
Sets each bit to 1 if one of two bits is 1
 ^
XOR
Sets each bit to 1 if only one of two bits is 1
NOT
Inverts all the bits
<< 
Zero fill left shift
Shift left by pushing zeros in from the right and let the leftmost bits fall off
>> 
Signed right shift
Shift right by pushing copies of the leftmost bit in from the left, and let the rightmost bits fall off



Lists

Python  Lists Python Collections (Arrays) There are four collection data types in the Python programming language: List  is a ...