Jump to content
  • Advertisement
Sign in to follow this  
dimescion

Python 3 Questions

This topic is 2072 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

This is just a list of random questions I asked myself while looking at a python tutorial on Youtube.
 
Are lists with just strings always mutable lists?
 
Are lists with integers always immutable?
 
What about combinations?
 
Mutable implies you can do something to "mutate" it or something. What would that be?
 
Naming a range for a variable results in it not listing the last number "201" in the shell. Why is this?
Why do you have to put "[:]" before stating a range value?
listCycle[:] = range(1,201)
 
I'm having trouble understanding "for" statements. What's a good place to learn more about them?
 
I know you can type;
       for i in listCycle:
              if (1%2) == 0
                     continue
              else:
                     print(i)
to only display the odd numbers. How would you display only even numbers?
 

Share this post


Link to post
Share on other sites
Advertisement

I don't know most of these (I don't use Python), but I can answer the odd/even numbers question.

I'll be assuming you meant if (i % 2) == 0, not if (1 % 2) == 0.

 

The % operator is called modulo. It returns the remainder of a division between the parameters on either side (left parameter divided by right). 3 % 2 is equal to 1, because 3 / 2 leaves 1 as a remainder.

If we modulo with 2 and check if it's equal to 0, that means the division with 2 has no remainder, which is the case for all even numbers.

The "continue" command that is triggered if this occurs makes the for loop skip through to the next iteration of the loop. In the code snippet you posted, all even numbers (which are perfectly divisible by 2) will just skip to the next iteration, without doing anything else.

 

So, if you want to print even numbers instead, you can simply swap the if (i % 2) == 0 to if (i % 2) == 1.

Edited by Lactose™

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!