Program to produce the following pattern - python-3.x

I am trying to print
2
1 3
2 4 6
1 3 5 7
2 4 6 8 10
I am not sure how to do it ,I could use print (...) And write everything but that is just stupid.
Is there a better way to do this?

for i in range(0, 5):
for j in range(0, i+1):
if i % 2 == 0:
print((j+1)*2, end=' ')
else:
print((j*2)+1, end=' ')
print()

maybe you can try this:
>>> def pattern_gen(layer):
... for i in range(layer):
... pattern=''
... start=(i+1)%2+1
... end=(i+(i+1)%2+1)*2
... for j in range(start,end,2):
... pattern+=f'{j} '
... print(pattern)
...
>>> pattern_gen(5)
2
1 3
2 4 6
1 3 5 7
2 4 6 8 10

Related

How can i Write a program that will read in a number and then print out a triangle of #s using nested loops?

Heres the code of the program.
def go( num ):
print(" ")
go( 1 )
go( 2 )
go( 3 )
go( 4 )
go( 9 )
go( 12 )
Now i am really having trouble understanding how to wrtie it so it goes through the list.
This is the code ive done so far
> def go( num ):
> row = 0
for i in range(row + 1):
for j in range(i):`
print(i, end=" ")
print("*")
go( 1 )
go( 2 )
go( 3 )
go( 4 )
go( 9 )
go( 12 )
I am currently getting a
"builtins.NameError: name 'row' is not defined" error,anything would help.
Also yeah i am kind of rushing this
I think you are looking something like this
def go(num):
for i in range(num+1):
for j in range(i):
print(i, end=" ")
print("")
go(2)
1
2 2
go(9)
1
2 2
3 3 3
4 4 4 4
5 5 5 5 5
6 6 6 6 6 6
7 7 7 7 7 7 7
8 8 8 8 8 8 8 8
9 9 9 9 9 9 9 9 9

how to add a space before a line in python

I want to print 2 space before every line of my code's output.
My code:
n = int(input())
for row in range(1, n+1):
for column in range(1, n+1):
print(column, end=' ')
print('')
input:
5
My output:
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
The output I want:
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
Try this:
n = int(input())
for row in range(1, n+1):
for column in range(1, n+1):
print(' ',column, end=' ') # we print some whitespace in front of every character and at the end.
print('')
This code prints 2 spaces before and 1 space after, like the output that you want.
You can achieve this by putting the whitespace in before the column value:
n = int(input())
for row in range(1, n+1):
for column in range(1, n+1):
print(' ', column, end='')
print()
This also removes the extra whitespace that your existing solution puts on the end of each line (though if you wanted that back, simple add more space to the 2nd print - i.e. print(' '))
You can use the join method for strings:
n = int(input())
for row in range(1, n+1):
print( " " + " ".join([str(x) for x in range(1, n+1)]) )

To print a pattern in Python using 'for' loop

I tried various programs to get the required pattern (Given below). The program which got closest to the required result is given below:
Input:
for i in range(1,6):
for j in range(i,i*2):
print(j, end=' ')
print( )
Output:
1
2 3
3 4 5
4 5 6 7
5 6 7 8 9
Required Output:
1
2 3
4 5 6
7 8 9 10
Can I get some hint to get the required output?
Note- A newbie to python.
Store the printed value outside of the loop, then increment after its printed
v = 1
lines = 4
for i in range(lines):
for j in range(i):
print(v, end=' ')
v += 1
print( )
If you don't want to keep track of the count and solve this mathematically and be able to directly calculate any n-th line, the formula you are looking for is the one for, well, triangle numbers:
triangle = lambda n: n * (n + 1) // 2
for line in range(1, 5):
t = triangle(line)
print(' '.join(str(x+1) for x in range(t-line, t)))
# 1
# 2 3
# 4 5 6
# 7 8 9 10

Python how to print 1D list as 2D

I want to print a list [1,2,3,4,5,6,7,8,9] as
1 2 3
4 5 6
7 8 9
Here is my code
for i,j in enumerate(list):
if i is not 0 and i % 3==0:
print()
else:
print(j,end=" ")
My result is
1 2 3
5 6
8 9
Can someone help explain why this happen and give me some advise?
You should print the list item j unconditionally instead of doing it only when you are not printing a newline:
l = [1,2,3,4,5,6,7,8,9]
for i,j in enumerate(l):
if i is not 0 and i % 3==0:
print()
print(j,end=" ")
You can do:
tgt=[1,2,3,4,5,6,7,8,9]
n=3
print('\n'.join([' '.join(map(str, sl)) for sl in [tgt[i:i+n] for i in range(0,len(tgt),n)]]))
Prints:
1 2 3
4 5 6
7 8 9

why dynamic integer right align in python 3

What I am trying to do below in my code is just print all the types of number formats available in right aligned manner.
def print_formatted(number):
for i in range(1, n+1):
width = len(format(i, 'b'))
print("{0:d} {0:o} {0:x} {0:{w}b}".format(i, w=len(format(i, 'b'))))
if __name__ == '__main__':
n = int(input())
print_formatted(n)
Input:
4
Expected Output:
1 1 1 1
2 2 2 10
3 3 3 11
4 4 4 100
But actual output:
1 1 1 1
2 2 2 10
3 3 3 11
4 4 4 100
The above code works fine if I give a static value in-place of 'w' but if I pass dynamic changing value it is not working as expected. What am I missing here
Thanks in advance for your help.

Resources