F.K: To my coauthor Paul for all your creative ideas and dedication. It was truly a pleasure working with you. To Jeanne, Stephanie, Matthew, and Conor, for all your love and support.
P.K.: To my coauthor F.K., for your persistence and thoughtful discussion. To Ryan, for your support and distraction when I needed a break.
Sara Miller McCune founded SAGE Publishing in 1965 to support the dissemination of usable knowledge and educate a global community. SAGE publishes more than 1000 journals and over 800 new books each year, spanning a wide range of subject areas. Our growing selection of library products includes archives, data, case studies and video. SAGE remains majority owned by our founder and after her lifetime will become owned by a charitable trust that secures the company’s continued independence.
Los Angeles | London | New Delhi | Singapore | Washington DC | Melbourne
Introduction to Python Programming for Business and Social Science Applications
Frederick Kaefer
Loyola University Chicago
Paul Kaefer
Carrot Health
Copyright © 2021 by SAGE Publications, Inc.
All rights reserved. Except as permitted by U.S. copyright law, no part of this work may be reproduced or distributed in any form or by any means, or stored in a database or retrieval system, without permission in writing from the publisher.
All third party trademarks referenced or depicted herein are included solely for the purpose of illustration and are the property of their respective owners. Reference to these trademarks in no way indicates any relationship with, or endorsement by, the trademark owner.
This site provides applications using data that has been modified for use from its original source, www.cityofchicago.org, the official website of the City of Chicago. The City of Chicago makes no claims as to the content, accuracy, timeliness, or completeness of any of the data provided at this site. The data provided at this site is subject to change at any time. It is understood that the data provided at this site is being used at one’s own risk.
FOR INFORMATION:
SAGE Publications, Inc.
2455 Teller Road
Thousand Oaks, California 91320
E-mail: [email protected]
SAGE Publications Ltd.
1 Oliver’s Yard
55 City Road
London, EC1Y 1SP
United Kingdom
SAGE Publications India Pvt. Ltd.
B 1/I 1 Mohan Cooperative Industrial Area
Mathura Road, New Delhi 110 044
India
SAGE Publications Asia-Pacific Pte. Ltd.
18 Cross Street #10-10/11/12
China Square Central
Singapore 048423
Library of Congress Cataloging-in-Publication Data
Names: Kaefer, Frederick, author. | Kaefer, Paul, author.
Title: Introduction to Python programming for business and social science applications / Frederick Kaefer, Loyola University Chicago, Paul Kaefer, Carrot Health.
Description: First Edition. | Thousand Oaks : SAGE Publications, Inc, 2020. | Includes bibliographical references.
Identifiers: LCCN 2020021883 | ISBN 9781544377445 (paperback) | ISBN 9781544377452 (epub) | ISBN 9781544377483 (epub) | ISBN 9781544377476 (ebook)
Subjects: LCSH: Business enterprises–Technological innovations. | Business enterprises–Computer programs. | Python (Computer program language)
Classification: LCC HD45 .K234 2020 | DDC 005.13/3–dc23
LC record available at https://lccn.loc.gov/2020021883
Printed in the United States of America
This book is printed on acid-free paper.
Acquisitions Editor: Leah Fargotstein
Editorial Assistant: Natalie Elliott
Production Editor: Veronica Stapleton Hooper
Copy Editor: Gillian Dickens
Typesetter: Hurix Digital
Proofreader: Wendy Jo Dymond
Indexer: Integra
Cover Designer: Candice Harman
Marketing Manager: Shari Countryman
Preface
Why Python?
This textbook focuses on the Python programming language, but some readers might ask, “Why use Python instead of more established statistical packages such as IBM® SPSS® (Statistical Package for the Social Sciences) and SAS® (Statistical Analysis System) or other programming languages such as R?” Several advantages that Python has compared to SPSS and SAS are that Python is open source and can run on many platforms. Users are free to make copies, distribute, and even change the software. Like Python, the R programming language is also an open-source programming language used for data analytics. There is prolific use of both R and Python in the business world and for academic and research purposes. We focus on Python because we see a need for a text that presents Python programming specifically for those in the fields of social sciences and business to develop applications for data analytics. Whereas some may prefer R for statistical analysis and plotting charts, Python is a general-purpose scripting language used to develop applications with graphical user interfaces (GUIs), and many favor Python when working with text-based data. While there are possibilities of developing GUI applications using R, it is cumbersome to develop applications using R the way we approach them in this book. In our experience, covering the development of GUIs using the Python tkinter package (Chapter 11) right after control logic (Chapter 4) has piqued the interest of students through the creation of interactive, user-friendly applications. Although working with the Python tkinter package has some challenges, we have found that after successfully developing GUI-based applications, students are well prepared to work with other packages. Users of our textbook will become comfortable working with packages, and since there are over 200,000 Python packages available in the Python package repository, this is likely the most valuable takeaway from our textbook.
Pedagogy
Our book does not assume that the reader has any background in programming or statistics. This book has detailed code examples specified with line numbers to point out specific elements of Python as we cover them. Examples in each chapter focus on two large-scale data sets (Chicago Taxi Trips and the General Social Survey), building from the basics in the beginning chapters to the actual usage and analysis of the data sets in later chapters. These code examples are all available on a companion website at study.sagepub.com/researchmethods/statistics/kaefer-intro-to-python so that readers can execute the code examples to “learn by doing” as we introduce them in the material. We also include exercises in each chapter for readers to apply the concepts