APRENDE FUNCIONES en PYTHON: def, pass, sintaxis, None, return vs print, argumentos, scope y más
Understanding Functions in Python
Introduction to Functions
- A function is a block of code that allows for compacting and reusing code. It can receive data, process it, and return results.
- The syntax begins with the keyword
def, followed by the function name, parentheses for parameters, and a colon. This forms the header of the function.
Indentation in Python
- In Python, indentation (or "sangría") is used instead of braces to define blocks of code. Indenting indicates where a block starts and ends.
- The
passstatement can be used as a placeholder in functions to indicate that no action will be taken at that point.
Calling Functions
- To call a function, simply write its name followed by parentheses. Initially using
passmeans nothing happens when called.
- When modifying the function to greet someone, an argument (like
nombre) must be passed into the parentheses during the call.
Handling Function Arguments
- If an argument isn't provided when calling a function expecting one, an error occurs indicating that required data was not supplied.
- The concept of
Noneis introduced as a type representing absence; if no value is returned from a function, it defaults toNone.
Returning Values from Functions
- A new example demonstrates creating a function (
media) that calculates averages using three arguments separated by commas.
- Using
return, values calculated within functions can be sent back to where they were called. This allows further use of those values outside the function context.
Differences Between Print and Return
- The distinction between
printandreturnis clarified: while print outputs information for debugging or user display, return sends computed values back for further use.
- Adding a return statement interrupts execution; once reached, subsequent lines are not executed unless they are part of another block or after another call.
Understanding Function Arguments and Scope in Programming
Importance of Argument Order
- The order of arguments in a function call is crucial; for example, subtracting 5 from 3 yields a different result than subtracting 3 from 5.
- Keyword arguments allow specifying values without relying on their position, enhancing clarity and reducing errors related to argument order.
Utilizing Keyword Arguments
- By assigning default values to keyword arguments, functions can be called with fewer parameters, preventing errors when no arguments are provided.
- It’s important to note that keyword arguments cannot precede positional arguments in a function call.
Understanding Scope
- Scope refers to the visibility and lifetime of variables; local variables exist only within the function they are defined in.
- Attempting to access a local variable outside its function results in an error as it is destroyed after execution.
Global vs Local Variables
- If a variable is defined globally and locally (with the same name), Python prioritizes the local version within the function.
- Using the
globalstatement allows functions to modify global variables instead of creating new local ones with identical names.
Recap of Key Concepts
- Important topics covered include:
- Definition and syntax of functions
- Understanding NoneType and its value
- Differences between
returnandprint
- Usage of keyword arguments
- Conceptual understanding of scope, including local vs. global variables
Indentation Practices
- Proper indentation is essential for code readability; PEP8 recommends using four spaces per indentation level.
- Both spaces and tabs can be used for indentation as long as they are consistent throughout the code.
Personal Preferences on Indentation
- A humorous discussion highlights preferences between spaces and tabs among programmers, emphasizing that consistency matters more than choice.