Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Discover millions of ebooks, audiobooks, and so much more with a free trial

From $11.99/month after trial. Cancel anytime.

Python QuickStart Guide: The Simplified Beginner's Guide to Python Programming Using Hands-On Projects and Real-World Applications
Python QuickStart Guide: The Simplified Beginner's Guide to Python Programming Using Hands-On Projects and Real-World Applications
Python QuickStart Guide: The Simplified Beginner's Guide to Python Programming Using Hands-On Projects and Real-World Applications
Ebook628 pages5 hours

Python QuickStart Guide: The Simplified Beginner's Guide to Python Programming Using Hands-On Projects and Real-World Applications

Rating: 5 out of 5 stars

5/5

()

Read preview
  • Python Programming

  • Programming

  • Python

  • Learning

  • Computer Science

  • Mentor

  • Chosen One

  • Mentorship

  • Journey

  • Quest

  • Power of Knowledge

  • Self-Improvement

  • Knowledge Is Power

  • Training Montage

  • Riddle

  • Object-Oriented Programming

  • Functions

  • Writing

  • Software Development

  • Classes

About this ebook

The Ultimate Beginner's Guide to Learning Python!
**Includes FREE Digital Bonuses! GitHub Repository, Cheat Sheets, and More!**
Learn Why QuickStart Guides are Loved by Over 1 Million Readers Around the World

Learn Python fundamentals that can be used in any programming setting – use the guidance in this book to program your own game in a unique and practical Python learning experience.

The Easiest Way to Learn Python in a Comprehensive, Step-by-Step Guide

From AI and machine learning to video game, app, and web development, Python is a critical behind-the-scenes component of everyday technology.

Python powers the services of household names like Google, Netflix, and Spotify along with tech pioneers like NASA, IBM, and Intel. Put simply, Python is the in-demand and easy-to-learn programming language that gets stuff done.

In Python QuickStart Guide, senior developer and programmer Robert Oliver lays out the quickest and most accessible path yet to the mastery of Python fundamentals.

Written by a Programming Expert with Over 20 Years of Experience

Distilling his experience drawn from over two decades of working with Python and other programming languages, Robert’s clear voice and writing present a practical, hands-on approach that anyone, at any experience level, can use to become a Python programmer.

It doesn’t matter if you are a new or existing programmer, a job seeker looking for a career change or promotion, or just someone who wants to learn how to automate basic tasks with Python—Robert’s step-by-step approach, complete with a hands-on companion Python video game project, is the perfect starting point to master Python fundamentals!

Python QuickStart Guide is Perfect for:
  • New or experienced programmers looking to enhance their career opportunities with an in-demand programming language
  • Job seekers who want to supercharge their resumes and increase their value in the job marketplace
  • Students or recent college grads who have their sights set on a lucrative position in the tech industry
  • Full stack developers or programmers who need to round out their programming skills to take on new projects
  • Anyone who wants to explore the world of programming, use Python to automate tedious tasks, or enhance their resume and future-proof their skills!
With Python QuickStart Guide, You'll Easily Understand These Crucial Concepts:
  • How to Use Python – Practical Examples, Code Snippets, Plus Follow Along to Code Your Own Game!
  • Python Fundamentals – How to Use Python for Web Design and Interfacing with GitHub, SQL, and Other Applications
  • Object-Oriented Programming Principles – Managing Data, Scripts, Logic, Inputs, Outputs, and More!
  • Programming Essentials – Debugging, Producing Clean Code, Best Practices, Time-Savers, and Tips
  • Python Next Steps –Testing, Optimization, Speed Improvements, and More!
Go Beyond the Book with Exclusive QuickClips Videos

Look for QuickClips QR codes when reading this book. Scan to access exclusive videos directly from the author at key points to enhance your learning and go beyond the book!

**LIFETIME ACCESS TO FREE BONUS RESOURCES**

