Simple Python number generation - python

I'm missing something very simple. I'm a new Python student, so bear with me.
for i in range(10):
print("\n")
for x in range(10):
print(x, end = " ")
Sample output:
0 1 2 3 4 5 6 7 8 9
0 1 2 3 4 5 6 7 8 9
0 1 2 3 4 5 6 7 8 9
...
The "i" variable is setting the size of what I want to output, and in the above example I am showing the range of 0-9, 10 times. How would I alter this to display the following without using the print command by way of a string or an array? I'm trying to use two nested "for" statements, but I'm drawing a blank on what to use in place of the range command.
Desired output:
0 0 0 0 0 0 0 0 0 0
1 1 1 1 1 1 1 1 1 1
2 2 2 2 2 2 2 2 2 2
3 3 3 3 3 3 3 3 3 3
...

You are printing the variable from your inner loop (x) instead of the variable from the outer loop (i). Do it this way:
for i in range(10):
print("\n")
for x in range(10):
print(i, end = " ")

why to loop twice, we know str*n give n times str
>>> for x in range(10):
... print((str(x) + ' ')*10)
...
0 0 0 0 0 0 0 0 0 0
1 1 1 1 1 1 1 1 1 1
2 2 2 2 2 2 2 2 2 2
3 3 3 3 3 3 3 3 3 3
4 4 4 4 4 4 4 4 4 4
5 5 5 5 5 5 5 5 5 5
6 6 6 6 6 6 6 6 6 6
7 7 7 7 7 7 7 7 7 7
8 8 8 8 8 8 8 8 8 8
9 9 9 9 9 9 9 9 9 9
demo:
>>> 'a'*5
'aaaaa'
>>> 'a'*10
'aaaaaaaaaa'
>>> 'hello**'*10
'hello**hello**hello**hello**hello**hello**hello**hello**hello**hello**'
>>> '9'*10
'9999999999'

This also works:
for i in range(10):
print ' '.join(str(i) * 10)

Related

Code that prints out a right triangle. Each line has numbers and that at the end of each line is the sum of all numbers in it

What I have so far:
n=10
for i in range(1,n+1):
for j in range(0,i+1):
print(j,end=' ')
print('')
That prints out:
0 1
0 1 2
0 1 2 3
0 1 2 3 4
0 1 2 3 4 5
0 1 2 3 4 5 6
0 1 2 3 4 5 6 7
0 1 2 3 4 5 6 7 8
0 1 2 3 4 5 6 7 8 9
0 1 2 3 4 5 6 7 8 9 10
The output should be:
0 1 1
0 1 2 3
0 1 2 3 6
0 1 2 3 4 10
0 1 2 3 4 5 15
0 1 2 3 4 5 6 21
0 1 2 3 4 5 6 7 28
0 1 2 3 4 5 6 7 8 36
0 1 2 3 4 5 6 7 8 9 45
0 1 2 3 4 5 6 7 8 9 10 55
At the end of each line is the sum of all the numbers in the line. For example, in line 5, the sum of all numbers in that line is 15.
As you can see I am missing the sum of all the numbers at the end of each line. Should I add another for-loop? Should I change the inner loop? I don't know what code I should use to get the sum of the numbers.
Thank you!
Perhaps the simplest way:
n=10
for i in range(1,n+1):
r = range(i+1)
print(*r, sum(r))
You can track your own sum, if you want to.
n=10
for i in range(1,n+1):
sumx = 0
for j in range(i+1):
sumx += j
print(j,end=' ')
print(sumx)
Or, microscopically better:
n=10
sumx = 0
for i in range(1,n+1):
for j in range(i+1):
print(j,end=' ')
sumx += i
print(sumx)

Task for printing table of numbers (patterns)

The task is to print this table of digits. I am a total beginner. How can I approach it, and are there any "lifehacks" for doing that kind of pattern? Right below I have attached my code - but this one is not what I need.
0
1 0 9
2 1 0 9 8
3 2 1 0 9 8 7
4 3 2 1 0 9 8 7 6
Code:
for start in range(10,14):
for i in range(start,6,-1):
print(i%10, end='')
print()
You have to find the pattern.
In each row the starting number is increased by 1. In each column the numbers are decreased by 1 and then modulo 10 is applied. The number of columns can be calculated from the row number.
for row in range(5):
for col in range(row * 2 + 1):
print((row - col) % 10, end = ' ')
print()
The code is flexible enough to support an arbitrary number of rows. Running the code with for row in range(12): will give you the following result
0
1 0 9
2 1 0 9 8
3 2 1 0 9 8 7
4 3 2 1 0 9 8 7 6
5 4 3 2 1 0 9 8 7 6 5
6 5 4 3 2 1 0 9 8 7 6 5 4
7 6 5 4 3 2 1 0 9 8 7 6 5 4 3
8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2
9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1
0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0
1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0 9
Here's a solution:
print(0)
for row in range(1, 10):
for i in range(row, 0-1, -1):
print(i, end=' ')
for i in range(1, row+1):
print(10-i, end=' ')
print()
As for lifehacks - you need to try and master easier problems first. Try these questions or these questions
And for writing the shortest code using some advanced tricks:
print(0)
for row in range(1, 10):
print(*range(row, 0, -1), end=' ')
print(0, end=' ')
print(*range(10-1, 10-row-1, -1), end=' ')
print()

