For

   The for statement iterates over a structure ("a sequence of something", list, tuple, set, dictionary, etc.)

A general syntax is like:

for ELEMENT in STRUCTURE:

statememn1

statement2

...


Example for iterating over a list:

mylist = [1, 'two', 'xyz', 8.35, True]

for element in mylist:

print(element)


#output

1

two

xyz

8.35

True


   We can use range() function in for to iterate over a sequence of numbers, or over a part of structure elements. For example, if we need to iterate/touch over first 2 elements:

mylist = [1, 'two', 'xyz', 8.35, True]

for i in range(2):

print(mylist[i])


#output

1

two

   General syntax for range function is range(start, stop, step=1), based on this we can iterate with other step different than 1, also iterate not only from the beginning of the structure but over a subset of elements, example:

mylist = [1, 'two', 'xyz', 8.35, True]

for i in range(1,3):

print(mylist[i])


#output

two

xyz

Here range function return values from start value till end value minus 1, i.e. range(1,3) give 1,2 values.

Tuples

   Tuples are python data structures similar to lists. A tuple is composed of values separated by commas and enclosed within parenthesis. Like comparison to remember that lists are enclosed in brackets.

Tuple example:

myTuple=('mnp', 5, 145.34, False, 'Blue sky')
print(myTuple)

#output
(False, 'Blue sky', 145.34, 5, 'mnp')


   Tuple elements are ordered and immutable (tuple is like a read-only list). Tuple elements are accessed similar like lists. Below are examples of accessing tuple elements:

myTuple=('mnp', 5, 145.34, False, 'Blue sky')


print(myTuple[1])     #accessing by index

#output

5


print(myTuple[2:4])     #accessing with slice notation

#output

(145.34, False)


print(myTuple[1:])     #accessing with slice notation

#output

(5, 145.34, False, 'Blue sky')


   Tuples elements are immutables (tuple is like a read-only list). If we try to modify an element from tuple similar like we do in lists case we will get an error. For example: 

myTuple = (5, 'test123', 23.54, False, 'xyz')

myTuple[0]=10


#output

Traceback (most recent call last):

  File "<pyshell#1>", line 1, in <module>

    myTuple[0]=10

TypeError: 'tuple' object does not support item assignment


   Even if tuple is immutable, a tuple can contain like elements a list, elements of which can be modified. For example:

tuple123 = (10, [12, 7.5, True], 34.54, 'abc')

tuple123[1][0]=11

print(tuple123)


#output

(10, [11, 7.5, True], 34.54, 'abc')

Here we see tuple123 contain like second element a list, i.e. [12, 7.5, True], means tuple123[1]= [12, 7.5, True]. There tuple123[1][0] access first element of list [12, 7.5, True]


In previous example we have a nested list in a tuple, or a tuple that contains elements like a list. 

Obvious we can have a tuple that contains other tuple(s) and so on..., nested tuples.

List In Python – II

In today's post I will present some of the most used list functions.


- len, display list length. Example:

mylist = [1, 'two', 'xyz', 8.35, True]

print(len(mylist))


#output

5


- insert, add an item to the list specified. Example:

mylist = [1, 'two', 'xyz', 8.35, True]

mylist.insert(2, False)

print(mylist)


#output

[1, 'two', False, 'xyz', 8.35, True]


- append, add an item to the list at the end of list. Example:

mylist = [1, 'two', 'xyz', 8.35, True]

mylist.append('abc')

print(mylist)


#output

[1, 'two', 'xyz', 8.35, True, 'abc']


- remove, delete an item from list. Example:

mylist = [1, 'two', 'xyz', 8.35, True, 'abc']

mylist.remove('two')

print(mylist)


#output

[1, 'xyz', 8.35, True, 'abc']


- del, delete entire list. Example:

mylist = [1, 'xyz', 8.35, True, 'abc']

del mylist

print(mylist)


#output

Traceback (most recent call last):

