Study Notes for Python for Everybody

Python for Everybody

Exploring Data Using Python 3
Dr. Charles R. Severance

Credits

  • Editorial Support: Elliott Hauser, Sue Blumenberg

  • Cover Design: Aimee Andrion

  • Printing History:

    • 2024-Jan-01: Update examples to Python 3.12, remove references to Twitter APIs, rewrite Databases chapter

    • 2023-Jun-29: Many errata included, switch from Google APIs to OpenStreetMap APIs

    • 2016-Jul-05: First Complete Python 3.0 version

    • 2015-Dec-20: Initial Python 3.0 rough conversion

Copyright Details

  • Copyright 2009- Dr. Charles R. Severance. This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License. This license is available at http://creativecommons.org/licenses/by-nc-sa/3.0/

Preface

  • This book is a remix of the book "Think Python: How to Think Like a Computer Scientist" by Allen B. Downey and others.

  • Goal: To focus on exploring data rather than understanding algorithms and abstractions.

  • Target audience: Students from various disciplines (librarians, managers, biologists, etc.) who wish to learn data handling skills in Python.

  • The structure accommodates data analysis right from the beginning with running examples and exercises.

  • Major changes from the original "Think Python" book:

    • Replacing number-oriented examples with data-oriented exercises.

    • Introduction of certain topics at more relevant points in the content.

Contents

  1. Why Should You Learn to Write Programs?

  2. Variables, Expressions, and Statements

  3. Conditional Execution

  4. Functions

  5. Iteration

  6. Strings

  7. Files

  8. Lists

  9. Dictionaries

  10. Tuples

  11. Regular Expressions

  12. Networked Programs

  13. Using Web Services

  14. Object-Oriented Programming

  15. Using Databases and SQL

  16. Visualizing Data

Chapter 1: Why Should You Learn to Write Programs?
  • Importance of Programming:

    • It allows you to solve data-related problems creatively and rewards you personally and financially.

    • It empowers individuals to utilize technology adeptly in their respective fields.

Creativity and Motivation
  • Professional programming is rewarding, but this book aims to enhance productivity using Python for data analysis.

Computer Hardware Architecture
  • CPU: Asks, “What next?” at a rate defined by its frequency (e.g., 3.0 Ghz = 3 billion times per second).

  • Main Memory: Fast storage but volatile (erased when power is off).

  • Secondary Memory: Slower storage (e.g., hard drives) but retains information when the computer is powered off.

  • I/O Devices: The means by which we interact with computers (keyboard, mouse, etc.).

Chapter 2: Variables, Expressions, and Statements
  • Values and Types:

    • Integer: Whole numbers (e.g., 17).

    • String: Sequences of characters (e.g., 'Hello, World!').

    • Float: Represents decimal values (e.g., 3.2).

  • Variable Creation: message = 'Hello', assigns string to variable.

  • Variable Names: Must start with a letter/underscore, followed by letters/numbers or underscores. No spaces or reserved words.

  • Operators: Include arithmetic operators (+, -, *, /) and logical operators (and, or, not).

Chapter 3: Conditional Execution
  • Boolean Expressions: Evaluate to True/False.

  • If Statements: Allow execution of a block based on a condition.

    • Example:

   if x > 0:
       print('Positive')
  • Alternative and Chained Execution: Use of if, elif, and else.

Chapter 4: Functions
  • Function Definition: def function_name(parameters):

  • Return Value: Functions can return results via return keyword.

  • Built-in Functions: Examples include len(), max(), min().

Chapter 5: Iteration
  • While Loop: Executes as long as condition is True.

    • Example:

   while condition:
       # code
  • Definite loops: Use for-loop for iterating over known values.

Chapter 6: Strings
  • String as Sequences: Access one character using index (e.g., s[0]). Strings are immutable.

  • String Operations: Includes slicing s[1:3], concatenation, and methods (upper(), find()).

Chapter 7: Files
  • File Handling: Open files using open(filename). Indexed by lines; can read or write data. Use with open(...) as f: to ensure closure.

  • Reading Lines and Data Manipulation: Loop through the file object for processing.

Chapter 8: Lists
  • Lists as Sequences: Create [item1, item2, ...]. Lists are mutable.

  • Common Operations: Includes append(), sort(), pop().

  • List Comprehensions: Concise way to create lists from existing sequences

Chapter 9: Dictionaries
  • Dictionaries: Mappings from keys to values. Creation: Use dict() or curly braces {}. Access via keys.

  • Histogram Creation: Counting character occurrences using dictionaries.

Chapter 10: Tuples
  • Tuples: Immutable sequences. Accessing elements is similar to lists but with no modifications allowed.

Chapter 11: Regular Expressions
  • Basic Regex: Extract patterns using re library.

  • Character Matching: Utilize special characters like ^, $, and . for complex matching.

Chapter 12: Networked Programs
  • HTTP Protocol: Retrieve web data via sockets.

  • Web Scraping: Data extraction from websites using Python.

Chapter 13: Using Web Services
  • XML and JSON: Formats for web data exchange. Parse using built-in libraries.

Chapter 14: Object-Oriented Programming
  • Classes and Objects: Encapsulation of data and methods. Overriding techniques and methods for constructing objects are covered.

Chapter 15: Using Databases and SQL
  • Database Overview: Tables, rows, and basic SQL commands like INSERT, SELECT, UPDATE, and DELETE to manipulate data.

Chapter 16: Visualizing Data
  • Data Visualization: Framework for creating meaningful representations of data.

Appendix A - Contributions
  • Translations: Various languages in which the material has been translated.

Appendix B - Copyright Detail
  • License Information: CC-BY license for educational materials.

Index
  • Terms and keywords used throughout the textbook.