Click here

Wednesday, September 19, 2018

Python - List comprehension

List comprehensions are a tool for transforming one list (any iterable actually) into another list. During this transformation, elements can be conditionally included in the new list and each element can be transformed as needed.

The basic syntax is
[ expression for item in list if conditional ]

This is equivalent to:

for item in list:
    if conditional:
        expression

Example
1)
x = 1
y = 1
z = 1

lis = []
lis = [ [i,j,k] for i in range(x+1) for j in range(y+1) for k in range(z+1) if (i+j+k) != N]
print lis

Output
[[0, 0, 0], [0, 0, 1], [0, 1, 0], [1, 0, 0], [1, 1, 1]]

Above is the nested for loop
2)
x = [i for i in range(10)]
print x

This will give the output:
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

Thursday, September 13, 2018

Python - Print without newline and space


In python , by default print inserts newline character at the end of an output

For example:



Output
1
2

1) To ignore the new line at the end of an output, you can do it multiple ways

In python 2.x



Output
1 2

2) To ignore the space in between



Output
12

Using backspace character replaces the space in between. Comma at the end replaces the newline character with an empty string.  If you use + operator , both the operators has to be in string format

3) To print without using string



Output
12

Comma at the end replaces the new line character. Printing an empty string suppresses the space in between

4) Python 3.x has an easy way of doing this



Using end statement, replaces the end terminator with an empty string.

print('a', 'b', 'c', sep='')
to suppress the white space separator between items.





Omicron - people gathers in crowd

Amidst omicron thread, people are gathered in crowd at markets and public places to buy their daily needs. Because of full lockdown at Sunda...