Tuesday, November 14, 2017

The Len, str, int, type and float function in python at codeLent





A function is a named sequence of statements that performs a computation. When you define a function, you specify the name and the sequence of statements. Later, you can “call” the function by name. We have already seen one example of a function call:

>>> type(32)

<type 'int'>

The name of the function is type. The expression in parentheses is called the argument of the function. The result, for this function, is the type of the argument.  It is common to say that a function “takes” an argument and “returns” a result. The result is called the return value.

Python provides built-in functions that convert values from one type to another. The int

function takes any value and converts it to an integer, if it can, or complains otherwise:

>>> int('32')

32

>>> int('Hello')

ValueError: invalid literal for int(): Hello


int can convert floating-point values to integers, but it doesn’t round off; it chops off the

fraction part:


>>> int(3.99999)

3

>>> int(-2.3)

-2


float converts integers and strings to floating-point numbers:

>>> float(32)

32.0

>>> float('3.14159')

3.14159


Finally, str converts its argument to a string:

>>> str(32)

'32'

>>> str(3.14159)

'3.14159'


>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

Take note of this descriptions

value: One of the basic units of data, like a number or string, that a program manipulates.

type: A category of values. The types we have seen so far are integers (type int), floatingpoint numbers (type float), and strings (type str).


integer: A type that represents whole numbers.


floating-point: A type that represents numbers with fractional parts.


string: A type that represents sequences of characters.


variable: A name that refers to a value.


statement: A section of code that represents a command or action. So far, the statements we have seen are assignments and print statements.


assignment: A statement that assigns a value to a variable.


state diagram: A graphical representation of a set of variables and the values they refer to.

keyword: A reserved word that is used by the compiler to parse a program; you cannot

use keywords like if, def, and while as variable names.


operator: A special symbol that represents a simple computation like addition, multiplication, or string concatenation.

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

If you want to concatenate an integer such as 29 with a string to pass to print(), you’ll need to get the value '29', which is the string form of 29. The str() function can be passed an integer value and will evaluate to a string value version of it, as follows:

>>> str(29)

'29'

>>> print('I am ' + str(29) + ' years old.')

I am 29 years old.

Because str(29) evaluates to '29', the expression 'I am ' + str(29) + ' years old.' evaluates to 'I am ' + '29' + ' years old.', which in turn evaluates to 'I am 29 years old.'. This is the value that is passed to the print() function.

The str(), int(), and float() functions will evaluate to the string, integer, and floating-point forms of the value you pass, respectively. Try converting some values in the interactive shell with these functions, and watch what happens.

>>> str(0)

'0'

>>> str(-3.14)

'-3.14'

>>> int('42')

42

>>> int('-99')

-99

>>> int(1.25)

1

>>> int(1.99)

1

>>> float('3.14')

3.14

>>> float(10)

10.0

The previous examples call the str(), int(), and float() functions and pass them values of the other data types to obtain a string, integer, or floating-point form of those values.

The str() function is handy when you have an integer or float that you want to concatenate to a string. The int() function is also helpful if you have a number as a string value that you want to use in some mathematics. For example, the input() function always returns a string, even if the user enters a number. Enter spam = input() into the interactive shell and enter 101 when it waits for your text.

>>> spam = input()

101

>>> spam

'101'

The value stored inside spam isn’t the integer 101 but the string '101'. If you want to do math using the value in spam, use the int() function to get the integer form of spam and then store this as the new value in spam.

>>> spam = int(spam)

>>> spam

101

Now you should be able to treat the spam variable as an integer instead of a string.

>>> spam * 10 / 5

202.0

Note that if you pass a value to int() that it cannot evaluate as an integer, Python will display an error message.

>>> int('99.99')

Traceback (most recent call last):

  File "<pyshell#18>", line 1, in <module>

    int('99.99')

ValueError: invalid literal for int() with base 10: '99.99'

>>> int('twelve')

Traceback (most recent call last):

  File "<pyshell#19>", line 1, in <module>

    int('twelve')

ValueError: invalid literal for int() with base 10: 'twelve'

The int() function is also useful if you need to round a floating-point number down. If you want to round a floating-point number up, just add 1 to it afterward.

>>> int(7.7)

7

>>> int(7.7) + 1

8

In your program, you used the int() and str() functions in the last three lines to get a value of the appropriate data type for the code.

⑥ print('What is your age?') # ask for their age

   myAge = input()

   print('You will be ' + str(int(myAge) + 1) + ' in a year.')

The myAge variable contains the value returned from input(). Because the input() function always returns a string (even if the user typed in a number), you can use the int(myAge) code to return an integer value of the string in myAge. This integer value is then added to 1 in the expression int(myAge) + 1.

The result of this addition is passed to the str() function: str(int(myAge) + 1). The string value returned is then concatenated with the strings 'You will be ' and ' in a year.' to evaluate to one large string value. This large string is finally passed to print() to be displayed on the screen.

Let’s say the user enters the string '4' for myAge. The string '4' is converted to an integer, so you can add one to it. The result is 5. The str() function converts the result back to a string, so you can concatenate it with the second string, 'in a year.', to create the final message. These evaluation steps would look something like Figure 1-4.

Text and Number Equivalence

Although the string value of a number is considered a completely different value from the integer or floating-point version, an integer can be equal to a floating point.

>>> 42 == '42'

False

>>> 42 == 42.0

True

>>> 42.0 == 0042.000

True

 happy coding seasoning

0 comments:

Post a Comment