Arguments in Python
An operator sometimes can be replaced by a name.
- Evaluation
-( 9 )
-9
- Protokoll
from operator import neg
neg( 9 )
-9
»neg( 9 )« actually is a call to the callable »neg«. But now the parentheses have an expression inside them. Such an expression within the parentheses of a call expression is known as an argument of the call expression.
A note on formatting style
I write »-( 9 )« and »neg( 9 )« with two spaces inside the parentheses, but the most common way is to write such calls without those spaces.
Ways of speaking
- call with argument
neg( 9 )
- Ways of speaking
- (the function) »neg« is called with nine (as an argument)
- (the function) »neg« is applied to nine (as an argument)
- (the function) »neg« accepts nine (as an argument)
- »neg« (of) 9
Syntax
- Call syntax (simplified)
expression
.------------. .-. .------------. .-.
--->| expression |--->( ( )---.--->| expression |---.--->( ) )--->
'------------' '-' | '------------' ^ '-'
'---------------------'
Semantics for calls with an argument expression
The value of the argument expression is called the argument value.
When a call is evaluated, the argument expression is evaluated first. Then the callee is activated, passing it the argument value.
Practice questions
? Pronunciation
How would you read the following text to someone on the phone (if it would appear in a longer Python program that you were reading aloud)?
- Expression
abs( 2 )
? Value
The function »abs« from the module »builtins« returns the absolute value of its argument value. The absolute value of a number is its distance from «0».
What is the value of the following call?
- Expression
abs( 2 )
? Arguments
What is the argument expression of the following call?
What is the argument value of the the following call?
- Console input
from math import floor
floor( 2 + 3.2 )
? Arguments
What is the argument expression of the following call?
What values can the argument expression have?
- Console input
from math import floor
floor( random() * 4 )
The floor of a value is the largest integer that is smaller than or equal to the value.
What values can the whole expression therefore have?
Exercises
/ Exercise
Write a call to the floor function »floor« with the argument expression »1.1 + 7.0«.
/ Exercise
Write an expression the value of which is an integer that is either 0 or 1 for each evaluation. The probabilities for each of the two values should be approximately equal.
/ Exercise
Write an expression the value of which is an integer between 1 (inclusive) and 6 (inclusive) for each evaluation. The probabilities for each of the values should be approximately equal.
Practice questions regarding nested expressions
? Order of activations
When evaluating the expression »abs( neg( 0 ))«, which function is executed first?
? Pronunciation
How would you read the »abs( neg( 2 ))« to someone on the phone (if it would appear in a longer Python program that you were reading aloud)?
? Value
What is the value of »abs( neg( 2 ))«?
Exercises regarding nested expressions
- Importe
from math import floor, sin, cos
from operator import neg
from random import random
? Writing expressions
Write an expression for the noun phrase „the floor of the the negative of three“ using the functions »floor« and »neg«.
? Writing noun phrases
Write an English noun phrase for the expression »sin( cos( 0 ))«.
? Writing noun phrases 1
Write an English noun phrase for the expression »floor( random() * 4 )«.
The function ›len‹
The function ›len‹ returns a number for some objects, which indicates the number of entries of the object (“entries” in a broad sense).
- Evaluation
len( 'v' )
1
- Evaluation
len( 'gamma' )
5
- Evaluation
len( 'γάμμα' )
5
- Evaluation
len( '' )
0
- Evaluation
len( input() )
abc
3
- A possible pronunciation of »len( input() )«
- „length of the input“
- Console transcript (shortened)
len( 2 )
TypeError: object of type 'int' has no len()
The number of attributes in the binding table of the current module:
- Evaluation
len( dir() )
7
The function ›print‹
- Evaluation
'abc'
'abc'
- Evaluation
print( 'abc' )
abc
- Two common object representations
expression --------------> object ------------------------.-----> repr representation
evaluation text representation | for example, 'abc'
|
'-----> str representation
for example, abc
The semantics of ›print‹
Effect The effect of a call is the output of the str representation of the argument value.
Value The value of a call is ›None‹.
Printing ›None‹
- Evaluation
None
- Evaluation
print( None )
None
›print‹ printing the value of a print call
- Evaluation
print( print( 'a' ))
a
None
›print‹ printing ›print‹
- Evaluation
print( print )
<built-in function print>
Printing special characters
Inserting a line terminator
- Evaluation
print( 'abc\ndef' )
abc
def
Inserting a literal backslash into a string
- Evaluation
print( 'abc\\ndef' )
abc\ndef
- Evaluation
len( '\n' )
1
- Evaluation
print( '\n' )
(blank line)
- Evaluation
len( '\\' )
1
- Evaluation
print( '\\' )
\
- Console transcript
from os import getcwd
getcwd()
'C:\\Users\\example\\AppData\\Local\\Programs\\Python\\Python39'
- Console transcript
from os import getcwd
print( getcwd() )
C:\Users\example\AppData\Local\Programs\Python\Python39
- Evaluation
print( 'x"x' + "o'o" )
x"xo'o
- Evaluation
'x"x' + "o'o"
'x"xo\'o'
Oddity:
- Evaluation
print( '\d' )
\d
Printing multi-line strings
- Transcript
from inspect import getdoc
getdoc( input )
'Read a string from standard input. The trailing newline is stripped.\n\nThe prompt string, if given, is printed to standard output without a\ntrailing newline before reading input.\n\nIf the user hits EOF (*nix: Ctrl-D, Windows: Ctrl-Z+Return), raise EOFError.\nOn *nix systems, readline is used if available.'
print( _ )
Read a string from standard input. The trailing newline is stripped.
The prompt string, if given, is printed to standard output without a
trailing newline before reading input.If the user hits EOF (*nix: Ctrl-D, Windows: Ctrl-Z+Return), raise EOFError.
On *nix systems, readline is used if available.- Transcript
from inspect import getdoc
print( getdoc( input ))
Read a string from standard input. The trailing newline is stripped.
The prompt string, if given, is printed to standard output without a
trailing newline before reading input.If the user hits EOF (*nix: Ctrl-D, Windows: Ctrl-Z+Return), raise EOFError.
On *nix systems, readline is used if available.
Types of what is printed
Objects have a type, but what is printed is always text (it has no type). So to “print the character 2”, one can evaluate any of the following expressions.
- Evaluation
print( 2 )
2
- Evaluation
print( '2' )
2
- Evaluation
print( 12.000 )
12.0
- Evaluation
print( "12.000" )
12.000
? Terminology
Use one form of each of the following verbs (lemmas): “pass”, “print”, “accept”, “read”, “deliver”, and “return”.
One uses »input« to __________ a string.
One uses »print« to __________ a value.
In »f( 5 )«, the value «5» is __________ to the function ›f‹ .
In »f( 5 )«, the function ›f‹ __________ the value «5».
»input« __________ a str value.
- An argument value is passed to a function. You can also say that the function accepts the argument value.
- »print« is used to print values.
- »input« is used to read a string from standard input
- A ₍rɪˈzʌlt₎ result value is returned or delivered by a called function.
? Evaluation statements
Write a statement that causes an evaluation of the expression »print( 2 )« when it is executed.
The function ›pprint‹
›pprint‹ can be used to pretty print some objects. Sometimes it will choose to use a multi-line representation for readability.
- Console transcript
from pprint import pprint
pprint( dir() )
['__annotations__',
'__builtins__',
'__doc__',
'__loader__',
'__name__',
'__package__',
'__spec__',
'pprint']- Console transcript
from pprint import pprint
pprint( vars() )
{'__annotations__': {},
'__builtins__': <module 'builtins' (built-in)>,
'__doc__': None,
'__loader__': <class '_frozen_importlib.BuiltinImporter'>,
'__name__': '__main__',
'__package__': None,
'__spec__': None,
'pprint': <function pprint at 0x0000000001E923A0>}
Raw string literals
A raw string literals starts with the letter »r«. Backslashes usually represent themselves, but must not appear in front of the last character of the literal.
- Ausgabeanweisung
print( r"'[Ljava.lang.Object; does not exist\nat com.example'" )
'[Ljava.lang.Object; does not exist\nat com.example'
- Ausgabeanweisung
print( r"\1" )
\1
- Ausgabeanweisung
print( r"1\" )
SyntaxError: EOL while scanning string literal
Formatting string literals
- Evaluation
f'abc'
'abc'
- Evaluation
f'abc{ 1 + 1 }'
'abc2'
- Evaluation
f'abc{1+1}'
'abc2'
- Evaluation
f'abc{1+1=}'
'abc1+1=2'
- Evaluation
f'abc{ 1 + 1 = }'
'abc 1 + 1 = 2'
- Console transcript
from math import pi
f'{pi}'
'3.141592653589793'
- Console transcript
from math import pi
f'{pi:.3f}'
'3.142'
- Console transcript
from math import pi
f'{pi=:.3f}'
'pi=3.142'
- Console transcript
from math import pi
f'{20:x}'
'14'
- Console transcript
f'{"abcdef":<10}'
'abcdef '
- Console transcript
f'{"abcdef":>10}'
' abcdef'
- Console transcript
f'{"abcdef":10}'
'abcdef '
- Console transcript
f'{"abcdef":.3}'
'abc'
- Console transcript
f'{"abcdef":10.3}'
'abc '
A formatting sublanguage (domain-specific language ) (one could see “2.4.3” [as used below] as belonging to a sublanguage of English).
- more …
The Python Language Reference, Release 3.8.0a0, section 2.4.3 Formatted string literals
The online manual for Python libraries
The function »help« can show manual pages for some keywords, for example, one can use »help( 'FORMATTING' )« to learn more about the formatting sublanguage. For more keywords, use: »help( 'topics' )«.
You can also get help on functions and classes.
- Console input
help( str )
- Console input
from random import random
help( random )
But you do not have to read those manual pages now.