Python Learning Notes

Source: Internet
Author: User

As a scripting language, the Python language is well-formed, with some similarities to the compilation language, formatted output, and strong typing. The feeling is not very easy to use, at the same time, I have been understanding it is not very clear. Until you build your own Python environment, it's really convenient to find python in some ways. First of all, the powerful module pack allows you to easily complete the function without having to reinvent the wheel, and the Python package is easy to install (Python setup.py install). Secondly, Python syntax is very flexible, can avoid the painstaking construction of the required variable form, greatly reducing the human consumption. Well, this is the current superficial understanding of python. Next, to some scattered knowledge points record, convenient for future View O (∩_∩) o haha ~

1, if not X to determine the true and false

' If x is not none ' and ' if not X is None ' notation equivalent, recommended ' If X is not none '
The premise of using if not X is that you must be clear that x equals None, False, empty string "", 0, empty list [], empty Dictionary {}, and empty tuple () do not affect your judgment.
Reference: http://blog.csdn.net/sasoritattoo/article/details/12451359

2. ID and is
(Ob1 is OB2) equivalent to (ID (ob1) = = ID (ob2))

The difference between "= =" and "is": "= =" is comparable content, such as variable values equal; " Is ' is the comparison object's ID is equal. Here is the test:

>>> A = 1
>>> B = 1
>>> ID (a)
505300120
>>> ID (b)
505300120
>>> print (A = = b)
True
>>> print (A is B)
True
>>> A = 1.0
>>> B = 1.0
>>> ID (a)
11372688
>>> ID (b)
11372608
>>> print (a ==b)
True
>>> print (A is B)
False
When a, B is 1, the ID is the same, and the time of 1.0 is different, this situation will also appear in string string, that is, when a very short string of a, B and a very short, their ID value is the same, and very long does not;
You can get a simple conclusion: The interpreter did a little bit of optimization on a small int and a very short string, assigning only one object to the same ID.
3. Determine if the variable is in the list if x not in list;if x in list
4. List derivation [x for x in list]
5. Tuples are similar to lists, except that elements of tuples cannot be modified. You can also press the subscript value, etc.
Reference: http://www.runoob.com/python/python-tuples.html python tuples
6. Lists and tuples can be assigned consecutively
X, y = (up) or (x, y) = (UP)
x=1,y=2
# Assign values from a list
(r,g,b) = ["Red", "Green", "Blue"]
Assert r = = "Red"
assert g = = "Green"
Assert b = = "Blue"

# Assign values from a tuple
(x, y) = (up to)
assert x = = 1
Assert y = = 2
7, __init__.py: Can be empty, as long as it exists, it indicates that this directory should be treated as a package. Python can import modules and functions
Reference: Http://www.cnblogs.com/BeginMan/p/3183629.html deep Python (2): __init__.py usage
8, connect the database with mysqldb return tuples, you can configure the return list
9. Common functions
Range
>>>range (5)
>>>[0,1,2,3,4]
Join
>>> str = ' goujinping '
>>> ', '. Join (STR)
' G,o,u,j,i,n,p,i,n,g '
Split
>>> str = "Line1-abcdef \nline2-abc \nline4-abcd"
>>> Str.split ()
[' Line1-abcdef ', ' line2-abc ', ' LINE4-ABCD ']
Append
>>>alist = [123, ' xyz ', ' Zara ', ' abc ']
>>>alist.append (2009)
>>>print "Updated List:", alist
Updated List: [123, ' xyz ', ' Zara ', ' abc ', 2009]

10, the foot label is flexible, can be negative

Python Learning Notes

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.