Making a 2D array without brackets - python

I'm trying to create a 2D array, but it prints out in one row and with brackets.
How do I make it into rows and columns without brackets?
rows = int(input("Please enter the number of rows: "))
board = [[i + (j * cols) for i in range(1, cols + 1)] for j in range(0, rows)]
print(board)

for row in board:
print(" ".join(list(map(str, row))))
We iterate through the board and for each row, make it a list of strings (using map) instead of numbers and join them with a space.

Maybe this is what you are looking for:
for row in board:
print(row)

Is this what you mean?
rows = int(input("Please enter the number of rows: "))
cols=rows#my thing would not run without this line
board = [[i + (j * cols) for i in range(1, cols + 1)] for j in range(0, rows)]
print(board)
arr=[]
for i in range(rows):
for c in range(cols):
print(str(i)+" "+str(board[i][c]))
Output:
0 1
0 2
0 3
0 4
0 5
1 6
1 7
1 8
1 9
1 10
2 11
2 12
2 13
2 14
2 15
3 16
3 17
3 18
3 19
3 20
4 21
4 22
4 23
4 24

Related

Find Python Multiplication from user input

Junior Python Coder here! Trying to print multiplication table with user input but stuck.
min_num=1
max_num=11
starting_range = int(input ("Enter the minimum number: "))
ending_range = int(input ("Enter the maximum number: "))
print ("The Multiplication Table of: ", starting_range)
for count in range(1, 11):
print (starting_range, 'x', count, '=', ending_range * count)
if max_num - min_num > 10 :
print('invalid range ')
else:
for num in range (min_num,max_num):
print ("The Multiplication Table of: ", ending_range)
for count in range(min_num, max_num):
print (starting_range, 'x', count, '=', ending_range * count)
I'm not sure I really understand what you're trying to do as your code isn't formatted, but for a multiplication table, a nested loop is a solution.
The basic idea is: For every number in the given range, loop over the whole range and multiply it by each element. This will print the whole multiplication table.
start = 1 # You can change these to be inputted by the user.
end = 10
for i in range(start, end + 1): # We add 1 to the end because range() is exclusive on endpoint.
for j in range(start, end + 1):
print(f"{i} x {j} = {i * j}")
If you only need the table as something like:
15 x 1
15 x 2
15 x 3
...
You can do this with one loop:
num = 10
for i in range(1, num + 1):
print(f"{num} x {i} = {num * i}")
I recommend you search up on F-strings in Python if you do not understand the print(f"..") parts. They're very convenient. Good luck!
Minimal change to original code:
min_num = 1
max_num = 11
starting_range = 4 # You can use int() and input() as in your original code to make this user defined.
ending_range = 7
# Removed the error checking, wasn't sure precisely what it was meant to do.
for num in range(starting_range,ending_range):
print ("The Multiplication Table of: ", num)
for count in range(min_num, max_num):
print (num, 'x', count, '=', num * count)
Try it at https://www.mycompiler.io/view/7JVNtxkT53k
This prints:
The Multiplication Table of: 4
4 x 1 = 4
4 x 2 = 8
4 x 3 = 12
4 x 4 = 16
4 x 5 = 20
4 x 6 = 24
4 x 7 = 28
4 x 8 = 32
4 x 9 = 36
4 x 10 = 40
The Multiplication Table of: 5
5 x 1 = 5
5 x 2 = 10
5 x 3 = 15
5 x 4 = 20
5 x 5 = 25
5 x 6 = 30
5 x 7 = 35
5 x 8 = 40
5 x 9 = 45
5 x 10 = 50
The Multiplication Table of: 6
6 x 1 = 6
6 x 2 = 12
6 x 3 = 18
6 x 4 = 24
6 x 5 = 30
6 x 6 = 36
6 x 7 = 42
6 x 8 = 48
6 x 9 = 54
6 x 10 = 60
Alternative code defining a function:
def print_multiplication_table(n, max_value=11):
if n > max_value or n <= 0:
raise ValueError
print(f"The Multiplication Table of: {n}")
for m in range(1, max_value):
print(f"{n} x {m} = {n*m}")
starting_range = 4 # You can use int() and input() as in your original code to make this user defined.
ending_range = 7
for i in range(starting_range, ending_range):
print_multiplication_table(i)
Try it at https://www.mycompiler.io/view/1uttFLmFEiD

Tables of multiples in Python

I would like to print a table that print rows*columns in that many rows and columns, with the beginning of each column incrementing by one and that row being multiples of the first number. For example: table(3,4)
1 2 3 4
2 4 6 8
3 6 9 12
Here is my code so far. It's currently going by ones, and I don't know how to make each row be a multiple of the first number. Thank you.
def table(rows, columns):
for i in range(rows):
print(*range(1+i*columns, 1+(i+1)*columns))
for h in range(rows%rows):
print(*range(1+h*columns%rows, 1+(h+1)*columns))
You should iterate over a range of rows and a range of columns, and output their products:
def table(rows, columns):
for i in range(1, rows + 1):
for h in range(1, columns + 1):
print(i * h, end=' ')
print()
so that:
table(3, 4)
would output:
1 2 3 4
2 4 6 8
3 6 9 12

