Python for Professionals
eBook - ePub

Python for Professionals

Learning Python as a Second Language

Matt Telles

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

Python for Professionals

Learning Python as a Second Language

Matt Telles

Book details
Book preview
Table of contents
Citations

About This Book

Learn to be a Python expert in ten easy lessons! Key Features

  • Acquire knowledge of Python programming simply and easily.
  • Learn about object-oriented programming and how it applies to Python.
  • Make a splash with list comprehensions, generators, and decorators.
  • Learn about file processing with Python, and how it makes JSON easy to deal with.
  • Work with dictionaries and sets quickly and easily.
  • Learn about what others have made available in the Python world.
  • Pick up tricks and tips that will make you look like a Python expert in no time.

  • Description
    This book is intended for the professional programmer who wants to learn Python for their place of business, or simply to extend their knowledge. You will learn the basics of the language--from how to define variables and implement looping and conditional constructs, to working with existing code. Once we have established the baseline for writing code in Python, you'll learn how to create your own functions and classes, how to extend existing code, and how to work with Python-specific things like comprehensions and generators. With a solid foundation, you will then move on to learn about the existing Python libraries, called packages, and how to use them, as well as discovering little tips and tricks that will make you a hit with all the programmers at work, and really aid you in nailing that programming interview. What will you learn
    By the time you have finished this book, you will know enough to write complex Python programs and work with existing Python code. You will find out about the packages that make Python one of the most popular programming languages and will understand the "Pythonic" way of thinking and programming. Who this book is for
    This book is designed for programmers who have experience in at least one programming language. No prior Python experience is necessary, but it is assumed that you understand the basics of loops, conditionals and object-oriented constructs, such as classes. You should have or have access to a system that runs Python 3 (any version). Table of Contents
    1. The history and installation of Python
    2. Python types and constructs
    3. The Nuts and Bolts
    4. Structuring your Python projects
    5. Object-oriented programming with Python
    6. Advanced manipulations
    7. File input and output
    8. Imports and Exports
    9. Miscellaneous
    10. Not re-inventing the wheel
    11. Tips and Tricks About the Author
    Matt Telles is a 35-year veteran in the software industry. He has worked with virtually all programming languages and has been a developer, manager, tester, and designer. He's been working on Python for several years and is constantly extending his knowledge in the field.
    Matt is married with three children, lives in New York, in the United States. He has a menagerie of cats, dogs and a turtle, and loves reading books on his Microsoft Surface on the train to work every morning. Your LinkedIn Profile: www.linkedin.com/in/matt-telles-362ba5

Frequently asked questions

How do I cancel my subscription?
Simply head over to the account section in settings and click on “Cancel Subscription” - it’s as simple as that. After you cancel, your membership will stay active for the remainder of the time you’ve paid for. Learn more here.
Can/how do I download books?
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.
What is the difference between the pricing plans?
Both plans give you full access to the library and all of Perlego’s features. The only differences are the price and subscription period: With the annual plan you’ll save around 30% compared to 12 months on the monthly plan.
What is Perlego?
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.
Do you support text-to-speech?
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.
Is Python for Professionals an online PDF/ePUB?
Yes, you can access Python for Professionals by Matt Telles in PDF and/or ePUB format, as well as other popular books in Informatique & Programmation orientée objet. We have over one million books available in our catalogue for you to explore.

Information

Year
2019
ISBN
9789389423754

CHAPTER 1

The History and Installation of Python

Introduction

Every professional programmer who has been in the industry for any length of time has seen changes. Whether it is a new job, or a new boss, or simply a new approach at work, change is the only constant in the programming world. New operating systems, new frameworks, new devices, and, of course, new programming languages. For those of us that came up in the Unix world, or using MS-DOS, change has been dramatic. If you’ve worked mostly in the Linux world, the change may have been less dramatic. Changes in your development language, however, are always both exiting and traumatic. Moving to a new language is like moving to a new house with all new things to get used to and to accept. If you are coming from the Java, C# or C++ world, transitioning to Python might be confusing. Not only has the syntax changed, but the very way of thinking has changed. The purpose of this book is to ease that transition, and to help you think like a Pythonista. A Pythonista, of course, is one that has adopted Python as their primary language, and struggles to master new concepts in Python at all times.

Structure

  • History
  • Selecting a Python version
  • The Zen of Python
  • Keeping it simple
  • Using virtual environments
  • Using pip
  • Python IDE’s and command line work

Objectives

By the end of this chapter, you should understand how to select a Python version to use, how to install the Python system, and how to use some of the general tools that Python developers need in their day to day experience. You will learn about the history of Python, the Zen of Python, and how to keep it simple.

Python: the language

One of the most frustrating things for professional developers is finding a good transition methodology. Let’s be honest, you don’t want to learn about what a memory location is, or how the compiler/interpreter works to do your job. You want to start out with that nice simple Hello world example, see how the types and statement work, and what the gotcha’s are in the language. You already know what loops are, what assignment statements are, and how to use functions and classes from your previous work. What you don’t know is how that new language implements all these things, and that’s what you will learn here.
We will look at the basics of Python in this chapter, as well as introducing some basic concepts that you’ll need to think about when writing in Python. You’ll find out a little bit of trivia, because what developer doesn’t love to know trivia about his or her language? You’ll find out the philosophy behind Python and why things are the way they are. Then we’ll get into the nuts and bolts of getting you a functional development environment, and show you how to create your first Python program.
Welcome to Python! May your journey be fruitful and your code concise.

