Leanpub Header

Skip to main content

Python Quick Start

A beginner's introduction to Python programming

Learn the basics of Python programming, whether you are a complete beginner, or moving to Python from another language. This book covers everything you need to know to start writing your own Python scripts and programs, with exercises to help you practise and cement your knowledge.

Minimum price

$9.99

$14.99

You pay

$14.99

Author earns

$11.99
$

...Or Buy With Credits!

You can get credits monthly with a Reader Membership
PDF
EPUB
WEB
174
Pages
40,752Words
About

About

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.

Share this book

Categories

Author

About the Author

Martin McBride

Martin McBride is a software developer, specialising in computer graphics, sound, and mathematical programming. He has been writing code since the 1980s in a wide variety of languages from assembler through to C++, Java and Python. He writes for PythonInformer.com and is the author of Functional Programming in Python. He is interested in generative art and works on the generativepy open source project.

Contents

Table of Contents

Foreword

  1. Who is this book for?
  2. About the author
  3. Keep in touch

Introduction

  1. What is Python
  2. History of Python
  3. Zen of Python

1Getting started with Python

  1. 1.1Installing Python
  2. 1.1.1Windows
  3. 1.1.2Linux
  4. 1.2Running IDLE
  5. 1.3Simple maths in Python
  6. 1.4Variables
  7. 1.4.1Calculations with variables
  8. 1.4.2Changing the value of a variable
  9. 1.4.3Variable names
  10. 1.5Strings
  11. 1.5.1Strings in variables
  12. 1.6Console shortcuts

2Hello world

  1. 2.1Writing a program
  2. 2.1.1Running the IDLE console window
  3. 2.1.2Opening an edit window
  4. 2.1.3Typing in your code
  5. 2.1.4Type, don’t copy and paste
  6. 2.1.5Running your code
  7. 2.1.6Seeing the result
  8. 2.2In case of errors
  9. 2.3Print statements - outputting text
  10. 2.4Sequencing
  11. 2.5Input statements - getting user input
  12. 2.5.1Inputting a number
  13. 2.6Selection
  14. 2.6.1Indentation
  15. 2.7Iteration
  16. 2.8Comments

3Variables and values

  1. 3.1Creating and using variables
  2. 3.2Variables can store any type of data
  3. 3.3Naming variables
  4. 3.3.1Legal variable names
  5. 3.3.2Reserved words
  6. 3.3.3Choosing variable names
  7. 3.3.4Naming conventions
  8. 3.3.5Unicode characters
  9. 3.4Variables point to values
  10. 3.5None
  11. 3.6Multiple assignments

4Numbers and maths

  1. 4.1Types of number
  2. 4.1.1Integers
  3. 4.1.2Floating point numbers
  4. 4.1.3Finding the type
  5. 4.2Basic maths operations
  6. 4.2.1Addition
  7. 4.2.2Int and float results
  8. 4.2.3Subtraction
  9. 4.2.4Multiplication
  10. 4.2.5Division
  11. 4.2.6Power
  12. 4.2.7Floor divide
  13. 4.2.8Modulo
  14. 4.2.9Unary operators
  15. 4.2.10Augmented assignment
  16. 4.3Precedence and brackets
  17. 4.3.1Statements and expressions
  18. 4.4Built-in maths functions
  19. 4.4.1abs()
  20. 4.4.2min() and max()
  21. 4.4.3int()
  22. 4.4.4float()
  23. 4.5math module
  24. 4.6Limitations of floats
  25. 4.7More advanced topics
  26. 4.7.1Scientific notation for floats
  27. 4.7.2Complex numbers

5Strings

  1. 5.1Creating strings
  2. 5.2String type
  3. 5.3Operations on strings
  4. 5.3.1Methods
  5. 5.4Converting between strings and other data types
  6. 5.5How to use special characters in a string
  7. 5.5.1Quote characters
  8. 5.5.2Unicode characters
  9. 5.5.3Newline characters
  10. 5.5.4The escape character \

6Loops

  1. 6.1For loops
  2. 6.1.1The loop variable
  3. 6.2The range function
  4. 6.2.1Changing the start value
  5. 6.2.2Using a step value
  6. 6.3For loop example - printing a times table
  7. 6.4While loops
  8. 6.5While loop example - getting user input
  9. 6.6Nested loops

