Learning Python: Learn to code like a professional with Python - an open source, versatile, and powerful programming language
5/5
()
Python Programming
Python
Programming
Web Development
Functions
Quest for Knowledge
Tech-Savvy Protagonist
Value of Collaboration
Thrill of Problem-Solving
Power of Open-Source
Never-Ending Learning Process
Software Development
Python Programming Language
Data Science
Data Types
Profiling
About this ebook
About This Book
- Learn the fundamentals of programming with Python – one of the best languages ever created
- Develop a strong set of programming skills that you will be able to express in any situation, on every platform, thanks to Python’s portability
- Create outstanding applications of all kind, from websites to scripting, and from GUIs to data science
Who This Book Is For
Python is the most popular introductory teaching language in U.S. top computer science universities, so if you are new to software development, or maybe you have little experience, and would like to start off on the right foot, then this language and this book are what you need. Its amazing design and portability will help you become productive regardless of the environment you choose to work with.
What You Will Learn
- Get Python up and running on Windows, Mac, and Linux in no time
- Grasp the fundamental concepts of coding, along with the basics of data structures and control flow.
- Write elegant, reusable, and efficient code in any situation
- Understand when to use the functional or the object oriented programming approach
- Create bulletproof, reliable software by writing tests to support your code
- Explore examples of GUIs, scripting, data science and web applications
- Learn to be independent, capable of fetching any resource you need, as well as dig deeper
In Detail
Learning Python has a dynamic and varied nature. It reads easily and lays a good foundation for those who are interested in digging deeper. It has a practical and example-oriented approach through which both the introductory and the advanced topics are explained. Starting with the fundamentals of programming and Python, it ends by exploring very different topics, like GUIs, web apps and data science. The book takes you all the way to creating a fully fledged application.
The book begins by exploring the essentials of programming, data structures and teaches you how to manipulate them. It then moves on to controlling the flow of a program and writing reusable and error proof code. You will then explore different programming paradigms that will allow you to find the best approach to any situation, and also learn how to perform performance optimization as well as effective debugging. Throughout, the book steers you through the various types of applications, and it concludes with a complete mini website built upon all the concepts that you learned.
Style and approach
This book is an easy-to-follow guide that will take you from a novice to the proficient level at a comfortable pace, using a lot of simple but effective examples. Each topic is explained thoroughly, and pointers are left for the more inquisitive readers to dig deeper and expand their knowledge.
Fabrizio Romano
Fabrizio Romano was born in Italy in 1975. He holds a master's degree in computer science engineering from the University of Padova. He's been working as a professional software developer since 1999. Fabrizio has been working at Sohonet since 2016, where he currently serves as a development manager. In 2020, the Television Academy honored him and his team with an Emmy Award in Engineering Development for advancing remote collaboration.
Read more from Fabrizio Romano
Learn Python Programming: A comprehensive, up-to-date, and definitive guide to learning Python Rating: 0 out of 5 stars0 ratingsPython编程入门与实战: Chinese Edition Rating: 0 out of 5 stars0 ratings
Related to Learning Python
Related ebooks
Python Essentials Rating: 5 out of 5 stars5/5The Ultimate Python Programming Guide For Beginner To Intermediate 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/5Python: Programming for Intermediates: Learn the Fundamentals of Python in 7 Days Rating: 4 out of 5 stars4/5Python 3 Programming: A Beginner Crash Course Guide to Learn Python 3 in 1 Week Rating: 3 out of 5 stars3/5PYTHON: Practical Python Programming For Beginners & Experts With Hands-on Project Rating: 5 out of 5 stars5/5Mastering Objectoriented Python Rating: 5 out of 5 stars5/5Python: Advanced Guide to Programming Code with Python: Python Computer Programming, #4 Rating: 0 out of 5 stars0 ratingsPython Programming: Your Beginner Guide To Learn Python in 7 Days Rating: 4 out of 5 stars4/5Python: For Beginners A Crash Course Guide To Learn Python in 1 Week Rating: 4 out of 5 stars4/5Python: Learn Python in 24 Hours Rating: 4 out of 5 stars4/5Learn Python in 10 Minutes Rating: 4 out of 5 stars4/5Functional Python Programming Rating: 0 out of 5 stars0 ratingsEssential Python 3 Rating: 0 out of 5 stars0 ratingsPython Made Simple: Learn Python programming in easy steps with examples Rating: 5 out of 5 stars5/5Python Programming: Your Step By Step Guide To Easily Learn Python in 7 Days Rating: 3 out of 5 stars3/5Python for Beginners: An Introduction to Learn Python Programming with Tutorials and Hands-On Examples Rating: 4 out of 5 stars4/5Python from the Very Beginning Rating: 0 out of 5 stars0 ratingsPython: Beginner's Guide to Programming Code with Python: Python Computer Programming, #1 Rating: 0 out of 5 stars0 ratingsPython for Beginners: A Crash Course to Learn Python Programming in 1 Week Rating: 0 out of 5 stars0 ratingsPython Data Analysis - Second Edition Rating: 0 out of 5 stars0 ratingsTiny Python Projects: Learn coding and testing with puzzles and games Rating: 4 out of 5 stars4/5Python In - Depth: Use Python Programming Features, Techniques, and Modules to Solve Everyday Problems Rating: 0 out of 5 stars0 ratings
Software Development & Engineering For You
Python For Dummies Rating: 4 out of 5 stars4/5The Hard Thing About Hard Things: Building a Business When There Are No Easy Answers Rating: 4 out of 5 stars4/5SQL For Dummies Rating: 0 out of 5 stars0 ratingsHand Lettering on the iPad with Procreate: Ideas and Lessons for Modern and Vintage Lettering Rating: 4 out of 5 stars4/5Level Up! The Guide to Great Video Game Design Rating: 4 out of 5 stars4/5How to Write Effective Emails at Work Rating: 4 out of 5 stars4/5Agile Practice Guide Rating: 4 out of 5 stars4/5Coding with AI For Dummies Rating: 1 out of 5 stars1/5Kodi Made Easy: Complete Beginners Step by Step Guide on How to Install Kodi on Amazon Firestick Rating: 0 out of 5 stars0 ratingsLearn to Code. Get a Job. The Ultimate Guide to Learning and Getting Hired as a Developer. Rating: 5 out of 5 stars5/5Coding All-in-One For Dummies Rating: 0 out of 5 stars0 ratingsPhotoshop For Beginners: Learn Adobe Photoshop cs5 Basics With Tutorials Rating: 0 out of 5 stars0 ratingsTeach Yourself VISUALLY iPhone 16 Rating: 0 out of 5 stars0 ratingsBeginning Programming For Dummies Rating: 4 out of 5 stars4/5Data Visualization: a successful design process Rating: 4 out of 5 stars4/5OneNote: The Ultimate Guide on How to Use Microsoft OneNote for Getting Things Done Rating: 1 out of 5 stars1/5The Inmates Are Running the Asylum (Review and Analysis of Cooper's Book) Rating: 4 out of 5 stars4/5Gray Hat Hacking the Ethical Hacker's Rating: 5 out of 5 stars5/5Android App Development For Dummies Rating: 0 out of 5 stars0 ratingsGitLab Cookbook Rating: 0 out of 5 stars0 ratingsFundamentals of Software Engineering: Designed to provide an insight into the software engineering concepts Rating: 0 out of 5 stars0 ratingsAdobe Illustrator CC For Dummies Rating: 5 out of 5 stars5/5Beginning C++ Game Programming - Second Edition: Learn to program with C++ by building fun games, 2nd Edition Rating: 0 out of 5 stars0 ratings3D Printing For Dummies Rating: 4 out of 5 stars4/5How Do I Do That In InDesign? Rating: 5 out of 5 stars5/5Google SketchUp Pro 8 step by step Rating: 0 out of 5 stars0 ratingsGit Essentials Rating: 4 out of 5 stars4/5
Reviews for Learning Python
2 ratings1 review
- Rating: 5 out of 5 stars5/5
Dec 19, 2020
I highly recommend it. It contains a lot of information.
Book preview
Learning Python - Fabrizio Romano
Table of Contents
Learning Python
Credits
About the Author
Acknowledgements
About the Reviewers
www.PacktPub.com
Support files, eBooks, discount offers, and more
Why subscribe?
Free access for Packt account holders
Preface
What this book covers
What you need for this book
Who this book is for
Conventions
Reader feedback
Customer support
Downloading the example code
Errata
Piracy
Questions
1. Introduction and First Steps – Take a Deep Breath
A proper introduction
Enter the Python
About Python
Portability
Coherence
Developer productivity
An extensive library
Software quality
Software integration
Satisfaction and enjoyment
What are the drawbacks?
Who is using Python today?
Setting up the environment
Python 2 versus Python 3 – the great debate
Installing Python
Setting up the Python interpreter
About virtualenv
Your first virtual environment
Your friend, the console
How you can run a Python program
Running Python scripts
Running the Python interactive shell
Running Python as a service
Running Python as a GUI application
How is Python code organized
How do we use modules and packages
Python's execution model
Names and namespaces
Scopes
Object and classes
Guidelines on how to write good code
The Python culture
A note on the IDEs
Summary
2. Built-in Data Types
Everything is an object
Mutable or immutable? That is the question
Numbers
Integers
Booleans
Reals
Complex numbers
Fractions and decimals
Immutable sequences
Strings and bytes
Encoding and decoding strings
Indexing and slicing strings
Tuples
Mutable sequences
Lists
Byte arrays
Set types
Mapping types – dictionaries
The collections module
Named tuples
Defaultdict
ChainMap
Final considerations
Small values caching
How to choose data structures
About indexing and slicing
About the names
Summary
3. Iterating and Making Decisions
Conditional programming
A specialized else: elif
The ternary operator
Looping
The for loop
Iterating over a range
Iterating over a sequence
Iterators and iterables
Iterating over multiple sequences
The while loop
The break and continue statements
A special else clause
Putting this all together
Example 1 – a prime generator
Example 2 – applying discounts
A quick peek at the itertools module
Infinite iterators
Iterators terminating on the shortest input sequence
Combinatoric generators
Summary
4. Functions, the Building Blocks of Code
Why use functions?
Reduce code duplication
Splitting a complex task
Hide implementation details
Improve readability
Improve traceability
Scopes and name resolution
The global and nonlocal statements
Input parameters
Argument passing
Assignment to argument names don't affect the caller
Changing a mutable affects the caller
How to specify input parameters
Positional arguments
Keyword arguments and default values
Variable positional arguments
Variable keyword arguments
Keyword-only arguments
Combining input parameters
Avoid the trap! Mutable defaults
Return values
Returning multiple values
A few useful tips
Recursive functions
Anonymous functions
Function attributes
Built-in functions
One final example
Documenting your code
Importing objects
Relative imports
Summary
5. Saving Time and Memory
map, zip, and filter
map
zip
filter
Comprehensions
Nested comprehensions
Filtering a comprehension
dict comprehensions
set comprehensions
Generators
Generator functions
Going beyond next
The yield from expression
Generator expressions
Some performance considerations
Don't overdo comprehensions and generators
Name localization
Generation behavior in built-ins
One last example
Summary
6. Advanced Concepts – OOP, Decorators, and Iterators
Decorators
A decorator factory
Object-oriented programming
The simplest Python class
Class and object namespaces
Attribute shadowing
I, me, and myself – using the self variable
Initializing an instance
OOP is about code reuse
Inheritance and composition
Accessing a base class
Multiple inheritance
Method resolution order
Static and class methods
Static methods
Class methods
Private methods and name mangling
The property decorator
Operator overloading
Polymorphism – a brief overview
Writing a custom iterator
Summary
7. Testing, Profiling, and Dealing with Exceptions
Testing your application
The anatomy of a test
Testing guidelines
Unit testing
Writing a unit test
Mock objects and patching
Assertions
A classic unit test example
Making a test fail
Interface testing
Comparing tests with and without mocks
Boundaries and granularity
A more interesting example
Test-driven development
Exceptions
Profiling Python
When to profile?
Summary
8. The Edges – GUIs and Scripts
First approach – scripting
The imports
Parsing arguments
The business logic
Second approach – a GUI application
The imports
The layout logic
The business logic
Fetching the web page
Saving the images
Alerting the user
How to improve the application?
Where do we go from here?
The tkinter.tix module
The turtle module
wxPython, PyQt, and PyGTK
The principle of least astonishment
Threading considerations
Summary
9. Data Science
IPython and Jupyter notebook
Dealing with data
Setting up the notebook
Preparing the data
Cleaning the data
Creating the DataFrame
Unpacking the campaign name
Unpacking the user data
Cleaning everything up
Saving the DataFrame to a file
Visualizing the results
Where do we go from here?
Summary
10. Web Development Done Right
What is the Web?
How does the Web work?
The Django web framework
Django design philosophy
The model layer
The view layer
The template layer
The Django URL dispatcher
Regular expressions
A regex website
Setting up Django
Starting the project
Creating users
Adding the Entry model
Customizing the admin panel
Creating the form
Writing the views
The home view
The entry list view
The form view
Tying up URLs and views
Writing the templates
The future of web development
Writing a Flask view
Building a JSON quote server in Falcon
Summary
11. Debugging and Troubleshooting
Debugging techniques
Debugging with print
Debugging with a custom function
Inspecting the traceback
Using the Python debugger
Inspecting log files
Other techniques
Profiling
Assertions
Where to find information
Troubleshooting guidelines
Using console editors
Where to inspect
Using tests to debug
Monitoring
Summary
12. Summing Up – A Complete Example
The challenge
Our implementation
Implementing the Django interface
The setup
The model layer
A simple form
The view layer
Imports and home view
Listing all records
Creating records
Updating records
Deleting records
Setting up the URLs
The template layer
Home and footer templates
Listing all records
Creating and editing records
Talking to the API
Deleting records
Implementing the Falcon API
The main application
Writing the helpers
Coding the password validator
Coding the password generator
Writing the handlers
Coding the password validator handler
Coding the password generator handler
Running the API
Testing the API
Testing the helpers
Testing the handlers
Where do you go from here?
Summary
A word of farewell
Index
Learning Python
Learning Python
Copyright © 2015 Packt Publishing
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 Packt Publishing, and its dealers and distributors will be held liable for any damages caused or alleged to be caused directly or indirectly by this book.
Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information.
First published: December 2015
Production reference: 1171215
Published by Packt Publishing Ltd.
Livery Place
35 Livery Street
Birmingham B3 2PB, UK.
ISBN 978-1-78355-171-2
www.packtpub.com
Credits
Author
Fabrizio Romano
Reviewers
Simone Burol
Julio Vicente Trigo Guijarro
Veit Heller
Commissioning Editor
Akram Hussain
Acquisition Editor
Indrajit Das
Content Development Editors
Samantha Gonsalves
Adrian Raposo
Technical Editor
Siddhi Rane
Copy Editors
Janbal Dharmaraj
Kevin McGowan
Project Coordinator
Kinjal Bari
Proofreader
Safis Editing
Indexer
Priya Sane
Graphics
Kirk D'Penha
Abhinash Sahu
Production Coordinator
Melwyn D'sa
Cover Work
Melwyn D'sa
About the Author
Fabrizio Romano was born in Italy in 1975. He holds a master's degree in computer science engineering from the University of Padova. He is also a certified Scrum master.
Before Python, he has worked with several other languages, such as C/C++, Java, PHP, and C#.
In 2011, he moved to London and started working as a Python developer for Glasses Direct, one of Europe's leading online prescription glasses retailers.
He then worked as a senior Python developer for TBG (now Sprinklr), one of the world's leading companies in social media advertising. At TBG, he and his team collaborated with Facebook and Twitter. They were the first in the world to get access to the Twitter advertising API. He wrote the code that published the first geo-narrowcasted promoted tweet in the world using the API.
He currently works as a senior platform developer at Student.com, a company that is revolutionizing the way international students find their perfect home all around the world
He has delivered talks on Teaching Python and TDD with Python at the last two editions of EuroPython and at Skillsmatter in London.
Acknowledgements
I would like to thank Adrian Raposo and Indrajit Das from Packt Publishing for their help and support and giving me the opportunity to live this adventure. I would also like to thank everyone at Packt Publishing who have contributed to the realization of this book. Special thanks go to Siddhi Rane, my technical editor. Thank you for your kindness, for working very hard, and for going the extra mile just to make me happy.
I would like to express my deepest gratitude to Simone Burol and Julio Trigo, who have gifted me with some of their precious free time. They have reviewed the book and provided me with invaluable feedback.
A big thank you to my teammates, Matt Bennett and Jakub Kuba Borys, for their interest in this book and for their support and feedback that makes me a better coder every day.
A heartfelt thank you to Marco Tex
Beri, who introduced me to Python with an enthusiasm second to none.
A special thanks to Dr. Naomi Ceder, from whom I learned so much over the last year. She has given me precious suggestions and has encouraged me to embrace this opportunity.
Finally, I would like to thank all my friends who have supported me in any way.
About the Reviewers
Simone Burol is an Italian software developer who was born in Treviso (Italy) in 1978. He obtained a master's degree in computer science engineering from the University of Padua (Italy), and since then worked in banking for 5 years in Venice (Italy). In 2010, he moved to London (United Kingdom), where he worked in warehouse automation for Ocado Technology and then in banking for Algomi.
Julio Vicente Trigo Guijarro is a computer scientist and software engineer with almost a decade of experience in software development. He is also a certified Scrum master, who enjoys the benefits of using agile software development (Scrum and XP).
He completed his studies in computer science and software engineering from the University of Alicante, Spain, in 2007. Since then, he has worked with several technologies and languages, including Microsoft Dynamics NAV, Java, JavaScript, and Python.
Some of the applications covered by Julio during his career include RESTful APIs, ERPs, billing platforms, payment gateways, and e-commerce websites.
He has been using Python on both personal and professional projects since 2012, and he is passionate about software design, software quality, and coding standards.
I would like to thank my parents for their love, good advice, and continuous support.
I would also like to thank all my friends that I met along the way, who enriched my life, for motivating me and helping me progress.
Veit Heller is a full stack developer, mostly working on the backend side of web projects. He currently resides in Berlin and works for a prototypical Pythonista company named Bright. In his free time, he writes interpreters for various programming languages.
I would like to thank the people at Bright for being a welcoming company that supports me in all my endeavors, my friends and my family for coping with my strangeness, and manufacturers of caffeinated drinks worldwide.
www.PacktPub.com
Support files, eBooks, discount offers, and more
For support files and downloads related to your book, please visit www.PacktPub.com.
Did you know that Packt offers eBook versions of every book published, with PDF and ePub files available? You can upgrade to the eBook version at www.PacktPub.com and as a print book customer, you are entitled to a discount on the eBook copy. Get in touch with us at
At www.PacktPub.com, you can also read a collection of free technical articles, sign up for a range of free newsletters and receive exclusive discounts and offers on Packt books and eBooks.
Support files, eBooks, discount offers, and morehttps://www2.packtpub.com/books/subscription/packtlib
Do you need instant solutions to your IT questions? PacktLib is Packt's online digital book library. Here, you can search, access, and read Packt's entire library of books.
Why subscribe?
Fully searchable across every book published by Packt
Copy and paste, print, and bookmark content
On demand and accessible via a web browser
Free access for Packt account holders
If you have an account with Packt at www.PacktPub.com, you can use this to access PacktLib today and view 9 entirely free books. Simply use your login credentials for immediate access.
To Alan Turing, the father of Computer Science.
To Guido Van Rossum, the father of Python.
To Adriano Romano, my father, my biggest fan.
Preface
Shortly after I started writing, a friend asked me if there really was a need of another Learning Python book.
An excellent question that we could also express in another form: What has this book to offer? What makes this book different from the average introductory book on Python?
I think there are two main differences and many good reasons why you would want to read it.
Firstly, we start with introducing some important programming concepts. We build a solid foundation by covering the critical aspects of this wonderful language.
The pace gradually increases, along with the difficulty of the subjects presented. By the end of Chapter 7, Testing, Profiling, and Dealing with Exceptions, we will cover all the fundamentals.
From Chapter 8, The Edges – GUIs and Scripts, onward, the book takes a steep turn, which brings us to difference number two.
To consolidate the knowledge acquired, there is nothing like working on a small project. So, in the second part of the book, each chapter delivers a project on a different subject. We explore scripting, graphical interfaces, data science, and web programming.
Each project is small enough to fit within a chapter and yet big enough to be relevant. Each chapter is interesting, conveys a message, and teaches something valuable.
After a short section on debugging, the book ends with a complete example that wraps things up. I tried to craft it so that you will be able to expand it in several ways.
So, this is definitely not the usual Learning Python book. Its approach is much more hands-on
and practical.
I wanted to empower you to help you become a true Python ninja. But I also did my best to entertain you and foster your logical thinking and creativity along the way.
Now, have I answered the question?
What this book covers
Chapter 1, Introduction and First Steps – Take a Deep Breath, introduces you to fundamental programming concepts. It guides you to getting Python up and running on your computer and introduces you to some of its constructs.
Chapter 2, Built-in Data Types, introduces you to Python built-in data types. Python has a very rich set of native data types and this chapter will give you a description and a short example for each of them.
Chapter 3, Iterating and Making Decisions, teaches you how to control the flow of your code by inspecting conditions, applying logic, and performing loops.
Chapter 4, Functions, the Building Blocks of Code, teaches you how to write functions. Functions are the keys to reusing code, to reducing debugging time, and in general, to writing better code.
Chapter 5, Saving Time and Memory, introduces you to the functional aspects of Python programming. This chapter teaches you how to write comprehensions and generators, which are powerful tools that you can use to speed up your code and save memory.
Chapter 6, Advanced Concepts – OOP, Decorators, and Iterators, teaches you the basics of object-oriented programming with Python. It shows you the key concepts and all the potentials of this paradigm. It also shows you one of the most beloved characteristics of Python: decorators. Finally, it also covers the concept of iterators.
Chapter 7, Testing, Profiling, and Dealing with Exceptions, teaches you how to make your code more robust, fast, and stable using techniques such as testing and profiling. It also formally defines the concept of exceptions.
Chapter 8, The Edges – GUIs and Scripts, guides you through an example from two different points of view. They are at the extremities of a spectrum: one implementation is a script and the other one a proper graphical user interface application.
Chapter 9, Data Science, introduces a few key concepts and a very special tool, the Jupyter Notebook.
Chapter 10, Web Development Done Right, introduces the fundamentals of web development and delivers a project using the Django web framework. The example will be based on regular expressions.
Chapter 11, Debugging and Troubleshooting, shows you the main methods to debug your code and some examples on how to apply them.
Chapter 12, Summing Up – A Complete Example, presents a Django website that acts as an interface to an underlying slim API written with the Falcon web framework. This chapter takes all the concepts covered in the book to the next level and suggests where to go to dig deeper and take the next steps.
What you need for this book
You are encouraged to follow the examples in this book. In order to do so, you will need a computer, an Internet connection, and a browser. The book is written in Python 3.4, but it should also work with any Python 3.* version. I have written instructions on how to install Python on the three main operating systems used today: Windows, Mac, and Linux. I have also explained how to install all the extra libraries used in the various examples and provided suggestions if the reader finds any issues during the installation of any of them. No particular editor is required to type the code; however, I suggest that those who are interested in following the examples should consider adopting a proper coding environment. I have given suggestions on this matter in the first chapter.
Who this book is for
Python is the most popular introductory teaching language in the top computer science universities in the US, so if you are new to software development or if you have little experience and would like to start off on the right foot, then this language and this book are what you need. Its amazing design and portability will help you become productive regardless of the environment you choose to work with.
If you have already worked with Python or any other language, this book can still be useful to you both as a reference to Python's fundamentals and to provide a wide range of considerations and suggestions collected over two decades of experience.
Conventions
In this book, you will find a number of text styles that distinguish between different kinds of information. Here are some examples of these styles and an explanation of their meaning.
Code words in text, database table names, folder names, filenames, file extensions, pathnames, dummy URLs, user input, and Twitter handles are shown as follows: Open up a Python console, and type import this.
A block of code is set as follows:
# we define a function, called local
def local():
m = 7
print(m)
m = 5
print(m)
When we wish to draw your attention to a particular part of a code block, the relevant lines or items are set in bold:
# we define a function, called local
def local():
m = 7
print(m)
m = 5
print(m)
Any command-line input or output is written as follows:
>>> from math import factorial >>> factorial(5) 120
New terms and important words are shown in bold. Words that you see on the screen, for example, in menus or dialog boxes, appear in the text like this: To open the console on Windows, go to the Start menu, choose Run, and type cmd.
Note
Warnings or important notes appear in a box like this.
Tip
Tips and tricks appear like this.
Reader feedback
Feedback from our readers is always welcome. Let us know what you think about this book—what you liked or disliked. Reader feedback is important for us as it helps us develop titles that you will really get the most out of.
To send us general feedback, simply e-mail <[email protected]>, and mention the book's title in the subject of your message.
If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, see our author guide at www.packtpub.com/authors.
Customer support
Now that you are the proud owner of a Packt book, we have a number of things to help you to get the most from your purchase.
Downloading the example code
You can download the example code files from your account at http://www.packtpub.com for all the Packt Publishing books you have purchased. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you.
Errata
Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you find a mistake in one of our books—maybe a mistake in the text or the code—we would be grateful if you could report this to us. By doing so, you can save other readers from frustration and help us improve subsequent versions of this book. If you find any errata, please report them by visiting http://www.packtpub.com/submit-errata, selecting your book, clicking on the Errata Submission Form link, and entering the details of your errata. Once your errata are verified, your submission will be accepted and the errata will be uploaded to our website or added to any list of existing errata under the Errata section of that title.
To view the previously submitted errata, go to https://www.packtpub.com/books/content/support and enter the name of the book in the search field. The required information will appear under the Errata section.
Piracy
Piracy of copyrighted material on the Internet is an ongoing problem across all media. At Packt, we take the protection of our copyright and licenses very seriously. If you come across any illegal copies of our works in any form on the Internet, please provide us with the location address or website name immediately so that we can pursue a remedy.
Please contact us at <[email protected]> with a link to the suspected pirated material.
We appreciate your help in protecting our authors and our ability to bring you valuable content.
Questions
If you have a problem with any aspect of this book, you can contact us at <[email protected]>, and we will do our best to address the problem.
Chapter 1. Introduction and First Steps – Take a Deep Breath
According to Wikipedia, computer programming is:
...a process that leads from an original formulation of a computing problem to executable computer programs. Programming involves activities such as analysis, developing understanding, generating algorithms, verification of requirements of algorithms including their correctness and resources consumption, and implementation (commonly referred to as coding) of algorithms in a target programming language
.
In a nutshell, coding is telling a computer to do something using a language it understands.
Computers are very powerful tools, but unfortunately, they can't think for themselves. So they need to be told everything. They need to be told how to perform a task, how to evaluate a condition to decide which path to follow, how to handle data that comes from a device such as the network or a disk, and how to react when something unforeseen happens, say, something is broken or missing.
You can code in many different styles and languages. Is it hard? I would say yes
and no
. It's a bit like writing. Everybody can learn how to write, and you can too. But what if you wanted to become a poet? Then writing alone is not enough. You have to acquire a whole other set of skills and this will take a longer and greater effort.
In the end, it all comes down to how far you want to go down the road. Coding is not just putting together some instructions that work. It is so much more!
Good code is short, fast, elegant, easy to read and understand, simple, easy to modify and extend, easy to scale and refactor, and easy to test. It takes time to be able to write code that has all these qualities at the same time, but the good news is that you're taking the first step towards it at this very moment by reading this book. And I have no doubt you can do it. Anyone can, in fact, we all program all the time, only we aren't aware of it.
Would you like an example?
Say you want to make instant coffee. You have to get a mug, the instant coffee jar, a teaspoon, water, and the kettle. Even if you're not aware of it, you're evaluating a lot of data. You're making sure that there is water in the kettle as well as the kettle is plugged-in, that the mug is clean, and that there is enough coffee in the jar. Then, you boil the water and maybe in the meantime you put some coffee in the mug. When the water is ready, you pour it into the cup, and stir.
So, how is this programming?
Well, we gathered resources (the kettle, coffee, water, teaspoon, and mug) and we verified some conditions on them (kettle is plugged-in, mug is clean, there is enough coffee). Then we started two actions (boiling the water and putting coffee in the mug), and when both of them were completed, we finally ended the procedure by pouring water in the mug and stirring.
Can you see it? I have just described the high-level functionality of a coffee program. It wasn't that hard because this is what the brain does all day long: evaluate conditions, decide to take actions, carry out tasks, repeat some of them, and stop at some point. Clean objects, put them back, and so on.
All you need now is to learn how to deconstruct all those actions you do automatically in real life so that a computer can actually make some sense of them. And you need to learn a language as well, to instruct it.
So this is what this book is for. I'll tell you how to do it and I'll try to do that by means of many simple but focused examples (my favorite kind).
A proper introduction
I love to make references to the real world when I teach coding; I believe they help people retain the concepts better. However, now is the time to be a bit more rigorous and see what coding is from a more technical perspective.
When we write code, we're instructing a computer on what are the things it has to do. Where does the action happen? In many places: the computer memory, hard drives, network cables, CPU, and so on. It's a whole world
, which most of the time is the representation of a subset of the real world.
If you write a piece of software that allows people to buy clothes online, you will have to represent real people, real clothes, real brands, sizes, and so on and so forth, within the boundaries of a program.
In order to do so, you will need to create and handle objects in the program you're writing. A person can be an object. A car is an object. A pair of socks is an object. Luckily, Python understands objects very well.
The two main features any object has are properties and methods. Let's take a person object as an example. Typically in a computer program, you'll represent people as customers or employees. The properties that you store against them are things like the name, the SSN, the age, if they have a driving license, their e-mail, gender, and so on. In a computer program, you store all the data you need in order to use an object for the purpose you're serving. If you are coding a website to sell clothes, you probably want to store the height and weight as well as other measures of your customers so that you can suggest the appropriate clothes for them. So, properties are characteristics of an object. We use them all the time: Could you pass me that pen?
– Which one?
– The black one.
Here, we used the black
property of a pen to identify it (most likely amongst a blue and a red one).
Methods are things that an object can do. As a person, I have methods such as speak, walk, sleep, wake-up, eat, dream, write, read, and so on. All the things that I can do could be seen as methods of the objects that represents me.
So, now that you know what objects are and that they expose methods that you can run and properties that you can inspect, you're ready to start coding. Coding in fact is simply about managing those objects that live in the subset of the world that we're reproducing in our software. You can create, use, reuse, and delete objects as you please.
According to the Data Model chapter on the official Python documentation:
Objects are Python's abstraction for data. All data in a Python program is represented by objects or by relations between objects.
We'll take a closer look at Python objects in Chapter 6, Advanced Concepts – OOP, Decorators, and Iterators. For now, all we need to know is that every object in Python has an ID (or identity), a type, and a value.
Once created, the identity of an object is never changed. It's a unique identifier for it, and it's used behind the scenes by Python to retrieve the object when we want to use it.
The type as well, never changes. The type tells what operations are supported by the object and the possible values that can be assigned to it.
We'll see Python's most important data types in Chapter 2, Built-in Data Types.
The value can either change or not. If it can, the object is said to be mutable, while when it cannot, the object is said to be immutable.
How do we use an object? We give it a name of course! When you give an object a name, then you can use the name to retrieve the object and use it.
In a more generic sense, objects such as numbers, strings (text), collections, and so on are associated with a name. Usually, we say that this name is the name of a variable. You can see the variable as being like a box, which you can use to hold data.
So, you have all the objects you need: what now? Well, we need to use them, right? We may want to send them over a network connection or store them in a database. Maybe display them on a web page or write them into a file. In order to do so, we need to react to a user filling in a form, or pressing a button, or opening a web page and performing a search. We react by running our code, evaluating conditions to choose which parts to execute, how many times, and under which circumstances.
And to do all this, basically we need a language. That's what Python is for. Python is the language we'll use together throughout this book to instruct the computer to do something for us.
Now, enough of this theoretical stuff, let's get started.
Enter the Python
Python is the marvelous creature of Guido Van Rossum, a Dutch computer scientist and mathematician who decided to gift the world with a project he was playing around with over Christmas 1989. The language appeared to the public somewhere around 1991, and since then has evolved to be one of the leading programming languages used worldwide today.
I started programming when I was 7 years old, on a Commodore VIC 20, which was later replaced by its bigger brother, the Commodore 64. The language was BASIC. Later on, I landed on Pascal, Assembly, C, C++, Java, JavaScript, Visual Basic, PHP, ASP, ASP .NET, C#, and other minor languages I cannot even remember, but only when I landed on Python, I finally had that feeling that you have when you find the right couch in the shop. When all of your body parts are yelling, Buy this one! This one is perfect for us!
It took me about a day to get used to it. Its syntax is a bit different from what I was used to, and in general, I very rarely worked with a language that defines scoping with indentation. But after getting past that initial feeling of discomfort (like having new shoes), I just fell in love with it. Deeply. Let's see why.
About Python
Before we get into the gory details, let's get a sense of why someone would want to use Python (I would recommend you to read the Python page on Wikipedia to get a more detailed introduction).
To my mind, Python exposes the following qualities.
Portability
Python runs everywhere, and porting a program from Linux to Windows or Mac is usually just a matter of fixing paths and settings. Python is designed for portability and it takes care of operating system (OS) specific quirks behind interfaces that shield you from the pain of having to write code tailored to a specific platform.
Coherence
Python is extremely logical and coherent. You can see it was designed by a brilliant computer scientist. Most of the time you can just guess how a method is called, if you don't know it.
You may not realize how important this is right now, especially if you are at the beginning, but this is a major feature. It means less cluttering in your head, less skimming through the documentation, and less need for mapping in your brain when you code.
Developer productivity
According to Mark Lutz (Learning Python, 5th Edition, O'Reilly Media), a Python program is typically one-fifth to one-third the size of equivalent Java or C++ code. This means the job gets done faster. And faster is good. Faster means a faster response on the market. Less code not only means less code to write, but also less code to read (and professional coders read much more than they write), less code to maintain, to debug, and to refactor.
Another important aspect is that Python runs without the need of lengthy and time consuming compilation and linkage steps, so you don't have to wait to see the results of your work.
An extensive library
Python has an incredibly wide standard library (it's said to come with batteries included
). If that wasn't enough, the Python community all over the world maintains a body of third party libraries, tailored to specific needs, which you can access freely at the Python Package Index (PyPI). When you code Python and you realize that you need a certain feature, in most cases, there is at least one library where that feature has already been implemented for you.
Software quality
Python is heavily focused on readability, coherence, and quality. The language uniformity allows for high readability and this is crucial nowadays where code is more of a collective effort than a solo experience. Another important aspect of Python is its intrinsic multi-paradigm nature. You can use it as scripting language, but you also can exploit object-oriented, imperative, and functional programming styles. It is versatile.
Software integration
Another important aspect is that Python can be extended and integrated with many other languages, which means that even when a company is using a different language as their mainstream tool, Python can come in and act as a glue agent between complex applications that need to talk to each other in some way. This is kind of an advanced topic, but in the real world, this feature is very important.
Satisfaction and enjoyment
Last but not least, the fun of it! Working with Python is fun. I can code for 8 hours and leave the office happy and satisfied, alien to the struggle other coders have to endure because they use languages that don't provide them with the same amount of well-designed data structures and constructs. Python makes coding fun, no doubt about it. And fun promotes motivation and productivity.
These are the major aspects why I would recommend Python to everyone for. Of course, there are many other technical and advanced features that I could have talked about, but they don't really pertain to an introductory section like this one. They will come up naturally, chapter after chapter, in this book.
What are the drawbacks?
Probably, the only drawback that one could find in Python, which is not due to personal preferences, is the execution speed. Typically, Python is slower than its compiled brothers. The standard implementation of Python produces, when you run an application, a compiled version of the source code called byte code (with the extension .pyc), which is then run by the Python interpreter. The advantage of this approach is portability, which we pay for with a slowdown due to the fact that Python is not compiled down to machine level as are other languages.
However, Python speed is rarely a problem today, hence its wide use regardless of this suboptimal feature. What happens is that in real life, hardware cost is no longer a problem, and usually it's easy enough to gain speed by parallelizing tasks. When it comes to number crunching though, one can switch to faster Python implementations, such as PyPy, which provides an average 7-fold speedup by implementing advanced compilation techniques (check http://pypy.org/ for reference).
When doing data science, you'll most likely find that the libraries that you use with Python, such as Pandas and Numpy, achieve native speed due to the way they are implemented.
If that wasn't a good enough argument, you can always consider that Python is driving the backend of services such as Spotify and Instagram, where performance is a concern. Nonetheless, Python does its job perfectly adequately.
Who is using Python today?
Not yet convinced? Let's take a very brief look at the companies that are using Python today: Google, YouTube, Dropbox, Yahoo, Zope Corporation, Industrial Light & Magic, Walt Disney Feature Animation, Pixar, NASA, NSA, Red Hat, Nokia, IBM, Netflix, Yelp, Intel, Cisco, HP, Qualcomm, and JPMorgan Chase, just to name a few.
Even games such as Battlefield 2, Civilization 4, and QuArK are implemented using Python.
Python is used in many different contexts, such as system programming, web programming, GUI applications, gaming and robotics, rapid prototyping, system integration, data science, database applications, and much more.
Setting up the environment
Before we talk about installing Python on your system, let me tell you about which Python version I'll be using in this book.
Python 2 versus Python 3 – the great debate
Python comes in two main versions—Python 2, which is the past—and Python 3, which is the present. The two versions, though very similar, are incompatible on some aspects.
In the real world, Python 2 is actually quite far from being the past. In short, even though Python 3 has been out since 2008, the transition phase is still far from being over. This is mostly due to the fact that Python 2 is widely used in the industry, and of course, companies aren't so keen on updating their systems just for the sake of updating, following the if it ain't broke, don't fix it philosophy. You can read all about the transition between the two versions on the Web.
Another issue that was hindering the transition is the availability of third-party libraries. Usually, a Python project relies on tens of external libraries, and of course, when you start a new project, you