Understanding Python Functions and Syntax
Remember that once you have properly understood and learn to use either of them, you can easily learn the changes between the two versions and adapt easily. The hard part is learning programming and understanding the core Python language itself. That is our goal in this book, and once you have achieved that goal, you can easily use Python 2 or Python 3 depending on your situation.
From Name to Result
A function gives a piece of behavior a name so that it can be used through a function call. The source introduces a function called say_hello that takes no parameters. It also explains the role of parameters: they act as inputs, allowing different values to be passed to a function so that it can produce corresponding results.
The central pattern is input moving into a function and a result coming back from it. A function with no parameters still follows the same general idea; it simply has no input values declared in its parentheses.
Reading a Function Definition
When the source defines say_hello, it describes a function with no parameters. The empty parentheses are meaningful: they show that no input variables are declared for this function. When parameters are present, they represent inputs that can receive different values during function calls.
A Function Without Parameters
Interpret the function described in the source: say_hello has no parameters.
Identify the name: say_hello is the name assigned to the function.
Inspect the parentheses: The parentheses contain no variables, so this function declares no parameters.
Consider the input: Because no parameters are declared, the function does not receive different input values through its parameter list.
Consider the result: The function can still be called as a function; the source presents functions as operations that can return results.
The important syntax idea is that an empty parameter list means the function takes no declared parameters.
Following a Function Call
A function call uses the function that has been defined. At a high level, control moves from the calling code to the function, the function works with its available inputs, and a result can move back to the caller. This is why parameters matter: they provide a way for the caller to pass different values and receive corresponding results.
Suppose a function is designed to process a value supplied by its caller. Calling it with one value and then another lets the same function be used with different inputs. The source describes this as passing in different values and getting back corresponding results. The function's name stays the same; the input value changes.
Names, Modules, and Methods
Dot notation is the syntax for calling a function in another module by writing the module name, a dot, and the function name.
A method call is similar to a function call because it takes arguments and returns a value, but its syntax differs. The method name is appended to a variable name, with a period used as the delimiter. This gives you two related patterns to recognize: a function can be called by its function name, while a method is called through a variable name followed by a period and the method name.
| Construct | How it is identified | What to remember |
|---|---|---|
| Function | Called through the function name | It can take arguments and return a value. |
| Function in another module | Module name followed by a period and function name | This is called dot notation. |
| Method | Variable name followed by a period and method name | Its call is similar to a function call, but its syntax is different. |
The source distinguishes ordinary function calls, module dot notation, and method calls.
Mistakes Beginners Make
Changing the capitalization of a function name
The source specifically warns that misspelling print as Print, including the capitalization change, causes Python to raise a syntax error.
Fix:
Check the spelling and capitalization of the function name exactly.Treating a method call as identical to an ordinary function call
The source says that methods are similar to functions but use different syntax: the method name is appended to a variable name with a period.
Fix:
Look for the variable name, period, and method name as one connected call pattern.Assuming an empty parameter list accepts an input
The source explains that say_hello takes no parameters because no variables are declared in its parentheses.
Fix:
Read the parentheses before deciding whether a function declares parameters.
Practice the Trace
For each situation, identify the relevant function idea: a function has no variables in its parentheses; a caller supplies a value that the function uses; a function is reached through a module name and a period; a method name follows a variable name and a period.
Hints
- Empty parentheses indicate that no parameters are declared.
- Parameters are inputs that allow different values to be passed.
- A module name followed by a period is dot notation.
- A variable name followed by a period and a method name identifies the method-call pattern.
What do you think happens?
A learner changes print to Print. What does the source say Python does?
Reveal answer
Answer: It raises a syntax error.
The source explicitly identifies the capitalization change from print to Print as a mistake that causes Python to raise a syntax error.
Core Skills Across Versions
The source emphasizes that learning programming and understanding the core Python language is the difficult and valuable part. Once those foundations are understood, learning the changes between Python 2 and Python 3 becomes easier, and a learner can adapt to either version depending on the situation.
Focus first on the underlying ideas: functions provide named behavior, parameters provide inputs, calls use functions, and returned values provide results. Version differences are easier to learn after these core concepts are understood.
Key Takeaways
- Parameters are function inputs; an empty parameter list means that no parameters are declared.
- A function call transfers control to the function, where inputs can be used and a result can return to the caller.
- Dot notation uses a module name, a period, and a function name to reach a function in another module.
- A method call places the method name after a variable name and a period, so its syntax differs from an ordinary function call.
- Strong understanding of core Python concepts makes Python 2 and Python 3 differences easier to learn.