Python QuickStart Guide comes with lifetime access to FREE digital resources you can access from inside the book! Each of these bonuses is crafted with our expert author to help you become a better programmer including:

  • GitHub Code Repository
  • Regular Expressions Cheat Sheet
  • Python resource library and more!
LanguageEnglish
Release dateApr 24, 2023
ISBN9781636100364
Author

Robert Oliver

Robert W. Oliver II is a senior developer and DevOps consultant with over two decades of experience in the field. A truly full-stack programmer, Robert has architected both front-end and back-end systems and designed algorithms used in technologies operating at scales ranging from small to enterprise. With decades of experience working in Python, PHP, Ruby on Rails, JavaScript, C/C++, Rust, and C#, he is fluent in the languages of programming and system design. 

Read more from Robert Oliver

Related to Python QuickStart Guide

Related ebooks

Programming For You

View More

Reviews for Python QuickStart Guide

Rating: 4.8 out of 5 stars
5/5

10 ratings5 reviews

What did you think?

Tap to rate

Review must be at least 10 words

  • Rating: 5 out of 5 stars
    5/5

    Sep 6, 2023

    This book is absolutely amazing. I don't have a coding background, but do have some coding experience. But the beginning of the book, I felt like I knew the author! I started coding when I was a kid for fun, just teaching myself BASIC. And in college, I took some HTML and PHP courses. Of course, that was years ago and don't remember anything now. I felt like this book was really easy to understand and broke out everything in very consumable pieces. I didn't find anything confusing and also entertaining along the way. This has inspired me to keep going with coding and learn more in my spare time!
  • Rating: 5 out of 5 stars
    5/5

    Jul 21, 2023

    I have a basic understanding of computer programming, mostly starting from the early days of computers on. Therefore, this book made it much easier for me to understand how to get my "feet wet" with Python. There are plenty of visual assistance graphics, user guides, and other places to seek assistance, if you lose your way. This book is a great way to introduce yourself to the Python programming language.

  • Rating: 4 out of 5 stars
    4/5

    Jul 15, 2023

    With Python QuickStart Guide, veteran computer programmer and software developer Robert Oliver attempts to deliver something very ambitious. He has written a guidebook to the Python programming language that purports to take absolute beginners to the coding world through the fundamental principles and advanced topics necessary for them to achieve enough proficiency to produce their own sophisticated programs. Having spent several weeks reading every chapter—and, more importantly, working through every example and line of Python code in this lengthy volume—I can attest to the fact that the author very nearly succeeds in that audacious quest.

    The book is organized into a series of short chapters that are collected into four sections, starting with basic Python operations (e.g., computer setup, data structure and storage, program flow techniques) before proceeding to more involved topics (e.g., functions and classes) and applications (e.g., math coding, internet access, file storage), and finally to advanced concepts (e.g., website development, Git, SQL). This is a logical progression and each concept in every chapter is illustrated with useful examples that give readers the opportunity to get plenty of hands-on experience with the code itself. (In fact, while all this code is listed in the print version of the book, the author has created a companion website from which the Python program files can be downloaded directly, which was quite helpful.) Connecting these concepts are the programs comprising a computer game—ClydeBank Coffee Shop—that is developed progressively throughout the entire volume.

    There is so much to recommend for this project, but there are also aspects of the exposition that were less successfully executed. Fortunately, the good far outweighs the bad overall, which makes for a guidebook that should be welcomed addition on many bookshelves. Oliver has a very approachable manner in laying out the myriad topics and his explanations are always accessible and thoughtfully delivered. The examples he creates are generally nicely done and on point, but it would have been beneficial for there to be more end-of-chapter exercises for readers to work thorough on the own as a way of solidifying the understanding of the underlying concepts. (There are only about a half dozen such exercises scattered throughout the book.) Finally, the author’s obvious enthusiasm for coding and program development is apparent everywhere in the text and that passion certainly helped this reader stay motivated.

    On the other hand, there are two significant issues that deserve mention as well. First, the concept of Python’s graphing and data plotting capabilities is not addressed anywhere in the guide. Given that data visualization is one of Python’s strengths as a programming language—and one of things that attracts a lot of users to begin with—not covering libraries such as Plotly, Matplotlib, and Seaborn seemed like a major omission for readers who are interested in writing more focused programs than they are in web development or game design. Second, despite my best (and repeated) efforts, I could not get all the code in the book to work properly. In particular, Visual Studio Code, the recommended code editor on which all examples were based, did not always work as described and some of the advanced topics (e.g, web development with Flask, contacting databases, SQLite interfacing) appeared to have missing elements and did not really run at all.

    So, while Python QuickStart Guide is an easy book for me to recommend, that recommendation must be at least somewhat tempered for certain potential users. If your goal is to become a serious program developer, this volume provides a good overview of the capabilities of the Python language, the skills you will need to master, and the resources you will have available to you. However, for true beginners with more modest programming ambitions, there are other references that might better serve your coding needs.
  • Rating: 5 out of 5 stars
    5/5

    Jul 5, 2023

    This book is formatted just as you'd expect any decent programming book would be. It starts with a brief description/history of the language, Python, in this case, then walks you through installing and opening the environment, and finally goes into codeing/programming, with the obligatory "Hello, World!" example to kick us off.

    The author takes us by the hand, explaining language structures, variables, functions, and the like. He uses examples and miniprojects along the way, to familiarize you with how it's done in Python, and get you doing it.

    If you're already familiar with programming in any other language, you can skim through a lot, and simply focus on learning how things are handled in this language, as opposed to whichever one you were already familiar with. I was coding adequately and knocking out the miniprojects within the hour. Every chapter builds on the prior one.

    By the end of the book, you'll be getting into more complex programs, including accessing databases, like a pro. There's not much more I can say about this book other than it's a great way to learn Python, and that it takes you all the way to basic proficiency.

    Highly recommended if you need to learn Python quickly, especially if this isn't your first time programming or your first programming language. I suspect it would be a great book if it was your first programming language, too.
  • Rating: 5 out of 5 stars
    5/5

    Jun 7, 2023

    I wish I had a guide like this when I first started working in basic back in the 80's.
    The instructions are well written and easy to understand. To be homest, there are over 300 pages and it will take a while to get that far working through the book as I go. From what I have already read and done....this is going to be a great reference book.

