Chapter 4 : Procedures Flashcards

(31 cards)

1
Q

What is a python ‘if’ structure made out of?

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

What must a header have?

A
  • The word if
  • A logical test
  • A colon
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is the header?

A

It controls the loop and sets the exit condition for the loop.

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

What is the body?

A

It is the commands that belong inside the loop. These commands are indented under the header.

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

What is a nested structure?

A

When a program structure is inside of another program structure. It is shown by a double indentation.

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

What is a module?

A

It is a ready-made block of code with a name that you can use in your program.

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

What is a procedure?

A

A procedure is a type of module that you can make yourself.

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

How can you use procedures?

A
  1. Define the procedure. Give it a name and store commands inside of it.
  2. Call the procedure. Put the name of the procedure in your prograam. All stored commands would be carried out.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

!! What is the structure of a procedure header?

A
  • The word def
  • The name of the procedure
  • Open and closed brackets
  • Colon
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

!! What must you remember when choosing a procedure name?

A
  • It must be a single word
  • It can only contain letters, numbers, and the underscore symbol.
  • It must begin with a letter.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

State 4 advantages of using modular coding.

A
  • Storing code is easier and makes your program shorter and easier to read.
  • Work can be shared among a team or group.
  • Code stored in a procedure can be stored and reused, saving time and effort.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Give 2 examples of built-in functions in python.

A
  • int()
  • print()
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What is modular programming?

A

It is using modules and procedures in your programming.

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

What is a search term?

A

A search term is the item you are looking for in a list.

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

What are the commands used in a binary search?

A
  • Sort the list
  • Find the midpoint of the list
  • Split the list in half at the midpoint.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What is the code for sorting a list?

A

listname.sort()

17
Q

What is a ‘slice’?

A

It is a part of a list. When you split a list in 2 halves, each half is called a slice.

18
Q

What is the code to find the slice from the 1st element to the 3rd element?

A

print(atoms [0:3])

19
Q

!! Give an example of a procedure.

A

def welcome()
print ( “Hello, welcome” )
print ( “=” * 20)

20
Q

What does it mean to ‘call the procedure’?

A

Calling the procedure means you put the name of the procedure into your program.

21
Q

What should the name of a procedure be?

A

The name should remind you of what the procedure does.

22
Q

How do you call call a procedure?

A

By adding the name of the procedure followed by an open and closed bracket.

e.g. linsearch()

23
Q

What does the relational operator ‘in’ do?

A

It compares a single value and a list. If the value is ‘in’ the list, then the test is true. If it is not ‘in’ the list, the test is false.

24
Q

What are 2 methods that cna be used to search a list?

A
  • Binary Search
  • Linear search
25
What is the code to find an element in a list using the relational operator 'in'?
topiclist = ["A", "B", "C", "D,"] topic = input ("enter a topic.") if topic in atoms: print (topic, "is in the list")
26
What is the code to find an element in a list using the relational operator 'in' using the if... else... verson.
topiclist = ["A", "B", "C", "D,"] topic = input ("enter a topic.") if topic in atoms: print (topic, "is in the list") else: print (topic, "is not in the list.")
27
What is the code to find an element in a list by using a linear search?
topic = input ("enter a topic") stop = len(topics) for i in range (stop): if atoms[i] == topic: print (topic, "is found in the list")
28
What are the steps in a linear search algorhithm?
1. The for loop to counts through all the items in the list. 2. It compares each value to the search term. 3. If there is an exact match, it displays a message saying the item is found.
29
What are the steps in a binary search algorhithm?
1. Sort the list into order 2. Find the midpoint of the list 3. Compare the search term to the midpoint if it is higher or lower. 4. Split the list in half and keep the higher or lower half. 5. Keep splitting the list in half until it only has 1 item.
30
How does a linear search work? What are the advantages and disadvantages?
How it works : It looks through every item in the list until it finds the search term. Advantage : Can be used when the list is not sorted. Disadvantage : Can be slow if the list has a lot of items.
31
How does a binary search work? What are the advantages and disadvantages?
How it works : Split the list in half over and over again until it only has one item? Advantage : Uses fewer operations and is faster. Disadvantage : Can only be used if the list is sorted.