Python All-in-One For Dummies

Python All-in-One For Dummies

by John C. Shovic, Alan Simpson

NOOK Book(eBook)

View All Available Formats & Editions

Available on Compatible NOOK Devices and the free NOOK Apps.
WANT A NOOK?  Explore Now
LEND ME® See Details


The one-stop resource for all your Python queries

Powerful and flexible, Python is one of the most popular programming languages in the world. It's got all the right stuff for the software driving the cutting-edge of the development world—machine learning, robotics, artificial intelligence, data science, etc. The good news is that it’s also pretty straightforward to learn, with a simplified syntax, natural-language flow, and an amazingly supportive user community. The latest edition of Python All-in-One For Dummies gives you an inside look at the exciting possibilities offered in the Python world and provides a springboard to launch yourself into wherever you want your coding career to take you.

These 7 straightforward and friendly mini-books assume the reader is a beginning programmer, and cover everything from the basic elements of Python code to introductions to the specific applications where you'll use it. Intended as a hands-on reference, the focus is on practice over theory, providing you with examples to follow as well as code for you to copy and start modifying in the "real world"—helping you get up and running in your area of interest almost right away. This means you'll be finishing off your first app or building and remote-controlling your own robot much faster than you can believe.

  • Get a thorough grounding in the language basics
  • Learn how the syntax is applied in high-profile industries
  • Apply Python to projects in enterprise
  • Find out how Python can get you into hot careers in AI, big data, and more

Whether you're a newbie coder or just want to add Python to your magic box of tricks, this is the perfect, practical introduction—and one you'll return to as you grow your career.

Related collections and offers

Product Details

ISBN-13: 9781119787624
Publisher: Wiley
Publication date: 03/29/2021
Format: NOOK Book
Pages: 720
Sales rank: 216,811
File size: 43 MB
Note: This product may take a few minutes to download.

About the Author

John Shovic, PhD, is a computer science faculty member at the University of Idaho specializing in robotics and artificial intelligence.

Alan Simpson is a web development professional who has published more than 100 articles and books on technology.

Table of Contents

Introduction 1

About This Book 1

Foolish Assumptions 2

What to Buy 2

Icons Used in This Book 4

Beyond the Book 4

Where to Go from Here 5

Book 1: Getting Started 7

Chapter 1: Starting with Python 9

Why Python is Hot 10

Choosing the Right Python 11

Tools for Success 13

Introducing Anaconda and VS Code 14

Installing Anaconda and VS Code 15

Writing Python in VS Code 19

Choosing your Python interpreter 21

Writing some Python code 22

Getting back to VS Code Python 23

Using Jupyter Notebook for Coding 23

Chapter 2: Interactive Mode, Getting Help, and Writing Apps 29

Using Python’s Interactive Mode 29

Opening Terminal 30

Getting your Python version 32

Going into the Python Interpreter 32

Entering commands 33

Using Python’s built-in help 33

Exiting interactive help 35

Searching for specific help topics online 36

Lots of free cheat sheets 36

Creating a Python Development Workspace 37

Creating a Folder for Your Python Code 39

Typing, Editing, and Debugging Python Code 41

Writing Python code 42

Saving your code 43

Running Python in VS Code 44

Learning simple debugging 45

Using the VS Code Python debugger 46

Writing Code in a Jupyter Notebook 47

Creating a folder for Jupyter Notebook 47

Creating and saving a Jupyter notebook 48

Typing and running code in a notebook 49

Adding Markdown text 49

Saving and opening notebooks 51

Chapter 3: Python Elements and Syntax 53

The Zen of Python 53

Introducing Object-Oriented Programming 56

Discovering Why Indentations Count, Big Time 57

Using Python Modules 59

Understanding the syntax for importing modules 61

Using an alias with modules 62

Chapter 4: Building Your First Python Application 63

Opening the Python App File 64

Typing and Using Python Comments 64

Understanding Python Data Types 66

Numbers 67

Words (strings) 68

Booleans 70

Working with Python Operators 71

Arithmetic operators 71

Comparison operators 72

Boolean operators 73

Creating and Using Variables 74

Creating valid variable names 75

Creating variables in code 75

Manipulating variables 76

Saving your work 78

Running your Python app in VS Code 78

