You can use a for loop and join a list comprehension for each row to print your table.
for i in range(0, 50, 10):
print(' '.join([str(j) for j in range(i, i + 10)]))
Similarly, you can unpack a row as a list into the print method's parameters.
for i in range(0, 50, 10):
print(*[j for j in range(i, i + 10)])
Answer from tdurnford on Stack OverflowYou can use a for loop and join a list comprehension for each row to print your table.
for i in range(0, 50, 10):
print(' '.join([str(j) for j in range(i, i + 10)]))
Similarly, you can unpack a row as a list into the print method's parameters.
for i in range(0, 50, 10):
print(*[j for j in range(i, i + 10)])
You can specify the end parameter of the print() function:
for x in range(50):
if (x+1)%10 == 0:
end_char = "\n"
else:
end_char = " "
print(x,end=end_char)
OUTPUT:
0 1 2 3 4 5 6 7 8 9
10 11 12 13 14 15 16 17 18 19
20 21 22 23 24 25 26 27 28 29
30 31 32 33 34 35 36 37 38 39
40 41 42 43 44 45 46 47 48 49
The print() function add a new line at the end of each statement by default, but you can modify how the string should end: "\n" (new line) if the next number is divisible for 10, " " (a space) otherwise
printing - Is it possible to print a table of a 'for loop' at the same position without moving ahead the lines of the Terminal using python? - Stack Overflow
python - How to print table in looping pyhton console - Stack Overflow
python - How to print a multiplication table using for loop? - Stack Overflow
for loop - Print a multiplication table in Python - Stack Overflow
def main():
print("\nCelsius","\t","Fahrenheit")
print("-------","\t","----------")
for cels in range(70,280,10):
fahr = 1.8 * cels + 32
print(cels,"\t\t",fahr)
main()this is my code and how it looks when ran
would it be possible to make all the Fahrenheit numbers line up evenly?
This is using alignement options from the format method.
product = 0
for x in range(1,11):
for y in range(1,11):
product = x * y
print('{:<4}'.format(product), end='')
print()
Reference: Format specification mini-language
I think all you would need to add to get what you want is to print a new line in your inner loop like so:
product = 0
for x in range(1,11):
print()
for y in range(1,11):
product = x * y
print(product, end="\t")
On the third line it'll start a new line after 10.
The output will look like this:
1 2 3 4 5 6 7 8 9 10
2 4 6 8 10 12 14 16 18 20
3 6 9 12 15 18 21 24 27 30
4 8 12 16 20 24 28 32 36 40
5 10 15 20 25 30 35 40 45 50
6 12 18 24 30 36 42 48 54 60
7 14 21 28 35 42 49 56 63 70
8 16 24 32 40 48 56 64 72 80
9 18 27 36 45 54 63 72 81 90
10 20 30 40 50 60 70 80 90 100
If you need to print those next to each other to make them look like a table you can replace your code with the below.
print('Number\tSquare\tCube')
for i in range(0,7,1):
print(i,'\t',i*i,'\t',i*i*i)
Output:
Number Square Cube
0 0 0
1 1 1
2 4 8
3 9 27
4 16 64
5 25 125
6 36 216
num = 0
print("number square cube" )
for i in range(0, 11):
m = i*i
print(num, '\t', m, '\t', num*m)
num = num+1
oUTPUT
Number Square Cube
0 0 0
1 1 1
2 4 8
3 9 27
4 16 64
5 25 125
6 36 216
7 49 393
8 64 512
9 81 729
10 100 1000