Concise Python Tutorials

Source: Internet
Author: User
Tags data structures sort in python

Brief introduction

The data structure is basically-they are structures that can handle some data. Or, they are used to store a set of related data.

There are three types of built-in data structures in Python-lists, tuples, and dictionaries. We'll learn how to use them and how they make programming easier.

List

A list is a data structure that handles a set of ordered items, that is, you can store a sequence of items in a list. Imagine you have a shopping list that says what you want to buy and you can easily understand the list. It's just that on your shopping list, you probably have everything on your own, and in Python, you separate each item with a comma.

The items in the list should be included in square brackets so that Python knows that you are specifying a list. Once you have created a list, you can add, delete, or search for items in the list. Since you can add or delete items, we say that the list is a mutable data type, that is, this type can be changed.

Quick start to objects and classes

Although I have been postponing discussion objects and classes, it is now possible to explain them a little better to understand the list. We will explore this topic in detail in the corresponding chapters.

A list is an example of using objects and classes. When you use the variable i and assign it a value, such as assigning an integer 5, you can assume that you have created an object (instance) I of the class (type) int. In fact, you can look at help (int) to better understand this.

A class also has a method, that is, to define a function for a class only. You can use these functions only if you have an object of that class. For example, Python provides a Append method for the list class, which lets you add an item at the end of the list. For example Mylist.append (' an item ') List MyList add that string. Note that you use the point number to use the object's method.

A class also has a domain, which is a variable defined only for a class. You can use these variables/names only when you have an object of that class. A class is also used by a dot number, such as Mylist.field.

Working with lists

Example 9.1 using a list

#!/usr/bin/python
# Filename: using_list.py
# This is my shopping list
shoplist = ['apple', 'mango', 'carrot', 'banana']
print 'I have', len(shoplist),'items to purchase.'
print 'These items are:', # Notice the comma at end of the line
for item in shoplist:
print item,
print '\nI also have to buy rice.'
shoplist.append('rice')
print 'My shopping list is now', shoplist
print 'I will sort my list now'
shoplist.sort()
print 'Sorted shopping list is', shoplist
print 'The first item I will buy is', shoplist[0]
olditem = shoplist[0]
del shoplist[0]
print 'I bought the', olditem
print 'My shopping list is now', shoplist

(source file: code/using_list.py)

Output

$ python using_list.py
I have 4 items to purchase.
These items are: apple mango carrot banana
I also have to buy rice.
My shopping list is now ['apple', 'mango', 'carrot', 'banana', 'rice']
I will sort my list now
Sorted shopping list is ['apple', 'banana', 'carrot', 'mango', 'rice']
The first item I will buy is apple
I bought the apple
My shopping list is now ['banana', 'carrot', 'mango', 'rice']

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.