Save
Busy. Please wait.
Log in with Clever
or

show password
Forgot Password?

Don't have an account?  Sign up 
Sign up using Clever
or

Username is available taken
show password


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.
Your email address is only used to allow you to reset your password. See 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.
focusNode
Didn't know it?
click below
 
Knew it?
click below
Don't Know
Remaining cards (0)
Know
0:00
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
Popular Computers sets

 

 



Voices

Use these flashcards to help memorize information. Look at the large card and try to recall what is on the other side. Then click the card to flip it. If you knew the answer, click the green Know box. Otherwise, click the red Don't know box.

When you've placed seven or more cards in the Don't know box, click "retry" to try those cards again.

If you've accidentally put the card in the wrong box, just click on the card to take it out of the box.

You can also use your keyboard to move the cards as follows:

If you are logged in to your account, this website will remember which cards you know and don't know so that they are in the same box the next time you log in.

When you need a break, try one of the other activities listed below the flashcards like Matching, Snowman, or Hungry Bug. Although it may feel like you're playing a game, your brain is still making more connections with the information to help you out.

To see how well you know the information, try the Quiz or Test activity.

Pass complete!
"Know" box contains:
Time elapsed:
Retries:
restart all cards