Python Course notes

Source: Internet
Author: User

Python variable principle: Numeric-based, numbers are stored in memory and assigned to different variables. Just the opposite of C.

In Python, there are 3 built-in data structures: lists, tuples, and dictionaries.
1. 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. Items in the list. The items in the list should be enclosed 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, the type can be changed, and the list can be nested.
Instance:
#coding =utf-8
animalslist=[' Fox ', ' Tiger ', ' rabbit ', ' snake '
print "I don t like these", Len (animalslist), ' animals ... '


For items in Animalslist:
Print items,


print "\ n after Operation"
#对列表的操作, add, remove, sort
Animalslist.append (' pig ')
Del Animalslist[0]
Animalslist.sort ()
For I in range (0,len (animalslist)):
Print Animalslist[i],
Results:
I don ' t like these 4 animals ...
Fox Tiger Rabbit Snake
After operation
Pig Rabbit Snake Tiger
2. Tuples
Ganso and lists are very similar, but tuples are immutable. That is, you cannot modify tuples. Tuples are defined by a comma-delimited list of items in parentheses. Tuples are typically used when a statement or user-defined function can safely take a set of values, that is, the value of the tuple being used does not change. Tuples can be nested.
>>> zoo= (' Wolf ', ' Elephant ', ' penguin ')
>>> zoo.count (' Penguin ')
1
>>> zoo.index (' Penguin ')
2
>>> zoo.append (' pig ')
Traceback (most recent):
File "", Line 1, in
Attributeerror: ' Tuple ' object has no attribute ' append '
>>> del Zoo[0]
Traceback (most recent):
File "", Line 1, in
TypeError: ' Tuple ' object doesn ' t support item deletion
3 Dictionaries
The dictionary is similar to the Address book where you find the address and contact details by contact name, i.e. we associate the key (first name) with the value (details). Note that the key must be unique, as if two people happen to have the same name, you cannot find the correct information.
Key-value pairs are tagged in the dictionary in such a way that: D = {key1:value1, key2:value2}. Note that their key/value pairs are separated by colons, and each pair is separated by commas, all of which are included in curly braces. Also, remember that the key/value pairs in the dictionary are not sequential. If you want a particular order, then you should sort them yourself before using them.
Instance:
#coding =utf-8
Dict1={' Zhang ': ' hellos ', ' Wang ': ' Wangbaoqiang ', ' li ': ' Bingbing ', ' Zhao ': ' Zhao Wei '}
#字典的操作, add, delete, print
dict1[' Huang ']= ' Huang Jiaju '
Del dict1[' Zhao ']
For Firstname,name in Dict1.items ():
Print Firstname,name
Results:
Li Bingbing
Wang Wangbaoqiang
Huang Huang Jiaju
Zhang Hellos


View the modules defined in the Python installation directory
RPM-QL python command lists the Python installation directory, you can see many defined modules

Python Course notes

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.