Preface
Welcome to Learning Python Through Projects. This book uses the Python Programming Language to teach Programming using various projects and exercises as the primary core aspect to learn programming. While it is useful to understand the hardware aspects connected to computing, this book focuses more on the software side of computing. The topic of binary and bits will appear in the earlier sections, but that is the extent of the explicit connection to hardware covered in this book.
What this book is about
As the title suggests, this book was created to teach programming in Python using various projects as the foundation towards effective learning. When learning to program, understanding the concepts is important, but understanding how to apply these concepts is far more useful and practical. With this theory and intention, this book was created to help teach programming with useful and real world projects to help develop a deeper understanding of programming concepts.
Who is this for?
This book is for anyone who is interested in getting into Programming and not sure where to start. The goal of this book is to help develop foundational programming skills for the reader by explaining various concepts in Python and providing exercises and projects to practice and reinforce the concepts.
Key terms
Throughout this book, the terms to program and to code will be used fairly interchangeably. There is a distinction between both terms, but for the purposes of this book, they will be used fairly synonymously. In general, to code refers to writing code for a machine to understand and interpret, whereas to to program refers to also writing code, but this includes the logical thinking, application of algorithms, understanding of runtimes, and many other more complicated components that constitute the more holistic definition of programming.
The term algorithm will be used frequently in the latter sections of the book. These consist of a grouping of code or instructions that run together to achieve some purpose. Debugging is another term that will appear often. This is the act of finding problems, or bugs, in our code and finding ways of fixing and resolving them.
Additionally, the terms of code editor, debugger, and integrated development environment, or ide for short, appear throughout this book. These are different tools that help us write code, find problems in our code, and applications that aid in the development process. A code editor is a tool that allows us to write code. It often is accompanied by a syntax highlighter, an additional tool that helps us see specific keywords, values, and variables in our code. At its core, a code editor is an editor that allows us to read and write code in a clean manner. A debugger is another tool that helps step through the code and view information and values while the code is running. This tool helps us learn how to debug our code and fix errors or bugs. Lastly, an integrated development environment, or ide for short, contains a code editor, debugger, and many other tools that help or streamline the development process.
Topic progression
This book begins by going over the foundational concepts of programming in Python. These foundational concepts occur in many programming languages with deviations in syntax and how to write these concepts. These foundational concepts are the following:
- Printing
- Data Types
- Variables
- Conditional Statements
- While Loops
- For Loops
- Functions
- Strings
- Lists
- Sets
- Dictionaries
Upon completing these foundational concepts, the book continues into more intermediate concepts. These intermediate concepts are more complicated and build off of the concepts from the foundational concepts. These intermediate concepts are the following:
- Recursion
- Exception Handling
- File I/O
- Test Driven Development
- Command Line Arguments
While these intermediate concepts aren't fundamentally difficult, they require a more conceptual understanding of the foundational concepts before beginning them. They are important to learn and understand, but it is also possible to be an effective programmer without having mastered these concepts. Due to this fact, they are in the intermediate space and serve as optional concepts to learn. Upon completing the intermediate concepts, this book provides some advanced concepts.
These advanced concepts aren't necessary to understand as a beginner, but they are useful to understand, especially if there is a goal to go deeper into programming and Computer Science. These more advanced topics are:
- Object Oriented Programming
- Functional Programming
- Dynamic Programming
- Algorithms
- Data Structures
While this book provides exercises to learn and understand the concepts, there may be a desire to master the concepts more and apply the concepts into developing a product. In order to accommodate that desire, a set of possible examples with applying the concepts is included at the end. These examples are the following:
- Web Applications
- Data Analysis
- Graphical User Interfaces
- Automating Scripts
- Web Scraping
- Games
While these examples aren't the only uses for programming or for Python, they serve as some useful examples on where Python appears in the real world and provide a starting point for creating projects around these concepts. These examples also aren't the only ways of creating the products, but rather provide some methods to develop those projects.
Brief Chapter Overview
Foundation
Chapter 1: Basics
This chapter begins with the topic of programming languages and what they are. The chapter then moves immediately into starting to program with printing, introducing data types, variables, and working with various mathematical operators.
Chapter 2: Conditional Statements
This chapter introduces the if, elif, else structure and introduces pseudocode as a way of thinking through code. This chapter also provides examples of working with these conditional keywords to write code that will execute depending on certain conditions.
Chapter 3: While Loops
This chapter introduces the first method of using loops and repeating code with a while loop. This chapter extends the concept of working with pseudocode by including while loops and how to write pseudocode with repeating code. This chapter will also introduce the idea of working with flag variables to track and check for conditions being met at least once.
Chapter 4: For Loops
This chapter introduces the second method of using loops and repeating code with a for loop. As Chapter 3 introduces While loops and pseudocode, this chapter extends that concept by introducing using a for loop with pseudocode.
Chapter 5: Functions
This chapter introduces the concept of functions, the concept that allows for the consolidation of reusable code and allowing for code reuse. The chapter also talks about variable scoping and returning values. Additionally, this chapter introduces the concept of DRY, or Don't Repeat Yourself.
Chapter 6: Strings
This chapter takes a deeper dive into strings, string functions, and immutability. While Chapter 1 briefly introduces the concept of strings, this chapter takes that extends that introduction and goes deeper into the topic.c
Chapter 7: Lists
This chapter introduces the first type of collection data type called a List. This allows for the storage of multiple values into a single variable. This chapter extends the idea of working with indexes as introduced in Chapter 6. This chapter also returns to the for loops section, as introduced in Chapter 4, and introduces another format of writing a for loop, called an enhanced for-loop or a for-each loop.
Chapter 8: Tuples
This chapter briefly discusses another type of collection called a Tuple. This chapter is really short and explains the use of a tuple.
Chapter 9: Sets
This chapter continues the collection data types and introduces the Set type. This chapter is fairly short and explains the uses of a Set versus a List.
Chapter 10: Dictionaries
This chapter introduces a key-value paired collection data type called a Dictionary. This dictionary is useful when storing a collection of data that needs to be accessed quickly. This chapter concludes the foundational concepts.
Intermediate
Chapter 11: Recursion
This is the first chapter of the intermediate concepts and introduces the concept of Recursion, or calling a function in a function. There is an emphasis on the base case and recursive case, with a number of examples of recursion. In this chapter, the functions of Factorial and Fibonacci will be solved in an iterative and recursive manner. This will demonstrate how to write the same function in different ways, along with how efficiency can change based on how they're written.
Chapter 12: Exception Handling
This chapter introduces preventing code from crashing by trying code, catching code, and using the finally keyword to clean up code. This chapter includes a list of different exceptions to be caught and handled, along with how to intentionally raise an exception.
Chapter 13: File I/O
This chapter introduces the concept of reading, writing, and working with files. This also introduces the keyword of "with" and how to work around resources.
Chapter 14: Test Driven Development
This chapter introduces a new way of programming, where test cases are written beforehand and provide the expected behavior of the code. This chapter also encourages this practice as a regular practice to ensure developed code behaves as expected.s
Chapter 15: Command Line Arguments
This chapter introduces a brief supplementary concept in Command Line Arguments. These are arguments given when running the code, which often tell our code how to run. This chapter also concludes the intermediate conceptual section.
Advanced
Chapter 16: Object Oriented Programming
This chapter begins the advanced topic section, introducing the concept of Object Oriented Programming, working with custom classes, objects, and various other inner concepts. This chapter is not necessary to learning programming, but it helps explain another paradigm of programming.
Chapter 17: Functional Programming
This chapter introduces the concept of functional programming. Similar to Chapter 15, this chapter serves as an introduction to another paradigm of programming. In this chapter, an introduction of working with functions as first class citizens will be given, along with an introduction of wrapper functions and calling functions within other functions. One of the wrapper functions introduced is a custom logger that prints out the function name and documentation.
Chapter 18: Dynamic Programming
In this chapter, dynamic programming is introduced. This is a brief chapter looking at how code can be written that utilizes storing and retrieving results from previous iterations, without having to run code again to find specific values. This chapter will introduce optimizing generating prime numbers and the fibonacci sequence.
Chapter 19: Algorithms
This chapter introduces some common and simple algorithms. It isn't an extensive list of algorithms, but serves as an introduction towards writing algorithms.
Chapter 20: Data Structures
Similar to Chapter 18, this chapter introduces some common and simple Data Structures. This isn't an exhaustive list of Data Structures, but provides an introduction into them.
Chapter 21: Concurrency and Threading
This chapter introduces the idea of developing concurrent code, using multithreads, and increasing the efficiency of code. This chapter also concludes the advanced section.
Examples
Chapter 22: Web App
This chapter begins the examples section, which provides an example of implementing the concepts into creating a product. This chapter will utilize the Flask module to assist with creating the website.
Chapter 23: Data Analysis
This chapter introduces basic data analysis concepts and implementing analytical concepts in Python.
Chapter 24: GUI
This chapter provides some examples to writing a graphical user interface into a project, allowing for a user to interact with an interface. This chapter will utilize the PySimpleGUI module to assist with generating the interface.
Chapter 25: Automation
This chapter briefly introduces writing scripts to automate some actions on a computer. Similar to the other examples, this chapter doesn't provide the only way to automate programs on a computer, but instead provides some possible automating programs.
Chapter 26: Web Scraping
This chapter briefly introduces an example of Web Scraping. Web scraping is the act of pulling information from a website and making use of that information. This chapter will utilize the modules of selenium and beautifulsoup to assist with opening a website and pulling the data from that website.
Chapter 27: Games
This chapter concludes the examples section with an example of developing a couple games. This chapter will utilize the Pygame module to assist with developing the games and creating the interface to interact with.
Organization of the Text
The text will teach Python by explaining each concept step-by-step. Each chapter will introduce a new concept or a new set of concepts. Due to the nature of programming naturally building off of prior concepts, each chapter will use concepts from previous chapters. Additional to introducing the concepts, each chapter will also provide various exercises to reinforce the concept, providing examples of the concept being used in the real world, and providing a chance to practice the idea. On top of exercises, each chapter will contain a project to demonstrate the concepts, which will also include concepts covered in prior chapters.