Comprehensive Guide to Python Functions and Scopes

Comprehensive Guide to Python Functions and Scopes
Slide Note
Embed
Share

Explore the fundamental concepts of Python functions, from basic organizational building blocks to return statements, default arguments, type hinting, and scope understanding. Gain insights into common pitfalls to avoid and enhance your coding skills with practical examples and explanations.

  • Python Functions
  • Scopes
  • Return Statements
  • Type Hinting
  • Python Basics

Uploaded on Oct 05, 2024 | 1 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.

E N D

Presentation Transcript


  1. Functions CSEG Python Training, Week 4

  2. Functions Basic organizational building block for your code. Very similar to a Fortran subroutine.

  3. hello_function.py

  4. add_function.py

  5. multi_return.py

  6. Return multiple variables Can return multiple variables Actually returns a Tuple that is unpacked into multiple values We ll talk more about Tuples later

  7. return_pitfall.py

  8. Return Pitfall Functions that have no return statement actually return None. If you forget to put in your return statement, your code will crash when you attempt to use your returned value. Linters will detect this.

  9. arbitrary_args.py

  10. keyword_args.py

  11. default_args.py

  12. Type Hinting Introduced in Python 3.5 Helps document your code Helps IDEs and linters reason about your code Not enforced by language Cover in more detail later

  13. Scope Local scope: Any variable created inside a function can be read/assigned only by that function. Global scope: Any variable created in the main body of a Python file can be read anywhere in the file. You can assign to a global variable inside a function, but only if you really mean it. Don t do this.

  14. scope.py

  15. scope.py

  16. main.py Avoid putting anything other than imports and constants in global scope.

  17. Built-in Functions https://docs.python.org/3/library/functions.html

  18. Homework 1. Write a function named exponent It should take two arguments and return the result of the first argument to the power of the second argument. 2. Skim the documentation for Python s built-in functions. https://docs.python.org/3/library/functions.html 3. Write a function named sum_range It should take two input arguments: start, end. Compute and return the sum of a range defined by the start and end arguments. 4. Write a function named hello It should take a single, optional, argument named noun. If the argument is not provided, then the function should print Hello world! If the argument is provided then world should be replaced by the value of noun.

  19. Example Files You can get copies of the example files with the following command: git clone https://gitlab.com/CSU-CIRA/python_training_resources/functions.git You can also download them from here: https://drive.google.com/drive/folders/1NavLyaxpo0fiXYWde7gsaBZDfeQbaTRr?usp=sharing

  20. Slides after this can be used if we have time, or during QA

  21. duck_typing.py

  22. Duck Typing If it walks like a duck, and it quacks like a duck, then it must be a duck. Python will attempt to use your variables as you ve specified. If an operation doesn t make sense for your variable, Python will raise an error.

More Related Content