21,99 €
Your first step toward thinking like a coder
Coding For Dummies is one of the most popular get-started guides for new coders. This all- new edition demystifies essential coding concepts before diving into building simple apps in Python and JavaScript. For anyone brand-new to writing code, this book is an ideal first step, because you'll get an overview of how coding works, the essential tools of coding, and how to apply coding in different ways. Plus, you'll get insight into how new tools like AI can quickly boost your coding know-how. Enter the world of code with this easy-to-follow Dummies guide!
Looking for a foundation in the basics of coding, plus simple tasks to help you get started? Coding For Dummies is the book for you.
Sie lesen das E-Book in den Legimi-Apps auf:
Seitenzahl: 429
Veröffentlichungsjahr: 2025
Cover
Table of Contents
Title Page
Copyright
Introduction
About This Book
Foolish Assumptions
Icons Used in This Book
Beyond the Book
Part 1: Coding Basics
Chapter 1: What Is Coding?
Programming: Making a Computer Do Your Bidding
What Is a Programming Language?
The Role of Programming Languages
Understanding How Code Is Written and Executed
Why Learn to Code? Let Me Count the Ways
Real-World Uses of Coding
Chapter 2: Coding Concepts You Need to Know
Storing Stuff in Variables
Dealing with Data Types
Constructing Expressions
Making Decisions with Conditionals
Automating Repetitive Tasks with Loops
Organizing Code into Functions
Introducing Objects
Documenting Code with Comments
Debugging Code and Handling Errors
Chapter 3: Getting to Know Some Programming Languages
Ranking Programming Languages
Assembly: Close to the Machine
Python: No Experience Required
JavaScript: The Glue that Binds the Web
Sneaking a Peek at a Few Other Popular Languages
Grokking the Difference between Interpreted and Compiled Languages
Part 2: Learning Python: The Beginner-Friendly Language
Chapter 4: Getting Started with Python
Getting to the Command Line
Installing Python
Running the Python Interpreter
Running Your First Python Program
Chapter 5: Getting Comfy with Some Python Fundamentals
Declaring Variables
Exploring Python Data Types
Constructing Expressions
Messing Around with Strings
Chapter 6: Storing Data and Controlling Your Code
Storing Stuff in Lists
More Ways to Store Stuff: Tuples and Dictionaries
Conditionals in Python
Python Loops
Adding Comments to Your Code
Example: Build Your Own Survey Bot
Chapter 7: Reusing Code
Making Your Code More Efficient with Functions
Getting the Hang of Variable Scope
Avoiding Wheel Reinvention with Modules
Reuse Heaven: Installing External Libraries
Reusing Data by Writing and Reading Files
Example: Building a Quotations Archive
Chapter 8: Expanding Your Python Skills
Waxing Pythonic
Comprehending List Comprehensions
Introducing Object-Oriented Programming (OOP)
Working with APIs
Handling Program Errors
Debugging Your Code
Example: Cat Fact Cards
Chapter 9: Building Some Useful Python Projects
Project 1: Anagram Guessing Game
Project 2: Text Analyzer
Part 3: Learning JavaScript: The Language of the Web
Chapter 10: Getting Your Feet Wet with JavaScript
JavaScript: Controlling the Machine
What You Need to Get Started
Basic Script Construction
Adding Comments to Your Code
Creating External JavaScript Files
Getting to Know the Console
Chapter 11: Getting the Hang of a Few JavaScript Fundamentals
I Do Declare: Variables in JavaScript
Code Looping in JavaScript
Harnessing the Power of Functions
Getting the Hang of Variable Scope
Chapter 12: Tackling Some JavaScript Objects
Pulling Strings
Dealing with Dates and Times
Working with Numbers: The Math Object
Working with Arrays
Chapter 13: Unleashing JavaScript in the Browser
Getting to Know the Document Object Model
Specifying Elements
Traversing the DOM
Manipulating Elements
Modifying CSS with JavaScript
Building Reactive Pages with Events
Chapter 14: Debugging JavaScript
Examining Your Debugging Tools
Debugging with the Console Window
Pausing Your Code
Stepping Through Your Code
Chapter 15: Putting JavaScript to Work
Project 1: A Photo Gallery
Project 2: Get a Random Quotation from an API
Part 4: The Part of Tens
Chapter 16: Ten Things to Know about AI and Coding
Understanding How You Can Use AI
Learning What AI Can and Can’t Do
Crafting Effective Prompts
Learning to Code Using AI
Avoiding AI Dependency
Reviewing the Major AI Coding Tools
Vibe Coding Is Fun
Best Practices for Integrating AI
Avoiding Common Mistakes
Understanding Code Before Using It
Chapter 17: Ten Vital HTML Tags and CSS Properties
Ten HTML Tags You Need to Know
Ten CSS Properties to Memorize
Ten CSS Selectors to Swoon Over
Index
About the Author
Connect with Dummies
End User License Agreement
Chapter 2
TABLE 2-1 Basic Arithmetic Operators
TABLE 2-2 Arithmetic Assignment Operators
TABLE 2-3 Comparison Operators
TABLE 2-4 Logical Operators
Chapter 4
TABLE 4-1 Terminal Keyboard Shortcuts
Chapter 5
TABLE 5-1 Python's Math Operators
TABLE 5-2 Python Arithmetic Assignment Operators
TABLE 5-3 Python's Comparison Operators
TABLE 5-4 Python’s Logical Operators
TABLE 5-5 Useful String Methods
Chapter 8
TABLE 8-1 Common Python Error Types
Chapter 12
TABLE 12-1 String Object Methods for Searching for Substrings
TABLE 12-2 String Object Methods for Extracting Substrings
TABLE 12-3 Arguments Associated with the Date Object
TABLE 12-4 Date Object Methods That Extract Date Values
TABLE 12-5 Date Object Methods That Set Date Values
TABLE 12-6 Some Properties of the Math Object
TABLE 12-7 Some Methods of the Math Object
TABLE 12-8 Some Methods of the Array Object
Chapter 4
FIGURE 4-1: The Windows Terminal app running Windows PowerShell.
FIGURE 4-2: The macOS Terminal app.
FIGURE 4-3: The Python site is smart enough to detect that your PC is running W...
FIGURE 4-4: The Python Setup dialog appears when you run the installer.
FIGURE 4-5: The Python site is clever enough to detect that you’re running macO...
FIGURE 4-6: Running Python from the Windows Start menu.
FIGURE 4-7: The Python interpreter waiting patiently for its next command in Wi...
FIGURE 4-8: The Python interpreter at your beck and call in macOS Terminal.
FIGURE 4-9: Running the example program one statement at a time in the REPL.
FIGURE 4-10: The example Python program saved to a
.py
file in the VS Code edit...
Chapter 9
FIGURE 9-1: A histogram of sentence lengths generated by Matplotlib’s
pyplot()
...
Chapter 10
FIGURE 10-1: The HTML viewer, such as Chrome’s Elements tab, enables you to ins...
FIGURE 10-2: A message displayed in the Chrome web browser’s console.
Chapter 13
FIGURE 13-1: The web page code as a hierarchy.
FIGURE 13-2: This code uses the
add()
method to add the class named
my-class
to...
FIGURE 13-3: The
click
event callback function adds some HTML and text to the
d
...
FIGURE 13-4: Type a key in the input box, and JavaScript displays the numeric c...
Chapter 14
FIGURE 14-1: The HTML viewer, such as Chrome’s Elements tab, enables you to ins...
FIGURE 14-2: In break mode, the web browser displays its debugging tool and hig...
FIGURE 14-3: In the browser's debugging tool, click a line number to set a brea...
FIGURE 14-4: In break mode, hover the mouse pointer over a variable name to dis...
Chapter 15
FIGURE 15-1: The interface for this project.
Cover
Table of Contents
Title Page
Copyright
Begin Reading
Index
About the Author
i
ii
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
329
330
331
Coding For Dummies®, All New Edition
Published by: John Wiley & Sons, Inc., 111 River Street, Hoboken, NJ 07030-5774, www.wiley.com
Copyright © 2026 by John Wiley & Sons, Inc. All rights reserved, including rights for text and data mining and training of artificial technologies or similar technologies.
Media and software compilation copyright © 2026 by John Wiley & Sons, Inc. All rights reserved, including rights for text and data mining and training of artificial technologies or similar technologies.
Published simultaneously in Canada
No part of this publication may be reproduced, stored in a retrieval system or transmitted in any form or by any means, electronic, mechanical, photocopying, recording, scanning or otherwise, except as permitted under Sections 107 or 108 of the 1976 United States Copyright Act, without the prior written permission of the Publisher or authorization through payment of the appropriate per-copy fee to the Copyright Clearance Center, Inc., 222 Rosewood Drive, Danvers, MA 01923, (978) 750-8400, fax (978) 750-4470, or on the web at www.copyright.com. Requests to the Publisher for permission should be addressed to the Permissions Department, John Wiley & Sons, Inc., 111 River Street, Hoboken, NJ 07030, (201) 748-6011, fax (201) 748-6008, or online at http://www.wiley.com/go/permissions.
The manufacturer’s authorized representative according to the EU General Product Safety Regulation is Wiley-VCH GmbH, Boschstr. 12, 69469 Weinheim, Germany, e-mail: [email protected].
Trademarks: Wiley, For Dummies, the Dummies Man logo, Dummies.com, Making Everything Easier, and related trade dress are trademarks or registered trademarks of John Wiley & Sons, Inc. and may not be used without written permission. All other trademarks are the property of their respective owners. John Wiley & Sons, Inc. is not associated with any product or vendor mentioned in this book.
LIMIT OF LIABILITY/DISCLAIMER OF WARRANTY: THE PUBLISHER AND THE AUTHOR MAKE NO REPRESENTATIONS OR WARRANTIES WITH RESPECT TO THE ACCURACY OR COMPLETENESS OF THE CONTENTS OF THIS WORK AND SPECIFICALLY DISCLAIM ALL WARRANTIES, INCLUDING WITHOUT LIMITATION WARRANTIES OF FITNESS FOR A PARTICULAR PURPOSE. NO WARRANTY MAY BE CREATED OR EXTENDED BY SALES OR PROMOTIONAL MATERIALS. THE ADVICE AND STRATEGIES CONTAINED HEREIN MAY NOT BE SUITABLE FOR EVERY SITUATION. THIS WORK IS SOLD WITH THE UNDERSTANDING THAT THE PUBLISHER IS NOT ENGAGED IN RENDERING LEGAL, ACCOUNTING, OR OTHER PROFESSIONAL SERVICES. IF PROFESSIONAL ASSISTANCE IS REQUIRED, THE SERVICES OF A COMPETENT PROFESSIONAL PERSON SHOULD BE SOUGHT. NEITHER THE PUBLISHER NOR THE AUTHOR SHALL BE LIABLE FOR DAMAGES ARISING HEREFROM. THE FACT THAT AN ORGANIZATION OR WEBSITE IS REFERRED TO IN THIS WORK AS A CITATION AND/OR A POTENTIAL SOURCE OF FURTHER INFORMATION DOES NOT MEAN THAT THE AUTHOR OR THE PUBLISHER ENDORSES THE INFORMATION THE ORGANIZATION OR WEBSITE MAY PROVIDE OR RECOMMENDATIONS IT MAY MAKE. FURTHER, READERS SHOULD BE AWARE THAT INTERNET WEBSITES LISTED IN THIS WORK MAY HAVE CHANGED OR DISAPPEARED BETWEEN WHEN THIS WORK WAS WRITTEN AND WHEN IT IS READ.
For general information on our other products and services, please contact our Customer Care Department within the U.S. at 877-762-2974, outside the U.S. at 317-572-3993, or fax 317-572-4002. For technical support, please visit https://hub.wiley.com/community/support/dummies.
Wiley publishes in a variety of print and electronic formats and by print-on-demand. Some material included with standard print versions of this book may not be included in e-books or in print-on-demand. If this book refers to media that is not included in the version you purchased, you may download this material at http://booksupport.wiley.com. For more information about Wiley products, visit www.wiley.com.
Library of Congress Control Number: 2025946286
ISBN 978-1-394-36556-2 (pbk); ISBN 978-1-394-36558-6 (ebk); ISBN 978-1-394-36557-9 (ebk)
Okay, whoa, wait a minute: a book about coding!? Now!? With our new artificial intelligence (AI) overlords about to take over everything, especially programming jobs? Are your eyes deceiving you? Is the author deluded, possibly even insane? Will these questions never end?
Fortunately for you, I can answer all these queries by paraphrasing the eminently paraphrasable Mark Twain:
The reports of the death of coding are greatly exaggerated.
Sure, AI tools such as ChatGPT and Microsoft Copilot are very good at generating and fixing programming code, but we’ve learned over the past year or two that the human element in coding is still very much needed. Coding is a craft just as much as it is a science, and coding well requires the kind of creativity that is noticeably lacking in current AI models.
And I’ll let you in on a little secret that no one seems to talk about very much: Coding generates joy. It’s true! When you get to the point where you know enough about coding that you can think of an idea, write the code for it, and then get your computer to run that code, well to my mind there’s no better feeling. It’s pure coding joy.
Oh, and I might as well let you in on yet another secret: Getting to the point where you can code whatever idea comes to mind is not that hard and does not take very much time! Even if you’ve never coded before, I promise that you are this close to getting there.
So, yes, a book about coding!
Welcome, then, to Coding For Dummies, All New Edition. This book teaches you everything you need to know about coding, from what it is, to how to get started, to writing and running your first programs. My goal is to show you that going from being clueless about coding to being comfortable with not one but two different programming languages is a lot easier than you probably think. This book shows that even the greenest rookie coder can learn how to build programs that will amaze their family and friends (and themselves).
Notice that I didn’t say that this book teaches you everything there is to know about coding. If you’re looking for lots of programming history, computer science theory, and long-winded explanations of coding concepts, I’m sorry, but you won’t find any of those things here. My philosophy throughout this book comes from Linus Torvalds, the creator of the Linux operating system: “Talk is cheap. Show me the code.” I explain what needs to be explained and then I move on without further ado (or, most of the time, without any ado at all) to examples and scripts that do more to illuminate a concept that any verbose explanations I could muster. (And believe me, I can muster verbosity with the best of them.)
Why does this book’s title have All New Edition tacked on to the end? Because this version of the book has been reimagined, reorganized, and rewritten from the ground up. What you get inside these pages is a fresh and modern take on learning how to code.
How you approach this book depends on your current level of web coding expertise (or lack thereof):
If you’re just starting out, begin at the beginning with the three chapters in
Part 1
. This will give you all the knowledge you need to pick and choose what you want to learn throughout the rest of the book.
If you want to learn how to code for its own sake or you want a solid foundation for learning other programming languages, the Python language is your best bet, and it’s the subject of the chapters in
Part 2
.
If you want to learn how to build dynamic web pages, that’s the province of JavaScript, so head straight to
Part 3
.
This book is not a primer on using a computer or editing text. This is a book on learning to code, pure and simple. This means I assume the following:
You know how to operate a basic text editor and how to get around the operating system and file system on your computer.
You have an internet connection.
You know how to use your web browser.
Yep, that’s it.
Okay, well, not quite. For the JavaScript portions of the book, I assume you’re already familiar with HTML and CSS. If you’re not, you can check out Chapter 17 to learn a few basics. However, if you really want to get into web coding, may I not-even-remotely-humbly suggest my book HTML, CSS, and JavaScript All-in-One For Dummies (Wiley), which will tell you everything you need to know.
This icon points out juicy tidbits that are likely to be repeatedly useful to you — so please don’t forget them.
Think of this icon as containing the fodder of an advice column. It offers what I hope is wise advice or a bit more information about a topic under discussion.
Look out! In this book, you see this icon when I’m trying to help you avoid mistakes that can cost you time, money, or embarrassment.
When you see this icon, you’ve come across material that isn’t critical to understand but will satisfy the curious. Think “inquiring minds want to know” when you see this icon.
Some extra content for this book is available on the web. Go online to find the following:
The examples used in the book: You can find these in the following places:
My website:
https://paulmcfedries.com/books/coding-fd
GitHub:
https://github.com/paulmcfe/coding-fd
The examples are organized by chapter. For each example, you can view the code, copy it to your computer’s clipboard, and run the code at the command line or in the browser. If possible, please resist the temptation to just copy and paste the code. The best way to learn any programming language is to get what I call a “fingertip feeling” for it by typing the code yourself. Sure, it takes more effort, but the code will seep into your brain faster this way.
The Web Dev Workbench:
To try your own HTML, CSS, and JavaScript code and see instant results, fire up the following site:
https://webdevworkshop.io/wb
You won’t break anything, so feel free to use the site to run some experiments and play around with HTML, CSS, and JavaScript.
Part 1
IN THIS PART …
Get acquainted with what coding is and what you can do with it.
Learn the most important coding concepts.
Review the most popular programming languages.
Get your coding career off to its best start.
Chapter 1
IN THIS CHAPTER
Understanding how programming works
Perusing a few programming languages
Taking a peek under the coding hood
Figuring out why you’d want to learn to code
Learning how code is used in the real world
Everybody should learn to program a computer because it teaches you how to think.
—STEVE JOBS
Okay, I'll admit it: I'm not one of those look-before-you-leap types. I'm a dedicated leaper. With almost every new thing I learn, my philosophy is that I learn best when I do the thing. I usually just jump in, get my hands dirty, make mistakes, fix them, and before long I develop a feel for this new hobby or skill.
Notice, though, that I said I do that with almost every new thing I learn. I don't do it when I'm learning something related to coding. Why not? “Just jumping in” is a lousy strategy for learning to code because the mistakes you make are dumb ones that teach you nothing (as opposed to smart mistakes that you can learn from). When it comes to learning anything related to code, it's always best to start with the basics and work your way slowly and steadily to competency, then proficiency, then mastery.
This chapter is your look-before-you-leap introduction to coding. Here you explore what coding is and how it works. You discover the myriad reasons why it's good to learn to code, and you investigate quite a few real-world uses for coding.
A computer is a machine that follows instructions. Or, to put a finer point on it, a computer is a machine that does nothing until someone or something tells it what to do. That might sound surprising. After all, computers cost many hundreds, sometimes even thousands, of dollars, and are positively bristling on the inside with electronic gadgetry. Surely something so expensive and so complex must be capable of doing some useful tasks on its own.
Nope.
Sure, when you turn on a new computer for the first time, some hieroglyphics appear on the screen and you eventually end up in Windows or macOS or some other desktop. Doesn’t that so-called boot process mean that the computer is doing something on its own?
Again, nope.
When you turn on a computer, it automatically loads a set of instructions that tell the computer what it must do to get the hardware (keyboard, mouse, screen, and so on) up and running and to get the operating system (Windows or macOS or whatever) loaded. That set of instructions is known as the computer’s firmware, which is a special type of program embedded in the computer hardware. When the firmware has completed its job, it calls the bootloader, which loads the operating system.
Okay, that’s all fine, but where did the firmware and bootloader come from? I’m glad you asked because at long last I can get to the point of all this: Someone coded them.
Some very smart person versed in the esoterica of computer hardware and system software programmed the firmware, and some other just as smart person coded the bootloader. So, let me repeat myself: Computers can’t do anything unless someone or something tells them what to do. And the way you tell a computer what to do is via code.
Coding firmware or a bootloader is hideously complex and requires years of study. Happily, you won’t be going anywhere near that level of complexity in this book. Whew! But everything you do learn here will be a variation on the overall theme of this section: telling a computer what you want it to do using code.
Am I talking about making a computer do anything you want? Alas, no, although that would be very useful! When you code, you’re given a set of tools for the job; the tools you work with vary depending on the language you’re using. As I discuss later in this book, the tools you get with Python (refer to Part 2) are much different than the tools you get with JavaScript (check out Part 3). As an analogy, the types of home projects you’d take on would be very different depending on whether you had a carpenter's toolbox or a sewing kit.
But no matter how you code — no matter what programming tools you have at your disposal — you’re almost always doing one (or sometimes both) of the following:
Solving a problem:
One of the most common reasons that a piece of code gets written is because the coder had a pain point or an inefficiency in their life and saw a way to use code to make their life easier or more streamlined.
Creating something new:
Another common reason to start coding is when you get a great idea and want more than anything to bring that idea to life.
No matter what you work on in your coding career, you’re almost always doing one (or both) of these things — solving problems, creating new stuff, or combining the two to make something that’s both new and improved.
Python and JavaScript are programming languages. Okay, fine, but what does it mean to call something a programming language? To understand this term, you need look no further than the language you use to speak and write. At its most fundamental level, human language is composed of two things — words and rules:
The words are collections of letters that have a common meaning among all the people who speak the same language. For example, the word
book
denotes a type of object; the word
heavy
denotes a quality; and the word
read
denotes an action.
The rules are the ways in which words can be combined to create coherent and understandable concepts. If you want to be understood by other speakers of the language, you have only a limited number of ways to throw two or more words together. “I read a heavy book” is an instantly comprehensible sentence, but “book a I read heavy” is gibberish.
The key goal of human language is being understood by someone else who is listening to you or reading something you wrote. If you use the proper words to refer to things and actions and if you combine those words according to the rules, the other person will understand you.
A programming language works in more or less the same way. That is, it, too, has words and rules:
The words are a set of terms that refer to the specific things that your program works with or the specific ways in which those things can be manipulated. These words are known as
reserved words
or
keywords.
The rules are the ways in which the words can be combined to produce the desired effect. In the programming world, these rules are known as the language’s
syntax.
The crucial concept here is that just as the fundamental purpose of human language is to be understood by another person, the fundamental purpose of a programming language is to be understood by whatever machine is processing the language. The key, however, is that being “understood” by the machine really means being able to control the machine. That is, your code “sentences” are commands that you want the machine to carry out.
Let’s say you travel to Igboland in Nigeria and want to ask a local for directions to the nearest bathroom. If that person speaks only Igbo (the native language of Igboland), one solution would be to find someone who speaks both English and Igbo and ask that person to translate your request as well as the response. Problem solved!
The person who can translate your English into Igbo is called an interpreter, and that task is essentially how we’re able to program a computer. The problem is that a computer understands only its native language, which is called machine language and consists of 1s and 0s. (I won’t get into this topic here, but if you’re curious to know more, check out the sidebar “How computers work: A crash course for would-be coders.”) A very simple machine language instruction to a computer might look something like this:
10111000 00000001 00000000 00000000 00000000 10111111 00000001 00000000 00000000 00000000 01001000 10111110 00000000 01100000 01100000 00000000 00000000 00000000 00000000 00000000 10111010 00001101 00000000 00000000 00000000 00001111 00000101 10111000 00111100 00000000 00000000 00000000 00110001 11111111 00001111 00000101
Yikes! No sane human wants to deal with something as weird as machine language, so one of the first things that engineers did after computers were invented was come up with two remarkable inventions:
A way of representing machine-language instructions as human-understandable English words
A way of converting those English words back into the machine language that the computer understands
The first invention is called a programming language and consists of, in part, English (or, sometimes, English-like) words such as if, while, and return. You use these generally comprehensible terms to construct statements, which are commands that you want the computer to carry out on your behalf.
For example, the preceding machine language code began life, in part, as the following statement:
printf("Hello, World!");
This statement, which is written in the C programming language, outputs the text Hello, World! C is an example of a high-level language, which describes any programming language that abstracts away the mind-numbing complexity of the computer's native machine language.
You might have heard someone say, with great authority, that “computers operate by processing 1s and 0s.” If, upon hearing that, you were flummoxed, let me tell you that your reaction is utterly normal. It really is incomprehensible to us mere mortals that computers, which can do all these incredible things, perform those wonders by slinging around just two values: 1 and 0. What’s behind this mystery?
At the lowest level, a computer is basically a collection of billions of unimaginably teensy components called transistors, which operate essentially as on/off switches for electrical current. When a transistor allows electrical current to pass through, by convention that state is represented by a 1. When a transistor blocks electrical current from passing through, by convention that state is represented by a 0. Each 1 or 0 is called a binary digit, or bit. One bit offers only two options: 1 or 0. Combining two bits offers four options: 00, 01, 10, or 11. Skipping ahead, I can tell you that combining eight bits offers 256 options, from 00000000 to 11111111 and every combo in between. A string of eight bits is called a byte and the 256 possible byte values is enough to code every letter, every number, every punctuation mark, plus a few other standard symbols that make up the American Standard Code for Information Interchange (ASCII) table. The uppercase letter H, for example, is 01001000 in binary. So, combine eight transistors, set them so that they form the byte 01001000, and you’ve got the letter H stored on your circuit board (which might be a memory module).
Do you need to memorize the byte values for every letter, number, and symbol to code a computer? No, not even close! In fact, the history of coding can be seen as the moving farther and farther away from how information is physically stored using transistors to being able to make the computer do your bidding using relatively simple English words.
C is a notoriously difficult language to learn, so aside from a brief mention in Chapter 3, I steer clear of it in this book. Instead, you learn two languages that reside at an even higher (read: easier) level than C: Python (covered in Part 2) and JavaScript (tackled in Part 3).
The second of the inventions I mentioned is called either an interpreter or a compiler, depending on the programming language. (I explain the difference between interpreters and compilers in Chapter 3.) Either way, the purpose of this invention is to take the English-like code of a programming language and convert it to something (such as machine language) that the computer can read and run. All of this happens behind the scenes, so, as a coder, you never have to lay your eyes on a single 1 or 0 (unless it's part of your Python or JavaScript code, of course).
At this very early stage of your programming career, the process of coding might seem more than a little mysterious, possibly even downright puzzling. After all, from the outside a computer is a mystifying machine, so the idea that you can somehow control this inscrutable hunk of electronica might seem the stuff of fantasy. Or even if you've already convinced yourself that you can make a computer do your bidding, how you do that might still have you scratching your head.
Perhaps the secret to being able to code a computer is having the right equipment, something like needing a loom for weaving or a lathe for woodworking.
Nope, you're way off. Maybe the most surprising thing about code is that it's nothing but text. (To keep things simple, for now I'm ignoring non-text files such as images and videos that you might incorporate in, say, a web page.) Ever used Notepad in Windows or TextEdit on a Mac? Those bare-bones text files are essentially what you use to write your code.
To describe the programming process in its most generic terms, I like to use what I call the “three-and-a-half Rs” of coding — write, run, revise, and repeat:
Write:
In a text file, you write your code as a series of statements, each of which is essentially an instruction to the interpreter or compiler for whatever programming language you're using.
Run:
You invoke the programming language's interpreter or compiler and tell it to process the code in the text file you wrote. The interpreter or compiler then executes the code, and the results appear, which could be the program's output or one or more error messages.
Revise:
Based on the results of the run, you edit your code to fix any errors that crop up or to improve your code.
Repeat:
You write more code (to, say, add new functionality), run it, revise it as needed, and then repeat the cycle until your program or app or web page or whatever is complete.
That's the bird's-eye view. The next two sections bring things slightly closer to the ground by looking at the coding processes specifically for Python and JavaScript.
I go into a pleasing amount of detail about Python in the chapters that make up Part 2, so here I just provide you with a quick overview of the Python coding process:
Using a text editor or code editor, write your Python language statements in a plain text file.
When you save your text file for the first time, be sure to name the file with the .py file extension, which identifies it as a Python file.
At the command line, type python, a space, and the name of the Python file, and then press Enter or Return.
For example, the following runs a file named hello.py:
python hello.py
The python part of the command invokes the Python interpreter, which processes the content in the Python file one statement at a time. Note that the Python interpreter is available on your computer only if you have installed Python, as I describe in Chapter 4.
The interpreter then displays the results of the code, which might be some output you defined or one or more error messages.
As I discuss in Chapter 4, there are other ways to execute Python code, including an interactive Python shell that enables you to run one Python statement at a time and code editors that enable you to run Python code from the editor's development environment.
Return to your code editor and modify the code as needed based on the results of the most recent run, especially to troubleshoot any errors that cropped up.
Repeat Steps 1 through 3 as required until your Python program is complete.
JavaScript is the subject of the chapters in Part 3, so I'll just whet your appetite here with a short-and-sweet review of the JavaScript coding process:
Using a text editor or code editor, write your JavaScript language statements in a plain text file.
I assume for simplicity that you want to run your JavaScript statements in a web page (that is, an HTML file, which usually uses the .html file extension), so there are two ways to go:
Create an external JavaScript file:
Save your text file using the
.js
file extension, which identifies it as a JavaScript file, and then modify your web page code to tell the browser about the file. For example, the following tags reference a file named
hello.js
:
<script src="hello.js"></script>
Embed the JavaScript code inside the HTML file:
Your JavaScript statements reside within a
<script></script>
block:
<script>JavaScript statements go here</script>
If this rapid-fire overview is confusing, don't sweat it for now. I discuss all this in more careful detail in Chapter 10.
Open the HTML file in a web browser or, if the HTML file is already open in the browser, refresh the page.
The web browser contains a built-in JavaScript interpreter, so as soon as the browser loads the HTML file, it begins processing the JavaScript code one statement at a time. The browser then displays the results of the code, which might be some output you defined, a web page modification, or one or more error messages.
Switch back to your code editor and edit the code as required given what happened when you opened or refreshed the HTML file. In particular, be sure to tackle any errors that cropped up.
Repeat Steps 1 through 3 as required until your JavaScript code is running the way you want it.
Since you're reading this book, I think it's safe to assume that you want to learn how to code. If that's true, feel free to skip merrily over the rest of this section. However, if you're still on the fence, trying to decide whether you want to spend the time and effort to learn to code, have I got a section for you!
If you're short on time, my immediate answer to the question, “Should I learn to code?” is a real timesaver: Yes, absolutely! If you still need to be convinced, let the next few sections serve as my long answer.
You might have an image of a stereotypical coder in your head, one that no doubt envisages some not-recently-washed nerd sitting in a dank, dimly lit basement surrounded by empty pizza boxes and crushed energy drink cans. Ah, so you have seen photos of my office!
I'm sure many coders fit that stereotype, but most don't and you certainly don't have to stop bathing and ruin your diet to code.
Lots of nerds code, but not all coders self-identify as nerds, so if the geeky reputation of coding is holding you back, forget about it. You can code just as you are.
While it's true that the essence of coding is writing instructions for a computer to follow, it's not like writing a list of items for your spouse to pick up when they go to the grocery store. That is, you can't just pick up the coding equivalent of a pen and start writing things down as they pop into your head. Coding requires multiple kinds of thought, so in a sense coding teaches you how to think.
For example, when you're considering how to tackle a coding project, it always helps if you can break down the project into smaller, more manageable chunks. Similarly, computers are relentlessly logical beasts, so successful coding requires that you use your logical reasoning skills to “think” like the computer. Every program ever written contains errors, so a big part of coding is troubleshooting problems, which requires understanding how your code works. Converting an idea in your head into code that brings the idea to life is a task that requires large doses of imagination.
All these skills — breaking down problems, logical reasoning, troubleshooting problems, critical thinking, and imagination — not only make you a good coder but are also tremendously useful outside programming. Whether it's business, finance, science, or trying to assemble some piece of IKEA furniture with its inscrutable instructions, the thinking skills you hone via coding will help with whatever you're doing.
In the preceding section, I mentioned that getting code to run requires a logical, channel-the-computer approach. I stand by that, but I'll also admit that “think like the computer” isn't a clarion call for fun. That's okay, though, because thinking logically is only part of what it takes to get a program running. Most of us associate creativity with artistic endeavors, but I'm here to tell you that coding is one of the most creative skills you can learn.
For starters, every non-trivial coding project you work on will present you with hurdles that at first seem insurmountable but will soon yield to some creative problem-solving.
But coding creativity really begins with designing and implementing whatever idea has fired your imagination. Want to make a game? Design a website? Create an app? Craft some digital artwork? Whatever it is, coding enables you to take almost any idea, no matter how pie-in-the-sky it might seem to you now, and turn it into an actual, working project that you and others can play, run, or view. Believe me when I tell you that building things from scratch and watching them come to life not only gives your creativity circuits a workout but is also the very definition of fun.
Being able to code is like having a superpower: If you can imagine something, you can build it. Want to create a website for your side hustle? Code it. Have an idea for an awesome game? Make it. Need an app to remind you to drink water? Build it. (Then send it to me. I could really use that app!)
When you learn to code, you give yourself the near-magical ability to create something out of nothing. This ability is incredibly rewarding because now you’re not just using apps — you’re making them.
Scoot over to the “Real-World Uses of Coding” section for a few practical and useful project ideas.
When people with different native languages want to communicate, they can sometimes use another language that they have in common. That common language is called a lingua franca.
In today's technical world, code often acts as a kind of lingua franca because programming is one of the few skills that works the same across every industry and country. A JavaScript developer in Japan writes the same kind of code as one in Canada. A Python script written in the U.S. can be used by someone in Germany.
This means coding opens up opportunities around the world. If you ever dream of working internationally (or remotely for a global company), coding can help you get there.
Speaking of working, coding is one of the best ways to land yourself a great job. Let’s start with a big one: money. Tech jobs consistently rank among the highest paying careers. What about opportunities? Even in the age of AI, the demand for programmers continues to grow. Companies in nearly every industry — finance, healthcare, entertainment, even agriculture — need developers. And many programming jobs offer remote work opportunities, flexible hours, and great benefits.
Here are just a few example careers:
Software developer
Web developer
Data scientist
Cybersecurity analyst
AI/machine-learning engineer
But you need a computer science degree, right? Not necessarily. Plenty of people who become professional programmers are self-taught using books just like this one!
Yep, I get it: Being a professional programmer isn't for everyone. It might be the hours, the constant sitting, all that screentime, or whatever. Turning pro is one coding path, but it's not the only one. Whatever field you work in (or want to work in), having even basic coding know-how can give you an edge over your peers.
For example, if you work in marketing, knowing how to code can enable you to automate reports and analyze customer data. If you work in finance, coding can help you write scripts to track stock prices and investments. If healthcare is your field, knowing how to code can help you manage patient data efficiently. If you're an educator, you can use code to create fun and interactive learning tools for students.
Whatever your career, knowing how to code is a bonus skill that makes you more valuable, more productive, and more creative.
Back in the dim mists of time otherwise known as the twentieth century, learning to code was hard. A few dedicated hobbyists taught themselves to program, mostly using a relatively simple language called BASIC, but the vast majority of programmers learned to code by obtaining expensive college degrees that required reading enormous textbooks filled with abstruse technical jargon and recondite computer science theory.
Today? Ah, today we have beginner-friendly languages like Python and JavaScript that enable anyone to learn to code, no fancy-schmancy college degree required. Forget the jargon and the theory. If you can think reasonably logically and you can break down a problem in smaller challenges, you can learn to code.
Back in 2011, the venture capitalist Marc Andreesen wrote an op-ed piece titled “Why software is eating the world.” He meant that software was and is transforming entire industries and disrupting traditional ways of doing business. He predicted that software would become a crucial and more deeply baked-in component of company operations, products, and services.
However, before software can eat anything, it has to be coded. Before software can be embedded into every facet of business, someone has to program it. Software is all around us now and will soon be ubiquitous. Learning to code now future-proofs your skills, ensuring that you stay relevant in this rapidly evolving, being-eaten-by-software world.
The overall theme of this chapter has been that coding, at its most basic, is just cajoling a computer into performing some task. As the early chapters in Part 2 (Python) and Part 3 (JavaScript) show, it's not that hard to write code that makes a computer do something trivial, such as display text on the screen. Simple and straightforward examples are a great learning tool and an easy way to build your coding confidence, but they lack, well, substance.
After writing and running a few such examples, you might start to wonder whether that's all there is to coding. Is programming all bun and no hamburger? All sizzle and no steak? Are any vegetarians still reading this?
Fortunately, even beginner-welcoming languages such as Python and JavaScript can be used to build useful and fun projects. You build some in this book a bit later, but for now I want to give you a taste of what's possible. Here, broken down into five development categories, are a few things that folks in the real world are building using Python:
Automation:
Python can automate boring, repetitive tasks like renaming files, sending emails, and scraping data from websites. For example, a marketer can write a Python script to automatically send out weekly email reports instead of doing it manually.
Data science:
Big-time firms such as Google, Netflix, and Facebook use Python to analyze the massive amounts of data they generate. Many business users take advantage of Python libraries such as Pandas and NumPy to help them make sense of customer behavior, market trends, and sales predictions.
Machine learning:
This branch of AI enables computers to learn from data and make decisions or predictions without being programmed. Python-based machine-learning tools such as TensorFlow and scikit-learn enable companies to develop AI-powered systems, such as recommendation engines (I'm looking at
you
, Netflix suggestions).
Scientific computing and engineering:
NASA and other scientific institutions take advantage of Python to create complex simulations and calculations. Python also helps engineers analyze large datasets in fields such as genetics, physics, and climate modeling.
Web development:
Python is used to build web applications using tools such as Django and Flask, and companies such as Instagram and Spotify rely on Python-based web services. However, in web development, Python is most often called upon for building server systems for handling chores such as data storage, user authentication, and security.
Once you're comfortable with Python, be sure to read Chapter 9, where I take you step-by-step through a few useful Python projects.
Here are some projects that coders in the real world are building using JavaScript, arranged into five development categories:
Web page development:
A sprinkling of JavaScript turns a boring web page into something interactive and dynamic. Browser-based JavaScript can request data from a server, display that data on the page, and handle user input. For example, when you type some text in a web page search box and a list of matching items appears lickety-split, that tells you that JavaScript is working feverishly in the background to fetch and display those search results.
Web server development:
A JavaScript tool called Node.js runs on many web servers and is used for
back-end
tasks, such as dealing with data, authenticating users, and providing cloud services. Behemoth companies such as LinkedIn and PayPal use Node.js to power their web apps.
Mobile apps:
JavaScript tools are available that enable developers to build mobile apps. Using the framework React Native, Facebook and Instagram (and many others) use JavaScript to offer apps that work on both iOS and Android devices.
Games:
JavaScript tools such as Phaser.js enable developers to use JavaScript to build games that run either in the browser or on mobile devices.
Smart devices:
JavaScript is used to program smart home devices, such as lights, security cameras, and thermostats. And with Node.js, developers can connect JavaScript applications to Internet of Things (IoT) systems.
After you have the JavaScript basics down, head over to Chapter 15 to learn how to code several practical JavaScript projects.