Not Getting proper number traingle in python

i would like to print a some triangle of numbers. please have look at below code. Can anybody tell me where it's going wrong in following code??
x=0
while x<10:
y=x
while (y >= 0):
print y,
y=y-1
print x
x=x+1
output:
0 0
1 0 1
2 1 0 2
3 2 1 0 3
4 3 2 1 0 4
5 4 3 2 1 0 5
6 5 4 3 2 1 0 6
7 6 5 4 3 2 1 0 7
8 7 6 5 4 3 2 1 0 8
9 8 7 6 5 4 3 2 1 0 9
required o/p:
0
1 0
2 1 0
3 2 1 0
4 3 2 1 0
5 4 3 2 1 0
6 5 4 3 2 1 0
7 6 5 4 3 2 1 0
8 7 6 5 4 3 2 1 0
9 8 7 6 5 4 3 2 1 0
Thanks
You're getting an extra number at the end of each line because you're printing x when you don't need to. I assume you have that print statement there so you can force a line break between different rows of results. If all you want is to move the cursor down a line, you can use print with no arguments.
x=0
while x<10:
y=x
while (y >= 0):
print y,
y=y-1
print
x=x+1
Just change the print x to print. That will output a newline without printing the value of x, which is where the unwanted numbers come from:
x=0
while x<10:
y=x
while (y >= 0):
print y,
y=y-1
print
x=x+1
The output is:
0
1 0
2 1 0
3 2 1 0
4 3 2 1 0
5 4 3 2 1 0
6 5 4 3 2 1 0
7 6 5 4 3 2 1 0
8 7 6 5 4 3 2 1 0
9 8 7 6 5 4 3 2 1 0

Nested Loops Python

Hi I am working with nested loops but can not figure out a way to start a new row for my nested loop below:
for i in range(0,10):
for j in range(10):
print(i,end =" ")
Output
0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 3 3 3 3 3 3 3 3 3 3 4 4 4 4 4 4 4 4 4 4 5 5 5 5 5 5 5 5 5 5 6 6 6 6 6 6 6 6 6 6 7 7 7 7 7 7 7 7 7 7 8 8 8 8 8 8 8 8 8 8 9 9 9 9 9 9 9 9 9 9
The output I want is:
0 0 0 0 0 0 0 0 0 0
1 1 1 1 1 1 1 1 1 1
2 2 2 2 2 2 2 2 2 2
and so on
Thank you but I found the answer!
for i in range(0,10):
for j in range(10):
print(i, end=" ")
print()
This (and minor variations) also work:
for i in range(10):
print((('%s '%i)*10)[:-1])

How can I print this?

I am trying to teach myself python using interactivepython.org. I have come across a problem that I can not figure out. I have the slope and the spacing correct. I need it to print one less number every time. Could anybody help a newbie out?...
The code I have written:
numLines = 10
for i in range(numLines):
for k in range(i):
print(' ', end = ' ')
for j in range(1, numLines):
print(j, end = ' ')
print()
print(" ")
Prints:
1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8 9
Want to Print:
0 1 2 3 4 5 6 7 8 9
0 1 2 3 4 5 6 7 8
0 1 2 3 4 5 6 7
0 1 2 3 4 5 6
0 1 2 3 4 5
0 1 2 3 4
0 1 2 3
0 1 2
0 1
0
Well, The above answers are perfectly fine. But this is my way of doing things... :)
Code:
l = map(str,range(0,10))
for i in range(10):
print ' '.join(l[:len(l)-i]).rjust(20)
Output:
0 1 2 3 4 5 6 7 8 9
0 1 2 3 4 5 6 7 8
0 1 2 3 4 5 6 7
0 1 2 3 4 5 6
0 1 2 3 4 5
0 1 2 3 4
0 1 2 3
0 1 2
0 1
0
Hope this helps :)
Try this:
numLines = 10
for i in range(numLines, 0, -1):
for j in range(0, numLines - i):
print " ",
for k in range(0, i):
print k,
print
How about this
numLines = 10
for i in range(numLines):
print "".join (" " for j in range(i)) + " ".join (str(j) for j in range(numLines - i))
Output
0 1 2 3 4 5 6 7 8 9
0 1 2 3 4 5 6 7 8
0 1 2 3 4 5 6 7
0 1 2 3 4 5 6
0 1 2 3 4 5
0 1 2 3 4
0 1 2 3
0 1 2
0 1
0
Never mind I figured it out. I had to decrement numLines by one inside the first for loop.

Categories