3 - Variables
In almost every programming language, variables are used to store data. Python is no exception to this.
Python Built-In Data Types
There are seven different base data types in Python.
There are four main types of numbers in Python; int, float, complex.
Integers are signed whole values. This means they can be negative as well as positive. You can also defined them using hexadecimal notation. Long values (bigger range of integer values) were removed in Python. Instead, all integers values are long types of varying size.
Floats are floating point real values.
Complex numbers hold two parts, a real part and an imaginary part. They are used in a lot of higher level math.
There are seven types of sequences: string, Unicode strings, lists, tuples, bytearrays, buffers, and xrange objects. The most common sequences that you will be using at first are strings and lists. Strings are actually sequences because they are a string of characters.
Example of a string sequence. Note that you can iterate through the string using an indexer.
There is one mapping type variable; dictionary. Dictionaries store a key/value pair that have methods to help access and manipulate the data stored in them. We will talk more in-depth with Python dictionaries in a later tutorial.
Example of a dictionary in Python. Notice how you can have multiple different values, but only one key. Also note that you can have different types for your keys and values.
File objects are implemented using C's stdio package. The functions they contain, allow you to read and write to files as well as a plethora of other actions.
Below is an example of a simple file read operation on a text file.
A class definition is a template for creating/instantiating objects within a program. Classes are very important for object oriented programming, and will b e covered in further detail in a later tutorial.
Instances are the objects created from a class.
An exception is a scenario which stops the normal flow of a program. For instance, when you try to iterate out of the length of a list you will get an out of range exception.