Book preview

Python QuickStart Guide - Robert Oliver

Introduction

Welcome to Python.

My name is Robert Oliver and I’ll be your guide through the exciting, engaging, and challenging endeavor of learning a programming language.

It Was a Dark and Stormy Night

While I now program quite a bit at night, my programming journey started on a bright, sunny early June morning. As a young teenager, I sat in front of my stepmother’s Tandy 1000 (an older computer, even in the early 1990s) and enjoyed playing the myriad of video games that had been installed on it. Even though I previously had some computer experience, I was mystified by the possibilities.

It was summer break, and I had all the time in the world, so I started reading the BASIC (Beginner’s All-purpose Symbolic Instruction Code) language source code to some of these games. It was a struggle at first, as I really didn’t know much about programming, but soon I started to see patterns in the code and how everything fit together to make the video games run.

Armed with several books, I taught myself DOS, BASIC, and even dabbled in a little Pascal, another programming language, as I fully extracted the computing goodness that old Tandy had to offer. Later, I got my own computer, a Tandy 2500, an Intel 386 computer that ran MS-DOS 5 and Windows 3.1. Armed with QBasic, I wrote my own video games and programs. It was fun, but I quickly ran into problems.

Video games take a lot of computing power, and computers back then didn’t exactly have that to spare. So, to extract the most performance from the machine and power my games, I wrote critical parts of the games in assembly language—a bare-metal language that computers natively understand—to speed things along. Granted, I ran into numerous problems talking back and forth between my BASIC and assembly programs, but I eventually worked out a solution that made me reasonably happy.

