Explain how python programs are structured and give an example of how methods are initiated

Python programs like most declarative programming languages are made up of small subroutines called "methods". To set-up an example program, you need to define a method (eg. def someMethod(): ) and then specify what you want that method to do. For example, it could print something to the console (like: print("Hello World!")) or it could perform a sequence of steps (aka an algorithm). In order to run the program, you need to declare the method in a separate section of the program (i.e. outside of the method you've defined) - typically in a main method (def main():) and then a declaration is needed to initialise the main method (if name = "main":).

PO

Related Python Mentoring answers

All answers ▸

When do we use a for-loop and when a while-loop?


How do you write code to implement a recursive fibonacci algorithm?


Write a recursive function that takes any integer n and prints the nth Fibonacci number.


Write a function that takes a list of numbers as input, and outputs the average of the numbers. The function should catch any errors.