click below
click below
Normal Size Small Size show me how
Function
Python Function
Term | Definition |
---|---|
module | A module is simply a file containing Python code. Every file containing Python code and whose file name ends in .py is a Python module. The code in a module is, of course, meant to be executed. |
print() | built-in, This function prints, within the interactive shell, whatever argument is given to it. |
input() | interact with the user Python treats as a string whatever the user types |
eval() | expect the user to enter a value that is not a string need to explicitly ask Python to evaluate what the user types as a Python expression |
eval( input() ) | expect the user to type an expression (a number, a list, etc.) when requested. wrap the eval() function around the input() function whatever the user types will be evaluated as an expression. >>> x = eval(input(‘Enter x: ’)) Enter x: 5 |
One-Way Decisions | if temp > 86: print(‘Its hot!’) print(‘Be sure to drink liquids.’) print(‘Goodbye.’) |
Two-Way Decisions | 1 temp = eval(input(‘Enter the current temperature: ’)) 2 3 if temp > 86: 4 5 print(‘It is hot!’) 8 else: 9 10 print(‘It is not hot.’) 13 print(‘Goodbye.’) |
Iteration Structures | |
sequences 2 types | A string can be viewed as a sequence of one-character strings; a list is a sequence of objects of any type (strings, numbers, even other lists). A task that is common to all sequences is to perform an action on every object in the sequence. |
spelling iteration | 1 name = input(‘Enter a word: ’) 2 print(‘The word spelled out: ’) 3 4 for char in name: 5 print(char) |
iterate over list | >>> animals = [‘fish’, ‘cat’, ‘dog’] >>> for animal in animals: print(animal) fish cat dog |
char | The variable char in for char in name: print(char) is just variable names, chosen to make the program more meaningful. Note: change the name of the for loop variable, we also need to change any occurrence of it in the body of the for loop. |
range() | built in function can iterate over the integers 0, 1, 2, 3, 4: >>> for i in range(5): print(i) 0 1 2 3 4 |
start, end, stop | >>> for i in range(1, 14, 3): print(i) The sequence printed by the for loop starts at 1, uses a step size of 3, and ends before 14. Therefore it will print 1, 4, 7, 10, and 13. |
print() versus return | So f() prints the computed value, but it does not return it. This means that f(2) returns nothing and thus evaluates to nothing in an expression. |
Function Definitions Are “Assignment” Statements | 1 s = input(‘Enter square or cube: ’) 2 if s == ‘square’: 3 def f(x): 4 return x*x 5 else: 6 def f(x): 7 return x*x*x The actual definition of f() depends on the input entered by the user at execution time. |
Comments | for understanding of: 1. for the user 2. for developer # comment Avoid by meaningful variable names, simple, well-designed code, makes program almost, self-explanatory. Comments should identify the main components, explain tricky parts. |
Docstrings | special comment added to the function definition, one that will be picked up by the help() tool. This is a string that should describe what the function does and must be placed directly below the first line of a function definition. |