Lists are positionally ordered collections of arbitrarily typed objects, and they have no fixed size. They are also mutable—unlike strings and tuples, lists can be modified in place by assignment to offsets as well as a variety of list method calls.

Create lists

Storing different type of variable in a list is totally possible.

As you see, we have three types of data ; apple is a string, 123 is an integer, 3.14 is a float number.

For some reasons, you may create an empty list. In order to populate later in your code.

Alternatively you can create an empty list by list() function.

To see how many elements you have in the list use len() function.

A string can be transformed into a list by list() function.

Indexing

Indexing is same as strings. You can reach each element in a list by its position, index number.

Construction that we use for indexing or slicing is [start point:end point:stride]

Check following examples:

Nesting

We use nested lists to build a matrix.

Mutable lists

You can replace a value inside a list by another data type;  integer, float or string by its index number.

Here we changed the 3rd position which was equal to number 6 with letter A.

Actions to perform while working with lists

You can use Python methods to perform some actions in list objects. Some of them are:

  • append adds the object to the end of list
  • clear                 Clear the list object.
  • copy                 Copy the list object.
  • count               returns the number of a given element occurs in a list.
  • extend             add a given item in a list.
  • index               returns the index number of the first occurrence of an element. Gives an an exception if the element is not in the list.
  • insert               insert a given item in the list on a given position.
  • pop                   remove a return the item in a given index number.
  • remove            delete the first occurrence of a given item from a list.
  • reverse            reverse the order of a list.
  • sort                  sort elements by ascending order.

I will give exmaples for each methods.

Let’s check the type of “0”. Its position is 7th.

Now I’ll add the string ‘ 0’ in index 0, to show you remove method.

Special characters in Print()

n Char

Is used to skip a new line.

t Char

This character puts between words “tab” space.

Escape Sequence  Meaning 
newline Ignored
\ Backslash ()
Single quote (‘)
Double quote (“)
a ASCII Bell (BEL)
b ASCII Backspace (BS)
f ASCII Formfeed (FF)
n ASCII Linefeed (LF)
r ASCII Carriage Return (CR)
t ASCII Horizontal Tab (TAB)
v ASCII Vertical Tab (VT)
ooo ASCII character with octal value ooo
xhh… ASCII character with hex value hh…

Type() function

We use type() function to print data type.

Print function has special parameters

Check this example:

“*” parameter

Formatting

Some times you want to introduce in your print function some variables as string,integer or float. This can be done easily with format() function.

Look at this example:

Numbers in {} tell python the order of printing variables.

Try your code here. Just delete the embed code and write your own!