File "<PATH>\test.py", line 3, in <module> print(mylist)

NameError: name 'mylist' is not defined. Did you mean: 'list'?


- clear, empty the list. Example:

mylist = [1, 'xyz', 8.35, True, 'abc']

mylist.clear()

print(mylist)


#output

[]


- pop, remove last item from list, and return it. Example:

mylist = [1, 'xyz', 8.35, True, 'abc']

last_item=mylist.pop()

print(last_item)

print(mylist)


#output

abc

[1, 'xyz', 8.35, True]


- copy, make a copy of list. Example:

mylist = [1, 'xyz', 8.35, True, 'abc']

listNo2 = mylist.copy()

print(listNo2)


[1, 'xyz', 8.35, True, 'abc']


- extend, add item of other list2 to the end of list1. Example:

list1 = [1, 'xyz', 8.35, True, 'abc']

list2 = ['bb', 25]

list1.extend(list2)

print(list1)


#output

[1, 'xyz', 8.35, True, 'abc', 'bb', 25]


- count, return the number of occurrences of item in list. Example:

mylist = [1, 'xyz', 8.35, True, 'xyz', 'abc']

nb=mylist.count('xyz')

print(nb)


#output

2


- index, return index of the first items with specified value. Example:

mylist = [1, 'xyz', 8.35, True, 'xyz', 'abc']

ni=mylist.index('xyz')

print(ni)


#output

1


- reverse, this method reverses the list items. Example:

mylist = [1, 'xyz', 8.35, True, 'xyz', 'abc']

mylist.reverse()

print(mylist)


#output

['abc', 'xyz', True, 8.35, 'xyz', 1]


- sort, this method sorts the items of list. Example: 

list1 = [5, 53, 7, 22, 18, 31]

list1.sort()

print(list1)


#output

[5, 7, 18, 22, 31, 53]

Observation: list items need to be sortable, means if we have a list with mixed items, numbers, strings, etc, it can't be sorted. Example:

mylist = [1, 'xyz', 8.35, True, 'xyz', 'abc']

mylist.sort()

print(mylist)


#output

Traceback (most recent call last):

File "<PATH>\test.py", line 2, in <module> mylist.sort()

TypeError: '<' not supported between instances of 'str' and 'int'


#-> error is expected 

Lists In Python – I

Lists are compound data types, items of lists are enclosed with [], square brackets and are separated by comma. 

A list definition:

mylist = [1, 'two', 'xyz', 8.35, True]

#printing list:

print(mylist)


#output: 

[1, 'two', 'xyz', 8.35, True]


We saw items in the list can be different data types, in our example: integer, string, float, boolean.


In some way, list is similar with arrays from C (C++, C#) hence items can be accessed based on index i.e. 

mylist = [1, 'two', 'xyz', 8.35, True]

print(mylist[0]) #first item from list have index 0

print(mylist[2])


#output:

1

xyz


Supplmentary, lists have slice operator, examples:

mylist = [1, 'two', 'xyz', 8.35, True]

print(mylist[0:3]) #it will print items 0, 1, 2 

print(mylist[2:])  #it will print items from 2 to the end

print(mylist[:3])  #it will print items from the biginning till item with index 3-1


#output: 

[1, 'two', 'xyz']

['xyz', 8.35, True]

[1, 'two', 'xyz']


Lists support -1 like index and it represent last item from list, example:

mylist = [1, 'two', 'xyz', 8.35, True]

print(mylist[-1])


#output:

True


+ Sign can be used to concatenate lists, example:

mylist = [1, 'two', 'xyz', 8.35, True]

thelist = [2, False, 'bb']

print(mylist+thelist)


#output:

[1, 'two', 'xyz', 8.35, True, 2, False, 'bb']


* Asterisk sign in list case is repetition operator, example: 

list2 = [2, True]

print(list2*2)

print(list2[1]*2)


#output:

[2, True, 2, True]