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
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.
See full terms
80% Royalties. Earn $16 on a $20 book.
We pay 80% royalties. That's not a typo: you earn $16 on a $20 sale. If we sell 5000 non-refunded copies of your book or course for $20, you'll earn $80,000.
(Yes, some authors have already earned much more than that on Leanpub.)
In fact, authors have earned$12,307,240writing, 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
Top Books
OpenIntro Statistics
David Diez, Christopher Barr, Mine Cetinkaya-Rundel, and OpenIntroA complete foundation for Statistics, also serving as a foundation for Data Science.
Leanpub revenue supports OpenIntro (US-based nonprofit) so we can provide free desk copies to teachers interested in using OpenIntro Statistics in the classroom and expand the project to support free textbooks in other subjects.
More resources: openintro.org.
Personal Finance
Jason AndersonThis textbook provides an in-depth analysis on personal finance that is both practical and straightforward in its approach. It has been written in such a way that the readers can gain knowledge without getting overwhelmed by the technical terms. Suitable for both beginners and advanced learners.
Getting to Know IntelliJ IDEA
Trisha Gee and Helen ScottIf we treat our IDE as a text editor, we are doing ourselves a disservice. Using a combination of tutorials and a questions-and-answers approach, Getting to Know IntelliJ IDEA will help you find ways to use IntelliJ IDEA that enable you to work comfortably and productively as a professional developer.
C++20 - The Complete Guide
Nicolai M. JosuttisAll new language and library features of C++20 (for those who know previous C++ versions).
The book presents all new language and library features of C++20. Learn how this impacts day-to-day programming, to benefit in practice, to combine new features, and to avoid all new traps.
Buy early, pay less, free updates.
Other books:
Mastering STM32 - Second Edition
Carmine NovielloWith more than 1200 microcontrollers, STM32 is probably the most complete ARM Cortex-M platform on the market. This book aims to be the most complete guide around introducing the reader to this exciting MCU portfolio from ST Microelectronics and its official CubeHAL and STM32CubeIDE development environment.
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.
Machine Learning Q and AI
Sebastian Raschka, PhDHave you recently completed a machine learning or deep learning course and wondered what to learn next? With 30 questions and answers on key concepts in machine learning and AI, this book provides bite-sized bits of knowledge for your journey to becoming a machine learning expert.
Stats One
William FooteThe Rails 7 Way
Obie Fernandez, Lucas Dohmen, and Tom Henrik AadlandThe Rails™ 7 Way is the comprehensive, authoritative reference guide for professionals delivering production-quality code using modern Ruby on Rails. It illuminates the entire Rails 7 API, its most powerful idioms, design approaches, and libraries. Building on the previous editions, this edition has been heavily refactored and updated.
Gradual Modularization for Ruby and Rails
Stephan HagemannGet yourself a new tool to manage your Rails application and your growing engineering organization! Prevent the ball-of-mud (and fix it!). Go for microservices or SOA if it makes sense not just because you don't have any other tool. Do all this through a low-overhead tool: packages. Enable better conversations to make practical changes today.
Top Bundles
- #1
Software Architecture
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
CCIE Service Provider Ultimate Study Bundle
2 Books
Piotr Jablonski, Lukasz Bromirski, and Nick Russo have joined forces to deliver the only CCIE Service Provider training resource you'll ever need. This bundle contains a detailed and challenging collection of workbook labs, plus an extensively detailed technical reference guide. All of us have earned the CCIE Service Provider certification... - #3
1500 QUIZ COMMENTATI (3 libri)
3 Books
Tre libri dei QUIZ MMG Commentati al prezzo di DUE! I QUIZ dei concorsi ufficiali di Medicina Generale relativi agli anni: 2000-2001-2003-2012-2013-2014-2015-2016-2017-2018-2019-2020-2021 +100 inediti Raccolti in unico bundle per aiutarvi nello studio e nella preparazione al concorso. All'interno di ogni libro i quiz sono stati suddivisi per... - #4
Pattern-Oriented Memory Forensics and Malware Detection
2 Books
This training bundle for security engineers and researchers, malware and memory forensics analysts includes two accelerated training courses for Windows memory dump analysis using WinDbg. It is also useful for technical support and escalation engineers who analyze memory dumps from complex software environments and need to check for possible... - #5
Practical FP in Scala + Functional event-driven architecture
2 Books
Practical FP in Scala (A hands-on approach) & Functional event-driven architecture, aka FEDA, (Powered by Scala 3), together as a bundle! The content of PFP in Scala is a requirement to understand FEDA so why not take advantage of this bundle!? - #6
Modern C++ Collection
3 Books
Get All about Modern C++C++ Standard Library, including C++20Concurrency with Modern C++, including C++20C++20Each book has about 200 complete code examples. Updates are included. When I update one of the books, you immediately get the updated bundle. You can expect significant updates to each new C++ standard (C++23, C++26, .. ) and also... - #7
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é... - #9
Development and Deployment of Multiplayer Online Games, Part ARCH. Architecture (Vol. I-III)
3 Books
What's the Big Idea? The idea behind this book is to summarize the body of knowledge that already exists on multiplayer games but is not available in one single place.And quite a fewof the issues discussed within this series (planned as three nine volumes ~300 pages each), while known in the industry, have not been published at all (except for... - #10
Growing Agile: The Complete Coach's Guide
7 Books
Growing Agile: Coach's Guide Series This bundle provides a collection of training and workshop plans for a variety of agile topics. The series is aimed at agile coaches, trainers and ScrumMasters who often find themselves needing to help teams understand agile concepts. Each book in the series provides the plans, slides, handouts and activity...