I poked and prodded into every corner of that machine’s memory, and by the time I had moved to Windows 98 (I largely skipped Windows 95), I could tell you everything about that computer you could possibly want to know, down to the basic hardware level. My immense love of systems programming came from this era, and it hasn’t left me.

My dad bought me Visual Basic 6 and some great manuals in the Windows 98 years, and from this I really started to formalize my programming knowledge, learning about classes, objects, and all the things I’d need to know to program professionally. From there, I went into web programming, but I continued to write scripts and programs—first with Perl, then with Python from 2006 onward.

Despite having learned so much since then, nothing can replace the joy I felt when I mastered the mystical world of computing by telling my computer exactly what I wanted it to do. I want you to feel that same joy, too.

By the end of this book, you’ll have a good understanding of Python and will have written a fair number of lines of code. In addition, we’ll be building a coffee shop game that will be both fun to write and fun to play. You’ll not only have experience coding in Python, but you’ll have a project to show for it, and the knowledge to create your own programs.

What Is Python?

There are two ways to answer that question. First, here’s the technical explanation with computer science terminology kept to a minimum.

Python is a multipurpose programming language. It is flexible enough to allow several different programming approaches and comes with a large array of functionality right out of the box. It enforces a strict code formatting style that places a high emphasis on readability.

Now, what is Python in everyday terms? It’s an awesome programming language that lets you do a wide variety of tasks—everything from system utilities to business programs to games to website backends and even artificial intelligence. Many Fortune 500 companies use Python, including household names like Google, IBM, Intel, Netflix, and Spotify. And Python is very popular in scientific circles. In fact, NASA uses Python heavily.

What Is Programming?

This might seem like an unusual question to ask in a programming book, but it’s a question I’ve been asked more than once by people who are considering getting into the field. If you aren’t a programmer, the definition can seem a bit nebulous.

Programming is the act of defining instructions that tell a computer how to solve a problem or perform some tasks. It differs from merely using a computer in that running a program like a word processor, spreadsheet, or video editor might create content (for instance, a balance sheet, a YouTube video, or a book), but it doesn’t tell the computer how to solve a specific problem. To continue with this analogy, using a word processing program to write a book is not programming, but programming would describe the creation of the word processor.

I have a friend who’s a very talented web designer who doesn’t really consider himself a programmer. He’s made the argument that someone who just copies and pastes code, modifying it to suit their purposes, is more of a hacker than a programmer. Whenever he mentions this, I tell him it’s pure hogwash. A programmer is anyone who writes programs, and writing a program includes modifying an existing program to do what you want it to do.

In a rapidly changing field like computer science, feeling like an impostor in a highly technical and competitive industry like this is a very real thing. In fact, many programmers, myself included, sometimes feel inadequate for the challenges we face. I remind my humble friend of that, and in doing so bolster my own self-esteem a bit. Anyone who wants to become more than a user of computers is already well on their way toward joining an elite and dedicated group of people who are willing to sacrifice sleep, sunshine, and physical activity for the sake of getting a computer to do what they tell it to do.

What We’ll Cover in This Book

First, we’ll learn the nuts and bolts of the Python programming language. Once we have a firm foundation, we’ll look at more complex data structures and logic. We’ll explore functions and object-oriented design, then dive deep into aspects of Python you’ll use daily, like handling text, numbers, input and output, website functionality, debugging, and even Git source code management.

How to Use This Book

We’ll learn as we go, working through sample exercises and building the ClydeBank Coffee Shop game with each chapter. This way, you won’t have to learn everything up front before doing something useful. By the time you finish your first chapter, you’ll have written a very simple program.

Rather than present you with a dry, boring reference book, I want to keep things interesting by giving you practical knowledge that you can use right away. I’d rather focus first on writing programs, then explore the theory behind it. I find it much easier to understand example code after I’ve seen it run.

