CodeAcademy Python Flashcards

You may prefer our related Brainscape-certified flashcards:
1
Q

How do you define a function in python?

A

def your_function_name()

the values that go inside can return a value

so when the function is called, a value is returned.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

How do you ensure that the program gives you output with decimal places when dividing an integer

A

you have to add the decimal place to the integer you are dividing.

e.g. 15 -> 15.0

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

How do you write strings with apostrophes?

A

Use \ before the apostrophe

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

If you wanted to output the character n in Python, how would you do that?

A

Since counting starts at 0, n would be the 5th character, and thus you would write Python[5]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

How do you find the length of a string?

And how would you print it ?

A

print len(parrot)

the input into len can be variable or a string

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

How do you turn a string into all lowercase?

A

add .lower() behind the string

e.g. parrot = “Norwegian Blue”.lower()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Why is dot notation limited to upper() and lower()?

A

because they can only be applied to strings

upper and lower are only used for strings

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

How do you do string substitution?

A

%s for where you want to substitute in the string and % before the set of variables you want to substitute in.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

How would you go about outputting date and time

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is the not equal sign?

A

!=

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What does the boolean operator not do ?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

what is the order of operation for boolean operators?

A

Boolean operators aren’t just evaluated from left to right. Just like with arithmetic operators, there’s an order of operations for boolean operators:

  1. not is evaluated first;
  2. and is evaluated next;
  3. or is evaluated last.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

what is the outcome of this boolean?

True or not False and False

A

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,

How well did you know this?
1
Not at all
2
3
4
5
Perfectly