16.01.2014 Views

Beginning Python - From Novice to Professional

Beginning Python - From Novice to Professional

Beginning Python - From Novice to Professional

SHOW MORE
SHOW LESS

Create successful ePaper yourself

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

30 CHAPTER 1 ■ INSTANT HACKING: THE BASICS<br />

Strings. Strings are really simple—they are just pieces of text. And yet there is a lot <strong>to</strong> know<br />

about them. In this chapter, you’ve seen many ways <strong>to</strong> write them, and in Chapter 3 you<br />

learn many ways of using them.<br />

New Functions in This Chapter<br />

Function<br />

Description<br />

abs(number)<br />

Returns the absolute value of a number<br />

cmath.sqrt(number)<br />

Square root, also for negative numbers<br />

float(object)<br />

Converts a string or number <strong>to</strong> a floating-point number<br />

help()<br />

Offers interactive help<br />

input(prompt)<br />

Gets input from the user<br />

int(object)<br />

Converts a string or number <strong>to</strong> an integer<br />

long(object)<br />

Converts a string or number <strong>to</strong> a long integer<br />

math.ceil(number)<br />

Returns the ceiling of a number as a float<br />

math.floor(number)<br />

Returns the floor of a number as a float<br />

math.sqrt(number)<br />

Square root, not for negative numbers<br />

pow(x, y[, z]) x <strong>to</strong> the power of y (modulo z)<br />

raw_input(prompt)<br />

Gets input from the user, as a string<br />

repr(object)<br />

Returns a string-representation of a value<br />

round(number[, ndigits])<br />

Rounds a number <strong>to</strong> a given precision<br />

str(object)<br />

Converts a value <strong>to</strong> a string<br />

What Now?<br />

Now that you know the basics of expressions, let’s move on <strong>to</strong> something a bit more advanced:<br />

data structures. Instead of dealing with simple values (such as numbers), you’ll see how <strong>to</strong><br />

bunch them <strong>to</strong>gether in more complex structures, such as lists and dictionaries. In addition,<br />

you’ll take another close look at strings. In Chapter 5, you learn more about statements, and<br />

after that you’ll be ready <strong>to</strong> write some really nifty programs.

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

Saved successfully!

Ooh no, something went wrong!