Python All-in-One For Dummies

Python All-in-One For Dummies

by John C. Shovic, Alan Simpson


View All Available Formats & Editions
Choose Expedited Shipping at checkout for delivery by Friday, December 3


Your one-stop resource on all things Python

Thanks to its flexibility, Python has grown to become one of the most popular programming languages in the world. Developers use Python in app development, web development, data science, machine learning, and even in coding education classes.

There's almost no type of project that Python can't make better. From creating apps to building complex websites to sorting big data, Python provides a way to get the work done. Python All-in-One For Dummies offers a starting point for those new to coding by explaining the basics of Python and demonstrating how it’s used in a variety of applications.

  • Covers the basics of the language
  • Explains its syntax through application in high-profile industries
  • Shows how Python can be applied to projects in enterprise
  • Delves into major undertakings including artificial intelligence, physical computing, machine learning, robotics and data analysis

This book is perfect for anyone new to coding as well as experienced coders interested in adding Python to their toolbox.

Related collections and offers

Product Details

ISBN-13: 9781119557593
Publisher: Wiley
Publication date: 05/07/2019
Series: For Dummies Books
Pages: 704
Sales rank: 143,963
Product dimensions: 7.30(w) x 9.20(h) x 1.50(d)

About the Author

John Shovic is a computer science faculty member at the University of Idaho. Alan Simpson is a web development professional and prolific tech author with over 100 publications to his credit.

Table of Contents

Introduction 1

About This Book 1

Foolish Assumptions 2

Icons Used in This Book 2

Beyond the Book 3

Where to Go from Here 3

Book 1: Getting Started with Python 5

Chapter 1: Starting with Python 7

Why Python is Hot 8

Choosing the Right Python 9

Tools for Success 11

An excellent, free learning environment 12

Installing Anaconda and VS Code 13

Writing Python in VS Code 17

Choosing your Python interpreter 19

Writing some Python code 20

Getting back to VS Code Python 21

Using Jupyter Notebook for Coding 21

Chapter 2: Interactive Mode, Getting Help, Writing Apps 27

Using Python Interactive Mode 27

Opening Terminal 28

Getting your Python version 28

Going into the Python Interpreter 30

Entering commands 30

Using Python’s built-in help 31

Exiting interactive help 33

Searching for specific help topics online 33

Lots of free cheat sheets 34

Creating a Python Development Workspace 34

Creating a Folder for your Python Code 37

Typing, Editing, and Debugging Python Code 39

Writing Python code 40

Saving your code 41

Running Python in VS Code 41

Simple debugging 42

The VS Code Python debugger 43

Writing Code in a Jupyter Notebook 45

Creating a folder for Jupyter Notebook 45

Creating and saving a Jupyter notebook 46

Typing and running code in a notebook 46

Adding some Markdown text 47

Saving and opening notebooks 48

Chapter 3: Python Elements and Syntax 49

The Zen of Python 49

Object-Oriented Programming 53

Indentations Count, Big Time 54

Using Python Modules 56

Syntax for importing modules 58

Using an alias with modules 59

Chapter 4: Building Your First Python Application 61

Open the Python App File 62

Typing and Using Python Comments 63

Understanding Python Data Types 64

Numbers 65

Words (strings) 66

True/false Booleans 68

Doing Work with Python Operators 69

Arithmetic operators 69

Comparison operators 70

Boolean operators 71

Creating and Using Variables 72

Creating valid variable names 73

Creating variables in code 74

Manipulating variables 75

Saving your work 76

Running your Python app in VS Code 76

What Syntax is and Why It Matters 78

Putting Code Together 82

Book 2: Understanding Python Building Blocks 83

Chapter 1: Working with Numbers, Text, and Dates 85

Calculating Numbers with Functions 86

Still More Math Functions 88

Formatting Numbers 91

Formatting with f-strings 91

Showing dollar amounts 92

Formatting percent numbers 93

Making multiline format strings 95

Formatting width and alignment 96

Grappling with Weirder Numbers 98

Binary, octal, and hexadecimal numbers 98

Complex numbers 99

Manipulating Strings 100

Concatenating strings 101

