Variables are an inherent part of programming, they’re a kind of container to hold data and information. There are different types of variables depending on what type of data they contain, for example – integers for whole numbers, floats for fractional numbers, strings for characters, booleans for flags.
When you want to create a variable you’re free to choose its name which is used to refer to it, and in Python a variable has to be defined at the same time, meaning given a value for it to hold. For example:
intNumber = 50
Here, we create an integer variable and give it the name intNumber which is how it’s referred to. At the same time, the variable is given the value 50 to hold using the = sign, which is also known a little pompously as the assignment operator. (There’s more on operators in the next chapter.)
Here we assign the string “bugs bunny” to the variable strBugs:
strBugs = "bugs bunny"
A string is a collection of characters, which can be letter numbers or symbols. It’s called that not because a piece of string holds the characters together, but it’s the definition of a sequence of related objects. Like in the expression ‘she had a string of bestselling novels.’ String data has to be enclosed within speech marks “ ” or double apostrophes ‘ ’
This is an example of a boolean variable:
booPlayerAlive = True
Booleans (named after the 19th century mathematician George Boole) can only have one of two values: True or False. Note that in Python the True and False need to have the initial letters capitalized, typing true or false will give an error. Boolean variables are also known as flags.
It’s good to choose variable names that are consistent with the variable’s function, it makes the code easier to understand. I also like to prefix the variable’s name with a 3 letter abbreviation of its data type, so it’s easier to remember. You can choose to do the same or not, whatever floats your boat.
To display the value which a variable holds, you can use the print() function:
intAge = 21
print (intAge)
Notice how the word ‘intAge’ isn’t printed on screen, but instead the value which the variable is holding is displayed.
To display the age as part of a sentence you can use:
intAge = 21
print ("John is now %d years of age." % intAge)
Here the %d (for decimal) in the string is replaced by the value in intAge.
A variable can be given a value by using the = sign (assignment operator) or you could get a value typed in by the user, with the input() function:
strInput = input("What is your name? ")
print ("Good evening", strInput)
LISTS
In Python data and variables can be collected together and organized into something called a List. For example, imagine you want to store information about your pet dog Fido. We use a string for his name, an integer for the age, another string for color and a boolean to show if he’s been vaccinated or not. Each of these data items is called an element of the list, they’re separated by commas and the list is surrounded with square brackets []. To access specific elements of the list, something called an index is used alongside the list name. this is just a number to identify which element we want (beginning with a 0 not 1.) For example:
lisFido = ["Fido", 5, "black", True]
print ("the dog's name is",lisFido[0])
print ("the dog's age is %d" % lisFido[1])
print ("the dog's color is",lisFido[2])
print ("vaccinated:",lisFido[3])
ADDING AND REMOVING LIST ELEMENTS
Let’s say you want to record more information about your dog, and have its weight added to the end of the list. We can use one of the list’s methods called append:
lisFido.append(42)
print ("the dog's weight is %d pounds." % lisFido[4])
Methods are similar to functions but are used with objects (there’s more on this later in the chapter on object oriented programming.)
If you change your mind about recording the weight, you can remove the final element in the list with the remove method:
lisFido.remove(42)
print (lisFido)
Notice how the remove method doesn’t use an index number to refer to the element to be deleted, but instead uses the element’s value to identify it for removing.
NESTED LISTS
It’s possible to have a list which contains others lists as its elements, a list of lists if you like, and this is called nesting. For instance:
lolDogs =[["Lady", 9 , "white", False] , ["Fido", 5, "black", True]]
print (lolDogs)
Comic Relief
Why do Java developers wear glasses?
Because they can’t C#