Understanding What Syntax is and Why It Matters 79

Putting Code Together 84

Book 2: Understanding Python Building Blocks 85

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

Calculating Numbers with Functions 87

Still More Math Functions 90

Formatting Numbers 93

Formatting with f-strings 93

Showing dollar amounts 94

Formatting percent numbers 95

Making multiline format strings 97

Formatting width and alignment 98

Grappling with Weirder Numbers 100

Binary, octal, and hexadecimal numbers 100

Complex numbers 101

Manipulating Strings 103

Concatenating strings 103

Getting the length of a string 104

Working with common string operators 105

Manipulating strings with methods 107

Uncovering Dates and Times 110

Working with dates 110

Working with times 114

Calculating timespans 116

Accounting for Time Zones 120

Working with Time Zones 122

Chapter 2: Controlling the Action 127

Main Operators for Controlling the Action 127

Making Decisions with if 129

Adding else to your if logic 132

Handling multiple else statements with elif 133

Ternary operations 135

Repeating a Process with for 136

Looping through numbers in a range 136

Looping through a string 138

Looping through a list 139

Bailing out of a loop 140

Looping with continue 141

Nesting loops 142

Looping with while 143

Starting while loops over with continue 145

Breaking while loops with break 146

Chapter 3: Speeding Along with Lists and Tuples 149

Defining and Using Lists 149

Referencing list items by position 150

Looping through a list 151

Seeing whether a list contains an item 152

Getting the length of a list 153

Adding an item to the end of a list 153

Inserting an item into a list 154

Changing an item in a list 155

Combining lists 155

Removing list items 156

Clearing out a list 158

Counting how many times an item appears in a list 159

Finding an list item’s index 160

Alphabetizing and sorting lists 161

Reversing a list 164

Copying a list 164

What’s a Tuple and Who Cares? 165

Working with Sets 167

Chapter 4: Cruising Massive Data with Dictionaries 171

Understanding Data Dictionaries 172

Creating a Data Dictionary 174

Accessing dictionary data 175

Getting the length of a dictionary 177

Seeing whether a key exists in a dictionary 177

Getting dictionary data with get() 178

Changing the value of a key 179

Adding or changing dictionary data 180

Looping through a Dictionary 182

Data Dictionary Methods 183

Copying a Dictionary 184

Deleting Dictionary Items 185

Having Fun with Multi-Key Dictionaries 188

Using the mysterious fromkeys and setdefault methods 190

Nesting dictionaries 193

Chapter 5: Wrangling Bigger Chunks of Code 195

Creating a Function 196

Commenting a Function 197

Passing Information to a Function 198

Defining optional parameters with defaults 200

Passing multiple values to a function 201

Using keyword arguments (kwargs) 203

Passing multiple values in a list 205

Passing in an arbitrary number of arguments 207

Returning Values from Functions 208

Unmasking Anonymous Functions 209

Chapter 6: Doing Python with Class 217

Mastering Classes and Objects 217

Creating a Class 220

Creating an Instance from a Class 221

Giving an Object Its Attributes 222

Creating an instance from a class 223

Changing the value of an attribute 226

Defining attributes with default values 227

Giving a Class Methods 228

Passing parameters to methods 230

Calling a class method by class name 231

Using class variables 232

Using class methods 234

Using static methods 236

Understanding Class Inheritance 238

Creating the base (main) class 240

Defining a subclass 241

Overriding a default value from a subclass 243

Adding extra parameters from a subclass 243

Calling a base class method 246

Using the same name twice 247

Chapter 7: Sidestepping Errors 251

Understanding Exceptions 252

Handling Errors Gracefully 254

Being Specific about Exceptions 255

Keeping Your App from Crashing 257

Adding an else to the Mix 259

Using try except else finally 261

Raising Your Own Exceptions 263

Book 3: Working with Libraries 269

Chapter 1: Working with External Files 271

Understanding Text and Binary Files 271

Opening and Closing Files 273

Reading a File’s Contents 279

Looping through a File 281

Looping with readlines() 281

Looping with readline() 283

Appending versus overwriting files 284

Using tell() to determine the pointer location 285

Moving the pointer with seek() 286

Reading and Copying a Binary File 287

Conquering CSV Files 290

