Python Forum
how to create pythonic codes including for loop and if statement?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
how to create pythonic codes including for loop and if statement?
#2
The only unpythonic parts that I see here are if i==0 & j==0 which should be if not(i or j) and i==0|j==0 which should be not(i and j). Note that & and | are bitwise operators and not logical operators. The result is also different because they have different levels of operator precedence. For example
>>> 0==0 & 7==0
True
>>> 0==0 and 7==0
False
Writing code on a single line does not necessarily give better Python code, and it is not necessarily 'pythonic'. I'm not sure there is a better solution here.
aupres likes this post
Reply


Messages In This Thread
RE: how to create pythonic codes including for loop and if statement? - by Gribouillis - Jan-02-2021, 06:10 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  How to create a variable only for use inside the scope of a while loop? Radical 10 1,759 Nov-07-2023, 09:49 AM
Last Post: buran
Photo Python code: While loop with if statement HAMOUDA 1 583 Sep-18-2023, 11:18 AM
Last Post: deanhystad
  Function to count words in a list up to and including Sam Oldman45 15 6,620 Sep-08-2023, 01:10 PM
Last Post: Pedroski55
  Multiply and Addition in the same loop statement with logic. joelraj 2 1,046 Feb-02-2023, 04:33 AM
Last Post: deanhystad
  Including data files in a package ChrisOfBristol 4 2,559 Oct-27-2021, 04:14 PM
Last Post: ChrisOfBristol
  Not including a constructor __init__ in the class definition... bytecrunch 3 11,945 Sep-02-2021, 04:40 AM
Last Post: deanhystad
  Create Dynamic For Loop quest 3 4,424 Apr-26-2021, 02:03 PM
Last Post: ibreeden
  Compare each element of an array in a logic statement without using a for loop leocsmith 3 5,882 Apr-01-2021, 07:57 PM
Last Post: deanhystad
  Python Matplotlib: How do I save (in pdf) all the graphs I create in a loop? JaneTan 1 8,895 Feb-28-2021, 06:20 PM
Last Post: Larz60+
  if statement in for loop researcher123 6 2,592 Oct-01-2020, 05:07 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020