New course! Every coder should learn Generative AI!
Try a free lesson+ 9
List comprehension
print(list(i+1 if i%2==0 else i-1 for i in range(50)))
+ 4
[i+1 if i%2==0 else i-1 for i in range(50) ]
+ 3
мʀυηαlι ʀαנρυт usually we create a list of items using this shorthand expression: [i for i in range(50)] ,
now we are just checking if that i%2 results in 0 ,if yes do i+1 else do i-1
so it takes this form
[i+1 if i%2==0 else i-1 for i in range(50)]
I think if you read it carefully it's self explanatory ,
+ 1
как дела???
+ 1
Use list comprehension