Opening a CSV file 292

Converting strings 293

Converting to integers 295

Converting to date 295

Converting to Boolean 297

Converting to floats 297

Converting from CSV to Objects and Dictionaries 299

Importing CSV to Python objects 300

Importing CSV to Python dictionaries 303

Chapter 2: Juggling JSON Data 307

Organizing JSON Data 307

Understanding Serialization 310

Loading Data from JSON Files 312

Converting an Excel date to a JSON date 313

Looping through a keyed JSON file 314

Converting Firebase timestamps to Python dates 317

Loading unkeyed JSON from a Python string 318

Loading keyed JSON from a Python string 319

Changing JSON data 320

Removing data from a dictionary 321

Dumping Python Data to JSON 322

Chapter 3: Interacting with the Internet 327

Seeing How the Web Works 327

Understanding the mysterious URL 328

Exposing the HTTP headers 329

Opening a URL from Python 331

Posting to the web with Python 333

Scraping the web with Python 334

Parsing part of a page 337

Storing the parsed content 337

Saving scraped data to a JSON file 340

Saving scraped data to a CSV file 341

Chapter 4: Libraries, Packages, and Modules 343

Understanding the Python Standard Library 343

Using the dir() function 344

Using the help() function 345

Exploring built-in functions 347

Exploring Python Packages 347

Importing Python Modules 349

Making Your Own Modules 352

Book 4: Using Artificial Intelligence 357

Chapter 1: Exploring Artificial Intelligence 359

AI is a Collection of Techniques 360

Neural networks 360

Machine learning 365

TensorFlow — A framework for deep learning 366

Current Limitations of AI 367

Chapter 2: Building a Neural Network 369

Understanding Neural Networks 370

Layers of neurons 371

Weights and biases 372

The activation function 373

Loss function 373

Building a Simple Neural Network in Python 374

The neural-net Python code 375

Using TensorFlow for the same neural network 385

Installing the TensorFlow Python library 386

Building a Python Neural Network in TensorFlow 387

Loading your data 388

Defining your neural-network model and layers 388

Compiling your model 388

Fitting and training your model 388

Evaluating the model 388

Breaking down the code 390

Checking the results 392

Changing to a three-layer neural network in TensorFlow and Keras 395

Chapter 3: Doing Machine Learning 399

Learning by Looking for Solutions in All the Wrong Places 400

Creating a Machine-Learning Network for Detecting Clothes Types 401

Setting up the software environment 402

Getting the data from the Fashion-MNIST dataset 403

Training the network 404

Testing our network 404

Breaking down the code 405

Results of the training and evaluation 407

Testing a single test image 408

Testing on external pictures 409

The results, round 1 411

The CNN model code 412

The results, round 2 414

Visualizing with MatPlotLib 415

Learning More Machine Learning 419

Chapter 4: Exploring AI 421

Limitations of the Raspberry Pi and AI 421

Adding Hardware AI to the Raspberry Pi 423

AI in the Cloud 425

Google Cloud 427

Amazon Web Services 427

IBM Cloud 427

Microsoft Azure 428

AI on a Graphics Card 428

Where to Go for More AI Fun in Python 430

Book 5: Doing Data Science 433

Chapter 1: Understanding the Five Areas of Data Science 435

Working with Big, Big Data 436

Volume 436

Variety 437

Velocity 437

Managing volume, variety, and velocity 437

Cooking with Gas: The Five-Step Process of Data Science 438

Capturing the data 438

Processing the data 438

Analyzing the data 439

Communicating the results 440

Maintaining the data 440

Chapter 2: Exploring Big Data 441

Introducing NumPy, Pandas, and MatPlotLib 442

NumPy 442

Pandas 443

MatPlotLib 444

Doing Your First Data Science Project 444

Diamonds are a data scientist’s best friend 444

Breaking down the code 447

Visualizing the data with MatPlotLib 449

Chapter 3: Using Big Data from Google Cloud 457

What is Big Data? 457

Understanding Google Cloud and BigQuery 458

Google Cloud Platform 458

BigQuery from Google 458

Computer security on the cloud 459

Signing up for BigQuery 460

Reading the Medicare Big Data 460

Setting up your project and authentication 460

The first big-data code 463

Breaking down the code 466