History

If you’ve never seen Python before, it might surprise you to know that it has been around for a very long time. Older than Java and C#, Python was first created in the late 1980s. It was created by Guido Van Rossum at CWI in the Netherlands. The first cut at the language didn’t get much traction in the real world of software development though. It wasn’t until the turn of the millennium, the year 2000, when Python 2.0 came out and people began to use it in earnest.
Where the original Python language was a very simple interpreted language best suited for UNIX scripting, the second version of the language was much more robust. Supporting memory management and garbage collection, not to mention full object-oriented programming (OOP) concepts like classes and inheritance, it made writing complex tasks very easy. Much like Visual Basic did for Windows, Python did for the early days of Linux, making it easy for non-developers to get quickly into programming. Unlike Visual Basic, however, Python was well thought out, enough so that advanced programming was done quickly. Because it was interpreted, the development cycle for Python was rapid, and thus was adopted in many companies for that reason alone.
As the language adoption increased, so did the packages, code libraries that accomplished complex tasks. We’ll look at those later in the book, but Python today is used for such diverse tasks as writing web servers and doing artificial intelligence. It has excellent mathematical libraries that make it ideal for doing statistical work as well. Because of its small footprint, Python is available on virtually every platform in existence. With the second release came support for Unicode, giving it international acceptance as well.
Python is a byte-code language, like Java or C#. That means that the interpreter will compile your scripts into a simplistic byte code that can be quickly and easily interpreted. This is good, in that it is fast and easy to use. At the same time, there is a cost, as there always is. Python interprets things as it reaches them, meaning that syntax errors aren’t caught at compile time, but at run-time.
The biggest reason that people gravitate to Python is its simplicity. Rather than some languages which have a dozen different constructs for looping, Python has but two; the for statement and the while statement. A language like Java requires a dozen lines to open a file and write to it, Python requires three. In C# or C++, you have to structure your code in a specific manner that the compiler accepts, in Python; you can implement a full program in a single line in a single file. Functional programming used to require hideously complicated languages like Scala, which in turn required the Java system; Python can do it in a few lines of code. That’s not to say that Python can replace all existing programming languages, it most certainly can’t. Being an interpreted language, it is naturally slower than a compiled language. It lacks the GUI libraries that many languages come with, and the security built into other byte-code languages like C# or Java.
Oh, and finally, the fun facts. Python isn’t named after a snake. Rather, it is named after the British comedy show Monty Python. It has become so popular that it is the de facto standard programming language at Google, a somewhat large multi-national software company. Python is open source, meaning that it isn’t controlled by a single company or a single person. Anyone can contribute to the Python system, there is a committee that approves language changes, but if you want your own personal version, you can do so. Finally, in a study done by Ocado Technology, Python is a more popular language than French in schools. Take that Napoleon!

Selecting a Python version

Jumping right in to the topic, as professionals do, let’s get started with Python. Of course, first we need to install the system on our devices. Python will run on almost anything, although it isn’t really recommended that you write code on your phone. You can, though, which speaks to the simplicity of the language and the compactness of the interpreter. Still, we’ll stick with writing code on a computer. For the purposes of this book, it doesn’t matter whether you use a PC, a Mac, or a Linux box, the language is exactly the same. The environment you choose to develop with may vary, but the reality is, you can use any editor you want, either an integrated (IDE) system or a simple text editor and command-line execution. Before we get to install the system, we need to make a big decision. Should we use Python 2 or Python 3?
For the purposes of this book, we will be using Python 3.x. almost all of the code and libraries will work just fine in the latest Python 2.7 release, but for consistency sake, we’ll choose Python 3.

Why not use 2.7?

Python 2 is coming to the end of its useful lifetime. That doesn’t mean that it will go away anytime soon; there are literally millions of lines of Python code out there that use the 2.x environments. However, the Python organization (see: https://www.python.org/) has decided that January of 2020 will be the last date for which new updates will be made to Python 2. It isn’t like the language will implode, or that all of your scripts and applications will suddenly stop working, but there are regular bug fixes and language feature updates that will no longer be applied to the Python 2.x branch. For this reason, we will be using Python 3.x in this book, and you should in your own code.
Migrating from Python 2.7 to 3.0 is not a major endeavor, and there are many guides to doing so. The biggest obvious change, for those of you that worry about this sort of thing, is that the print statement, which looked like this in Python 1.0 and 2.0:
print "something"
Will be converted into a full-fledged function:
print("something")
This change can be annoying if you get used to writing code without the parentheses. In addition, it works fine with parentheses in Python 2.x, so you might as well just get used to it. To be honest, there was never a good reason to omit the parentheses around statements; it was a holdover from older languages like BASIC.

Which 3.x to use?

Once we have made the decision to go with Python 3.x, the next question is which number to fill in after the x. The answer is that it really doesn’t matter. As of the writing of this book, Python 3.7 is the most current version, but 3.6 is the most commonly used version. There are a few packages out there that have not yet been updated to support some changes in 3.7, so a number of people are not yet using it. For our purposes, any version will do, as we are not going to get into the internals of the language and interpreter. In the code, it may assume you are using ...

Table of contents