Wednesday, August 17, 2011

Conditional Fun: playing with snakes 4

After a day of being locked outside of a car and watching a friend get infuriated by from dust I think I shall continue the Playing with snakes series here. Last time I talked about lists mostly in preparation for loops but before that we need to work with a lot more pieces of coding. The keep it a bit short due to seeing how easy this bloats I'll stick to logic operation today.

Computers are very good at repetitive logic comparing. It is a fundamental process of how they work. Different languages have different ways of performing logic operators, for basic python it isn't too hard. The only one that provides problems is equivalency which is just '=='. I say it has problems as you can easily put the equal sign which is for assigning information to a variable. The other common ones you may remember from math class are less than '<', greater than '>', greater than or equal to '<=', and less than or equal to '>='. When python encounters these it checks whatever is before and after it and compares to the best of it's abilities and will return True or False. This will be useful in the future but for now some useless testing of theory. Please type the following or otherwise look at the image attached.

print 1==1
print 1==2
print 1>1
print 1>2
print 2>1
print 1<1
print 1<2
print 2<1



Very basic things. I could make a longer list using all of the basic 5 comparison statements but that would be overkill. Things to note this also works with characters based on ASCII designation and to a degree strings, though that is another can of worms. instead of something trivial you can use variables within the conditionals to use them for their intended purposes. The intended purpose to be used in conditional statements.

For a quick demonstration of variables in use type the follwoing

a = 4
print a<3
print a<4
print a<5
print a==5
print a==6
print a==4

Logical operators can even use two variables to compare to produce their result of Truth or false. At the moment this is pretty trivial but I believe next Playing with snakes I'll get into the if statements and scopes. After that some loops and a bit of object work and we'll have 'fighter' remade.

5 comments:

  1. You make python seem kind of easy to understand

    ReplyDelete
  2. Locked out of a car? I know the feeling.

    ReplyDelete
  3. @shaw
    I try, it's not that hard in the small scale. Large scale I don't know even a good proportion of it.

    ReplyDelete
  4. Nothing but curious pleasures, as from thence Following!

    ReplyDelete