Python for Beginners
eBook - ePub

Python for Beginners

Learn It as Easy as Pie

  1. English
  2. ePUB (mobile friendly)
  3. Available on iOS & Android
eBook - ePub

Python for Beginners

Learn It as Easy as Pie

About this book

Whether you are an absolute beginner or an experienced programmer, learn Python programming in a simple, concise, and straightforward manner. Learn to build four smashing projects: a calculator, a drawing app, a login system, and a notes app. This book will walk you through the first steps of becoming a programmer as easy as pie.

Frequently asked questions

Yes, you can cancel anytime from the Subscription tab in your account settings on the Perlego website. Your subscription will stay active until the end of your current billing period. Learn how to cancel your subscription.
At the moment all of our mobile-responsive ePub books are available to download via the app. Most of our PDFs are also available to download and we're working on making the final remaining ones downloadable now. Learn more here.
Perlego offers two plans: Essential and Complete
  • Essential is ideal for learners and professionals who enjoy exploring a wide range of subjects. Access the Essential Library with 800,000+ trusted titles and best-sellers across business, personal growth, and the humanities. Includes unlimited reading time and Standard Read Aloud voice.
  • Complete: Perfect for advanced learners and researchers needing full, unrestricted access. Unlock 1.4M+ books across hundreds of subjects, including academic and specialized titles. The Complete Plan also includes advanced features like Premium Read Aloud and Research Assistant.
Both plans are available with monthly, semester, or annual billing cycles.
We are an online textbook subscription service, where you can get access to an entire online library for less than the price of a single book per month. With over 1 million books across 1000+ topics, we’ve got you covered! Learn more here.
Look out for the read-aloud symbol on your next book to see if you can listen to it. The read-aloud tool reads text aloud for you, highlighting the text as it is being read. You can pause it, speed it up and slow it down. Learn more here.
Yes! You can use the Perlego app on both iOS or Android devices to read anytime, anywhere — even offline. Perfect for commutes or when you’re on the go.
Please note we cannot support devices running on iOS 13 and Android 7 or earlier. Learn more about using the app.
Yes, you can access Python for Beginners by Yatin Bayya in PDF and/or ePUB format, as well as other popular books in Informatica & Compilatori. We have over one million books available in our catalogue for you to explore.

Information

