Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Simple Code Understanding
#1
Hi,

I'm quite new to Python, looking to get a general understand how the below out puts what it does:

my_list = range(4,11)
for num in my_list:
    if (1==num%2):
       print (str(num))
Output (5, 7, 9)

Why is 1 (in the if statement) unable to be changed?

Any information would be great. Thank you Smile
Reply
#2
nom % 2 - This operator is called modulo operator and it yields the remainder of the division.

1 == x checks if x is equal to 1.

The whole expression 1 == num % 2 checks if num is odd.
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Help with simple code JacobSkinner 1 228 Mar-18-2024, 08:08 PM
Last Post: deanhystad
  I have a code which is very simple but still I cannot detect what's wrong with it max22 1 440 Nov-07-2023, 04:32 PM
Last Post: snippsat
  Code understanding: Need help in understanding dictionary code jt123 0 450 Jul-09-2023, 01:13 PM
Last Post: jt123
  New to python/coding Need help on Understanding why this code isn't working. Thanks! mat3372 8 1,664 May-09-2023, 08:47 AM
Last Post: buran
  help me simple code result min and max number abrahimusmaximus 2 870 Nov-12-2022, 07:52 AM
Last Post: buran
  Simple encoding code ebolisa 3 1,400 Jun-18-2022, 10:59 AM
Last Post: deanhystad
  How would you (as an python expert) make this code more efficient/simple coder_sw99 3 1,751 Feb-21-2022, 10:52 AM
Last Post: Gribouillis
  Understanding a piece of code Michael1 4 1,373 Jan-20-2022, 07:14 PM
Last Post: Michael1
  Simple code question about lambda and tuples JasPyt 7 3,238 Oct-04-2021, 05:18 PM
Last Post: snippsat
  My simple code don't works !! Nabi666 1 1,577 Sep-06-2021, 12:10 PM
Last Post: jefsummers

Forum Jump:

User Panel Messages

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