7Input and output

  1. 7.1Input prompts
  2. 7.2Inputting numbers
  3. 7.3Print
  4. 7.4Print separators

8If statements

  1. 8.1if statements
  2. 8.1.1Example if statement
  3. 8.2Comparison operators
  4. 8.3Else statement
  5. 8.4Elif statement
  6. 8.5Compound conditions
  7. 8.6Comparison operator chaining
  8. 8.7Precedence

9Lists

  1. 9.1What is a list?
  2. 9.1.1Lists vs arrays
  3. 9.2Creating lists
  4. 9.2.1Empty lists
  5. 9.2.2The list function
  6. 9.3The list type
  7. 9.4Accessing list elements
  8. 9.4.1Reading an element
  9. 9.4.2Changing an element
  10. 9.5Operations on lists
  11. 9.6Looping over a list
  12. 9.6.1Doing calculations in a loop
  13. 9.7List functions
  14. 9.7.1Finding the length of the list
  15. 9.7.2Finding the min a max values
  16. 9.7.3Adding up the values in a list
  17. 9.8List methods
  18. 9.8.1Adding elements
  19. 9.8.2Searching for elements in a list
  20. 9.8.3Removing elements
  21. 9.8.4Other methods
  22. 9.9Two-dimensional lists
  23. 9.9.1Ragged arrays
  24. 9.10Aliasing
  25. 9.10.1Reassigning variables
  26. 9.10.2Does aliasing affect numbers?
  27. 9.11Examples
  28. 9.11.1Finding every element of a given value
  29. 9.11.2Creating a 2-dimensional list of zeros

10Using functions

  1. 10.1Why use functions?
  2. 10.2Built-in functions
  3. 10.2.1repr
  4. 10.2.2chr and ord
  5. 10.2.3help and dir
  6. 10.3Importing modules
  7. 10.3.1Simple import statement
  8. 10.3.2Importing individual functions
  9. 10.3.3Aliasing a module
  10. 10.3.4Importing all functions (don’t do this)
  11. 10.4How to call functions
  12. 10.4.1Optional parameters
  13. 10.4.2Variable number of arguments
  14. 10.4.3Named optional parameters
  15. 10.5Defining your own functions
  16. 10.5.1Printing @s
  17. 10.5.2Printing more @s
  18. 10.5.3Returning a value

11More loops

  1. 11.1The break operator
  2. 11.1.1Break in a for loop
  3. 11.1.2Break in a while loop
  4. 11.2The continue operator
  5. 11.3Using else with a loop
  6. 11.4Nested loop statements
  7. 11.4.1Printing times tables
  8. 11.5Modifying a for loop
  9. 11.6Looping in reverse order
  10. 11.6.1Using range() to count backwards - ugly
  11. 11.6.2reversed()
  12. 11.7sorted()
  13. 11.8Looping over multiple items
  14. 11.8.1zip
  15. 11.8.2How zip() works
  16. 11.9More about zip()
  17. 11.10Accessing the loop count using enumerate
  18. 11.11Looping over selected items
  19. 11.11.1Using filter
  20. 11.11.2The advantage of using filter

12Programming logic

  1. 12.1Boolean types
  2. 12.2Comparison operators and their opposites
  3. 12.3Boolean operations and De Morgan’s Laws
  4. 12.4Ternary operators
  5. 12.5Comparing containers
  6. 12.5.1Strings
  7. 12.5.2Lists
  8. 12.5.3Other containers
  9. 12.6Membership testing
  10. 12.7Identity testing
  11. 12.8Truthy values
  12. 12.8.1Numbers
  13. 12.8.2Strings
  14. 12.8.3Lists
  15. 12.8.4Other collections
  16. 12.8.5None
  17. 12.8.6Other objects
  18. 12.9Short circuit evaluation
  19. 12.9.1Short-circuiting with the or operator
  20. 12.9.2Short-circuiting with the and operator
  21. 12.9.3Short-circuiting to avoid errors
  22. 12.9.4The value of an or/and expression
  23. 12.9.5Getting a true or false value

13Slices

  1. 13.1Slicing a list
  2. 13.2Using negative indices
  3. 13.3Delete or replace a slice of a list
  4. 13.4Slicing tuples and strings
  5. 13.5Loop over a slice
  6. 13.6Using steps

