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
```

2022-09-30 11:22

`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 ]
```

2022-09-30 11:22

I think it would be good to make it a list.

```
a=[1,2,3]
b = [i**2 for i in a ]
```

2022-09-30 11:22

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

2022-09-30 11:22

Popular Tags

python x 4433
android x 1590
java x 1475
javascript x 1383
c x 903
c++ x 831
ruby-on-rails x 681
php x 678
python3 x 651
html x 631

Popular Questions

350 (python) Abs function re-create question

344 swiftui alert type 'CGFloat' has no member 'main'

342 Unable to exeize tkinterdnd

358 I'd like to set the upper limit for display of the Rails each statement.

358 in a regular expression, a way of writing in which the same word matches n or more iterations

© 2022 OneMinuteCode. All rights reserved.