CodeAcademy Python Flashcards
How do you define a function in python?
def your_function_name()
the values that go inside can return a value
so when the function is called, a value is returned.
How do you ensure that the program gives you output with decimal places when dividing an integer
you have to add the decimal place to the integer you are dividing.
e.g. 15 -> 15.0
How do you write strings with apostrophes?
Use \ before the apostrophe
If you wanted to output the character n in Python, how would you do that?
Since counting starts at 0, n would be the 5th character, and thus you would write Python[5]
How do you find the length of a string?
And how would you print it ?
print len(parrot)
the input into len can be variable or a string
How do you turn a string into all lowercase?
add .lower() behind the string
e.g. parrot = “Norwegian Blue”.lower()
Why is dot notation limited to upper() and lower()?
because they can only be applied to strings
upper and lower are only used for strings
How do you do string substitution?
%s for where you want to substitute in the string and % before the set of variables you want to substitute in.
How would you go about outputting date and time
first you need to import the date and time
you do that using
from datetime import datetime
Now you can define datetime as a variable for easier use.
now = datetime**.now()
datetime needs to be followed by .now()
Next, you need to substitute the data in a string
print ‘%s/%s/%s %s:%s:%s‘ % (now.month, now.day, now.year, now.hour, now.minute, now.second)
%s is where you substitute in, % precedes what you want to substitute in.
What is the not equal sign?
!=
What does the boolean operator not do ?
The boolean operator not returns True for false statements and False for true statements.
For example:
not False will evaluate to True, while not 41 > 40 will return False.
what is the order of operation for boolean operators?
Boolean operators aren’t just evaluated from left to right. Just like with arithmetic operators, there’s an order of operations for boolean operators:
- not is evaluated first;
- and is evaluated next;
- or is evaluated last.
what is the outcome of this boolean?
True or not False and False
True
True or not False and False. not gets evaluated first, so we have True or True and False. and goes next, so we get True or False. As we’ve seen, True or False is True,