14More strings

  1. 14.1Raw strings
  2. 14.2Multi-line strings
  3. 14.3Looping over a string
  4. 14.4Joining strings
  5. 14.5Splitting a string
  6. 14.6Formatting strings
  7. 14.7The in operator
  8. 14.8Other string methods
  9. 14.8.1Changing case
  10. 14.8.2Dealing with whitespace
  11. 14.8.3Padding
  12. 14.8.4Search
  13. 14.8.5Splitting strings
  14. 14.8.6Categorising strings

15Tuples

  1. 15.1What is a tuple?
  2. 15.2Creating a tuple
  3. 15.3Accessing elements
  4. 15.4Packing and unpacking tuples
  5. 15.4.1Packing a tuple
  6. 15.4.2Unpacking a tuple
  7. 15.4.3Real-life examples
  8. 15.5Tuple vs list operations
  9. 15.5.1Tuple slices
  10. 15.5.2Adding elements to a tuple
  11. 15.5.3Finding elements
  12. 15.5.4Removing elements
  13. 15.5.5Looping
  14. 15.5.6In case of emergency
  15. 15.6Pros and cons of immutability

16Exceptions

  1. 16.1Program errors
  2. 16.2What are exceptions
  3. 16.3Exception types
  4. 16.3.1ImportError
  5. 16.3.2IndexError
  6. 16.3.3TypeError
  7. 16.3.4ValueError
  8. 16.3.5ZeroDivisionError
  9. 16.4Catching exceptions
  10. 16.4.1Only catch exceptions you can handle
  11. 16.4.2Accessing the exception message
  12. 16.5Using else with exceptions
  13. 16.6Using finally with exceptions
  14. 16.7Throwing exceptions

17Working with files

  1. 17.1Using files
  2. 17.1.1Opening the file
  3. 17.1.2Reading and writing data
  4. 17.1.3Closing the file
  5. 17.2Reading data
  6. 17.2.1Code for reading a file
  7. 17.2.2Reading lines from a file
  8. 17.2.3Looping over the lines in a file
  9. 17.3Writing data
  10. 17.3.1Code to write a file
  11. 17.3.2The write function
  12. 17.4Using with statements
  13. 17.5CSV data
  14. 17.5.1Reading CSV data
  15. 17.5.2Trying the code
  16. 17.5.3Formatting the output
  17. 17.5.4Writing CSV data
  18. 17.5.5Understanding the code
  19. 17.5.6Trying the code

18More books from this author

  1. 18.1Numpy Recipes
  2. 18.2Computer Graphics in Python with Pycairo
  3. 18.3Functional Programming in Python

Get the free sample chapters

Click the buttons to get the free sample in PDF or EPUB, or read the sample online here

The Leanpub 60 Day 100% Happiness Guarantee

Within 60 days of purchase you can get a 100% refund on any Leanpub purchase, in two clicks.

Now, this is technically risky for us, since you'll have the book or course files either way. But we're so confident in our products and services, and in our authors and readers, that we're happy to offer a full money back guarantee for everything we sell.

You can only find out how good something is by trying it, and because of our 100% money back guarantee there's literally no risk to do so!

So, there's no reason not to click the Add to Cart button, is there?

See full terms...

Earn $8 on a $10 Purchase, and $16 on a $20 Purchase

We pay 80% royalties on purchases of $7.99 or more, and 80% royalties minus a 50 cent flat fee on purchases between $0.99 and $7.98. You earn $8 on a $10 sale, and $16 on a $20 sale. So, if we sell 5000 non-refunded copies of your book for $20, you'll earn $80,000.

(Yes, some authors have already earned much more than that on Leanpub.)

In fact, authors have earned over $14 million writing, publishing and selling on Leanpub.

Learn more about writing on Leanpub

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) and EPUB (for phones, tablets and 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

Write and Publish on Leanpub

You can use Leanpub to easily write, publish and sell in-progress and completed ebooks and online courses!

Leanpub is a powerful platform for serious authors, combining a simple, elegant writing and publishing workflow with a store focused on selling in-progress ebooks.

Leanpub is a magical typewriter for authors: just write in plain text, and to publish your ebook, just click a button. (Or, if you are producing your ebook your own way, you can even upload your own PDF and/or EPUB files and then publish with one click!) It really is that easy.

Learn more about writing on Leanpub