Getting the length of a string 102

Working with common string operators 102

Manipulating strings with methods 105

Uncovering Dates and Times 107

Working with dates 108

Working with times 112

Calculating timespans 114

Accounting for Time Zones 118

Working with Time Zones 120

Chapter 2: Controlling the Action 125

Main Operators for Controlling the Action 125

Making Decisions with if 126

Adding else to your if login 130

Handling multiple else’s with elif 131

Ternary operations 133

Repeating a Process with for 134

Looping through numbers in a range 134

Looping through a string 136

Looping through a list 137

Bailing out of a loop 138

Looping with continue 140

Nesting loops 140

Looping with while 141

Starting while loops over with continue 143

Breaking while loops with break 144

Chapter 3: Speeding Along with Lists and Tuples 147

Defining and Using Lists 147

Referencing list items by position 148

Looping through a list 150

Seeing whether a list contains an item 150

Getting the length of a list 151

Adding an item to the end of a list 151

Inserting an item into a list 152

Changing an item in a list 153

Combining lists 153

Removing list items 154

Clearing out a list 156

Counting how many times an item appears in a list 157

Finding an list item’s index 158

Alphabetizing and sorting lists 159

Reversing a list 161

Copying a list 162

What’s a Tuple and Who Cares? 163

Working with Sets 165

Chapter 4: Cruising Massive Data with Dictionaries 169

Creating a Data Dictionary 171

Accessing dictionary data 172

Getting the length of a dictionary 174

Seeing whether a key exists in a dictionary 175

Getting dictionary data with get() 176

Changing the value of a key 177

Adding or changing dictionary data 177

Looping through a Dictionary 179

Data Dictionary Methods 181

Copying a Dictionary 182

Deleting Dictionary Items 182

Using pop() with Data Dictionaries 184

Fun with Multi-Key Dictionaries 186

Using the mysterious fromkeys and setdefault methods 188

Nesting Dictionaries 190

Chapter 5: Wrangling Bigger Chunks of Code 193

Creating a Function 194

Commenting a Function 195

Passing Information to a Function 196

Defining optional parameters with defaults 198

Passing multiple values to a function 199

Using keyword arguments (kwargs) 200

Passing multiple values in a list 202

Passing in an arbitrary number of arguments 204

Returning Values from Functions 205

Unmasking Anonymous Functions 206

Chapter 6: Doing Python with Class 213

Mastering Classes and Objects 213

Creating a Class 216

How a Class Creates an Instance 217

Giving an Object Its Attributes 218

Creating an instance from a class 219

Changing the value of an attribute 222

Defining attributes with default values 222

Giving a Class Methods 224

Passing parameters to methods 226

Calling a class method by class name 227

Using class variables 228

Using class methods 230

Using static methods 232

Understanding Class Inheritance 234

Creating the base (main) class 236

Defining a subclass 237

Overriding a default value from a subclass 239

Adding extra parameters from a subclass 239

Calling a base class method 242

Using the same name twice 243

Chapter 7: Sidestepping Errors 247

Understanding Exceptions 247

Handling Errors Gracefully 251

Being Specific about Exceptions 252

Keeping Your App from Crashing 253

Adding an else to the Mix 255

Using try … … … except else finally 257

Raising Your Own Errors 259

Book 3: Working with Python Libraries 265

Chapter 1: Working with External Files 267

Understanding Text and Binary Files 267

Opening and Closing Files 269

Reading a File’s Contents 276

Looping through a File 277

Looping with readlines() 277

Looping with readline() 279

Appending versus overwriting files 280

Using tell() to determine the pointer location 281

Moving the pointer with seek() 283

Reading and Copying a Binary File 283

Conquering CSV Files 286

Opening a CSV file 288

Converting strings 290

Converting to integers 291

Converting to date 292

Converting to Boolean 293

Converting to floats 293

From CSV to Objects and Dictionaries 295

Importing CSV to Python objects 296

Importing CSV to Python dictionaries 299

Chapter 2: Juggling JSON Data 303

Organizing JSON Data 303

Understanding Serialization 306

Loading Data from JSON Files 307

Converting an Excel date to a JSON date 309

