Back Python Chrestomathics Ahead |
Part 6 — Some other built-in functions
Some more additions — For your programming toolbox — type, id, round, escape
Section 6.1: Functions type() and id()
- One of the ways Python differs from some sibling langauges is that all values have object representations. If you explore Python in depth, you will discover even integers and decimals have some method functions. However, most programmers never use these methods.
- Back in Part 03, we presented a depiction of a frame — a memory table kept by Python for the variables currently in use. A variable entry records its name, type, and identity (indicator of its memory location). The depiction is repeated below.
Identifier | Type | Identity |
---|---|---|
distance | integer | #459720864 |
hours | integer | #459721184 |
speed | decimal | #459726944 |
- Python provides two built-in functions
type()
andid()
to access frame information.
- Function
type()
, when given a variable as an argument, will return the variable’s type.
- Function
id()
, when given a variable as an argument, will return its identity; that is, an indicator of the variable’s memory location.
- Consider the follow code segment from type_id.py. The program displays the type and id information for a string, integer, and decimal variables.
# get variable values
reply = input( "Enter integer and decimal: " )
n, x = reply.split()
n = int( n )
x = float( x )
print()
# display variable characteristics
print( "reply:", type( reply ), id( reply ) )
print( "n:", type( n ), id( n ) )
print( "x:", type( x ), id( x ) )
- The output of two program runs is reproduced below. Both runs have the same input. The runs show that different memory was used to store the variables in the different runs. That is not surprising. Other apps are running on my machine and they have changing memory needs.
Enter integer and decimal: 1112 2.5
reply: <class 'str'> 140191214505136
n: <class 'int'> 140191214778256
x: <class 'float'> 140191214778448
Enter integer and decimal: 1112 2.5
reply: <class 'str'> 140162385530032
n: <class 'int'> 140162385803152
x: <class 'float'> 140162385803344
Section 6.2: Function round()
- Function
round()
is another helpful built-in function. The function works with either one or two arguments.
- If there is only one argument, the function returns the nearest integer to the argument.
- If there are two arguments, the first is the value of interest and the second is the number of digits of precision needed. The function returns the value rounded to the indicated number of digits after the decimal point.
- The below code from program i_get_around.py demonstrates
round()
function usage.
# get input and convert to decimal
reply = input( "Enter number: " )
number = float( reply )
# compute eight versions of number
number6 = round( number, 6 ) # rounds to six places after the decimal
number5 = round( number, 5 ) # rounds to five places after the decimal
number4 = round( number, 4 ) # rounds to four places after the decimal
number3 = round( number, 3 ) # rounds to three places after the decimal
number2 = round( number, 2 ) # rounds to two places after the decimal
number1 = round( number, 1 ) # rounds to one place after the decimal
number0 = round( number, 0 ) # rounds to zero places after the decimal
# (produces a decimal)
rounded = round( number ) # rounds to an integer
- Here are runs of the program that demonstrate the function works as advertised.
Enter number: 3.1415926
number: 3.1415926
round( number, 6 ): 3.141593
round( number, 5 ): 3.14159
round( number, 4 ): 3.1416
round( number, 3 ): 3.142
round( number, 2 ): 3.14
round( number, 1 ): 3.1
round( number, 0 ): 3.0
round( number ): 3
Enter number: 2.7182818
number: 2.7182818
round( number, 6 ): 2.718282
round( number, 5 ): 2.71828
round( number, 4 ): 2.7183
round( number, 3 ): 2.718
round( number, 2 ): 2.72
round( number, 1 ): 2.7
round( number, 0 ): 3.0
round( number ): 3
Section 6.3: Escape sequences
- To support string creation, Python provides escape sequences. An escape sequence is a sequence of characters beginning with a backslash (
\
) whose meaning is different than the actual sequence entered. For example, the sequences\t
,\n
, and\"
represent respectively the tab, newline, and double quote characters. What is handy about the\"
escape sequence is that the double quote is not a string terminating quote. Python supports ten other escape sequences. The below table lists the five often-used escape sequences.
Escape sequence | Meaning |
---|---|
\\ | Backslash (\ ) |
\" | Double quote (" ) |
\' | Single quote (' ) |
\t | Tab |
\n | Newline |
- The below code from program escaping.py defines three variables and prints their values. The strings uses backslash escape sequences for the tab, newline, and double quote characters.
a = "x\ty\tz"
b = "Hello\nGoodbye"
c = "They said \"How are you\""
- When variable
a
is printed, there will be one tabstop amount of space separating characters x from y, and characters y and z. When variableb
is printed Hello and Goodbye will be printed on separate lines. When variablec
is printed, there will be quotes around How are you.
x y z
Hello
Goodbye
They said "How are you"
Section 6.4: What’s next
- We next turn our attention the creation, manipulation, and analyzing lists of values.
Back Python Chrestomathics Ahead |