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

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

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

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

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

easiest types of errors to resolve and result when we do not follow the rules for correctly specifying Python code. Exceptions are the types of errors that occur when we attempt to do something in Python that is not possible or not allowed. Logic errors occur when code executes without terminating with an error message but has incorrect results.

      We also 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. Next we 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. Next, 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.

      In the next chapter, we will delve deeper into compound data types, including lists, tuples, and dictionaries. We will see how useful these are when working with data and develop an even deeper understanding of the capabilities of the Python programming language. We will also discuss techniques for handling exceptions and resolving logic errors in our code.

      Glossary

      Arithmetic operatorsSymbols used to add, subtract, multiply, divide, and exponentiate (raise to a power) values.Assignment statementsPython code statements used to specify what value something is to take. Read from right to left, in which you assign the value on the right-hand side of the equal sign to the variable on the left-hand side of the equal sign.BooleanData type that can have either True or False logical values.ClassesUsed to create objects.CommentsBegin with a pound sign (#). The interpreter does not process comments.ConcatenationOperation that combines multiple strings into one string.Data typeSpecific attributes that determine the kind of values a piece of data can have and the kind of operations performed on the data.defPython keyword that is at the beginning of every function definition.ExceptionOccurs during Python code execution when you attempt an action that is not possible or not allowed.FloatData type, which can have decimal values.FunctionSubroutine of code developed to perform specific tasks.IndentationThe number of spaces and/or tabs at the beginning of a line of Python code.IntegerData type that can store whole numbers.Logic errorOccurs when a program executes without terminating with an error condition but produces incorrect results.MethodAn action that can be done to or with an object.ObjectsThe building blocks of Python. Objects or relations between objects represent all data in a Python program.StringData type, which can have text made up of letters, characters, and numbers. Enclosed in quotes when referenced in code.Syntax errorOccurs when Python code does not follow the rules that dictate the specification of Python code statements.type functionUsed to determine the data type of an object.VariableUsed to store and access values.

      End-of-Chapter Exercises

       2.1 Write Python code that uses the input built-in function to ask the user to enter a whole number between 1 and 100. The input function always returns a string value, so use the int built-in function to convert the value entered to an integer data type and square the number that the user entered using the exponentiation operator. Print a message to the user stating the value that they entered and the square of the value that they entered.

       2.2 Write Python code that uses the input built-in function to ask the user to enter the year they were born as a four-digit number. The input function always returns a string value, so use the int built-in function to convert the year value entered to an integer data type and subtract the year entered from the current year (i.e., 2019). Print a message to the user stating the value that they entered and their calculated age. Why might the calculated age not be correct?

       2.3 Write Python code that uses the input built-in function to ask the user to enter a decimal formatted number between 1 and 100. The input function always returns a string value, so use the float built-in function to convert the value entered to a float data type and square the number that the user entered using the exponentiation operator. Print a message to the user stating the value that they entered and the square of the value that they entered.

       2.4 Modify the code in Exercise 2.3 to round the values reported to the user to two decimal places (use the round built-in function).

       2.5 Write Python code that uses the input built-in function to ask the user to enter a sentence of their choosing. Use the len built-in function to determine how many characters were in the string entered and report this information back to the user.

       2.6 Write Python code that uses the input built-in function to ask the user to enter a weight in pounds. The input function always returns a string value, so use the float built-in function to convert the value entered to a float data type and determine the equivalent weight in kilograms (you can use the conversion factor that 1 pound = 0.453592 kilograms). Print a message to the user stating the weight in pounds that they entered and the equivalent weight in kilograms.

       2.7 Write Python code that uses the input built-in function to ask the user to enter a temperature in the Fahrenheit temperature scale. The input function always returns a string value, so use the float built-in function to convert the value entered to a float data type and determine the equivalent temperature in the Celsius temperature scale (use the conversion factor °C = (°F – 32) × (5/9)). Print a message to the user stating the temperature in Fahrenheit that they entered and the equivalent temperature in Celsius. You can verify that your code executes properly by entering in 32°F (equivalent is 0°C) and 212°F (equivalent is 100°C).

      References

      Kaefer, P. (2018). Code like it matters: Writing code that’s readable and shareable. SAS Global Forum Proceedings. Retrieved from https://www.sas.com/content/dam/SAS/support/en/sas-global-forum-proceedings/2018/2520-2018.pdf

      Martin, R. C. (2009). Clean code: A handbook of agile software craftsmanship. Upper Saddle River, NJ: Prentice Hall.

      Python Software Foundation. (2019, June 17). Python 3.7.3 documentation. Retrieved from https://docs.python.org/3/

      van Rossum, G., Warsaw, B., & Coghlan, N. (2001, July 5). PEP 8—Style guide for Python code. Retrieved from https://www.python.org/dev/peps/pep-0008/

      Visit study.sagepub.com/researchmethods/statistics/kaefer-intro-to-python for data sets and code to accompany this text!

      Descriptions of Images and Figures

      Back to Figure

      There are seven lines of code as follows. Line 1: # This is a comment line that begins with a pound sign “#”. Line 3: # Example 1a: assigning a string value to a variable creates a string object type variable. Line 4: trip_id = “da7a62fce04” # This assignment is assigning a string to a variable named trip_id. Line 5: print(“The data type for the trip_id variable is:”, type(trip_id)). Line 7: #Example 1b: assigning an integer value to a variable creates an integer object type variable. Line

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