• Complain

Real Python Part 1: Introduction to Python

Here you can read online Real Python Part 1: Introduction to Python full text of the book (entire story) in english for free. Download pdf and epub, get meaning, cover and reviews about this ebook. genre: Computer. Description of the work, (preface) as well as reviews are available. Best literature library LitArk.com created for fans of good reading and offers a wide selection of genres:

Romance novel Science fiction Adventure Detective Science History Home and family Prose Art Politics Computer Non-fiction Religion Business Children Humor

Choose a favorite category and find really read worthwhile books. Enjoy immersion in the world of imagination, feel the emotions of the characters or learn something new for yourself, make an fascinating discovery.

Unknown Real Python Part 1: Introduction to Python
  • Book:
    Real Python Part 1: Introduction to Python
  • Author:
  • Genre:
  • Rating:
    3 / 5
  • Favourites:
    Add to favourites
  • Your mark:
    • 60
    • 1
    • 2
    • 3
    • 4
    • 5

Real Python Part 1: Introduction to Python: summary, description and annotation

We offer to read an annotation, description, summary or preface (depends on what the author of the book "Real Python Part 1: Introduction to Python" wrote himself). If you haven't found the necessary information about the book — write in the comments, we will try to find it.

The first half of the Introduction to Python is a quick yet thorough overview of all the Python basics. You do not need any prior experience with programming to get started. The second half, meanwhile, is focused on solving interesting, real-world problems in a practical manner.

Unknown: author's other books


Who wrote Real Python Part 1: Introduction to Python? Find out the surname, the name of the author of the book and a list of all author's works by series.

Real Python Part 1: Introduction to Python — read online for free the complete book (whole text) full work

Below is the text of the book, divided by pages. System saving the place of the last page read, allows you to conveniently read the book "Real Python Part 1: Introduction to Python" online for free, without having to search again every time where you left off. Put a bookmark, and you can go to the page where you finished reading at any time.

Light

Font size:

Reset

Interval:

Bookmark:

Make
Getting Started
Getting Started

Let's start with the basics...

Interlude: Leave yourself helpful notes
Interlude: Leave yourself helpful notes

As you start to write more complicated scripts, you'll start to find yourself going back to parts of your code after you've written them and thinking, "What the heck was that supposed to do"?

