☰
Python Across Disciplines
with Python + AI Tool   
×
Table of Contents

1.1.   Introduction 1.2.   About the Author & Contact Info 1.3.   Book Conventions 1.4.   What (Who) is a Programmer? 1.5.   Programming Across Disciplines 1.6.   Foundational Computing Concepts 1.7.   About Python 1.8.   First Steps 1.8.1 Computer Setup 1.8.2 Python print() Function 1.8.3 Comments
2.1. About Data 2.2. Data Types 2.3. Variables 2.4. User Input 2.5. Data Structures (DS)         2.5.1. DS Concepts         2.5.2. Lists         2.5.3. Dictionaries         2.5.4. Others 2.6. Files         2.6.1. Files & File Systems         2.6.2. Python File Object         2.6.3. Data Files 2.7. Databases
3.1. About Processing 3.2. Decisions         3.2.1 Decision Concepts         3.2.2 Conditions & Booleans         3.2.3 if Statements         3.2.4 if-else Statements         3.2.5 if-elif-else Statements         3.2.6 In-Line if Statements 3.3. Repetition (a.k.a. Loops)         3.3.1  Repetition Concepts         3.3.2  while Loops         3.3.3  for Loops         3.3.4  Nested Loops         3.3.5  Validating User Input 3.4. Functions         3.4.1  Function Concepts         3.4.2  Built-In Functions         3.4.3  Programmer Defined Functions 3.5. Libraries         3.5.1  Library Concepts         3.5.2  Standard Library         3.5.3  External Libraries 3.6. Processing Case Studies         3.6.1  Case Studies         3.6.2  Parsing Data
4.1. About Output 4.2. Advanced Printing 4.3. Data Visualization   4.4  Sound
  4.5  Graphics
  4.6  Video
  4.7  Web Output
  4.8  PDFs & Documents
  4.9  Dashboards
  4.10  Animation & Games
  4.11  Text to Speech

5.1 About Disciplines 5.2 Accounting 5.3 Architecture 5.4 Art 5.5 Artificial Intelligence (AI) 5.6 Autonomous Vehicles 5.7 Bioinformatics 5.8 Biology 5.9 Bitcoin 5.10 Blockchain 5.11 Business 5.12 Business Analytics 5.13 Chemistry 5.14 Communication 5.15 Computational Photography 5.16 Computer Science 5.17 Creative Writing 5.18 Cryptocurrency 5.19 Cultural Studies 5.20 Data Analytics 5.21 Data Engineering 5.22 Data Science 5.23 Data Visualization 5.24 Drone Piloting 5.25 Economics 5.26 Education 5.27 Engineering 5.28 English 5.29 Entrepreneurship 5.30 Environmental Studies 5.31 Exercise Science 5.32 Film 5.33 Finance 5.34 Gaming 5.35 Gender Studies 5.36 Genetics 5.37 Geography 5.38 Geology 5.39 Geospatial Analysis ☯ 5.40 History 5.41 Humanities 5.42 Information Systems 5.43 Languages 5.44 Law 5.45 Linguistics 5.46 Literature 5.47 Machine Learning 5.48 Management 5.49 Marketing 5.50 Mathematics 5.51 Medicine 5.52 Military 5.53 Model Railroading 5.54 Music 5.55 Natural Language Processing (NLP) 5.56 Network Analysis 5.57 Neural Networks 5.58 Neurology 5.59 Nursing 5.60 Pharmacology 5.61 Philosophy 5.62 Physiology 5.63 Politics 5.64 Psychiatry 5.65 Psychology 5.66 Real Estate 5.67 Recreation 5.68 Remote Control (RC) Vehicles 5.69 Rhetoric 5.70 Science 5.71 Sociology 5.72 Sports 5.73 Stock Trading 5.74 Text Mining 5.75 Weather 5.76 Writing
6.1. Databases         6.1.1 Overview of Databases         6.1.2 SQLite Databases         6.1.3 Querying a SQLite Database         6.1.4 CRUD Operations with SQLite         6.1.5 Connecting to Other Databases
Built-In Functions Conceptss Data Types Date & Time Format Codes Dictionary Methods Escape Sequences File Access Modes File Object Methods Python Keywords List Methods Operators Set Methods String Methods Tuple Methods Glossary Index Appendices   Software Install & Setup
  Coding Tools:
  A.  Python    B.  Google CoLaboratory    C.  Visual Studio Code    D.  PyCharm IDE    E.  Git    F.  GitHub 
  Database Tools:
  G.  SQLite Database    H.  MySQL Database 


