Frederick Kaefer

Introduction to Python Programming for Business and Social Science Applications


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

      The title displayed in the title bar at the top is Python 3.7.3 Shell. The options in the ribbon at the top are File, Edit, Shell, Debug, Options, Window, and Help. The five lines of text and code are as follows. Line 1: Python 3.7.3 (v3.7.3:ef4ec6ed12, Mar 25 2019, 21:26:53) [MSC v.1916 32 bit (Intel)] on win 32. Line 2: Type “help”, “copyright”, “credits” or “license()” for more information. Line 3: >>> print (“Hey, taxi!”). Line 4: Hey, taxi! Line 5: >>>

      Back to Figure

      The title displayed in the title bar at the top is Python 3.7.3 Shell. The options in the ribbon at the top are File, Edit, Shell, Debug, Options, Window, and Help. The five lines of text and code are as follows. Line 1: Python 3.7.3 (v3.7.3:ef4ec6ed12, Mar 25 2019, 21:26:53) [MSC v.1916 32 bit (Intel)] on win 32. Line 2: Type “help”, “copyright”, “credits” or “license()” for more information. Line 3: >>> help. Line 4: Type help() for interactive help, or help (object) for help about object. Line 5: >>> help(). The lines are followed by paragraphs of the following text.

      Welcome to Python 3.7’s help utility!

      If this is your first time using Python, you should definitely check out the tutorial on the Internet at https://docs.python.org/3.7/tutorial/.

      Enter the name of any module, keyword, or topic to get help on writing Python programs and using Python modules. To quit this help utility and return to the interpreter, just type “quit”.

      To get a list of available modules, keywords, symbols, or topics, type “modules”, “keywords”, “symbols”, or “topics”. Each module also comes with a one-line summary of what it does; to list the modules whose name or summary contain a given string such as “spam”, type “modules spam”.

      help>

      Back to Figure

      The title displayed in the title bar at the top is I:\Fig 1_6 heytaxi.py - Notepad++. The options in the ribbon at the top are File, Edit, Search, View, Encoding, Language, and Settings. Some icons under the ribbon correspond to New file, Open, Save, Print, Cut, Copy, Paste, Undo, and Redo. The tab Fig 1_6 heytaxi.py is selected. The file shows one line of code. Line 1: print (“Hey, Taxi!”).

      Back to Figure

      The title displayed in the title bar at the top is Python 3.7.3 Shell. The options in the ribbon at the top are File, Edit, Shell, Debug, Options, Windows, and Help. File is selected. The options listed under the File option are New File Ctrl+N, Open… Ctrl+O, Open Module… Alt+M, Recent Files, Module Browser Alt+C, Path Browser, Save Ctrl+S, Save As… Ctrl+Shift+S, Save Copy As… Alt+Shift+S, Print Window Ctrl+P, Close Alt+F4, and Exit Ctrl+Q. New File is selected.

      Back to Figure

      The title displayed in the title bar at the top of the shell window is Python 3.7.3 Shell. The options in the ribbon at the top are File, Edit, Shell, Debug, Options, Windows, and Help. The three lines of introductory text in the shell window are as follows. Line 1: Python 3.7.3 (v3.7.3:ef4ec6ed12, Mar 25 2019, 21:26:53) [MSC v.1916 32 bit (Intel)] on win 32. Line 2: Type “help”, “copyright”, “credits” or “license()” for more information. Line 3: >>>. The title displayed in the title bar at the top of the file is Fig 1_6 heytaxi.py - I:\Fig 1_6 heytaxi.py (3.7.3). The options in the ribbon at the top are File, Edit, Format, Run, Options, Windows, and Help. The one line of text in the file is as follows. print (“Hey, Taxi!”).

      Back to Figure

      The title displayed in the title bar at the top of the shell window is Python 3.7.3 Shell. The options in the ribbon at the top are File, Edit, Shell, Debug, Options, Windows, and Help. The shell window shows three lines of introductory text in the shell window are as follows. Line 1: Python 3.7.3 (v3.7.3:ef4ec6ed12, Mar 25 2019, 21:26:53) [MSC. Line 2: Type “help”, “copyright”, “credits” or “license()” for more i. Line 3: >>>. The title displayed in the title bar at the top of the file is Fig 1_6 heytaxi.py - I:\Fig 1_6 heytaxi.py (3.7.3). The options in the ribbon at the top are File, Edit, Format, Run, Options, Windows, and Help. Run option is selected. The expanded menu lists Python Shell, Check Module Alt+X, and Run Module F5. Run Module is selected.

      Back to Figure

      The title displayed in the title bar at the top of the shell window is Python 3.7.3 Shell. The options in the ribbon at the top are File, Edit, Shell, Debug, Options, Window, and Help. There are three lines of introductory text in the shell window, followed by the result of code execution. The result is titled, RESTART: I: \Fig 1_6 heytaxi.py. There is one line of result. Line 1: Hey, Taxi!

      2 Building Blocks of Programming

      Learning Objectives

       Explain good programming practice guidelines

       Describe variables and commonly used data types in Python

       Write code statements that assign values to variables

       Classify types of errors that can occur in Python code

       Develop functions that contain code segments

       Write code statements using Python functions

      Introduction

      Programming in the Python language uses instructions to tell the computer what actions you want performed. This chapter begins by discussing good programming practice. We then explain the basic elements of Python code, beginning with Python keywords, objects, operators and delimiters, data types, and variables. We explain comments and assignment statements next. Python code statements must comply with strict syntax rules in Python and the incorrect specification of code statements result in errors. This chapter explains the three types of errors that often result. Following the discussion of error types, we introduce functions. The packaging of Python code into smaller components helps to organize code and simplify the identification and resolution of errors. We also illustrate commonly used Python built-in functions and their usage. The chapter concludes with the development of a code module used by Python code in a different file.

      Good Programming Practice

      Many references provide advice and recommendations for best practices in programming. We will discuss some basics that can influence your development practices as you learn more about Python. Best practices do not just apply to Python code, and in fact, there are entire books on the subject (i.e., Martin, 2009).

      To begin, consider who else will be reading your code. Is it a coworker or classmate with whom you might work on the same project? Is it someone grading your homework or project? On the other hand, is it your future self, as you may be working on a long-term project? Consider whether your code will be easy to follow and understand to whomever may be reading your code after you write it (Kaefer, 2018).

      To facilitate easy-to-understand code, be sure to use descriptive variable names. While variable names like a, i, or x may be simple to type and illustrate concepts, consider naming your variables after what they represent or their use. For example, customer_name is better than c, and survey_respondant_age is better than x. Be sure to add plenty of helpful comments in your code. Leave comments before functions to explain what they do. Include comments throughout code that may be complex or counterintuitive on a first glance. Above all, be consistent. Avoid using different styles throughout your code and follow any style guidelines that may be in place in your organization.

      Good programming practice is not simply about the readability of your code. A programmer should always consider whether there is a better way to do something. Does your code require doing the same thing repeatedly? A loop is a logical response (we cover loops in Chapter 4). Also, consider breaking the code into functions and having a loop call the function. These are the basics