Python Data Variables (Numbers, Lists, Tuples, Strings and Dictionaries) - PYTHON MADE SIMPLE (2015)

PYTHON MADE SIMPLE (2015)

Chapter 8: Python Data Variables (Numbers, Lists, Tuples, Strings and Dictionaries)

Variables are reserved memory locations in python where values can be stored. In Python, you are allowed to create a variable which you can reserve in the memory.

In the basis of the type of data input on python, the interpreter will be able to provide memory and make a decision on whether it can be stored on the memory reserved for the operation. Hence, when you assign a different data type to specific variables as you decide, you will be able to keep the integers, decimals and or characters in this variable.

How do you assign a value to a variable?

An equal sign is used as an operand for assigning variable to a name. The operant to the left of the equal sign is referred to as the name of the variable while the operand to the right hand side is the value of the variable to be stored in Python.

Name of variable = Value of the variable

This can be shown in the figure below:

When this code is saved and run. The following results are shown in the output.

It is a simple process. Just type in your python program the text as provided and it should offer you the same output as shown in the example above.

There are various data types that can be used in python:

They include:

1. Numbers

2. Strings

3. Lists

4. Tuple

5. Dictionaries

Numbers in Python

You can create a number as an object when you assign a value to them. Just like the name and value of the variable operands are stated, they can be used also for assigning numbers. For instance:

Var1 = 2

Var2 = 4

Types of numerical supported by Python

Python best works with the following numericals

1. int (recognized as integers)

2. long (long integers [are represented as hexa or octadecimals])

3. float (floating point real values)

4. complex numbers

Examples

Int = 10, 100, -792, -0x250, 0x75

Long numbers = 51924576L, 0122L, 0xDEFABCGDRT, 53267687675L, -05238675443L

Float = 0.0, 14.90, -21.9, 32.3 +e20, -90

Complex numbers = 3.14j, 45.j, 9.322e-36j, 0.876j

Python strings

A string is a contiguous set of characters that are placed in between the quotation marks

For example:

What does this program do?

See the comments in the program:

The output with respect to the comments in the same order is as shown below:

For more help on strings on python for using classes, functions and data, just type [help (“string”) or help (‘string’) and then press enter] in the python shell.

A full list on how the classes can be implemented will be provided for you to follow and how they can be applied.

Python Lists:

Python lists are very variable and use in compound data types. A list is simply a a number of items that are separated by commas and enclosed within square brackets ([])

For example:

Running this program will produce the following output in the order of the command print

Python has an interactive command for help on lists. Just type [help (‘list’) or help (“list”) followed by enter] in python shell. This will provide a list of all forms of classes that can be applied on lists

Python Tuples

This is another form of a sequence data type. It is very similar to the lists in python. It also consists of values that are separated by commas. They are however enclosed by parentheses ().

They are often considered as read only lists.

Outputs of the above code in the order of the print command are as follows:

For help on tuples, just type [help (‘tuple’) or help (“tuple”) followed by enter] in python shell. This will provide a list of all forms of classes that can be applied on lists

Python Dictionary

These are hash table types values. They are associative arrays or a hash which are similarly found in Perl and consists of key value pairs.

Examples of pairs include the following values:

The output is expected to look like this when this program is run:

Data variables apply in all data codes development and understanding how they work and operate is very essential in learning program.

Connection between Lists and Dictionaries

It is possible to work with both lists and dictionaries and changing between lists and dictionaries and vice versa. This is a common application for most of the programmer and sometimes they have to interchange the data types especially when working with databases which will be learned later in this book (Chapter 12). This is a strength in Python that can never be found in any other language since it has this functionality, that is why Python is referred to as an interactive language.

Let us consider the following example of a dictionary: D is the Dictionary and L is the list.

we could turn this into a list with two-tuples:

Note that both the D and L have the same information. It can also be described that their entropy (organization and arrangement of each word) is also the same. But, it should be noted that sometimes the information may be harder to be retrieved when as a list compared to a dictionary.

When we want to find values in Key’s, we will have to first go through the tuples of the lists compare the components of the list with the keys. This can be well performed when used as a dictionary.

Lists from Dictionaries

It is very easy to develop a list from a dictionary. This is achieved by using the function (items (), keys (), and values ().

Just the method keys () will be able to create a list. This will plainly consist of keys of the dictionary. In addition, values () will be able to make a list of values. Moreover, items () can also be used to create of list of items made of 2 tuples with keys and values (keys, values)-pairs:

However, when a method items () is used in a dictionary, we may not be able to get a list back. But, we can be able to use the item views, the item views will be converted to a list when used with a list function. No information will be lost by converting a dictionary into a list.

It is also possible to return to the original dictionary from the items (), even when formed as a list of tuples, they have the same entropy. However, the effectiveness of the same approaches is similar.

Therefore, dictionary also serves as an efficient method to access, replace, change and vary elements of the dictionary. However, in lists, these have to be coded by a programmer.

Turn Lists into Dictionaries

The lists can also be turned into Python; if these lists satisfy certain conditions. Let us consider two lists containing dishes and other countries:

A dictionary can be created that will be able to assign a dish to a country. This can be done using a function zip (). This will be able to join the lists like a zip. This will be able to iterate over a list. This suggests that we have to wrap a list () function on the zip so as to call to get a list:

The country specific lists of dishes are now completely converted to a list. This is a list of two tuples which has keys and values which can also be automatically be convertible to a dictionary by using the function dict().

The only mystery will be to understand the use of the function zip() when one list is long that the other.

This is not complicated either. The program will be able to ignore any extra lists that may not be paired in any way.

Here, Switzerland will not be paired in this case and will be ignored in the output as presented.

All in one

Normally, we recommend not implementing too many steps in one programming expression, though it looks more impressive and the code is more compact. Using "talking" variable names in intermediate steps can enhance legibility. Though it might be alluring to create our previous dictionary just in one go:

This can also be written in this other way:

Summary

The data variables come in the five ways: They can be in form of numbers, lists, tuples, strings and finally dictionaries. Each of them is very unique and working with python at every single process requires a basic understanding of their functions and classes.

In python, to better understand how this can be applied. You can seek help from the python by typing help (‘object’) or help (“object”) followed by enter. This will provide you with a solution on how any of the variables can be applied.