Python Across Disciplines
by John Gordon © 2023

Table of Contents

Table of Contents  »  Chapter 3 : Processing : Functions: Concepts

Function Concepts

Subscribe Contact


Contents

Overview

Functions in programming are reusable blocks of code that perform a specific task. Functions help programmers create reusable, modular, and manageable code. In Python, there are two main categories of functions: built-in and programmer-defined functions. Built-in functions are those that are included in Python and are always accessible. For example, print() is a built-in function that we have used extensively in this book. The other category is programmer-defined functions which the programmer creates to perform specific tasks. Once the programmer has written a function, it can be called (executed) just like any built-in function by invoking the name of the function and including any necessary parameters inside a following set of parentheses, like this:

my_function(10)

In this example, "my_function" is the name given to the function by the programmer, and the number inside the parentheses is an example of a parameter. By using built-in and programmer-defined functions, programmers can simplify complex problems, reduce code redundancy, and enhance the readability and maintainability of their code.

Concept: Functions

In Python, functions are fundamental building blocks that allow for the modularization and reuse of code. Created using the def keyword, a function encapsulates a sequence of statements into a single unit that can be executed whenever the function is called. Functions can accept parameters, which are variables passed into the function, allowing for customization and flexibility in their operation. They often return a value using the return statement, which is not mandatory; functions without a return statement implicitly return None. Functions in Python can perform a wide range of tasks—from simple operations like adding two numbers to complex logic like processing data or handling files. They support concepts like default arguments, variable-length arguments (*args and **kwargs), and recursion. The ability to define and use functions enables more organized, readable, and maintainable code, making them a cornerstone of Python programming.

Concept: Arguments

In Python, function arguments are the values passed to a function at its invocation, enabling functions to accept input data and behave differently based on that input. Python supports various types of arguments: positional arguments, which are mandatory and must be passed in the correct order; keyword arguments, where each argument is accompanied by a parameter name (e.g., func(param1=value)), offering more clarity and flexibility; default arguments, which have a default value and are optional during the function call; and variable-length arguments, allowing a function to accept an arbitrary number of arguments. These are denoted by *args for non-keyword variable-length arguments and **kwargs for keyword variable-length arguments. This flexibility in argument handling allows Python functions to be incredibly versatile, accommodating a wide range of use cases from simple to complex function calls.

Concept: Parameters

In Python, function parameters are the variables listed inside the parentheses in the function definition. They act as placeholders for the values the function can accept when called. There are several types of parameters: positional parameters, which are mandatory and are bound to arguments based on their order; default parameters, which have a predefined value and are optional in a function call; and keyword parameters, which are named and can be passed in any order as long as the name matches. Python also supports variable-length parameters, *args for accepting arbitrary numbers of positional arguments, and **kwargs for arbitrary numbers of keyword arguments. These parameters provide the functions with the flexibility to handle various inputs, enhancing the function's adaptability and reusability. How these parameters are defined and used plays a crucial role in how a function processes input and produces output, making them a fundamental aspect of function design in Python.

Built-In Functions

Python comes with an extensive standard library that includes many built-in functions. These functions are always available for use without the need to import any additional modules. These functions are designed to provide basic functionality that is commonly needed in Python programs. The following page provides an introduction to built-in functions in Python.

Programmer-Defined Functions

In addition to the standard library functions, Python allows programmers to define their own functions. These programmer-defined functions are created to perform specific tasks the standard library might not cover. This is where the real power of functions becomes evident, as they can be tailored to the exact needs of the program. Programmer-defined functions are covered in detail later in this chapter.

Advantages of Using Functions

Functions are a fundamental aspect of Python programming. They help organize code into manageable chunks, promote code reuse, and make complex programs more understandable. Whether using Python's robust standard library or creating your own functions, they are essential for effective programming.



 





© 2023 John Gordon
Cascade Street Publishing, LLC