Chapter 4 : Procedures Flashcards
(31 cards)
What is a python ‘if’ structure made out of?
- A header
- A body
What must a header have?
- The word if
- A logical test
- A colon
What is the header?
It controls the loop and sets the exit condition for the loop.
What is the body?
It is the commands that belong inside the loop. These commands are indented under the header.
What is a nested structure?
When a program structure is inside of another program structure. It is shown by a double indentation.
What is a module?
It is a ready-made block of code with a name that you can use in your program.
What is a procedure?
A procedure is a type of module that you can make yourself.
How can you use procedures?
- Define the procedure. Give it a name and store commands inside of it.
- Call the procedure. Put the name of the procedure in your prograam. All stored commands would be carried out.
!! What is the structure of a procedure header?
- The word def
- The name of the procedure
- Open and closed brackets
- Colon
!! What must you remember when choosing a procedure name?
- It must be a single word
- It can only contain letters, numbers, and the underscore symbol.
- It must begin with a letter.
State 4 advantages of using modular coding.
- 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.
Give 2 examples of built-in functions in python.
- int()
- print()
What is modular programming?
It is using modules and procedures in your programming.
What is a search term?
A search term is the item you are looking for in a list.
What are the commands used in a binary search?
- Sort the list
- Find the midpoint of the list
- Split the list in half at the midpoint.
What is the code for sorting a list?
listname.sort()
What is a ‘slice’?
It is a part of a list. When you split a list in 2 halves, each half is called a slice.
What is the code to find the slice from the 1st element to the 3rd element?
print(atoms [0:3])
!! Give an example of a procedure.
def welcome()
print ( “Hello, welcome” )
print ( “=” * 20)
What does it mean to ‘call the procedure’?
Calling the procedure means you put the name of the procedure into your program.
What should the name of a procedure be?
The name should remind you of what the procedure does.
How do you call call a procedure?
By adding the name of the procedure followed by an open and closed bracket.
e.g. linsearch()
What does the relational operator ‘in’ do?
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.
What are 2 methods that cna be used to search a list?
- Binary Search
- Linear search