01. Document and Structure Code 1 Flashcards

1
Q

Explain the main purpose of using a function in a program.

A

The main purpose of functions is to group code that is executed multiple times.

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

The function below makes use of the ‘name’ variable.

  1. In the context of a function, what is its correct name?
  2. When you call a function and pass a value, what is its correct name?

def hello(name):
print(‘Hello ‘ + name)

hello(‘Alice’)
hello(‘Bob’)

A
  1. A parameter = the variable inside the function.
  2. An argument = the value passed in the function call.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Describe what is happening with the following code example.

def plusOne(number):
return number + 1

newNumber = plusOne(5)
print(newNumber)

A
  1. The function, plusOne is being called and passed the value of 5. This is the argument.
  2. The functions parament ‘number’ is then assigned the value of 5.
  3. The return statement then adds 1 to this parameter resulting in the value of 6.
  4. The value of 6 is then returned and assigned to the variable ‘newNumber’
  5. The print statement will then print the value of 6.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What does the end= keyword do in python?

print(‘Hello’, end=”?”)
print(‘World’)

A
  1. By default, the print statement in python is set with the newline character.
  2. This can be suppressed using the end= keyword.
  3. So in the example above, the two words would print on the same line and only be separated by the question mark, i.e. Hello?World.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What does the sep keyword do in python?

print(‘cat’, ‘rat’, ‘dog’ , sep=’ABC’)

A
  1. For print statements, the separator character defaults to a single space character.
  2. This can be changed using sep=
  3. Using the example of print(‘cat’, ‘rat’, ‘dog’ , sep=’ABC’)
  4. The print statement output would be catABCratABCdog
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What scope do the following variables have?

spam = 42

def eggs():
spam = 42

print(‘Anything’)

A

spam = 42 # global variable

def eggs():
spam = 42 # local variable

print(‘Anything’)

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

What type of code should be indented?

A

Anything inside of a routine, such as a function, loop, or decision should be indented.

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

What symbol is used to insert a comment into python?

A

The pound or hash symbol, i.e. #

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

What is the function of the following command

C:\Users\admin>python -m pydoc pass

A

In Python, Pydoc is a help/documentation module. You can use this on your Windows terminal to understand what a function in python does.

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

What does the def keyword do in Python?

A

The def keyword means define, or in other words, “I’m starting a function”.

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

Is python picky about where functions are written?

A

Yes, python functions must be written before they are called.

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