To avoid these moments, you can leave yourself notes in your code; they don't affect the way the script runs at all, but they help to document what's supposed to be happening. These notes are referred to as comments, and in Python you start a comment with a pound (#) sign. Our first script could have looked like this:

# This is my first script phrase = "Hello, world." print(phrase) # this line displays "Hello, world"

The first line doesn't do anything, because it starts with a #. This tells Python to ignore the line completely because it's just a note for you.

Likewise, Python ignores the comment on the last line; it will still print phrase, but everything starting with the # is simply a comment.

Of course, you can still use a # symbol inside of a string. For instance, Python won't mistake the following for the start of a comment:

print( "#1" )

If you have a lot to say, you can also create comments that span over multiple lines by using a series of three single quotes (''') or three double quotes (""") without any spaces between them. Once you do that, everything after the ''' or """ becomes a comment until you close the comment with a matching ''' or """. For instance, if you were feeling excessively verbose, our first script could have looked like this:

"""This is my first script.It prints the phrase "Hello, world."The comments are longer than the script""" phrase = "Hello, world." print(phrase) """The line above displays "Hello, world""""

The first three lines are now all one comment, since they fall between pairs of """. You can't add a multi-line comment at the end of a line of code like with the # version, which is why the last comment is on its own separate line. (We'll see why in the next chapter.)

Besides leaving yourself notes, another common use of comments is to "comment out code" while you're testing parts of a scripts to temporarily stop that part of the code from running. In other words, adding a # at the beginning of a line of code is an easy way to make sure that you don't actually use that line, even though you might want to keep it and use it later.

Interact with PDF files
Interact with PDF Files

PDF files have become a sort of necessary evil these days. Despite their frequent use, PDFs are some of the most difficult files to work with in terms of making modifications, combining files, and especially for extracting text information.

Fortunately, there are a few options in Python for working specifically with PDF files. None of these options are perfect solutions, but often you can use Python to completely automate or at least ease some of the pain of performing certain tasks using PDFs.

The most frequently used package for working with PDF files in Python is named PyPDF2 and can be found here. You will need to download and install this package before continuing with the chapter. In you terminal or command line type:

$ pip3 install PyPDF2

If that doesn't work, you will need to download and unzip the .tar.gz file and install the module using the setup.py script as explained in the previous chapter on installing packages from source.

Debian/Linux: Just type the command: sudo apt-get install python-PyPDF2

The PyPDF2 package includes a PdfFileReader and a PdfFileWriter; just like when performing other types of file input/output, reading and writing are two entirely separate processes.

First, let's get started by reading in some basic information from a sample PDF file, the first couple chapters of Jane Austen's Pride and Prejudice via Project Gutenberg:

import os from PyPDF2 import PdfFileReaderpath = "C:/book1-exercises/chp11/practice_files" input_file_name = os.path.join(path, "Pride and Prejudice.pdf" )input_file = PdfFileReader(open(input_file_name, "rb" ))print( "Number of pages:" , input_file.getNumPages())print( "Title:" , input_file.getDocumentInfo().title)

NOTE: Be sure to update the path variable with the correct path for you system.

NOTE: Because we are reading PDF files, we must use the argument 'rb' with the open() method.

We created a PdfFileReader object named input_file by passing a file() object with rb (read binary) mode and giving the full path of the file. The additional "binary" part is necessary for reading PDF files because we aren't just reading basic text data. PDFs include much more complicated information, and saying "rb" here instead of just "r" tells Python that we might encounter characters that can't be represented as standard readable text.

We can then return the number of pages included in the PDF input file. We also have access to certain attributes through the getDocumentInfo() method; in fact, if we display the result of simply calling this method, we will see a dictionary with all of the available document info:

>>> print (input_file.getDocumentInfo()){ '/CreationDate' : u 'D:20110812174208' , '/Author' : u 'Chuck' , '/Producer' :u 'Microsoft Office Word 2007' , '/Creator' : u 'Microsoft Office Word 2007' , '/ModDate' : u 'D:20110812174208' , '/Title' : u 'Pride and Prejudice, by JaneAusten' }>>>

We can also retrieve individual pages from the PDF document using the getPage() method and specifying the index number of the page (as always, starting at 0). However, since PDF pages include much more than simple text, displaying the text data on a PDF page is more involved. Fortunately, PyPDF2 has made the process of parsing out text somewhat easier, and we can use the extractText() method on each page:

>>> print (input_file.getPage().extractText()) The Project Gutenberg EBook of Pride and Prejudice, by Jane Austen This eBook is for the use of anyone anywhere at no cost and with almost norestrictions whatsoever. You may copy it, give it away or re-use it under the terms of the Project Gutenberg License included with this eBook or online at www.gutenberg.orgTitle: Pride and Prejudice Author: Jane Austen ReleaseDate: August , 2008 [EBook #1342] [Last updated: August 11, 2011] Language: English Character set encoding: ASCII *** START OF THIS PROJECT GUTENBERGEBOOK PRIDE AND PREJUDICE *** Produced by Anonymous Volunteers, and DavidWidger PRIDE AND PREJUDICE By Jane Austen Contents>>>

Formatting standards in PDFs are inconsistent at best, and it's usually necessary to take a look at the PDF files you want to use on a case-by-case basis. In this instance, notice how we don't actually see newline characters in the output; instead, it appears that new lines are being represented as multiple spaces in the text extracted by PyPDF2. We can use this knowledge to write out a roughly formatted version of the book to a plain text file (for instance, if we only had the PDF available and wanted to make it readable on an untalented (err dumb) mobile device):

import os from PyPDF2 import PdfFileReaderpath = "C:/book1-exercises/chp11/practice_files" input_file_name = os.path.join(path, "Pride and Prejudice.pdf" )input_file = PdfFileReader(file(input_file_name, "rb" ))output_file_name = os.path.join(path, "Output/Pride and Prejudice.txt" )output_file = open(output_file_name, "w" )title = input_file.getDocumentInfo().title # get the file title total_pages = input_file.getNumPages() # get the total page count output_file.write(title + "\n" )output_file.write( "Number of pages: {}\n\n" .format(total_pages)) for page_num in range(, total_pages): text = input_file.getPage(page_num).extractText() text = text.replace( " " , "\n" ) output_file.write(text)output_file.close()
Next page
Light

Font size:

Reset

Interval:

Bookmark:

Make

Similar books «Real Python Part 1: Introduction to Python»

Look at similar books to Real Python Part 1: Introduction to Python. We have selected literature similar in name and meaning in the hope of providing readers with more options to find new, interesting, not yet read works.


Reviews about «Real Python Part 1: Introduction to Python»

Discussion, reviews of the book Real Python Part 1: Introduction to Python and just readers' own opinions. Leave your comments, write what you think about the work, its meaning or the main characters. Specify what exactly you liked and what you didn't like, and why you think so.