Data Management in Python



As was discussed in Chapter 4, Data Structures are a very important part of programming. All programming languages have their own ways of dealing with data. Python is both a dynamically typed as well as strongly typed development language regarding data types.

When the term dynamically typed is used it refers to the way in which data types or variables are declared. The opposite approach would be statically typed data types and what that means is that any data type that may be required in the program needs to be declared before it is used. In addition to that, the programmer needs to decide what data type it is: E.g. either an integer or a string. In a dynamically typed language, data types are discovered at execution time. What this means is that you do not have to decide what data type it is. The interpreter will determine what data type is required and keep track of it. Python is a dynamically typed language as opposed to C++ which is a statically typed development language.

In many cases it becomes necessary for variable types to be altered during run time and there are two main approaches to this in different programming languages. The first approach is known as strongly typed. In this approach data types are always enforced and what this means is that if you have an integer, you can't treat it like a string without explicitly converting it. Java and Python are examples of strongly typed languages.

Finally you also get weakly typed programming languages in which types may be ignored; the opposite of strongly typed. VBScript is weakly typed. In VBScript, you can concatenate the string '12' and the integer 3 to get the string '123', and then treat that as the integer 123, all without any explicit conversion. In Python you would have to convert the data to strings, concatenate them and then parse the result as an integer again.

More about Keywords and Variable names in Python:

There is one important rule common to practically all programming languages that should always be bore in mind: All variables, identifiers and keywords are case sensitive. What that means is that if you declare a variable called ‘bob’ and then later try to access it as ‘Bob’, the Python interpreter is not going to recognise it and say that ‘Bob’ doesn’t exist within the environment. In other words, ‘bob’ and ‘Bob’ are not the same names.

By convention no variable names start with a capital letter or a number even though you are free to use numbers and capitals within the variable names. In python this rule extends to all identifiers: Variables, functions and classes.

KeyWTblAnother rule that is common to all programming languages is that there is a set of keywords. Keywords define the language's rules and structure, and they cannot be used as variable names. Python has thirty one keywords as can be seen in the following table:


Statements are instructions that the Python interpreter can execute. Statements can be executed at the ActivePython command line and will either produce some output or perform an assignment. The keyword ‘print’ will typically produce output: 

               >>> print “Hello world!”
               Hello world!

An assignment statement allows data or a value to be assigned to a variable, for example, if I want to assign an integer (5) to the letter ‘x’ (variable name) you would enter the following statement at the command prompt.       

             >>> x = 5
             >>> x = x + 2   #You can also enter x += 2
             >>> print x

In the example above you can see that you can then alter the value in x by adding two to it and then when you want to see what the resulting value of x is you make use of the print statement to output the data.

The example above can also be regarded as a script. A script is basically a set of statements that are run in sequential order.


Expressions are basically a combination of values, variables, and operators. When you type an expression at the command prompt, the interpreter will evaluate it and either assign the resulting value to a variable or simply output it. In the previous example the integer (2) was added to the variable ‘x’ which already had the number 5 assigned to it, using the ‘+’ operator. The Python interpreter evaluated the statement and assigned the new value to ‘x’.

Operators and Operands:

Operators are special symbols used within a programming language that allows mathematical operations to be carried out on operands (the values being operated on). Examples of such operations include addition, subtraction, multiplication and division. The symbols +, -, and /, and the use of parenthesis for grouping, mean in Python what they mean in mathematics. The asterisk (*) is the symbol for multiplication, and ** is the symbol for exponentiation.

When using the division operator (/) it is important to know that if you are dividing two integers, the answer will be output as an integer, rounded down to the nearest integer. Therefore the statement:

>>> 3/4 will result in an answer of 0.

In a case like this you can force Python to output the answer as a floating point number by entering numbers as follows:

>>> 3.0/4.0 which results in an output of 0.75 which makes more sense than 0.

Notice how by simply including a decimal in the numbers, Python automatically does floating point division and not integer division resulting in a real number.

Another operator that is very useful is the modular operator (%). What it does is that when you use the modulator between two numbers, it divides them and returns the remainder (not the quotient). For example: 

>>> 4%3 è will return 1 since that is the remainder when you divide 4 by 3.
>>> 6%3 è will return 0 as there is no remainder when dividing those two numbers.


« Previous || Next »