Learn to code with Everything You Need to Ace Computer Science and Coding in One Big Fat Notebook!
Yes, it’s true, even kids can code with the latest installment of the Big Fat Notebooks series. Perfect for middle school students who are interested in programming or computer science beginners, this book is a complete course in all things code. Parents can follow along as well with this quick lesson on lists and boolean expressions!
Excerpted from Everything You Need to Ace Computer Science and Coding in One Big Fat Notebook. Copyright © 2020.
LISTS AND BOOLEAN EXPRESSIONS
A LIST is a variable that stores multiple values. This is useful when you have several pieces of information that you want to store in one place. Lists can store all types of values including numbers, strings, and other lists.
In Python, lists are formatted using square brackets (and sometimes quotation marks), with a comma between each item. For example, we can make a list called “fruits” and assign it the values apples, bananas, grapes, oranges, and pears:
Note: Each item is separated by a comma, but make sure the commas are on the outside of the quotation marks so they are not included as part of the string.
Think of a list as a row of boxes, where each box holds a value. Every box on the list gets a number, starting with 0. This number is called the INDEX.
The index is used to find or change specific values, or items, within the list. In the fruit example, apples is located at index location 0, and the index location of pears is 4.
A specific item in a list is located using the format list[#], where “list” is the name of the list and “#” is the index location of the item.
So, fruits is apples and fruits is pears.
To display the second item of the fruits list, use the print function, enter “fruits” as the name of the list, and “1” for the index location of the item:
You can also print out a section of the list by naming the range of indexes to print, like this:
This will print: [‘bananas’, ‘grapes’, ‘oranges’] Because the index starts at 0, the second item has an index of 1. The ending index (4: pears) is not included.
When you use print() to display more than one item in a list, the output will include the brackets and quotation marks. That’s because you’re printing a list, not just items in a list.
The last item isn’t listed— only the items up to the ending index position are included in the list.
There are lots of different ways to update a list.
You can replace an item in a list after the list has been created. To replace a value of an item within a list,
- reference (name) the index location
- reassign the value
To replace grapes with kiwi: [‘apples’, ‘bananas’, ‘grapes’, ‘oranges’, ‘pears’]
The index location of grapes is 2. You can replace grapes by assigning index 2 of the fruits list to kiwi: fruits = “kiwi”
The list is now: [‘apples’, ‘bananas’, ‘kiwi’, ‘oranges’, ‘pears’]
In programming, it ’s very common to want to know if something is true or false. For example, in a game we want to know if the game is over, or if it is still going, or if a user got a question right or wrong.
Python automatically detects a Boolean-type variable when the value is set to “True” or “False,” (a Boolean expression). When you’re set ting a variable to “True” or “False,” make sure you capitalize the T in True and the F in False. For example: my_value = True.
Boolean variables are like light switches. There are only two options: on or off (True or False).
When you assign a Boolean expression to a variable, Python will set the value to “True” or “False” depending on whether the Boolean expression is true or false. For example:
More About Everything You Need to Know to Ace Computer Science and Coding in One Big Fat Notebook
From the editors of Brain Quest, America’s #1 educational bestseller!
This Big Fat Notebook makes it all “sink in” with key concepts, mnemonic devices, definitions, diagrams, and doodles to help you understand computer science.
- Computing systems
- Binary code
- Computational thinking
- Loops, events, and procedures
- Programming in Scratch and Python
- Boolean Expressions
- Web development
- …and more!
The Big Fat Notebook series is built on a simple and irresistible conceit—borrowing the notes from the smartest kid in class. Each book in the series meets Common Core State Standards, Next Generation Science Standards, and state history standards, and are vetted by National and State Teacher of the Year Award–winning teachers. They make learning fun and are the perfect next step for every kid who grew up on Brain Quest.