15.04.2018 Views

programming-for-dummies

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Using Comparison Operators 173<br />

Comparing two numbers is straight<strong>for</strong>ward, such as<br />

5 > 2<br />

Comparing two numbers always calculates the same result. In this case, 5 > 2<br />

always returns a True value. What gives comparison operators more flexibility<br />

is when they compare variables, such as<br />

Age > 2<br />

Depending on what the value of the Age variable may be, the value of this<br />

comparison can be either True or False.<br />

Comparing numbers may be straight<strong>for</strong>ward, but comparing strings can be<br />

more confusing. Remember, computers only understand numbers, so they<br />

use numbers to represent characters, such as symbols and letters.<br />

Computers use the number 65 to represent A, the number 66 to represent B,<br />

all the way to the number 90 to represent Z. To represent lowercase letters,<br />

computers use the number 97 to represent a, 98 to represent b, all the way<br />

up to 122 to represent z.<br />

Book II<br />

Chapter 3<br />

Manipulating Data<br />

The specific numbers used to represent every character on the keyboard can<br />

be found on the ASCII table, which you can view at www.asciitable.com.<br />

That’s why in Table 3-7 the comparison between A > a is False because the<br />

computer replaces each character with its equivalent code. So the comparison<br />

of characters<br />

“A” > “a”<br />

actually looks like this to the computer:<br />

65 > 97<br />

The number 65 isn’t greater than 97, so this comparison returns a False<br />

value.<br />

Comparing a string of characters works the same way as comparing single<br />

characters. The computer examines each string, character by character, and<br />

translates them into their numeric equivalent. So if you had the comparison<br />

“aA” > “aa”<br />

The computer converts all the characters into their equivalent values,<br />

such as<br />

97 65 > 97 97

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!