I'd like to define 6 lines of a1, a2, a3, b1, b2, b3 shorter for the following code. Is there a good way to use .format()
, but it didn't work.
a1=1
a2 = 2
a3 = 3
b1 = a1**2
b2 = a2**2
b3 = a3**2
a*
variables have their own values, and b*
variables have a common formula.
If you want to give it to me, it's like this
>>a1,a2,a3 = 1,2,3
>>>b1,b2,b3 = [n**2 for n in (a1, a2, a3)]
If you want to use a set (for example, if there are many items), you can use it like this.
vals=a1,a2,a3=1,2,3
b1, b2, b3 = [n**2 for n in vals ]
I think it would be good to make it a list.
a=[1,2,3]
b = [i**2 for i in a ]
For Python 3.8 and later, use walrus operator as follows:
b1,b2,b3=(a1:=1)**2,(a2:=2)**2,(a3:=3)**2
*I don't recommend it
342 Memory layouts learned in theory don't work out as expected when actually printed as addresses.
341 Understanding the Meaning of mpm prefork Settings
354 I have saved several codes written in python to a visual studio file.
356 I want to create an array of sequences from "1" to a specified number.
© 2023 OneMinuteCode. All rights reserved.