Variables I: Single Value Variables
[outline]
In this part we will look at how we store basic data types in "variables". First though, let's look at the basic syntax of the language.
The syntax of the language is its grammar. Learning the syntax: what can and can't go next in an instruction to the computer, is key to learning a language. Syntax mistakes are commonly picked up by the interpreter when we try to run the program, and might be contrasted with logical bugs, which are present in a program that runs ok, but doesn't do what we want it to do.
Basic syntax (powerpoint)
Further info:
It is probably worth repeating this section of the lecture, as it can get quite confusing. While most time a single instruction in Python is on a single line, you can put more than one instruction on a line if they are semi-colon separated, and, more commonly, you can split lines.
Split lines are counted as one when the first ends with a backslash, \ provided this isn't part of a piece of non-comment text or followed by a comment.
Lines inside { } ( ) [ ] can also be split (and commented); though non-comment text needs quotemarks around each line.
Triple quoted text may be split across lines, but the formatting (spaces; linebreaks) will be kept in the text when used (i.e. if it's not a comment).
Quiz: The following code is syntactically wrong because of ________________________________________
a = 5
if a < 10 ;
print (a)
print ('''done''')
- incorrect punctuation.
- incorrect double-quotes.
- incorrect indenting.
Correct! Although the coder has used three single quotes, this is fine for Python, which will allow either one or three single or double quotes around a piece of text. The indenting is fine: it may be logically wrong, in terms of what you want the code to do, but it is syntactically correctly constructed. The issue is that the coder has used a semi-colon to start the compound statement, not a colon.
Having looked at the core syntax, now let's look at how we can store simple data, like single numbers or words. We'll come back, later in the course, to storing multiple words or numbers. Simple data is stored in "variables", or, more accurately, a variable is a combination of a label and a data element it is attached to. As soon as we have a label, we can talk about the value.
Variable assignment (powerpoint)
Further info:
The Python styleguide is PEP-0008.
A nice clear discussion of static vs dynamic typing of languages can be found on Steve Ferg's Python blog.
We tend to use i
, j
, and k
for counters because in the first popular third generation language, FORTRAN, letters had to be used for certain variables, and certain types of number could only be assigned to certain letters. Integer values (whole numbers) could only be assigned to the letters from i
(for integer) onwards. For this reason, people still use i
, j
, k
for counting things (but not lowercase "L
" – it looks too much like a one).
In spatial analysis we also tend to use x
, y
, and z
for metre-based coordinate systems, and lat
and long
or lng
, for angular coordinates.
Quiz: In the following code b = ________________________________________
a = 5
b = a
a = 10
- a.
- 10.
- 5.
Correct! As soon as a
is assigned the new value 10
the old a
is destroyed, and a new one created. The b
is unaffected, so still attached to the old value it shared with a
: 5
.
Now we've seen how to create variables, we'll look in a little more detail how to use them. We'll look at the types of simple variables you can get, and the kinds of operations you can do on them.
Variable use (powerpoint)
Further info:
For a list of the functions associated with strings, see the library docs
A list of major disasters caused by numerical errors in code has been compiled by Kees Vuik. If you're going to do anything critical with floating point numbers, please check out the information in the Python tutorial. You might also be interested in this introduction to the issues. See also "Hacker's Delight" by Henry S. Warren Jr, and Randall Hyde's "Write Great Code" books, all of which can be found on the book list. The classic paper on this is:
Goldberg, David (1991) What Every Computer Scientist Should Know About Floating-Point Arithmetic ACM Computing Surveys 23:1 5—48.
We won't deal in detail on complex/imaginary numbers here, but if you're interested then a good starting point is the Python docs. "J" is used to denote complex/imaginary numbers, rather than the more traditional "i" because this is the convention in electrical engineering where "i" has other meanings, and because capital "I" looks too much like one.
If you *really* need to know, the operator precedence for Python can be found in the language reference.
We'll cover string formatting later, but if you are interested you can find out more in the documentation.
We talked briefly about other bases than decimal. Oct is a little obscure these days, though we'll see one use of it later in the course. Hex on the other hand is still used a lot, for example to represent colours on the web (where colours are represented as red, green, and blue values between 0 and 255, compressed as hex numbers, so, for example, #FF00FF is purple, i.e. 255,0,255). Converting fractions to other bases isn't generally done because of the uses in computing bases are put to. If you're interested, there's some information here.
Note that in many languages there's shorthand for:
a = a + 1
and
a = a - 1
Which are a++
and a--
respectively, and sometimes ++a
and --a
(depending on whether you want to use the current value first or not).
These don't exist in Python, however, as +
and -
have normal meanings, they will compile in some cases, e.g.
b = a++10
Watch this if you use code from other languages.
Incidentally,
"++
" is pronounced "plus plus". Although it doesn't seem to be the origin of the use, this aligns nicely with George Orwell's 1984, where things are described as "doubleplus good" when very good. The originator of the language C++ (the old C language, but one better) pointed out the convenience of this when naming it (see: Parsons, David (1997) "Object Oriented Programming with C++", Second Edition, p.15).
Quiz: The code below prints ________________________________________
a = 10.0 % 3
print(str(type(a)) + " " + str(a))
- <class 'int'> 1
- <class 'float'> 1.0
- <class 'float'> 1
Correct! Although one might expect modulus, which returns the whole-number remainder of a division, to create an int, because there's a floating point number involved,
everything gets converted to floats, even where an int would do the job and be more accurate. If we used 10
rather than 10.0
we'd have got an
int.
[Key ideas from this part]