Introduction to Python Programming for Business and Social Science Applications. Frederick Kaefer

Чтение книги онлайн.

Читать онлайн книгу Introduction to Python Programming for Business and Social Science Applications - Frederick Kaefer страница 15

Introduction to Python Programming for Business and Social Science Applications - Frederick Kaefer

Скачать книгу

are not part of a function, to have those lines of code at the very end.

       Stop, Code, and Understand!

      SCU 2.7 Modify a User-Defined Function

      Download the file “SCU 2_7.py” from the companion website and save it either on your computer or on a removable storage device. Open the file in the Python IDLE editor and modify the function “my_function” so it prints the average of x and y. Execute the modified program after the change to verify that the revised code runs and produces the correct result.

      Lessons learned: In this section, we learned about a few functions that Python already has defined and that one common use of built-in functions is converting object data types from one type to another. We also learned about user-defined functions and how functions can have arguments (values passed to them) as well as return values. We saw how we must be careful when specifying arguments when we call functions or else errors will result.

       Python Insight

      Python uses indentation to differentiate levels of code. Indentation refers to the number of spaces and/or tabs at the beginning of a line of Python code. As we have already seen, function definition uses indentation to indicate what code is within the function. We will use indentation to an even greater degree in the next chapter when we cover control logic and loops, which use different blocks of code. Python 3 does not allow mixing of tabs and spaces for indentation (van Rossum et al., 2001). The recommendation is to use spaces for indenting (specifically four), unless one is working with code that already is using tabs for indentation, in which case tabs should be used consistently for indentation. This reduces the processing of the Python interpreter needed to treat all the possible combinations of tabs and spaces the same. To help visually ensure the proper indentation, text editors such as Notepad++ have features to show white space (spaces and tabs). In Notepad++, you can turn this feature on and off through the menu selections View/Show Symbol and clicking on the selection: “Show White Space and TAB.”

      Using Modules of Python Code

      As discussed in Chapter 1, Guido van Rossum’s vision for Python was that users could create their own coding modules and make those coding modules available to others. The previous section detailed how to create functions and to use them in Python code. In addition to using functions created within a Python file, you can use functions that others develop that are in other files. There are numerous modules available for Python, and users can even create their own. There are also built-in modules that come with Python. For example, the random module lets you generate pseudo-random numbers, which you can use to add an element of chance to a program or generate simulation data. Additionally, the fractions module enables the programmer to perform arithmetic on fractions stored as strings like “4/5,” and the math module provides functions like sin, cos, and tan. We explain and give an example of using several useful packages later in this book. A searchable index of Python modules is available at https://pypi.org/.

      We use functions in modules by importing modules. The Python code in Figures 2.28 and 2.29 illustrate how you accomplish this.

      A screenshot displays two lines of code with find_average function in a Python module.Description

      Figure 2.28 Module with find_average Function

      A screenshot displays code lines in a Python module for finding average using function.Description

      Figure 2.29 Find Average using Function in Module

      Figure 2.28 is a Python module that has the find_average function defined within it. The Python module name uses extra underscore characters so that it does not have any spaces in it. This is important, because when you import Python modules, the file name referenced can’t have any spaces in it. Figure 2.29 shows how to import and use a function from a different Python code module. Line 1 of Figure 2.29 uses the import statement to import the code in the Python module and creates the alias “fam” for the module. Line 9 of the Python code in Figure 2.29 uses the function in the imported module by referencing the alias, which indicates that the function is in that module. Figure 2.30 shows the output of executing the code in Figure 2.29, showing the same output as seen in Figure 2.27. We will make extensive use of Python code modules beginning in Chapter 4.

A screenshot displays output in a Python module. The output is titled, RESTART: I:\Fig 2_29 Find_Average_Using_Module.py. There is one line of output as follows. Line 1: The average of the two numbers is: 6.0.

      Figure 2.30 Output from Execution of Code using Function in Module

       Stop, Code, and Understand!

      SCU 2.8 Using Functions in Modules

      Download the two files “SCU 2_8.py” and “UserFunction.py” from the companion website and save them either on your computer or on a removable storage device. Open the file “SCU 2_8.py” in the Python IDLE editor and insert a line of code to import the UserFunction module so that the code will report the average of the two numbers entered by the user using the function my_function, which is in the file “UserFunction.py.” Execute the modified program after the change to verify that the revised code runs and produces the correct result.

      Lessons learned: In this section, we learned about how we can import Python code from different modules of code so our code can use their functions. As we will see throughout the book, this is an important mechanism for using Python code that is in packages.

      Chapter Summary

      This chapter introduced the building blocks of Python programming, including Python data types, coding statements, and functions. We first learned about some practices to follow when developing code, which becomes very important when you work with other people on programming projects. We next learned about basic elements of Python code, including Python keywords, objects and classes, variables and data types, and operators and delimiters, which are combined into various statements that make up Python code.

      We then learned about how to use variables in assignment statements, using print statements to visualize our results. We also learned how to use the Python type function to determine and report the data type of an object in a Python program and why the order of operations is important when we have Python code that performs calculations. We also learned there are three types of errors in Python: syntax errors, exceptions, and logic

Скачать книгу