Looping through a keyed JSON file 310

Converting firebase timestamps to Python dates 313

Loading unkeyed JSON from a Python string 314

Loading keyed JSON from a Python string 315

Changing JSON data 316

Removing data from a dictionary 317

Dumping Python Data to JSON 318

Chapter 3: Interacting with the Internet 323

How the Web Works 323

Understanding the mysterious URL 324

Exposing the HTTP headers 325

Opening a URL from Python 327

Posting to the Web with Python 328

Scraping the Web with Python 330

Parsing part of a page 333

Storing the parsed content 333

Saving scraped data to a JSON file 335

Saving scraped data to a CSV file 336

Chapter 4: Libraries, Packages, and Modules 339

Understanding the Python Standard Library 339

Using the dir() function 340

Using the help() function 341

Exploring built-in functions 343

Exploring Python Packages 343

Importing Python Modules 345

Making Your Own Modules 348

Book 4: Using Artificial Intelligence in Python 353

Chapter 1: Exploring Artificial Intelligence 355

AI is a Collection of Techniques 356

Neural networks 356

Machine learning 359

TensorFlow — A framework for deep learning 361

Current Limitations of AI 363

Chapter 2: Building a Neural Network in Python 365

Understanding Neural Networks 366

Layers of neurons 367

Weights and biases 368

The activation function 369

Loss function 369

Building a Simple Neural Network in Python 370

The neural-net Python code 370

Using TensorFlow for the same neural network 381

Installing the TensorFlow Python library 382

Building a Python Neural Network in TensorFlow 383

Loading your data 384

Defining your neural-network model and layers 384

Compiling your model 384

Fitting and training your model 384

Breaking down the code 386

Evaluating the model 388

Changing to a three-layer neural network in TensorFlow/Keras 390

Chapter 3: Doing Machine Learning in Python 393

Learning by Looking for Solutions in All the Wrong Places 394

Classifying Clothes with Machine Learning 395

Training and Learning with TensorFlow 395

Setting Up the Software Environment for this Chapter 396

Creating a Machine-Learning Network for Detecting Clothes Types 397

Getting the data — The Fashion-MNIST dataset 398

Training the network 398

Testing our network 398

Breaking down the code 399

Results of the training and evaluation 402

Testing a single test image 402

Testing on external pictures 403

The results, round 1 405

The CNN model code 406

The results, round 2 409

Visualizing with MatPlotLib 409

Learning More Machine Learning 413

Chapter 4: Exploring More AI in Python 415

Limitations of the Raspberry Pi and AI 415

Adding Hardware AI to the Raspberry Pi 418

AI in the Cloud 420

Google cloud 421

Amazon Web Services 421

IBM cloud 422

Microsoft Azure 422

AI on a Graphics Card 423

Where to Go for More AI Fun in Python 424

Book 5: Doing Data Science with Python 427

Chapter 1: The Five Areas of Data Science 429

Working with Big, Big Data 430

Volume 430

Variety 431

Velocity 431

Managing volume, variety, and velocity 432

Cooking with Gas: The Five Step Process of Data Science 432

Capturing the data 433

Processing the data 433

Analyzing the data 434

Communicating the results 434

Maintaining the data 435

Chapter 2: Exploring Big Data with Python 437

Introducing NumPy, Pandas, and MatPlotLib 438

Doing Your First Data Science Project 440

Diamonds are a data scientist’s best friend 440

Breaking down the code 443

Visualizing the data with MatPlotLib 444

Chapter 3: Using Big Data from the Google Cloud 451

What is Big Data? 451

Understanding the Google Cloud and BigQuery 452

The Google Cloud Platform 452

BigQuery from Google 452

Computer security on the cloud 453

Signing up on Google for BigQuery 454

Reading the Medicare Big Data 454

Setting up your project and authentication 454

The first big-data code 457

Breaking down the code 460

A bit of analysis next 461

Payment percent by state 464

And now some visualization 465

Looking for the Most Polluted City in the World on an Hourly Basis 466

Book 6: Talking to Hardware with Python 469

Chapter 1: Introduction to Physical Computing 471

Physical Computing is Fun 472

