Python for Serious Beginners: A Practical Introduction to Modern Python with Simple Hands-on Projects: Real Programming Lessons for Smart Learners, #2
By Harry Yoon
()
About this ebook
Learn Real Python Programming Today! With Hands-On Coding Projects and Exercises
Wanna learn programming? Wanna learn Python? Start from this book! This book will teach you the fundamentals of programming and the Python language basics, in a series of thoughtfully organized lessons for the most effective learning experience. It includes many hands-on exercises!
Python for (Serious) Beginners will give you the best introduction to programming in Python whether you are coming from a different programming language background or you are learning programming for the first time. This book covers all the essential features of Modern Python (Python 3.10/3.11) through the carefully designed code examples.
Python for (Serious) Beginners starts from the absolute basics such as how to install the Python tools on your machine, and how to use the Python interactive shell, and it covers all the key concepts of Python 3 with enough depth to be useful even to the experienced programmers.
Python for (Serious) Beginners is rather unique in that, throughout the book, we cover the fundamentals of Python programming while working on a few simple real programming projects. The book also includes a few "lab sessions" with a number of practical exercises, in which the readers can practice real hands-on programming.
Python for (Serious) Beginners covers the following topics, among others:
- How to install Python locally on your machine.
- How to effectively use the Python REPL (interactive shell)
- The basic structure of a Python program.
- Python modules and packages.
- Basic constructs of Python such as expressions and statements.
- Simple built-in data types, e.g., as integer, float, bool, and string.
- Complex built-in data types, e.g., list, tuple, and dictionary.
- Objects. Variables and assignments.
- Immutability vs mutability.
- Arithmetic and comparison operations.
- Built-in functions and methods, e.g., print(), input(), type(), etc.
- Loops using the for and while statements.
- If-else conditional expressions and conditional statements.
- The new `match` statement. (New as of 3.10.) Structural pattern matching.
- How to define a function using the def statement.
- How to define a custom type using the class statement.
- How to create a new enum type.
- Typing and type annotations.
- Fundamental concepts of programming such as "recursion".
- Object oriented programming (OOP).
- Basics of the software development process.
Order your copy and start learning real Python programming today!
Note 1: The book primarily uses CLI (terminal programs), and not IDEs, to illustrate the software development practice.
Note 2: Smart people will most likely pick up this book and end up learning real solid Python programming in the long run!
Other titles in Python for Serious Beginners Series (1)
Python for Serious Beginners: A Practical Introduction to Modern Python with Simple Hands-on Projects: Real Programming Lessons for Smart Learners, #2 Rating: 0 out of 5 stars0 ratings
Read more from Harry Yoon
The Art of Go - Basics: Introduction to Programming in Go: Read and Learn Programming Rating: 0 out of 5 stars0 ratingsEinstein's Relativity: The Special Theory and the General Theory - Free Chapter 2 Rating: 0 out of 5 stars0 ratingsEinstein's Relativity: The Special Theory and the General Theory - Free Chapter 1 Rating: 0 out of 5 stars0 ratingsEinstein's Relativity Part I - The Special Theory of Relativity Rating: 0 out of 5 stars0 ratingsEinstein's Seven Essays on Relativity, Geometry, and Scientific Methods Rating: 0 out of 5 stars0 ratingsEinstein's Relativity: The Special Theory and the General Theory - Free Chapter 3 Rating: 0 out of 5 stars0 ratings
Related to Python for Serious Beginners
Related ebooks
Basic Core Python Programming: A Complete Reference Book to Master Python with Practical Applications (English Edition) Rating: 0 out of 5 stars0 ratingsPython In - Depth: Use Python Programming Features, Techniques, and Modules to Solve Everyday Problems Rating: 0 out of 5 stars0 ratingsThe Ultimate Python Programming Guide For Beginner To Intermediate Rating: 4 out of 5 stars4/5The 1 Page Python Book Rating: 2 out of 5 stars2/5Essential Python 3 Rating: 0 out of 5 stars0 ratingsPYTHON PROGRAMMING Rating: 4 out of 5 stars4/5Python Rating: 0 out of 5 stars0 ratingsPython from the Very Beginning Rating: 0 out of 5 stars0 ratingsPython Made Simple: Learn Python programming in easy steps with examples Rating: 5 out of 5 stars5/5Easy-To-Follow Tutorial To Learn Python Programming In Less Than One Week Rating: 3 out of 5 stars3/5Mastering Python Programming: From Basics to Expert Proficiency Rating: 0 out of 5 stars0 ratingsPython for Beginners: An Introduction to Learn Python Programming with Tutorials and Hands-On Examples Rating: 4 out of 5 stars4/5Fundamentals of Programming: Using Python Rating: 5 out of 5 stars5/5PYTHON: Practical Python Programming For Beginners & Experts With Hands-on Project Rating: 5 out of 5 stars5/5Python Essentials Rating: 5 out of 5 stars5/5Profound Python Rating: 5 out of 5 stars5/5Tiny Python Projects: Learn coding and testing with puzzles and games Rating: 4 out of 5 stars4/5Python Programming : How to Code Python Fast In Just 24 Hours With 7 Simple Steps Rating: 4 out of 5 stars4/5Mastering Objectoriented Python Rating: 5 out of 5 stars5/5Python: Beginner's Guide to Programming Code with Python Rating: 0 out of 5 stars0 ratingsPython Simplified: Learn Programming Through Practical Examples Rating: 0 out of 5 stars0 ratingsPython For Data Science Rating: 0 out of 5 stars0 ratings
Internet & Web For You
How to Be Invisible: Protect Your Home, Your Children, Your Assets, and Your Life Rating: 4 out of 5 stars4/5Coding All-in-One For Dummies Rating: 4 out of 5 stars4/5Coding For Dummies Rating: 5 out of 5 stars5/5More Porn - Faster!: 50 Tips & Tools for Faster and More Efficient Porn Browsing Rating: 3 out of 5 stars3/5The Gothic Novel Collection Rating: 5 out of 5 stars5/5No Place to Hide: Edward Snowden, the NSA, and the U.S. Surveillance State Rating: 4 out of 5 stars4/5Python: Learn Python in 24 Hours Rating: 4 out of 5 stars4/5The $1,000,000 Web Designer Guide: A Practical Guide for Wealth and Freedom as an Online Freelancer Rating: 4 out of 5 stars4/5The Digital Marketing Handbook: A Step-By-Step Guide to Creating Websites That Sell Rating: 5 out of 5 stars5/548 Really Useful Web Sites Rating: 5 out of 5 stars5/5Beginner's Guide To Starting An Etsy Print-On-Demand Shop Rating: 0 out of 5 stars0 ratingsSocial Engineering: The Science of Human Hacking Rating: 3 out of 5 stars3/5Ultimate guide for being anonymous: Avoiding prison time for fun and profit Rating: 5 out of 5 stars5/5SEO For Dummies Rating: 4 out of 5 stars4/5Kill All Normies: Online Culture Wars From 4Chan And Tumblr To Trump And The Alt-Right Rating: 3 out of 5 stars3/5Content Chemistry: The Illustrated Handbook for Content Marketing Rating: 5 out of 5 stars5/5The Beginner's Affiliate Marketing Blueprint Rating: 4 out of 5 stars4/5Canva Tips and Tricks Beyond The Limits Rating: 3 out of 5 stars3/5Everybody Lies: Big Data, New Data, and What the Internet Can Tell Us About Who We Really Are Rating: 4 out of 5 stars4/5How to Disappear and Live Off the Grid: A CIA Insider's Guide Rating: 0 out of 5 stars0 ratingsTor and the Dark Art of Anonymity Rating: 5 out of 5 stars5/5Ready, Set, Brand!: The Canva for Work Quickstart Guide Rating: 5 out of 5 stars5/5Surveillance and Surveillance Detection: A CIA Insider's Guide Rating: 3 out of 5 stars3/5Cybersecurity For Dummies Rating: 5 out of 5 stars5/5Mastering YouTube Automation: The Ultimate Guide to Creating a Successful Faceless Channel Rating: 0 out of 5 stars0 ratingsLying and Lie Detection: A CIA Insider's Guide Rating: 0 out of 5 stars0 ratings
Reviews for Python for Serious Beginners
0 ratings0 reviews
Book preview
Python for Serious Beginners - Harry Yoon
Table of Contents
Preface
Introduction
Modern Python
Book Organization
Absolute Basics
1. Hello Monty Python
1.1. The First Step
1.2. The Project
2. Development Environment Setup
2.1. Command Line Interface (CLI)
2.2. Python Installation
2.3. Visual Studio Code
2.4. Python Extension
2.5. Test Program
2.6. How to Run Python Programs
2.7. Code Review
2.8. Type Annotations
2.9. Summary
3. Interactive Tour of Python
3.1. Python REPL
3.2. Basic Concepts of Programming
3.3. Summary
4. Numbers, Strings, and More
4.1. Starting Python Interactive Session
4.2. Numbers in Python
4.3. Builtin type Function
4.4. Errors
4.5. Expressions
4.6. Interactive vs Non-Interactive Modes
4.7. Builtin print Function
4.8. The None Object
4.9. Boolean Expressions
4.10. Dynamic Typing
4.11. Builtin bool Function
4.12. Simple and Compound Statements
4.13. Conditional Statement
4.14. Strings in Python
4.15. String Concatenations
4.16. Ending Python Interactive Session
4.17. Summary
5. Tuples, Lists, and Some Inspirations
5.1. Complex Types
5.2. Tuple Literals
5.3. Expression List
5.4. Tuple Type
5.5. List Literals
5.6. List Operations
5.7. Names
in Python
5.8. Assignment
5.9. Slicing
5.10. Sorting
5.11. Help!!
5.12. Inspirations
5.13. Summary
6. Review - Basics
6.1. Questions
6.2. Exercises
Rock Paper Scissors Project
7. Hello Rock Paper Scissors!
7.1. Working on a Project
7.2. Let’s Play Rock Paper Scissors
8. Software Design
8.1. Deconstructing Rock Paper Scissors
8.2. Tasks
9. Project Setup
9.1. Workspace
9.2. Virtual Environments
9.3. Package Install
9.4. Source Control System
Imperative Programming
10. Main Project - Rock Paper Scissors
10.1. Rock Paper Scissors
10.2. Import Statement
10.3. Function Definition
10.4. Comparison Operators
10.5. The if Statement
10.6. Builtin input Function
10.7. Variables/Names
10.8. Rules on Names
10.9. Naming Conventions
10.10. Scopes
10.11. String Methods
10.12. Random Module
10.13. Boolean Operators
10.14. Lines in Python
10.15. Error Handling
10.16. Putting It All Together
10.17. Code Review
11. Lab 1 - Expressions and Statements
11.1. Echo
11.2. Dice Rolls
11.3. Is It Positive?
11.4. To Uppercase
11.5. Random Letters
11.6. Random Arithmetic
11.7. Can I Buy a Vowel?
11.8. All True or Not
11.9. Spade, Heart, Diamond, or Clubs
11.10. Random Suit
11.11. The Same Suit Or Not
11.12. Rock Paper Scissors
Procedural Programming
12. Rock Paper Scissors - The Sequel
12.1. Python Modules
12.2. Python Packages
12.3. Tuple Unpacking
12.4. Function Definitions
12.5. Function def with Type Annotations
12.6. Expression Statements
12.7. Doc Strings
12.8. Ellipsis (…)
12.9. random.choice()
12.10. Sequence Replication
12.11. f-String Expressions
12.12. Conditional Expressions
12.13. States
12.14. For Range Loop
12.15. While Loop
12.16. Error Handling
12.17. Putting It All Together
12.18. Code Review
13. Lab 2 - Functions, Loops, and More
13.1. Sum
13.2. Product
13.3. Filtered Sum
13.4. Singular vs Plural Nouns
13.5. Power Operator
13.6. Tuple Parameter
13.7. Reverse a List
13.8. Rock Paper Scissors
13.9. How Many Rounds?
13.10. Best of Seven
Object Oriented Programming
14. Rock Paper Scissors - The Finale
14.1. Modules and Packages
14.2. Backslashes
14.3. Optional and Union Types
14.4. Boolean Context
14.5. Recursion
14.6. Object Oriented Programming (OOP)
14.7. Class
14.8. Class Objects
14.9. Constructors
14.10. Class Variables
14.11. Instance Objects
14.12. Instance Variables
14.13. Instance Methods
14.14. Private Members
14.15. Dunder Attributes
14.16. Inheritance
14.17. Polymorphism
14.18. Truth Values
14.19. Enum
14.20. Match Statement
14.21. Dictionary
14.22. Putting It All Together
14.23. Code Review
15. Lab 3 - OOP and Other Modern Features
15.1. Days of the Week
15.2. I’ll Be Going …
15.3. Playing Cards
15.4. Length Function
15.5. Sum Function
15.6. Product Function
15.7. String Length Comparison
15.8. String Concat Function
15.9. Multiplication Table
15.10. Fibonacci Sequence
15.11. Rock Paper Scissors
Wrapping Up
16. Final Projects
16.1. Computer vs Computer
16.2. How to Prevent Cheating
16.3. Student Records
16.4. War (Card Game)
17. Epilog - Let’s Play!
Credits
About the Author
Community Support
Copyright
Python for Beginners:
A Practical Introduction to Modern Python
© 2021-2022 Coding Books Press
All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews.
Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the author, nor its dealers and distributors will be held liable for any damages caused or alleged to be caused directly or indirectly by this book.
Published: November 2021
Harry Yoon
San Diego, California
Preface
Slow and Steady Wins the Race.
Python is one of the most popular programming languages. In fact, it is widely used just about anywhere programming is done. People use Python to automate the system administration tasks. People use Python to build their Websites and Web applications. People build robots and controllers using Python. And, Python is now becoming the language of choice for many people to do machine learning and data science.
Python is a high level
programming language. It is one of the most favorite languages among the people who are just starting to learn programming. It is easy to learn and use, and yet it provides enough complexity and flexibility. As a matter of fact, Python is one of the most powerful modern programming languages.
If you are new to programming, then this book will guide you through the initial steps in your journey to the wonderful world of programming in Python. If you have some experience with programming, then this book will give you a quick taste of the modern Python
, covering the most recent releases, up to 3.10 and 3.11, as of this writing.
Python for Beginners will not make you the master
of Python, as many beginner’s books claim. Many programming books put too much emphasis on the programming language syntax and a laundry list of the language features, and not enough on the real programming.
This book takes a rather different and unique approach. Throughout the book, we will work on one main programming project. In this book, we will build a Python version of the game of Rock Paper Scissors. The choice of the particular project that we use is, however, not that important. In the process of building the game program, we will cover all the basic elements of software development in Python (some, with enough theoretical depth to be useful even to the more experienced programmers).
Unlike the vast majority of the books targeted to the programming beginners, this book will make you think and do. If you are looking to gain some quick and superficial knowledge only, this book is not for you.
The book starts from the absolute basics of programming. And, by the end of the book, you will have built a complete software that fully functions. (In fact, a few times over.) This will give you a sense of accomplishment and the motivation to learn more.
After learning the basics of Python programming using this book, you will be ready to pursue the more difficult topics in Python programming. And, above all, you will likely be eager to do more programming. If so, then this book has done its job.
Good luck!
Revision 1.6.2, 2022-09-07
Introduction
Practice makes perfect.
Modern Python
Python comes with many builtin high-level data structures, with a very simple and clean literal syntax. It includes a large number of builtin functions and methods. It also comes with an extensive set of standard libraries. It makes it very easy to share your code with the community using modules and packages. Python, like many other high-level languages, also does automatic memory management on behalf of the programmers.
Python supports the object-oriented programming styles as well as the functional programming styles. Its flexibility, such as the dynamic typing, makes it an ideal language for scripting and rapid prototyping in many application areas.
Dynamic languages like Python provide more freedom to the developers. They are more forgiving to the beginning programmers. For small and quick projects, they are ideal. On the flip side, it is generally harder to build larger software systems using the languages like Python.
Python has a long history. It has been around for over 30 years. The language version 2 has been deprecated, and Python
now means Python 3. As of this writing, Python 3.10 is the most recent version.
Although this is a beginner’s book, we will try to use some of the modern features of Python, whenever applicable, so that the readers do not have to go through a re-learning process in a short while. One of the most notable features that we will use in this book, which is not generally taught in the beginner’s books, is what is called the typing
, or the type hints
or type annotations
. Although Python is a dynamically typed language, typing provides (some of) the benefits of the static typing, which can help reduce the number of possible bugs in Python programs, especially in large projects.
Book Organization
Python for Beginners: A Practical Introduction to Modern Python is organized into a few dozen lessons
. In the first few lessons, we will briefly cover the absolute basics of programming, and programming in Python. Even if you have some experience with programming in other languages, you may find it useful to go through this part. Python is a rather unique language, and if you are coming from other C-style programming languages
like C/C++, Java, C#, or even Javascript, then you may find these foundational lessons useful.
In some sense, this part is almost like a (mini) book in a book. It can be independently read, if you are absolute beginners, without having to go through the entire book, and you will still be able to get the essence of Python programming.
This first part also sets the common tone for the book for the readers with different backgrounds. As suggested, even if you have some experience with programming, we highly recommend that you at least browse this part. We cover some basics
in this book that may be considered advanced topics
for some beginners.
For the remainder of the book, starting from Hello Rock Paper Scissors!, we focus on our main project, namely, building a CLI version of the Rock Paper Scissors game, from beginning to end. If you are an impatient type, and have some prior experience with programming in Python, then you can also start this book from the beginning of the Main Project - Rock Paper Scissors, without losing too much context.
There is a fair amount of repetitions throughout the book. This is deliberate. We learn through repetitions. The first introductory part and this later project part have some overlaps on the one hand, but they are also complementary to each other on the other hand.
We will work on a few different variations of the Rock Paper Scissors game in this book. By doing so, we will introduce various essential concepts of programming in modern Python.
Part 3A - We create a rock paper scissors game with simple procedural programming.
Part 3B - We introduce the basic concepts of Python functions, and we re-implement the game using functions.
Part 3C - In the third and final iteration, we use the object-oriented programming techniques to implement the rock paper scissors game.
After completing each implementation, we provide a lab session
, in which the readers can practice programming in Python on their own.
Overall, Python for Beginners: A Practical Introduction to Modern Python covers the following topics, among others:
How to install the Python tools locally on your machine.
How to effectively use the Python interactive shell (aka REPL).
The basic structure of a Python program.
Python modules and packages.
Basic constructs of Python such as expressions and statements.
Simple builtin data types, e.g., integer, float, bool, and string.
Complex builtin data types, e.g., list, tuple, and dictionary.
Objects. Variables and assignments.
Immutability vs mutability.
Arithmetic and comparison operations.
Builtin functions and methods, e.g., print, input, type, etc.
Loops using the for and while statements.
Conditional expressions and conditional statements.
The new match statement. (New as of 3.10.)
How to define a function using the def statement.
How to define a custom type using the class statement.
How to create a new enum type.
Typing and type annotations.
Fundamental concepts of programming such as recursion
.
Object oriented programming.
Basics of the software development process.
Finally, in the (optional) chapter titled Final Projects
in Part 4, we include a few project ideas so that the readers can practice what they have learned in this book. The readers are encouraged to try at least one of these projects.
As stated, Python is an easy
language to learn, and to start programming with. But, building a good foundation is essential if you want to become a skilled Python programmer in the long run. Hope you find this book helpful in your journey into the programming world, in Python.
Now, let’s get started!
Absolute Basics
Our main focus in this book will be going through a few complete programs, from beginning to end. But, we will start with some absolute basics of Python programming.
For the readers who have never done any Python programming, we will start with how to set up a basic development environment. There are many options, including using some of the online tools, but we will mainly cover the traditional setup that uses our own computers as development machines.
As mentioned in the beginning, we primarily, in fact exclusively, use CLI for development. You will need some familiarity with the terminal programs and Unix shell commands, etc. (We will, however, try to explain everything that is relevant as we go along.)
Python is an interpreted language
. One of the best tools available to Python programmers is the Python REPL
, an interactive Python interpreter. (Or, more precisely, the Python interpreter used in the interactive mode.) We will introduce its basic uses in the earlier lessons so that the readers can explore various features of Python on their own, if desired, while reading this book.
This first part comprises five chapters, or lessons
, in which we will cover some basic concepts of the programming (in Python) for the benefit of the readers who do not have any real programming experience. Although it is an informal introduction, these lessons will set the tone for the rest of the book.
Now, let’s get started!
1. Hello Monty Python
Hello Monty Python1.1. The First Step
Have you ever heard of Monty Python’s Flying Circus? It was an old British TV show. Let’s say Hi to the Pythons.
Listing 1. hello_python/main.py
It is only one line, but this is a complete Python program. We will learn in this book how to write a program like this and run it on your computer. Without specifying how exactly for now, if we run this program on the terminal, then we get the following output:
In this book, we will work on slightly more complicated programs. But, first things first. What does this simple program mean? What does it do? How does it work?
Python is a procedural, or imperative, programming language. It supports the object oriented and functional programming styles, among others, but ultimately it is imperative (as in giving commands
). This means that a Python program consists of a series of statements
. A statement is an instruction to the computer as to what needs to be done. In the imperative programming world
, you will need to tell the computer what should be done and how they should be done, step by step, using the statements. That is programming.
This Hello Monty Python program includes one statement, print(Hello Monty Python!
). print is a function
. (Note the pair of parentheses following the function name.) In particular, it is a builtin function
, meaning that this function is defined in the language itself (and directly built into the language interpreter
). Programmers can also define their own custom functions in their programs. We will discuss what exactly a function is and how we can create our own functions, in more detail, later in the book.
In this sample code, we call
the builtin print function with a string argument
, that is, Hello Monty Python!
. (Note the pair of double quotes.) The job of this function is to print, to the terminal, the string, or text, passed in to it as an argument. (An argument is what is between the pair of parentheses.) And, that was what it did when we ran the program above.
The argument Hello Monty Python!
is an object
of the string type. In Python, everything that we deal with is an object. An object has an identity
, and each object has a type
and a value
. More on this later.
If you are new to programming, and if it all sounds gibberish to you, no worries. We will go over these concepts throughout this book. As indicated, we will do repetition, repetition, and repetition. You do not have to understand everything on your first encounter. At the end of the day, what we call knowledge mostly boils down to familiarity
.
1.2. The Project
This book takes an interesting, and rather unusual and unique, approach in teaching the basics of programming, in Python.
We will work on one main software project in this book. And, we will mostly focus on the language features and the programming techniques so far as they are useful, or relevant, to the project. On the one hand, this means that you may, or may not, be able to get the complete and comprehensive view of the programming in Python. On the other hand, the real advantage of this approach is that you will get to do the whole software project without being distracted, or overwhelmed, by the nitty gritty details of the entire programming language. The benefits of doing this way will be enormous, especially for the beginning programmers.
Over time, with more training and practice, you will gradually get more exposure to various different aspects of Python programming. But, for now, let’s focus. After completing the project, you will get a sense of accomplishment. You will have learned how to really program in Python, from start to end. That will be a huge accomplishment.
The main project that we will work on in this book is the children’s game of rock paper scissors. We will create a computer program that lets a user play rock paper scissors by themselves (e.g., without requiring a game partner).
Rock Paper Scissors is one of the most popular games. But in case you need a refresher, here’s a Wikipedia page: Rock paper scissors. The particular project that we work on is somewhat secondary. Our focus in this book is learning Python programming. It will be still helpful, however, if you play a few games before we start working on the project so that you are fully familiar with the problem that we are going to tackle. In any real-world programming problems, in general, a lack of domain knowledge can make your job harder as a programmer.
The computer is just a tool. It is you who will have to solve the problems.
2. Development Environment Setup
Development Environment SetupWe are going to start working on the rock paper scissors game shortly, but our work has already begun. The very first thing we need to do is to set up a development environment
. If you already have done this, and have done some programming with Python, then you can skip this lesson.
It goes without saying that there are many different ways in which you can develop Python programs. We describe one particular method in this lesson. Although it is not required, if you use the same setup as described here, then it will be a bit easier for you to follow the lessons in this book.
2.1. Command Line Interface (CLI)
In this book, we will mainly use the CLI tools for development. The IDEs, or Integrated Development Environment, can be useful, but they are not absolutely necessary to do programming. In fact, it is better for the beginning programmers to start with the CLI tools. The IDEs can obscure some basic processes of software development.
If you are on Linux or Mac, then you should be familiar with terminal programs. For example, on Ubuntu many people use the Terminal program that comes with the standard desktop distribution. On Mac, iTerm2 is one of the more popular console programs.
If you are on Windows, then we strongly recommend that you install WSL, Windows Subsystem for Linux, with a Linux distribution such as Ubuntu 20.04LTS. Then you can use the Windows Terminal app with a Unix shell.
We will primarily use Unix shell for illustrations in this book, when relevant. If you use Command Prompt (CMD) or PowerShell on Windows, then you may need to do some mental translations
.
2.2. Python Installation
We will need the Python interpreter tools. In some systems, Python may come pre-installed. Let’s try and see if you already have Python. Open a terminal app, and type the following:
Or
If either of these commands returns an output that indicates that you have a python version 3.10 or later, e.g., Python 3.10.0 or Python 3.10.1, etc., then you are good to go. Use that command which generated the desired version output. Otherwise, you will need to install a recent version of Python.
The dollar sign $ is used to represent the shell prompt in this book. You type the commands after the $
prompt. The shell prompt may look different on your system.
You can download the Python source code from their official download page, Download Python, and you can build it yourself if you feel brave enough. That is, however, not recommended for the beginners.
You can just use one of the platform-dependent pre-built Python distributions. (Although some of them may not be official
, most Python developers use one of these distributions.) Try to install the most recent stable version. (3.10.4 is the current stable release as of this writing, and by the time when you read this, newer versions like 3.11 may have been released.)
It is not required to use the most recent version to learn Python. But, if you are just starting, then there is no reason to use an older version. Every time a new version of Python is released, you will have to go through some (small or big) relearning process, sooner or later. By starting with the most current version, you are doing yourself a big favor by not having to go through these relearning processes. Besides, some of the sample code of this book use some features from Python 3.10 or later.
If you are on Windows, here’s the binary distribution: Python Releases for Windows. Note that if you use WSL on Windows, as we recommend, then you may instead want to install an appropriate distribution for Linux on your Windows subsystem.
If you are on Mac, you can find the installers on this page: Python Releases for macOS.
If you use Linux (including Linux on WSL), then you may have to search the Web to find the appropriate installation instruction for your specific Linux distribution. Most Linux distributions come with Python, but the version might be old. As stated, we recommend you install version 3.10 or later.
For example, you can do a Web search with the keyword phrases like these: how to install the latest version of python on ubuntu
, install python 3.10 on debian
, etc. Note that you can have multiple installations of Python (e.g., with different versions) on your system.
If you use Ubuntu 20.04 or 22.04LTS, for example, then you can install Python 3.10 using the apt tool.
①
The python3 package currently includes the version 3.10.
Make sure that your installation was successful by trying out a few python commands. For example,
The actual command name can be different on your system. It could be python or python3 or something else. Although Python version 2.x has been deprecated, due to the historical reasons, many (Linux) Python distributions may still use the name python3, or something similar, for the python command.
Does it successfully