Home Page Icon
Home Page
Table of Contents for
Introducing Python
Close
Introducing Python
by Bill Lubanovic
Introducing Python
Preface
Audience
Outline
Python Versions
Conventions Used in This Book
Using Code Examples
Safari® Books Online
How to Contact Us
Acknowledgments
1. A Taste of Py
Python in the Real World
Python versus Language X
So, Why Python?
When Not to Use Python
Python 2 versus Python 3
Installing Python
Running Python
Using the Interactive Interpreter
Use Python Files
What’s Next?
Your Moment of Zen
Things to Do
2. Py Ingredients: Numbers, Strings, and Variables
Variables, Names, and Objects
Numbers
Integers
Precedence
Bases
Type Conversions
How Big Is an int?
Floats
Math Functions
Strings
Create with Quotes
Convert Data Types by Using str()
Escape with
Combine with +
Duplicate with *
Extract a Character with []
Slice with [ start : end : step ]
Get Length with len()
Split with split()
Combine with join()
Playing with Strings
Case and Alignment
Substitute with replace()
More String Things
Things to Do
3. Py Filling: Lists, Tuples, Dictionaries, and Sets
Lists and Tuples
Lists
Create with [] or list()
Convert Other Data Types to Lists with list()
Get an Item by Using [ offset ]
Lists of Lists
Change an Item by [ offset ]
Get a Slice to Extract Items by Offset Range
Add an Item to the End with append()
Combine Lists by Using extend() or +=
Add an Item by Offset with insert()
Delete an Item by Offset with del
Delete an Item by Value with remove()
Get an Item by Offset and Delete It by Using pop()
Find an Item’s Offset by Value with index()
Test for a Value with in
Count Occurrences of a Value by Using count()
Convert to a String with join()
Reorder Items with sort()
Get Length by Using len()
Assign with =, Copy with copy()
Tuples
Create a Tuple by Using ()
Tuples versus Lists
Dictionaries
Create with {}
Convert by Using dict()
Add or Change an Item by [ key ]
Combine Dictionaries with update()
Delete an Item by Key with del
Delete All Items by Using clear()
Test for a Key by Using in
Get an Item by [ key ]
Get All Keys by Using keys()
Get All Values by Using values()
Get All Key-Value Pairs by Using items()
Assign with =, Copy with copy()
Sets
Create with set()
Convert from Other Data Types with set()
Test for Value by Using in
Combinations and Operators
Compare Data Structures
Make Bigger Data Structures
Things to Do
4. Py Crust: Code Structures
Comment with #
Continue Lines with
Compare with if, elif, and else
What Is True?
Do Multiple Comparisons with in
Repeat with while
Cancel with break
Skip Ahead with continue
Check break Use with else
Iterate with for
Cancel with break
Skip with continue
Check break Use with else
Iterate Multiple Sequences with zip()
Generate Number Sequences with range()
Other Iterators
Comprehensions
List Comprehensions
Dictionary Comprehensions
Set Comprehensions
Generator Comprehensions
Functions
Positional Arguments
Keyword Arguments
Specify Default Parameter Values
Gather Positional Arguments with *
Gather Keyword Arguments with **
Docstrings
Functions Are First-Class Citizens
Inner Functions
Closures
Anonymous Functions: the lambda() Function
Generators
Decorators
Namespaces and Scope
Uses of _ and __ in Names
Handle Errors with try and except
Make Your Own Exceptions
Things to Do
5. Py Boxes: Modules, Packages, and Programs
Standalone Programs
Command-Line Arguments
Modules and the import Statement
Import a Module
Import a Module with Another Name
Import Only What You Want from a Module
Module Search Path
Packages
The Python Standard Library
Handle Missing Keys with setdefault() and defaultdict()
Count Items with Counter()
Order by Key with OrderedDict()
Stack + Queue == deque
Iterate over Code Structures with itertools
Print Nicely with pprint()
More Batteries: Get Other Python Code
Things to Do
6. Oh Oh: Objects and Classes
What Are Objects?
Define a Class with class
Inheritance
Override a Method
Add a Method
Get Help from Your Parent with super
In self Defense
Get and Set Attribute Values with Properties
Name Mangling for Privacy
Method Types
Duck Typing
Special Methods
Aggregation and Composition
When to Use Classes and Objects versus Modules
Named Tuples
Things to Do
7. Mangle Data Like a Pro
Text Strings
Unicode
Format
Match with Regular Expressions
Binary Data
bytes and bytearray
Convert Binary Data with struct
Other Binary Data Tools
Convert Bytes/Strings with binascii()
Bit Operators
Things to Do
8. Data Has to Go Somewhere
File Input/Output
Write a Text File with write()
Read a Text File with read(), readline(), or readlines()
Write a Binary File with write()
Read a Binary File with read()
Close Files Automatically by Using with
Change Position with seek()
Structured Text Files
CSV
XML
HTML
JSON
YAML
A Security Note
Configuration Files
Other Interchange Formats
Serialize by Using pickle
Structured Binary Files
Spreadsheets
HDF5
Relational Databases
SQL
DB-API
SQLite
MySQL
PostgreSQL
SQLAlchemy
NoSQL Data Stores
The dbm Family
Memcached
Redis
Other NoSQL
Full-Text Databases
Things to Do
9. The Web, Untangled
Web Clients
Test with telnet
Python’s Standard Web Libraries
Beyond the Standard Library: Requests
Web Servers
The Simplest Python Web Server
Web Server Gateway Interface
Frameworks
Bottle
Flask
Non-Python Web Servers
Other Frameworks
Web Services and Automation
The webbrowser Module
Web APIs and Representational State Transfer
JSON
Crawl and Scrape
Scrape HTML with BeautifulSoup
Things to Do
10. Systems
Files
Create with open()
Check Existence with exists()
Check Type with isfile()
Copy with copy()
Change Name with rename()
Link with link() or symlink()
Change Permissions with chmod()
Change Ownership with chown()
Get a Pathname with abspath()
Get a symlink Pathname with realpath()
Delete a File with remove()
Directories
Create with mkdir()
Delete with rmdir()
List Contents with listdir()
Change Current Directory with chdir()
List Matching Files with glob()
Programs and Processes
Create a Process with subprocess
Create a Process with multiprocessing
Kill a Process with terminate()
Calendars and Clocks
The datetime Module
Using the time Module
Read and Write Dates and Times
Alternative Modules
Things to Do
11. Concurrency and Networks
Concurrency
Queues
Processes
Threads
Green Threads and gevent
twisted
asyncio
Redis
Beyond Queues
Networks
Patterns
The Publish-Subscribe Model
TCP/IP
Sockets
ZeroMQ
Scapy
Internet Services
Web Services and APIs
Remote Processing
Big Fat Data and MapReduce
Working in the Clouds
Things to Do
12. Be a Pythonista
About Programming
Find Python Code
Install Packages
Use pip
Use a Package Manager
Install from Source
Integrated Development Environments
IDLE
PyCharm
IPython
Name and Document
Testing Your Code
Check with pylint, pyflakes, and pep8
Test with unittest
Test with doctest
Test with nose
Other Test Frameworks
Continuous Integration
Debugging Python Code
Debug with pdb
Logging Error Messages
Optimize Your Code
Measure Timing
Algorithms and Data Structures
Cython, NumPy, and C Extensions
PyPy
Source Control
Mercurial
Git
Clone This Book
How You Can Learn More
Books
Websites
Groups
Conferences
Coming Attractions
A. Py Art
2-D Graphics
Standard Library
PIL and Pillow
ImageMagick
Graphical User Interfaces (GUIs)
3-D Graphics and Animation
Plots, Graphs, and Visualization
matplotlib
bokeh
Games
Audio and Music
B. Py at Work
The Microsoft Office Suite
Carrying Out Business Tasks
Processing Business Data
Extracting, Transforming, and Loading
Additional Sources of Information
Python in Finance
Business Data Security
Maps
Formats
Draw a Map
Applications and Data
C. Py Sci
Math and Statistics in the Standard Library
Math Functions
Working with Complex Numbers
Calculate Accurate Floating Point with decimal
Perform Rational Arithmetic with fractions
Use Packed Sequences with array
Handling Simple Stats by Using statistics
Matrix Multiplication
Scientific Python
NumPy
Make an Array with array()
Make an Array with arange()
Make an Array with zeros(), ones(), or random()
Change an Array’s Shape with reshape()
Get an Element with []
Array Math
Linear Algebra
The SciPy Library
The SciKit Library
The IPython Library
A Better Interpreter
IPython Notebook
Pandas
Python and Scientific Areas
D. Install Python 3
Install Standard Python
Mac OS X
Windows
Linux or Unix
Install Anaconda
Install and Use pip and virtualenv
Install and Use conda
E. Answers to Exercises
Chapter 1, A Taste of Py
Chapter 2, Py Ingredients: Numbers, Strings, and Variables
Chapter 3, Py Filling: Lists, Tuples, Dictionaries, and Sets
Chapter 4, Py Crust: Code Structures
Chapter 5, Py Boxes: Modules, Packages, and Programs
Chapter 6, Oh Oh: Objects and Classes
Chapter 7, Mangle Data Like a Pro
Chapter 8, Data Has to Go Somewhere
Chapter 9, The Web, Untangled
Chapter 10, Systems
Chapter 11, Concurrency and Networks
F. Cheat Sheets
Operator Precedence
String Methods
Change Case
Search
Modify
Format
String Type
String Module Attributes
Fin
Index
Search in book...
Toggle Font Controls
Playlists
Add To
Create new playlist
Name your new playlist
Playlist description (optional)
Cancel
Create playlist
Sign In
Email address
Password
Forgot Password?
Create account
Login
or
Continue with Facebook
Continue with Google
Sign Up
Full Name
Email address
Confirm Email Address
Password
Login
Create account
or
Continue with Facebook
Continue with Google
Prev
Previous Chapter
Cover
Next
Next Chapter
Introducing Python
Introducing Python
Bill Lubanovic
Add Highlight
No Comment
..................Content has been hidden....................
You can't read the all page of ebook, please click
here
login for view all page.
Day Mode
Cloud Mode
Night Mode
Reset