Example Code

Most of the examples and all the game code are available on GitHub at https://github.com/clydebankmedia/python-quickstartguide. While the code is provided as a handy digital reference, I recommend typing each code example as we go, either in the Python interpreter or Visual Studio Code, because doing so will strengthen your understanding of the concepts we’re learning.

I’ll explain the differences between these two methods in chapter 1, but for now, the important thing is that you start writing Python code as you read the book. Simply reading the content will help, but it won’t give you the hands-on experience involved in writing Python.

What You’ll Need

First, you’ll need a computer. A phone or tablet won’t work for this exercise, unfortunately. However, if you have the e-book version of this title, you’ll find it handy to have it beside your computer as you work through the exercises. I’ve been known to use my iPad to reference material while I code.

As for the computer, it can be any modern Windows, macOS, or Linux computer. For Windows, that would mean a computer running Windows 10 or 11. There isn’t anything stopping you from running an older version of Windows, but using an out-of-date version means you could be missing important security updates. Additionally, newer versions of Python might not run on very old versions of Windows.

For macOS: I wrote these exercises on macOS Monterey, but any recent, supported version of macOS should be fine. Fortunately, Python comes preinstalled on macOS, so that part of the setup will be easy. However, if your version of macOS is older, it may come with Python 2 rather than 3. If updating to a modern version of macOS isn’t possible, consider installing Homebrew (commonly called brew, available at https://brew.sh) and running brew install python to install Python 3.

Any modern Linux distribution should be fine. Debian, Ubuntu, Linux Mint, Pop!_OS, Fedora, Arch, and Manjaro all make it super easy to install Python (and some even have it preinstalled).

A Google Chromebook® can technically run the Python interpreter in its Linux terminal, but you won’t be able to run Visual Studio Code (the editor we’ll be using in this book), so your experience will be severely hampered. We don’t recommend that type of computer for use with this book.

Operating System and Python Version Notes

There have been many individual versions of Python released since version 0.9 in 1991, but two major branches, versions 2 and 3, have dominated the Python landscape for nearly fifteen years.

Python 3 introduced a lot of new concepts into the language, but also created some incompatibility issues with version 2. Since Python 2 had a large ecosystem of modules, there was a split in the community between versions 2 and 3, with developers (especially in the early days) having to choose between version 2 compatibility and version 3 features.

In 2022, this isn’t really a concern. Python 3 was released in 2008, and Python 2 has been unsupported (end of life) since 2020. Thus we’ll be focusing entirely on version 3 in this book.

When I talk about the ecosystem of a project or version, I am referring to not only the software itself but the documentation, third-party modules and support, and community that is engaged in that particular version.

Getting Ready

Installing Python

First, we need to install Python. The instructions for this vary a bit depending on your operating system.

Windows

If you’re on Windows, you have two choices: install Python from the Microsoft Store or go to https://python.org and click Downloads. If you use the store version, please select version 3.10 or higher.

If you choose to download it from the website, it should automatically detect that you’re on Windows and offer the download link. Once you’ve downloaded the installer, run it, and accept the default options. Once that’s done, Python should be installed.

macOS

If you’re on macOS, it’s already installed, so there’s no need to do anything else.

Linux

If you’re on Linux, you probably already have Python installed. To check, open a terminal / command shell and type:

python3 --version

If that doesn’t work, try:

python --version

If the python command works but python3 doesn’t, and the version shown begins with a 2, then you need to specifically install Python 3. One of these commands should do the trick:

sudo apt install python3

sudo yum install python3

sudo dnf install python3

sudo pacman -S python

If these don’t work, consult the documentation for your version of Linux for instructions on installing Python 3.

Installing Visual Studio Code

There are many code editors on the market, many of them free/open-source. Here is a list of commonly used code editors with Python support, including their operating system support and license:

Visual Studio Code (Windows, macOS, Linux; free, mostly open source)

Notepad++ (Windows only; free, open source)

Sublime (Windows, macOS, Linux; paid software with free trial)

PyCharm (Windows, macOS, Linux; paid software with free trial)

Vim (Windows, macOS, Linux; free, open source)

Emacs (Windows, macOS, Linux; free, open source)

In this book, we’ll be using Visual Studio Code because it’s well supported on all operating systems and it’s free/open source. It’s an excellent code editor and development environment. That said, if you’re fond of another editor, please feel free to use it—just know that the instructions here will center around Visual Studio Code.

The simplest way to install Visual Studio Code on Windows is to use the Microsoft Store.

For macOS or Linux, go to https://code.visualstudio.com and download it for your platform. On macOS, the disk image can be mounted by double-clicking it. Then drag Visual Studio Code to your Applications folder. Linux users will need to download either a .deb (Debian, Ubuntu, Linux Mint, MX Linux) package or a .rpm (Fedora, openSUSE, Red Hat Enterprise Linux) package and install it on their system.

We’ll configure it in the next section, after creating a place for our code.

Other than Visual Studio Code for this book’s exercises, I’m not particularly recommending one editor over another. Code editor choice is a question of personal preference and workflow, and everyone has their own opinions on the subject. Nevertheless, if you’re looking for an alternative to Visual Studio Code, here are a few thoughts I have on each:

Notepad++ is an excellent code editor but is only available for Windows. If you only use Windows, this might be just fine, but its lack of multi-platform support creates issues for my workflow.

I haven’t had much experience with Sublime, but I know several colleagues who really like it. The price is reasonable and has some unique features you might find appealing.

I quite like the JetBrains products, including PyCharm, but this is a paid solution, so I didn’t want to focus on it in the book. Since I use other JetBrains products like DataGrid and RubyMine, PyCharm fits in well.

Vim and Emacs are more advanced editors. I use Vim quite a bit in my day-to-day programming and system administration work, and it has a lot of advantages, but I wouldn’t necessarily recommend it or Emacs to someone new to programming, as they both have a steep learning curve.

Creating a Folder for Your Code

I recommend making a folder in which to save all your Python code files. Where this is on your disk is entirely up to you, but I’ll share my approach, for inspiration.

Regardless of my operating system (macOS, Windows, Linux—I use them all), I create a folder called Source in my home directory. On macOS, you can access your home directory in Finder by clicking the Go menu, then clicking Home. On Windows, press the Windows key and R together, then type %USERPROFILE% in the box and click OK, or navigate to C:\Users\You (where You is your username). On Linux, most file managers open to this folder by default, but you can find it at /home/you (where you is your username).

Once in the home folder, I create a folder inside of that for each project I make. If you’re unsure how you want to proceed, just save it to a new folder called PythonQSG in your Source folder. You can always move it later if you like.

Even though the screenshots in this book are mostly taken from macOS, this doesn’t mean Windows and Linux users will be left out in the cold. As I said, I use all three platforms and love all of them for their strengths and weaknesses alike. Since Python is cross-platform, there are few instances where platform-specific instructions are necessary. However, any time there is a difference I’ll explain it on all three systems, so you’ll be able to enjoy Python no matter what computer you have.

Visual Studio Code Walk-Through

Visual Studio Code is a powerful open-source editor that makes it easy to manage programming projects both large and small. It supports many different programming languages, but we’ll be focused on Python.

Technically, the core of Visual Studio Code is open-source and is available at https://github.com/Microsoft/vscode; however, the Visual Studio Code product we’ll use that’s accessible through your Digital Assets contains proprietary Microsoft technology. Nevertheless, the bulk of the editor is open-source.

When you first start Visual Studio Code, you should see a link on the page to install support for Python. If so, click it and the necessary extensions will be installed. If you don’t see a link, that’s fine—there’s a way to get to the extensions from the left-hand pane (figure 1).

The Visual Studio Code welcome screen. The Extensions icon is highlighted in this figure on the left-hand pane.

When you click the Extensions icon, a list of common extensions will appear. Type Python in the search box and you’ll see the official Python extension from Microsoft in the list. You’ll know it’s the official extension because it says IntelliSense (Pylance), Linting … etc., and has Microsoft as the publisher with a blue check mark next to it. If it doesn’t have a green Install button beside it, it’s already installed. If it does, go ahead and click the button to install it (figure 2).

Searching for the Python extension in the extensions list of Visual Studio Code. In this figure, the official Python extension is shown with a check mark badge and the publisher, Microsoft, beneath the listing.

While you’re here, you can install other extensions if you like. If you’re new to programming in general, I don’t recommend you install anything else, except perhaps a theme. A theme in Visual Studio Code changes the colors of the editor to help with readability or simply to allow you to make adjustments according to your own preferences. To see the available themes, click on the funnel icon above the Extensions search box, click the Category menu, then select Themes.

This will show a list of themes available in Visual Studio Code. You have a lot to choose from. If you aren’t sure which you like, click one that interests you and it will generally display screenshots. Click Install next to the one you want to install, and it will automatically switch to it once downloaded.

If you’re looking for suggestions, I would recommend the Dracula Official theme or One Dark Pro. If you prefer lighter themes, GitHub Theme is quite nice. Filtering by theme (as shown in figure 3), then typing the name of the theme you’d like to install, will filter the available options to show only those for the name you type.

The GitHub Theme has several variations, so you’ll need to switch to it if you want the light variety. To switch themes at any time, click the Code menu at the top (or click the File menu on Windows and Linux), click Preferences, then Color Theme (figure 4).

Accessing the Themes category filter in the extension viewer.

The Color Theme menu item in the Code > Preferences menu.

Alternatively, you can use CTRL+K then CTRL+T on Windows and Linux, or CMD+K then CMT+T on macOS. A selection box will be displayed where you can change the theme (figure 5).

The color theme selector.

Now that we have Python support installed and our preferred color theme selected, let’s open the folder we created for our Python code. Click on the File menu and click Open Folder. Then navigate to the folder you chose and click the Open button. To browse the folder, click the Explorer icon (or press CMD+E on macOS, CTRL+SHIFT+E on Windows and Linux) in the left-hand pane (highlighted in figure 6).

The Explorer icon is in the box.

When you create new Python code files in the folder you selected for your code, they will show in the Explorer pane of Visual Studio Code.

That’s all you need to know about Visual Studio Code for now. We’ll pick up the actual coding workflow in chapter 1.

Code that should appear on a single line but must be broken due to page constraints is indicated using arrows. A straight arrow (↦) at the end of a line and and a curved arrow (↪) at the beginning of the next line indicate that those lines should be combined to make one continuous line.

Chapter by Chapter

Part I: Getting Started with Python

In part I, we’ll learn the basics of Python, from a tour of the Python interpreter (the core of the language) to understanding variables and working with conditionals and loops. We’ll also cover how to handle errors.

In chapter 1, we’ll get our feet wet with Python, learning about the interpreter and how to run a program in Visual Studio Code, and we’ll talk about strings and numbers.

In chapter 2, Python data structures are covered, including lists, tuples, sets, and dictionaries. We also explore working with multiple sets of data.

In chapter 3, program flow is discussed, including if statements and their variants, as well as loops.

In chapter 4, we’ll learn how to handle errors (called exceptions in Python) in our program.

Part II: Functions and Classes

In part II, we explore functions and classes and introduce the object-oriented programming paradigm, which will empower you to solve complex problems with simple yet logically organized code. We’ll also cover inheritance, modules, and packages.

In chapter 5, we explore the world of functions and learn how to pass arguments to them. Functions let us avoid repeating ourselves and modularize our code.

In chapter 6, object-oriented programming and classes are introduced. Classes are structures that contain both data and code, letting us organize our application’s functionality.

In chapter 7, we dive deep into inheritance and common design patterns.

In chapter 8, a survey of dataclasses shows how to save time by helping us prototype our classes.

In chapter 9, we modularize and reuse our code files and logic with modules and packages.

Part III: Python in Action

In part III, we’ll delve into advanced Python functionality, like math, statistics, input/output, and interacting with the internet. We’ll also tour several popular website frameworks and learn about debugging techniques.

In chapter 10, advanced strings, input validation, and compression are covered. We also delve into the immensely powerful world of regular expressions.

In chapter 11, we explore the wealth of Python’s math and statistics functionality.

In chapter 12, input and output functionality is covered, and we discuss how to serialize our classes.

In chapter 13, we’ll interact with the internet, send email, and fetch web pages.

In chapter 14, we learn how to debug our Python applications.

Part IV: Advanced Python

In part IV, we’ll finish our journey by learning about interfacing with databases, test-driven development, managing source code with Git, and optimization. And we cover a cornucopia of additional functionality you’ll use in your everyday Python work.

In chapter 15, the focus turns to web development frameworks like web.py, Flask, and Django.

In chapter 16, the SQLite database system is explored with a practical example database.

In chapter 17, test-driven development is covered.

In chapter 18, the basics of the Git source code management system are discussed.

In chapter 19, we pull out the junk drawer and go over several smaller topics that didn’t quite deserve an entire chapter but are important for Python developers.

In chapter 20, profiling and optimizing Python code are discussed, both for absolute performance and apparent speed.

In chapter 21, I suggest some next steps in your Python journey.

I’m excited to teach you Python. Let’s get started!

PART I

GETTING STARTED WITH PYTHON

| 1 |

Getting to Know Python

Chapter Overview

The Python interpreter runs Python code

Python programs are text files ending in .py

Variables store data, including strings and numbers

The Python interpreter is the heart of the Python programming language. True to its name, it interprets and runs the commands we give it, either interactively or, most commonly, from a file. Just as a text file generally ends in .txt and an image file in .jpg, a Python program is essentially nothing more than a text file with a .py extension that contains instructions for the Python interpreter. In the simplest of terms, writing a Python program is like writing a text file with special commands that the Python interpreter understands.

Before Our First Line of Code

Programming is an art and not an exact science. Many beginning programmers feel intimidated about making a mistake or not making the right choices. That’s perfectly natural. If you’re concerned about starting what you fear is a daunting task, let me say right now that you will make mistakes and things will go wrong.

Perhaps that doesn’t sound very reassuring, but take comfort in the fact that this happens to every programmer all the time. I have been frustrated for days over something not working right, only to discover that one simple letter had broken my code and the fix for it took less than half a second. Programming can be frustrating, but it can also be wildly rewarding. As you learn more about Python and write programs, you will feel more comfortable with the language.

So get comfortable, take a deep breath, and let’s dive in!

Hello, World!

The very first thing most programmers learn to write is the classic Hello, World! program. This was popularized in the 1978 book The C Programming Language by Brian Kernighan and Dennis Ritchie. The first example in the book was a simple program that wrote Hello, World! to the screen and exited. Carrying on with this fine tradition, let’s write our first program.

Since Python programs can be entered interactively in the interpreter or in a text file ending in .py, we’re going to use both methods so you can get a feel for each. In most of the examples in this book, we’ll be using Python files, but entering commands in the interpreter can be an excellent way to test small snippets of code and get immediate results.

If you haven’t set up Python or Visual Studio Code, please refer to the introduction.

In the Interpreter

Let’s launch Python. If you’re on Windows and used the Microsoft Store to install Python, you can find it in the Start menu. Just search for Python and then run the program.

Enjoying the preview?
Page 1 of 1