Publisher
BAYYA
Year
2020
eBook ISBN
9780578792248
Edition
1
Subtopic
Compilatori
CHAPTER 1:
Introduction
The digital age is revolutionizing the world in terms of technology and software. Programmers provide the most useful software, enhance the world, and communicate with computers by thinking creatively.
Python is the language of choice for many of these next-generation problem-solvers. It is a powerful language whose explicit syntax allows new programmers to focus on developing software. As of 2020, Python is one of the most popular programming languages and ranks consistently at the top.
This book teaches the essential concepts and syntax of Python 3 and provides guidance for implementation with tutorials on building projects.
Getting Started
Without further ado, here is how to install Python 3 and get started creating applications.
To begin the installation, go to the “Downloads” page on the Python website (https://www.python.org/downloads). Select the latest stable download version (versions 3.x) for your operating system. Open the file after the download completes.
On Windows:
img
In this window, check “Add Python 3.8 to PATH” and click “Install Now.”
On macOS:
img
In this window, go through all the steps until “Installation Type” and then click on the button “Customize” and make sure that everything is selected as shown in the preceding window. After you have confirmed, everything is selected, finish the installation.
Once completed, search for the Integrated Development and Learning Environment (IDLE) application which is installed with Python earlier:
img
This is known as an interpreter, which evaluates and converts the code typed in there on the fly. Python is a high-level programming language that is easier for developers to read. In contrast, the computer understands the low-level machine code that is more difficult to read. The solution for the computer to understand Python is to convert this high-level code into machine code. There are several implementations (ways of converting code) to achieve this:
The first option is to use a compiler that translates the code into machine code and then runs it. The machine code created by the compiler is executable, which can be run on other computers but is not fully cross-platform compatible. The executable cannot run on any operating system unless the source code is shared, and each operating system has its own version.
Another way is to use an interpreter that translates the code on the fly and then runs it, rather than saving a separate file. The downside is that when the application needs to run on someone else’s machine, the source code is exposed. Additionally, the client will need an interpreter to run the code. This approach is often slower than compilers because it requires to recompile every single time.
Both options have advantages and disadvantages, but the intermediate approach or bytecode combines their benefits. A virtual machine converts the source code to the lowest level possible while remaining cross-platform compatible. When running it, the compiler translates it into machine code. This code is not executed by the CPU but rather by the Python Virtual Machine (PVM). Python takes this approach, but its process is still, primarily “interpreted,” and it also has some compilations under the intermediate approach or bytecode.
In the IDLE menu bar, click on the “File” button and then on the “New File” option:
img
This will open a blank window for Python code. The first program will be a simple program that displays “Hello World” when executed:
img
Click on the “Run” option and then “Run Now,” and save the file to execute the code. Now it should show the printed result of “Hello World,” where commands execute and receive a result. “Print” is a built-in function. A function is a block of code that can be executed by using its name. Python’s source code defines a function to display a message and calls it to print. Essentially, the “print” function code outputs and displays a string (text).
The “print” function’s syntax is as follows: print, parentheses, opening single/double quote, the message, the matching closing quote, closing parentheses. The message in the function must be enclosed in quotes because that differentiates code from text. Adding comments to code helps developers from a readability perspective, but the interpreter ignores them:
img
The octothorpe (#) denotes a comment, and all the text on the right-hand side is commented out—or not executed—and acts like plain text, doing nothing.
The code is syntax highlighted. Meaning it color codes keywords and certain distinct parts of the code, in IDLE and other text editors or integrated development environments (IDEs). Although it is possible to type Python code in any program that edits text, text editors and IDEs provide niche features. A text editor is simply a program that allows the user to type text with some syntax highlighting ability, while an IDE is similar but must also offer feature-rich programming tools for the developer.
Any text editor or IDE can be used for the purposes of this book, but, PyCharm is recommended.
Installing and Using PyCharm
PyCharm is an application that executes Python programs. Download the “Community” version (https://www.jetbrains.com/pycharm/download) and follow through the installation process. Once it is installed, go through these steps:
  1. Create a Project.
    1. Give it a name.
    2. Be sure to uncheck the “Create a main.py welcome script.”
  2. In the project:
    1. Check whether the interpreter is selected there:
      1. On Windows:
        Click “File” on the menu and go to “Settings.”
      2. On macOS:
        Click on “PyCharm” on the menu and go to “Preferences.”
      3. Search for the “Python Interpreter” and open it.
      4. Select the Python version installed:
      img
    2. Create a new Python file and give it a name.
      1. Open that file.
      2. In the file, call or use the print function to display “Hello World.”
      3. Right-click and run the application.
      4. “Hello World” should appear in the bottom window.
It is recommended to create a new Project for every program you write. Each project should have related files that all come together to create a program.
Using the Interpreter
When IDLE is opened without choosing a file, it will open an interpreter, which can also be accessed by typing in “python” to the Terminal (macOS/Linux) or Command Prompt (Windows) application, which should be already on your system.
The Python code, entered in the interpreter, will be evaluated or executed on the fly:
img
img
As seen in the last page, the code is executed on the fly and the output is given immediately.
CHAPTER 2:
Variables and Data
A variable is a name with an ...

Table of contents

  1. Title Page
  2. Copyright
  3. Author’s Note
  4. Table of Contents
  5. Preface
  6. Chapter 1: Introduction
  7. Chapter 2: Variables and Data
  8. Chapter 3: Control Flow
  9. Chapter 4: Building a Project: Calculator
  10. Chapter 5: Functions
  11. Chapter 6: Modules and Packages
  12. Chapter 7: Turtle
  13. Chapter 8: Building a Project: Drawing App
  14. Chapter 9: Working with Text Files
  15. Chapter 10: Building a Project: Log-in System
  16. Chapter 11: Object Oriented Programming
  17. Chapter 12: More Python
  18. Chapter 13: GUI: Tkinter
  19. Chapter 14: Building a Project Notes App
  20. Chapter 15: Future Roadmap
  21. Cheat Sheet
  22. Back Cover