1. Function:Print
Print a sequence of arguments for a user to read. Multiple arguments are separated by spaces.
a. During execution of a function call, if the end of the function body is reached without executing a return statement, that function call produces a value None.
eg.
>>> def square_return(num):
return num**2
return_answer=square_return(2) //Nothing happens! Type!
return_answer
4
num(2)-3 //This is a numeric.
1
>>> def square_print(num):
print "The square answer is" num**2
print_answer=print_answer(2)
The square answer is 4
num(2)-3 //This is not a numeric.
Error
Hint: Remenber that the print funxtion only prints a value onto the screen. A function that does not have a return statement returns None.
2. Function: Input
input: get a string from the user.
The argument to input is called a prompt.
The functin pauses until the user types a newline.
The return value is the string the user typed.
eg.
>>> input("What is your name?")
What is your name?Jen
'Jen'
>>> name = input("What is your name? ")
What is your name? Jen
>>> name
'Jen'
>>> location = input("What is your location? ")
What is your location? Toronto
>>> name
'Jen'
>>> location
'Toronto'
>>> print(name, "lives in", location)
Jen lives in Toronto
Triple-quoted strings
We have used single- and double- quotes to represent strings. The third string format uses triple-quotes and a triple-quoted string cab span multiple lines. For example:
>>> print(''' How
are
you?''')
How
are
you?
Escape Sequences
Python has a special character called an escape character:\. When the escape character is used in a string, the character. When the escape character is used in a string, the character following the escape character is treated differently from normal. The escape character together with the character that follows it is an escape sequence.
Escape Sequence | Name | Example | Output |
\n | newline (ASCII linefeed - LF) | print('''How | How |
\t | tab (ASCII horizontal tab - TAB) | print('3\t4\t5') | 3 4 5 |
\\ | backslash (\ ) | print('\\') | \ |
\' | single quote (' ) | print('don\'t') | don't |
\" | double quote (" ) | print("He says, \"hi\".") | He says, "hi". |