PYTHON NOTES JEGAN K K
GENERATORS IN PYTHON:
Generators can be used to create a sequence of values in order (or) it can also be used to
create sequence of values in a specific order with validation of few conditions in it.
Syntax ⇒ var = ( i for i in range (1, 5) )
The above example creates a generator object with range of values starting from 1 and
ends at n-1 (i.e. 5-1 = 4)
Syntax ⇒ var = ( i for i in range (1, 10) if i%2 == 0 )
The above example creates a generator object with only even numbers based on the
condition specified.
Note: Generators can also be used to create lists and also alter list of lists using two generator
expressions at the same time.
Syntax ⇒ list3 = [ [(i in list2) for i in j] for j in list1]
In the above case suppose that we have a list of lists (i.e, list1). We check each sub-list
values of list1 if they are present in another normal list with individual elements (i.e, list2). This
creates a new list (list3) which has the result list of sub-lists and whether they are True or False is
each value outcome. For example, list1 = [[1,2],[3,4]], list2 = [1,4],
list3 would be ⇒ [[True, False], [False, True]]
ITERTOOLS IN PYTHON:
Itertools is used to render the combinations and permutations of a given sequence of
values. Initially we need the statement "import itertools" to utilize them in our code script.
Syntax ⇒ a = itertools.combinations("abc", 2)
The above example would render the doublets from the strings as combinations. (i.e ab,
ac, bc)
Syntax ⇒ a = itertools.permutations("abc", 2)
The above example would render the doublet permutation in all possible combos
including reverse combos. (i.e. ab, ac, ba, bc, ca, cb)
Syntax ⇒ a = itertools.product("abc","def")
The above example would render the possible doublet combinations between the 2 strings
"abc" , "def" (i.e ad, ac, af, bd, be, bf, cd, ce, cf)
For combinations and permutations, the number parameter can be any number 1 to 3 in the above
case and it will generate the combinations or permutations based on the length of substrings
needed accordingly.
GENERATORS IN PYTHON:
Generators can be used to create a sequence of values in order (or) it can also be used to
create sequence of values in a specific order with validation of few conditions in it.
Syntax ⇒ var = ( i for i in range (1, 5) )
The above example creates a generator object with range of values starting from 1 and
ends at n-1 (i.e. 5-1 = 4)
Syntax ⇒ var = ( i for i in range (1, 10) if i%2 == 0 )
The above example creates a generator object with only even numbers based on the
condition specified.
Note: Generators can also be used to create lists and also alter list of lists using two generator
expressions at the same time.
Syntax ⇒ list3 = [ [(i in list2) for i in j] for j in list1]
In the above case suppose that we have a list of lists (i.e, list1). We check each sub-list
values of list1 if they are present in another normal list with individual elements (i.e, list2). This
creates a new list (list3) which has the result list of sub-lists and whether they are True or False is
each value outcome. For example, list1 = [[1,2],[3,4]], list2 = [1,4],
list3 would be ⇒ [[True, False], [False, True]]
ITERTOOLS IN PYTHON:
Itertools is used to render the combinations and permutations of a given sequence of
values. Initially we need the statement "import itertools" to utilize them in our code script.
Syntax ⇒ a = itertools.combinations("abc", 2)
The above example would render the doublets from the strings as combinations. (i.e ab,
ac, bc)
Syntax ⇒ a = itertools.permutations("abc", 2)
The above example would render the doublet permutation in all possible combos
including reverse combos. (i.e. ab, ac, ba, bc, ca, cb)
Syntax ⇒ a = itertools.product("abc","def")
The above example would render the possible doublet combinations between the 2 strings
"abc" , "def" (i.e ad, ac, af, bd, be, bf, cd, ce, cf)
For combinations and permutations, the number parameter can be any number 1 to 3 in the above
case and it will generate the combinations or permutations based on the length of substrings
needed accordingly.