Write a program to prompt for square height h and print the even numbers in a square shape?

I'm not sure how to do this. I know how to print a square shape by prompting for row and col, but don't quite understand how to only prompt for height and print an array of even numbers.
col = eval(input("Enter the number of columns: "))
row = eval(input("Enter the number of rows: "))
for j in range(row):
for i in range(col):
print("*", end=" ")
print()
this is how I would set up printing a square of asterisks, but how would I do this while only prompting for height and printing out even numbers? For example, if my height is "3", it should print out an array that looks like this:
0 2 4
6 8 10
12 14 16
Using next() on an iterator of the numbers:
h = 3
ns = iter(range(0,h**2*2,2))
for r in range(h):
for c in range(h):
print(next(ns), end='\t')
print()
which gives:
0 2 4
6 8 10
12 14 16
Using slicing and str.join:
h = 4
ns = list(range(0,h**2*2,2))
for r in range(0,len(ns),h):
print(" ".join(str(c) + "\t" for c in ns[r:r+h]))
which gives:
0 2 4 6
8 10 12 14
16 18 20 22
24 26 28 30
Here is an approach by making a nested list and printing it out using a formatting function. Can replace the list comprehension with a generator comprehension for large lists.
def print_even_matrix(h):
l = [[2*(i + j) for j in range(h)] for i in range(0, h**2, h)]
listtab = lambda x: '\t'.join(map(str, x))
print '\n'.join(map(listtab, l))
>>> print_even_matrix(3)
0 2 4
6 8 10
12 14 16
>>> print_even_matrix(4)
0 2 4 6
8 10 12 14
16 18 20 22
24 26 28 30

For loop a square pattern of numbers

In this question I had to create a program that prompts the user for a number and then prompt again for how many rows to create. Something like:
1 1 1 1 1 1 1
2 2 2 2 2 2 2
3 3 3 3 3 3 3
4 4 4 4 4 4 4
This is what I came up with and I have tried many different ways to get the same result but it didn't work.
num=int(input("Enter a number between 1 and 10: "))
rows=int(input("Enter how many rows to of numbers: "))
for i in range(num):
print(i,end=" ")
for x in range(rows):
print (x)
This is the output I came up with:
Enter a number between 1 and 10: 6
Enter how many rows to of numbers: 4
0 1 2 3 4 5 0
1
2
3
You may simply do it like:
num = 5
rows = 4
for i in range(1, num+1):
print('{} '.format(i) * rows)
Output:
1 1 1 1
2 2 2 2
3 3 3 3
4 4 4 4
5 5 5 5
Explanation: When you multiply a str with some number say n, new string is returned with original string repeated n times. Doing this you will eliminate your nested loop
Simple solution: Just use nested for loops:
num = int(input("Enter a number between 1 and 10: "))
rows = int(input("Enter how many rows to of numbers: "))
for i in range(num):
print ('\n')
for x in range(rows):
print (i + 1)
The code above will go through the range 0 to num, printing first a new line and then printing the current number rows times.
rows = 5
side = int(input("Please Enter any Side of a Square : "))
for i in range(side):
for j in range(side):
if(i == 0 or i == side - 1 or j == 0 or j == side - 1):
print(i, end = ' ')
else:
print(i, end = ' ')
print()

Reverse Multiplication Table

I'm having troubling reversing my multiplication table.
This is what I have so far:
def reverseTable(n):
for row in range(1, n+1):
print(*("{:3}".format(row*col) for col in range(1, n+1)))
But I want to reverse it to:
25 20 15 10 5
20 16 12 8 4
15 12 9 6 3
10 8 6 4 2
You need to reverse your range so it counts backwards. The range() function accepts 3 parameters, range(start, stop, step) so to count from 10 to 1 you would use range(10, 0, -1)
Try this:
def reverseTable(n):
for row in range(n, 0, -1):
print(*("{:3}".format(row*col) for col in range(n, 0, -1)))
for row in range(9,0,-1):
print(end="\t")
for column in range(9,0,-1):
print(row*column,end="\t ")
print()
reverse multiplication table in python taking value from user
num = int(input("enter the number= "))
i=10
while i>=1:
print(num,"X",i,"=",num*i)
i= i-1
output
enter the number= 3
3 X 10 = 30
3 X 9 = 27
3 X 8 = 24
3 X 7 = 21
3 X 6 = 18
3 X 5 = 15
3 X 4 = 12
3 X 3 = 9
3 X 2 = 6
3 X 1 = 3
num=int(input("enter your table number: "))
for i in range(10,0,-1):
print(str(num) + "x" + str(i) + "=" + str(num*i))
Here's your answer
num=int(input("Enter the number"))
for i in range (10,0,-1):
print(f"{num}*{i}={num*i}")
num = int(input("enter a no."))
count = 10
while count >= 1:
X = num*count
print(num, "x", count, "=", X)
count = count - 1
i used this i dont know if it is of any help to you since I am a beginner and have just started learning python
num= int(input("please enter the number: "))
n= 11
for i in range(0,10):
n -= 1
print(f"{num} x {n} = {num*n} ")

Categories

Resources