Busy. Please wait.
or

show password
Forgot Password?

Don't have an account?  Sign up 
or

Username is available taken
show password

why


Make sure to remember your password. If you forget it there is no way for StudyStack to send you a reset link. You would need to create a new account.
We do not share your email address with others. It is only used to allow you to reset your password. For details read our Privacy Policy and Terms of Service.


Already a StudyStack user? Log In

Reset Password
Enter the associated with your account, and we'll email you a link to reset your password.

Remove ads
Don't know
Know
remaining cards
Save
0:01
To flip the current card, click it or press the Spacebar key.  To move the current card to one of the three colored boxes, click on the box.  You may also press the UP ARROW key to move the card to the "Know" box, the DOWN ARROW key to move the card to the "Don't know" box, or the RIGHT ARROW key to move the card to the Remaining box.  You may also click on the card displayed in any of the three boxes to bring that card back to the center.

Pass complete!

"Know" box contains:
Time elapsed:
Retries:
restart all cards




share
Embed Code - If you would like this activity on your web page, copy the script below and paste it into your web page.

  Normal Size     Small Size show me how

Function

Python Function

TermDefinition
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.
Created by: kbaldwin