What is a Raspberry Pi? 472

Making Your Computer Do Things 474

Using Small Computers to Build Projects That Do and Sense Things 474

The Raspberry Pi: A Perfect Platform for Physical Computing in Python 476

GPIO pins 477

GPIO libraries 477

The hardware for “Hello World” 478

Assembling the hardware 478

Controlling the LED with Python on the Raspberry Pi 482

But Wait, There is More 485

Chapter 2: No Soldering! Grove Connectors for Building Things 487

So What is a Grove Connector? 488

Selecting Grove Base Units 489

For the Arduino 489

Raspberry Pi Base Unit — the Pi2Grover 490

The Four Types of Grove Connectors 492

The Four Types of Grove Signals 493

Grove digital — All about those 1’s and 0’s 493

Grove analog: When 1’s and 0’s aren’t enough 494

Grove UART (or serial) — Bit by bit transmission 495

Grove I2C — Using I2C to make sense of the world 497

Using Grove Cables to Get Connected 499

Grove Patch Cables 499

Chapter 3: Sensing the World with Python: The World of I2C 505

Understanding I2C 506

Exploring I2C on the Raspberry Pi 507

Talking to I2C devices with Python 508

Reading temperature and humidity from an I2C

device using Python 511

Breaking down the program 514

A Fun Experiment for Measuring Oxygen and a Flame 517

Analog-to-digital converters (ADC) 518

The Grove oxygen sensor 519

Hooking up the oxygen experiment 520

Breaking down the code 522

Building a Dashboard on Your Phone Using Blynk and Python 525

HDC1080 temperature and humidity sensor redux 525

How to add the Blynk dashboard 527

The modified software for the Blynk app 531

Breaking down the code 533

Where to Go from Here 536

Chapter 4: Making Things Move with Python 537

Exploring Electric Motors 538

Small DC motors 538

Servo motors 539

Stepper motors 539

Controlling Motors with a Computer 540

Python and DC Motors 540

Python and running a servo motor 548

Python and making a stepper motor step 554

Book 7: Building Robots with Python 565

Chapter 1: Introduction to Robotics 567

A Robot is Not Always like a Human 567

Not Every Robot Has Arms or Wheels 568

The Wilkinson Bread-Making Robot 569

Baxter the Coffee-Making Robot 570

The Griffin Bluetooth-enabled toaster 571

Understanding the Main Parts of a Robot 572

Computers 572

Motors and actuators 573

Communications 573

Sensors 573

Programming Robots 574

Chapter 2: Building Your First Python Robot 575

Introducing the Mars Rover PiCar-B 575

What you need for the build 576

Understanding the robot components 577

Assembling the Robot 586

Calibrating your servos 588

Running tests on your rover in Python 591

Installing software for the CarPi-B Python test 591

The PiCar-B Python test code 592

Pi camera video testing 592

Chapter 3: Programming Your Robot Rover in Python 595

Building a Simple High-Level Python Interface 595

The motorForward function 596

The wheelsLeft function 596

The wheelsPercent function 596

Making a Single Move with Python 597

Functions of the RobotInterface Class 598

Front LED functions 598

Pixel strip functions 600

Ultrasonic distance sensor function 601

Main motor functions 602

Servo functions 603

General servo function 606

The Python Robot Interface Test 606

Coordinating Motor Movements with Sensors 610

Making a Python Brain for Our Robot 613

A Better Robot Brain Architecture 620

Overview of the Included Adeept Software 621

Where to Go from Here? 622

Chapter 4: Using Artificial Intelligence in Robotics 623

This Chapter’s Project: Going to the Dogs 624

Setting Up the Project 624

Machine Learning Using TensorFlow 625

The code 627

Examining the code 629

The results 632

Testing the Trained Network 633

The code 634

Explaining the code 636

The results 637

Taking Cats and Dogs to Our Robot 640

The code 640

How it works 643

The results 643

Other Things You Can Do with AI Techniques and the Robot 645

Cat/Not Cat 645

Santa/Not Santa 646

Follow the ball 646

Using Alexa to control your robot 646

AI and the Future of Robotics 646

Index 647

Customer Reviews