Python Quick Start
Python Quick Start
A beginner's introduction to Python programming
About the Book
Python is an extremely popular language, and is an excellent choice for first time programmers, because it is simple to get started with, but extremely powerful once you get to know it. Python in the language of choice for machine learning and web development, but is can also be used for anything from system scripts to games to desktop applications. It is also embedded in many existing applications to provide a scripting mechanism, which can be used to automate many applications such as Paintshop or Blender.
This book will help you learn Python whether you are a complete beginner or you already have experience programming in other languages. It teaches all the core features of the language, step by step, with example code.
Table of Contents
-
Foreword
- Who is this book for?
- About the author
- Keep in touch
-
Introduction
- What is Python
- History of Python
- Zen of Python
-
1 Getting started with Python
-
1.1 Installing Python
- 1.1.1 Windows
- 1.1.2 Linux
- 1.2 Running IDLE
- 1.3 Simple maths in Python
-
1.4 Variables
- 1.4.1 Calculations with variables
- 1.4.2 Changing the value of a variable
- 1.4.3 Variable names
-
1.5 Strings
- 1.5.1 Strings in variables
- 1.6 Console shortcuts
-
1.1 Installing Python
-
2 Hello world
-
2.1 Writing a program
- 2.1.1 Running the IDLE console window
- 2.1.2 Opening an edit window
- 2.1.3 Typing in your code
- 2.1.4 Type, don’t copy and paste
- 2.1.5 Running your code
- 2.1.6 Seeing the result
- 2.2 In case of errors
- 2.3 Print statements - outputting text
- 2.4 Sequencing
-
2.5 Input statements - getting user input
- 2.5.1 Inputting a number
-
2.6 Selection
- 2.6.1 Indentation
- 2.7 Iteration
- 2.8 Comments
-
2.1 Writing a program
-
3 Variables and values
- 3.1 Creating and using variables
- 3.2 Variables can store any type of data
-
3.3 Naming variables
- 3.3.1 Legal variable names
- 3.3.2 Reserved words
- 3.3.3 Choosing variable names
- 3.3.4 Naming conventions
- 3.3.5 Unicode characters
- 3.4 Variables point to values
- 3.5 None
- 3.6 Multiple assignments
-
4 Numbers and maths
-
4.1 Types of number
- 4.1.1 Integers
- 4.1.2 Floating point numbers
- 4.1.3 Finding the type
-
4.2 Basic maths operations
- 4.2.1 Addition
- 4.2.2 Int and float results
- 4.2.3 Subtraction
- 4.2.4 Multiplication
- 4.2.5 Division
- 4.2.6 Power
- 4.2.7 Floor divide
- 4.2.8 Modulo
- 4.2.9 Unary operators
- 4.2.10 Augmented assignment
-
4.3 Precedence and brackets
- 4.3.1 Statements and expressions
-
4.4 Built-in maths functions
- 4.4.1 abs()
- 4.4.2 min() and max()
- 4.4.3 int()
- 4.4.4 float()
- 4.5 math module
- 4.6 Limitations of floats
-
4.7 More advanced topics
- 4.7.1 Scientific notation for floats
- 4.7.2 Complex numbers
-
4.1 Types of number
-
5 Strings
- 5.1 Creating strings
- 5.2 String type
-
5.3 Operations on strings
- 5.3.1 Methods
- 5.4 Converting between strings and other data types
-
5.5 How to use special characters in a string
- 5.5.1 Quote characters
- 5.5.2 Unicode characters
- 5.5.3 Newline characters
- 5.5.4 The escape character \
-
6 Loops
-
6.1 For loops
- 6.1.1 The loop variable
-
6.2 The range function
- 6.2.1 Changing the start value
- 6.2.2 Using a step value
- 6.3 For loop example - printing a times table
- 6.4 While loops
- 6.5 While loop example - getting user input
- 6.6 Nested loops
-
6.1 For loops
-
7 Input and output
- 7.1 Input prompts
- 7.2 Inputting numbers
- 7.3 Print
- 7.4 Print separators
-
8 If statements
-
8.1 if statements
- 8.1.1 Example if statement
- 8.2 Comparison operators
- 8.3 Else statement
- 8.4 Elif statement
- 8.5 Compound conditions
- 8.6 Comparison operator chaining
- 8.7 Precedence
-
8.1 if statements
-
9 Lists
-
9.1 What is a list?
- 9.1.1 Lists vs arrays
-
9.2 Creating lists
- 9.2.1 Empty lists
- 9.2.2 The list function
- 9.3 The list type
-
9.4 Accessing list elements
- 9.4.1 Reading an element
- 9.4.2 Changing an element
- 9.5 Operations on lists
-
9.6 Looping over a list
- 9.6.1 Doing calculations in a loop
-
9.7 List functions
- 9.7.1 Finding the length of the list
-
9.7.2 Finding the
min
amax
values - 9.7.3 Adding up the values in a list
-
9.8 List methods
- 9.8.1 Adding elements
- 9.8.2 Searching for elements in a list
- 9.8.3 Removing elements
- 9.8.4 Other methods
-
9.9 Two-dimensional lists
- 9.9.1 Ragged arrays
-
9.10 Aliasing
- 9.10.1 Reassigning variables
- 9.10.2 Does aliasing affect numbers?
-
9.11 Examples
- 9.11.1 Finding every element of a given value
- 9.11.2 Creating a 2-dimensional list of zeros
-
9.1 What is a list?
-
10 Using functions
- 10.1 Why use functions?
-
10.2 Built-in functions
- 10.2.1 repr
- 10.2.2 chr and ord
- 10.2.3 help and dir
-
10.3 Importing modules
- 10.3.1 Simple import statement
- 10.3.2 Importing individual functions
- 10.3.3 Aliasing a module
- 10.3.4 Importing all functions (don’t do this)
-
10.4 How to call functions
- 10.4.1 Optional parameters
- 10.4.2 Variable number of arguments
- 10.4.3 Named optional parameters
-
10.5 Defining your own functions
- 10.5.1 Printing @s
- 10.5.2 Printing more @s
- 10.5.3 Returning a value
-
11 More loops
-
11.1 The break operator
- 11.1.1 Break in a for loop
- 11.1.2 Break in a while loop
- 11.2 The continue operator
- 11.3 Using else with a loop
-
11.4 Nested loop statements
- 11.4.1 Printing times tables
- 11.5 Modifying a for loop
-
11.6 Looping in reverse order
- 11.6.1 Using range() to count backwards - ugly
- 11.6.2 reversed()
- 11.7 sorted()
-
11.8 Looping over multiple items
- 11.8.1 zip
- 11.8.2 How zip() works
- 11.9 More about zip()
- 11.10 Accessing the loop count using enumerate
-
11.11 Looping over selected items
- 11.11.1 Using filter
- 11.11.2 The advantage of using filter
-
11.1 The break operator
-
12 Programming logic
- 12.1 Boolean types
- 12.2 Comparison operators and their opposites
- 12.3 Boolean operations and De Morgan’s Laws
- 12.4 Ternary operators
-
12.5 Comparing containers
- 12.5.1 Strings
- 12.5.2 Lists
- 12.5.3 Other containers
- 12.6 Membership testing
- 12.7 Identity testing
-
12.8 Truthy values
- 12.8.1 Numbers
- 12.8.2 Strings
- 12.8.3 Lists
- 12.8.4 Other collections
- 12.8.5 None
- 12.8.6 Other objects
-
12.9 Short circuit evaluation
- 12.9.1 Short-circuiting with the or operator
- 12.9.2 Short-circuiting with the and operator
- 12.9.3 Short-circuiting to avoid errors
- 12.9.4 The value of an or/and expression
- 12.9.5 Getting a true or false value
-
13 Slices
- 13.1 Slicing a list
- 13.2 Using negative indices
- 13.3 Delete or replace a slice of a list
- 13.4 Slicing tuples and strings
- 13.5 Loop over a slice
- 13.6 Using steps
-
14 More strings
- 14.1 Raw strings
- 14.2 Multi-line strings
- 14.3 Looping over a string
- 14.4 Joining strings
- 14.5 Splitting a string
- 14.6 Formatting strings
- 14.7 The in operator
-
14.8 Other string methods
- 14.8.1 Changing case
- 14.8.2 Dealing with whitespace
- 14.8.3 Padding
- 14.8.4 Search
- 14.8.5 Splitting strings
- 14.8.6 Categorising strings
-
15 Tuples
- 15.1 What is a tuple?
- 15.2 Creating a tuple
- 15.3 Accessing elements
-
15.4 Packing and unpacking tuples
- 15.4.1 Packing a tuple
- 15.4.2 Unpacking a tuple
- 15.4.3 Real-life examples
-
15.5 Tuple vs list operations
- 15.5.1 Tuple slices
- 15.5.2 Adding elements to a tuple
- 15.5.3 Finding elements
- 15.5.4 Removing elements
- 15.5.5 Looping
- 15.5.6 In case of emergency
- 15.6 Pros and cons of immutability
-
16 Exceptions
- 16.1 Program errors
- 16.2 What are exceptions
-
16.3 Exception types
- 16.3.1 ImportError
- 16.3.2 IndexError
- 16.3.3 TypeError
- 16.3.4 ValueError
- 16.3.5 ZeroDivisionError
-
16.4 Catching exceptions
- 16.4.1 Only catch exceptions you can handle
- 16.4.2 Accessing the exception message
- 16.5 Using else with exceptions
- 16.6 Using finally with exceptions
- 16.7 Throwing exceptions
-
17 Working with files
-
17.1 Using files
- 17.1.1 Opening the file
- 17.1.2 Reading and writing data
- 17.1.3 Closing the file
-
17.2 Reading data
- 17.2.1 Code for reading a file
- 17.2.2 Reading lines from a file
- 17.2.3 Looping over the lines in a file
-
17.3 Writing data
- 17.3.1 Code to write a file
- 17.3.2 The write function
- 17.4 Using with statements
-
17.5 CSV data
- 17.5.1 Reading CSV data
- 17.5.2 Trying the code
- 17.5.3 Formatting the output
- 17.5.4 Writing CSV data
- 17.5.5 Understanding the code
- 17.5.6 Trying the code
-
17.1 Using files
-
18 More books from this author
- 18.1 Numpy Recipes
- 18.2 Computer Graphics in Python with Pycairo
- 18.3 Functional Programming in Python
Authors have earned$10,098,681writing, publishing and selling on Leanpub, earning 80% royalties while saving up to 25 million pounds of CO2 and up to 46,000 trees.
Learn more about writing on Leanpub
The Leanpub 45-day 100% Happiness Guarantee
Within 45 days of purchase you can get a 100% refund on any Leanpub purchase, in two clicks.
See full terms
Free Updates. DRM Free.
If you buy a Leanpub book, you get free updates for as long as the author updates the book! Many authors use Leanpub to publish their books in-progress, while they are writing them. All readers get free updates, regardless of when they bought the book or how much they paid (including free).
Most Leanpub books are available in PDF (for computers), EPUB (for phones and tablets) and MOBI (for Kindle). The formats that a book includes are shown at the top right corner of this page.
Finally, Leanpub books don't have any DRM copy-protection nonsense, so you can easily read them on any supported device.
Learn more about Leanpub's ebook formats and where to read them
Top Books
C++20
Rainer GrimmC++20 is the next big C++ standard after C++11. As C++11 did it, C++20 changes the way we program modern C++. This change is, in particular, due to the big four of C++20: ranges, coroutines, concepts, and modules.
The book is almost daily updated. These incremental updates ease my interaction with the proofreaders.
A Guide to Artificial Intelligence in Healthcare
Dr. Bertalan MeskoCan we stay human in the age of A.I.? To go even further, can we grow in humanity, can we shape a more humane, more equitable and sustainable healthcare? This e-book aims to prepare healthcare and medical professionals for the era of human-machine collaboration. Read our guide to understanding, anticipating and controlling artificial intelligence.
Atomic Kotlin
Bruce Eckel and Svetlana IsakovaFor both beginning and experienced programmers! From the author of the multi-award-winning Thinking in C++ and Thinking in Java together with a member of the Kotlin language team comes a book that breaks the concepts into small, easy-to-digest "atoms," along with exercises supported by hints and solutions directly inside IntelliJ IDEA!
Introducing EventStorming
Alberto BrandoliniThe deepest tutorial and explanation about EventStorming, straight from the inventor.
C++ Best Practices
Jason TurnerLevel up your C++, get the tools working for you, eliminate common problems, and move on to more exciting things!
Ansible for DevOps
Jeff GeerlingAnsible is a simple, but powerful, server and configuration management tool. Learn to use Ansible effectively, whether you manage one server—or thousands.
Everyday Rails - RSpecによるRailsテスト入門
Junichi Ito (伊藤淳一), AKIMOTO Toshiharu, 魚振江, and Aaron SumnerRSpecを使ってRailsアプリケーションに信頼性の高いテストを書く実践的なアドバイスを提供します。詳細で丁寧な説明は本書のオリジナルコンテンツです。また、説明には実際に動かせるサンプルアプリケーションも使用します。本書は2017年版にアップデートされ、RSpec 3.6やRails 5.1といった新しい環境に対応しています!さあ、自信をもってテストできるようになりましょう!
The Hundred-Page Machine Learning Book
Andriy BurkovEverything you really need to know in Machine Learning in a hundred pages.
Composing Software
Eric ElliottAll software design is composition: the act of breaking complex problems down into smaller problems and composing those solutions. Most developers have a limited understanding of compositional techniques. It's time for that to change.
R Programming for Data Science
Roger D. PengThis book brings the fundamentals of R programming to you, using the same material developed as part of the industry-leading Johns Hopkins Data Science Specialization. The skills taught in this book will lay the foundation for you to begin your journey learning data science. Printed copies of this book are available through Lulu.
Top Bundles
- #1
Software Architecture for Developers: Volumes 1 & 2 - Technical leadership and communication
2 Books
"Software Architecture for Developers" is a practical and pragmatic guide to modern, lightweight software architecture, specifically aimed at developers. You'll learn:The essence of software architecture.Why the software architecture role should include coding, coaching and collaboration.The things that you really need to think about before... - #2
All the Books of The Medical Futurist
6 Books
We put together the most popular books from The Medical Futurist to provide a clear picture about the major trends shaping the future of medicine and healthcare. Digital health technologies, artificial intelligence, the future of 20 medical specialties, big pharma, data privacy, digital health investments and how technology giants such as Amazon... - #3
Cisco CCNA 200-301 Complet
4 Books
Ce lot comprend les quatre volumes du guide préparation à l'examen de certification Cisco CCNA 200-301. - #4
Linux Administration Complet
4 Books
Ce lot comprend les quatre volumes du Guide Linux Administration :Linux Administration, Volume 1, Administration fondamentale : Guide pratique de préparation aux examens de certification LPIC 1, Linux Essentials, RHCSA et LFCS. Administration fondamentale. Introduction à Linux. Le Shell. Traitement du texte. Arborescence de fichiers. Sécurité... - #5
Learn Git, Bash, and Terraform the Hard Way
3 Books
Learn Git, Bash and Terraform using the Hard Way method.These technologies are essential tools in the DevOps armoury. These books walk you through their features and subtleties in a simple, gradual way that reinforces learning rather than baffling you with theory. - #6
PowerShell
3 Books
Buy every PowerShell book from Adam Bertram at a 20% discount! - #7
Software Architecture and Beautiful APIs
2 Books
There is no better way to learn how to design good APIs than to look at many existing examples, complementing the Software Architecture theory on API design. - #8
9 Books-Bundle: Shut Up and Code!
9 Books
"Shut up and code." Laughter in the audience. The hacker had just plugged in his notebook and started sharing his screen to present his super-smart Python script. "Shut up and code" The letters written in a white literal coding font on black background was the hackers' home screen background mantra. At the time, I was a first-year computer... - #9
CCDE Practical Studies (All labs)
3 Books
CCDE lab - #10
Modern C++ by Nicolai Josuttis
2 Books