This the second lesson in my "Programming for Complete Beginners" series. Here is lesson 1 if you missed it. This lesson will explain two vital data types in Python that are used in all programs. Booleans and arrays.
Boolean Logic
In programming languages, a boolean data type can either be true or false. Here is an example in Python:
myVariable = True
myOtherVariable = False
- Notice how True and False here are not in quotes and they start with an uppercase letter.
You should also understand how boolean values work together. For example, a statement is true if all its operands evaluate to true. Example:
trueStatement = (4 > 3) and (3 < 9)
Here the binary operator 'and' makes sure that both sides of the statement are true, thus making trueStatement
evaluate to True. If one of the statements on either side of the operator were false then the whole thing would be false too. There is also the 'or' operator where if only one of the operands is true then the whole thing is true.
The third and final operator is a unary operator which means it only changes one operand. This is the negation ('not' in Python). It changes false to true and true to false.
Here is an example of all 3 operators.
randomVariable = ((3 > 1) or (10 > 12)) and (not False or (9 > 19))
Evaluating all statements:
randomVariable = (True or False) and (True or False) = True and True = True
Boolean variables can also be used with anything else such as strings.
Arrays
Arrays are one of the most useful data structures in any programming languages. They allow you to save more than one value in a variable. For example:
myFirstArray = [17, 2, 3, 100]
This is an array of 4 numbers which means the array has a length of 4. You can also dynamically get the length of a list by calling the len()
function as follows:
myLength = len(myFirstArray)
(4)
It is important to note that in Python and most programming languages, arrays (and strings) have indices that start with 0. Which means that if you want to access the first element of the array you would do this:
firstElement = myFirstArray[0]
(firstElement = 17)
To add another element to the array you simply call the .append()
function as follows.
myFirstArray.append(20)
(myFirstArray = [17, 2, 3, 100, 20] )
The append function pushes the value to the end of your list. If you want to access a sub-list of the array you can use the ':' operator in Python (which basically translates to the word 'until').
For example if you want to have a list of the first 3 elements of the above array you would do the following:
myNewList = myFirstArray[0:3]
(0:3 means index 0 until index 2. The 3 is exclusive)
This means that the new list will have elements from myFirstArray
that are located in index 0-2 (3 elements). If you leave out either side of the :
Python will get all remaining elements. For example [2:] means all elements from index 2 until the end of the list. [:3] means all elements from the beginning of the list until index 2.
Try it Yourself
As always, I recommend trying everything above yourself here. Arrays specifically take some getting used to because indices can get a little confusing at times. However practice makes perfect. Play around with arrays and boolean values until the next lesson which explain loops!
Good lesson - well thought out and organized. Python's a pretty good language for beginners to get started with.
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
Thanks! Yeah I agree, Python is pretty much the best language for newbies.
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit