This video demonstrates how to create a simple Python program using the Spyder IDE. The aim of the video is to demonstrate how to work with lists, tuples, and dictionaries. Even if you aren’t using a Spyder IDE, you can still use the same code that is demonstrated in the video for other IDEs such as Python Idle or Eclipse with the Pidev plug-in or any other environment for that matter. Python code is independent of the IDE, and as a result can be implemented universally on any given Python IDE.
The Spyder IDE can be obtained by installing the Python(x,y) distribution as the Spyder IDE comes bundled with it. You can refer to a previous video demonstration to get a better idea about the installation of Spyder IDE. Once you have launched Spyder, which can be easily done through the Start Menu on Windows. The first thing you might want to do is create a new project. Name the project as “working_with_data_structures”. Once you have done this, the IDE could ask you to specify a workspace. You can chose any empty directory as the workspace and then the project would be created.
Once you have created the new project, you can go ahead and include more files within the workspace. The first thing you need to do is create a new module by right clicking on your new project. You can then name your new module. For the sake of convenience, the module is named as “lists_and_tuples” in the video.
Introduction to lists
A list in Python is denoted by square brackets and it contains multiple values that can be accessed individually by specifying a subscript. For example, “nums = [10, 20, 30]” by doing this you will be defining a list and in the next line if you say print nums this would refer to the list. As you can see, the list consists of three integers and you can access the first integer in the list by simply specifying subscript ‘0’. This will look like print nums[0]. However, you will not be able to run this program at this point of time. If you try to run this program, the IDE will throw an error saying that no Python shell has been currently selected to run the project. You will need to select or open a new Python interpreter to successfully run this program.
Doing this is quite simple. All you need to do is go to the interpreters tab and select the option ‘open a Python interpreter’ and you will notice the interpreter appearing on the bottom right corner of the screen. Now if you run the program again, the interpreter will print out the entire list first and then the first element within that list.
You can also loop through your list. To do this, you may need the help of a for loop. For example, consider this piece of code ‘for x in nums’. The general syntax would be ‘for variable in list’. Once you have done this, the IDE will automatically indent the cursor. Now you can print the variable by saying ‘print x’. The indentation in Python is very important as it suggests that you are in a particular block of code. So if your code isn’t indenting properly, you need to check your code as there may be some errors. If you run this code you will see that the for loop is being executed and all the elements in the list are being printed out.
You can add some operators also to your list. All you need to do is add nums.append or pop or any other operation that you like. If you don’t know exactly what may happen, look at the screen on the top right corner and you will find the description of whatever operation you have chosen. You can use these functions for many purposes and have a large number of variations and manipulations of your list.
Introduction to tuples
Tuple is a data type that is very much similar to the data type lists. The only difference between the two is that a list is mutable, which means that it can be grown and shrunk whereas a tuple is not mutable. A tuple cannot be modified after creation. However, you could redefine a tuple. A tuple is denoted by a pair of parenthesis in Python. For example, you can redefine the list nums as a tuple by simply replacing the square brackets with parenthesis. If you want to find the data type that you are working with, all you need to do is enter ‘print type(nums)’ and run the program. This will show you the data type you are working with.
You may notice that Python now reads the previous list as a tuple after being redefined. As a result, the earlier portion of the code has been garbage collected. You can also perform many similar functions on tuples as with the list. For example, you can loop through the tuple much like a list. But the operations that can be made on the tuple are less as compared to the lists.
Dictionaries
Dictionaries are nothing but data structures that contain unordered key value pairs. You are not allowed to have duplicates in keys when dealing with dictionaries. However, any duplicates in value are allowed. A dictionary is denoted with flower braces. For example consider the following dictionary “states = {‘CO : Colorado’ , ‘UT: Utah’ , ‘WA : Washington’}” In this example, CO, UT and WA are the keys and the state names are the values. Notice that each of these are paired with a colon.
Like lists and tuples, you can also loop through dictionaries. However, the process of looping is a bit different. The best way to loop through a dictionary is to use this following format “for k,v in states.iteritems” the k and v here represent the key and value. In the next line you can add “print k, v”. When you run this, you will notice the key and variables being printed on your output. There is no specific way to controlling the order of a dictionary unless you extract the keys, sort them in a particular order, and then use the loop. Adding a simple ‘keys.sort()’ before you print the result will put the result in an ASCII order. You can also perform a range of other functions on dictionaries such as get, values and so on. However, there is one rule you need to follow when creating dictionaries and that is the key should always be an immutable object.
This is covered in our Python curriculum. Click here to review our Python training courses
Please Share This Knowledge With Others!