Doing a bit of analysis 467

Payment percent by state 470

Now some visualization 471

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

Book 6: Talking to Hardware 475

Chapter 1: Introducing Physical Computing 477

Physical Computing is Fun 478

What is a Raspberry Pi? 478

Building Projects That Move and Sense the Environment 480

Sensing the Environment with the Raspberry Pi 482

GPIO pins 482

GPIO libraries 482

Buying and assembling the hardware for “Hello World” 483

Controlling an LED with Python 487

But Wait, There’s More 489

Chapter 2: No Soldering! Using Grove Connectors for Building 493

Working with the Grove System 494

Selecting a Grove base unit 494

Error-proofing with a Grove connector 496

Grove Connectors 498

Grove digital — All about those 1s and 0s 498

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

Grove UART (or serial) — bit-by-bit transmission 500

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

Connecting with Grove Cables 503

An example of the power of the patch! 505

Second example: The Adafruit Ultimate GPS 506

Chapter 3: Sensing the World 509

Understanding I2C 509

Enabling I2C on the Raspberry Pi 511

The hardware for reading temperature and humidity 512

Reading temperature and humidity from an I2C device using Python 515

Breaking down the program 518

Measuring Oxygen and a Flame 521

Analog-to-digital converters (ADC) 522

The Grove oxygen sensor 522

Hooking up the oxygen experiment 524

Breaking down the code 527

Interpreting the results 528

Building a Dashboard on Your Phone with Blynk 530

HDC1080 temperature and humidity sensor redux 530

Adding the Blynk dashboard 531

The modified software for the Blynk app 534

Breaking down the code 536

Where to Go from Here 539

Chapter 4: Making Things Move 541

Exploring Electric Motors 541

Small DC motors 542

Servo motors 543

Stepper motors 543

Controlling a DC Motor 544

Grove I2C motor driver 545

Python DC motor software 548

Running a Servo Motor 551

Python servo software 555

Breaking down the code 556

Making a Stepper Motor Step 558

Python stepper software 566

Breaking down the code 567

Book 7: Building Robots 569

Chapter 1: Introducing Robotics 571

A Robot is Not Always Like a Human 571

Not Every Robot Has Arms or Wheels 572

The Wilkinson bread-making robot 573

Baxter, the coffee-making robot 574

The Griffin Bluetooth-enabled toaster 575

Understanding the Main Parts of a Robot 576

Computers 576

Motors and actuators 577

Communications 577

Sensors 577

Programming Robots 578

Chapter 2: Building Your First Python Robot 579

Introducing the Mars Rover PiCar-B 580

What you need for the build 580

Understanding the robot components 581

Assembling the Robot 590

Testing Your Robot 592

Calibrating your servos 592

Preparing for running tests on your rover in Python 595

Installing software for the PiCar-B Python test 595

The PiCar-B Python test code 596

Pi camera video testing 597

Chapter 3: Programming Your Robot Rover 601

Building a Simple, High-Level Python Interface 601

The motorForward() function 602

The wheelsLeft function() 602

The wheelsPercent function() 603

Making a Single Move with Python 603

Functions of the RobotInterface Class 604

Front LED functions 605

Pixel strip functions 606

Ultrasonic distance sensor function 608

Main motor functions 608

Servo functions 609

General servo function 613

The Python Robot Interface Test 613

Coordinating Motor Movements with Sensors 617

Making a Python Brain for Our Robot 621

Overview of the Included Adeept Software 628

Where to Go from Here 629

Chapter 4: Using Artificial Intelligence in Robotics 631

This Chapter’s Projects: Going to the Dogs 632

Setting Up the First Project 632

Machine Learning Using TensorFlow 633

The code 635

How the code works 637

The results 640

Testing the Trained Network 642

The code 642

How the code works 644

The results 646

Taking Cats and Dogs to Our Robot 648

The code 649

How it works 652

The results 652

Setting Up the Second Project 654

The Python Program 655

The structure of the program 656

The ultrasonic thread 656

The video display thread 657

The OpenCV frame analyzer thread 657

The Main Program 661

The program’s configuration 661

Setting the ball’s color 662

Chasing the ball 664

Program notes 664

AI and the Future of Robotics 666

Index 667

Customer Reviews