Menu
Home
Log in / Register
 
Home arrow Computer Science arrow Python Programming Fundamentals
< Prev   CONTENTS   Next >

3.1 Operators

If you take a look at Chap. 10 to peruse the string methods you will notice there are two kinds of methods described there. At the beginning of the appendix there are operators like <=. These operators are just special methods in Python. They describe methods that are not written using the reference.method(arguments) format.

Instead, the <= method describes an infix operation that can be performed between two string objects to see if one string is less than or equal to another string object.

Example 3.3 Consider the following code.

1 s = i n p u t ("Please enter a your name:")

2 t = i n p u t ("Please enter your mom's name:")

3 i f s <= t:

4 p r i n t ("Your name comes before your mom's name.")

5 e l s e :

6 p r i n t ("Your mom's name comes before your name.")

The code in Example 3.3 asks the user to enter two strings and compares the two strings. If your name would appear first alphabetically it prints the first message, otherwise it prints the second message. The comparison of s <= t on the third line of code is possible because of the existence of the __le__ method for strings. This is a special method that you will see if you type help(str).

When reading Chap. 10 most of the operators are really methods that aren't called in the usual way. These methods are sometimes called hooks, syntactic sugar, or just operators. A hook in Python is just a special way of calling a method. Most methods are called in the usual way by writing reference.method(arguments). In fact, even the special hook methods can be called in the usual way. So, comparing two strings, s and t, to see if one is less than or equal to the other could be written s.__le__(t). Of course, it is more convenient and descriptive to use the operator format and write

s <= t when comparing two strings. This is why it is called syntactic sugar. It is much nicer to write the comparison operator s <= t than to write s.__le__(t). Syntactic sugar refers to the ability to write a part of a program in a pleasing way as

opposed to having to always stick to writing code using the same rules.

Operators are methods that are not called using the reference.method(arguments) format. Figure 3.2 has examples of calling several of the string operators and some of the string methods. All the string methods can be found in Chap. 10. Chapters 8 and 9 describe operators on integers and floats that are similar to the string operators and are called in a similar fashion.

Practice 3.3 Use Fig. 3.2 and Chap. 10 to help you write a program that asksthe user to enter “yes” or “no”. If they enter “yes” then you should print “Youentered yes”. and likewise if they enter “no”. However, make sure you accept“Yes”, “yEs”, or any other combination of upper and lower case letters for“yes” and for “no”. Identify the syntactically sugared methods that you arecalling on the string class in your answer.

Operator

Returns

Result

Comments

str(90)

str

“90”

for most argument types

chr(90)

str

“Z”

ASCII character equivalent of int

ord(“Z”)

int

90

ASCII int equivalent of character

s+t

“how”+“are”+“you”

str

“hithere”

“howareyou”

same as s. add (t)

s in t

'he' in “there”

bool

False

True

same as s. in (t)

s==t

s=='hi'

bool

False

True

same as s. eq (t)

s> =t

bool

False

same as s. ge (t)

s< =t

bool

True

same as s. le (t)

s> t

bool

False

same as s. gt (t)

s< t

bool

True

same as s. lt (t)

len(s)

int

2

same as s. len ()

t[1:4]

t[:3]

t[1:]

str

“her”

“the”

“here”

same as t. getslice (1,4)

same as t. getslice (0,3) same as t. getslice (1,len(t))

s.upper()

str

“HI”

does not change s

s.strip()

str

“hi”

removes surrounding whitespace

u.split()

list

[“how”,“are”,“you”]

splits on whitespace

All examples assume s = “hi”, t = “there”, and u = “ how are you ”

Fig. 3.2 String operators and common methods

 
Found a mistake? Please highlight the word and press Shift + Enter  
< Prev   CONTENTS   Next >
 
Subjects
Accounting
Business & Finance
Communication
Computer Science
Economics
Education
Engineering
Environment
Geography
Health
History
Language & Literature
Law
Management
Marketing
Philosophy
Political science
Psychology
Religion
Sociology
Travel