Python Data Types and Variables
Variables are used to store information to be referenced and manipulated in a computer language . They also provide a way of labelling data with a detailed naming, so our programs can be understood more clearly by the reader and ourselves.
Every variable in Python is considered as an object. Variables in Python follow the standard nomenclature of an alphanumeric name beginning in a letter or underscore. Based on the data type of a variable, the interpreter allocates memory and decides what can be stored in the reserved memory. You do not need to declare variables before using them, or declare their type. Variable names are case sensitive . Most variables in Python are local in scope to their own function or class. Global variables , however, can be declared with the global keyword.
Assigning Values to Variables
When you assign a variable, you use the = symbol. The name of the variable goes on the left and the value you want to store in the variable goes on the right.
Python Native Datatypes
A Data type provides a set of values from which an expression may take its values. The type defines the operations that can be done on the data, the meaning of the data, and the way values of that type can be stored. Python supports the following data types:
Python supports four distinct numeric types : integers, long, float and complex numbers. In addition, Booleans are a subtype of plain integers. Integers or int are positive or negative whole numbers with no decimal point . Long integers have unlimited precision and floats represent real numbers and are written with a decimal point dividing the integer and fractional parts. Complex numbers have a real and imaginary part, a + bc, where a is the real part and b is the imaginary part.
A String is an array of characters. They are formed by a list of characters, which is really an "array of characters". They are less useful when storing information for the computer to use. An important characteristic of each string is its length, which is the number of characters in it. There are numerous algorithms for processing strings, including for searching, sorting, comparing and transforming.
In Python, string is a sequence of Unicode character . Unicode was introduced to include every character in all languages and bring uniformity in encoding. We can create them simply by enclosing characters in quotes. Python treats single quotes the same as double quotes.
Python strings are "immutable" which means they cannot be changed after they are created. Characters in a string can be accessed using the standard [ ] syntax and zero-based indexing.
More about Python String will be discussed later in the chapter.
Python List is one of the most frequently used and very versatile datatype. Lists work similarly to strings: use the len() function and square brackets [ ] to access data, with the first element at index 0.
TupleA tuple is a container which holds a series of comma-separated values between parentheses. A tuple is similar to a list. Since, tuples are quite similar to lists , both of them are used in similar situations as well. The only the difference is that list is enclosed between square bracket, tuple between parenthesis and List have mutable objects whereas Tuple have immutable objects.
Pyhton Dictionaries allow you store and retrieve related information in a way that means something both to humans and computers. Dictionaries are non-ordered and contain "keys" and "values" . Each key is unique and the values can be just about anything, but usually they are string, int, or float, or a list of these things. Like lists dictionaries can easily be changed, can be shrunk and grown ad libitum at run time. Dictionaries don't support the sequence operation of the sequence data types like strings, tuples and lists. Dictionaries belong to the built-in mapping type.