Python Supplements (ii) __python

Source: Internet
Author: User

21, the Python list can be used in the Swap method
Mylist[i], mylist[j] = Mylist[j], mylist[i]
So we can swap two elements.
And also
A ^= b
b ^= A
A ^= b
22. Python Rounding and keeping decimals

#encoding: utf-8
import math
#向上取整
print Math.ceil (2.6)
3.0
#向下取整
print Math.floor (2.6)
2.0
#四舍五入
print round (2.6)
3.0
#这三个的返回结果都是浮点型

#将a保留到小数点后n位
Round (A, N)
Print (3.1415926, 3) #
3.142

23, Python copy (deep copy deepcopy and light copy copy)
Objects in Python are assigned by reference, and if you need to copy objects, you need to use the Copy module in the standard library.
A, copy.copy a shallow copy copies only the parent object, does not copy the object's inner child objects.
B, Copy.deepcopy deep Copy object and its child objects

>>> import copy >>> a = [1,2,3,4,[' A ', ' B ']] #原始对象 >>> b = A #赋值, passing objects References >>> C = copy.copy (a) >>> d = copy.deepcopy (a) >>> a.append (5) >>> a[4].append (' C ') >>> print ' a= ', a a= [1, 2, 3, 4, [' A ', ' B ', ' C '], 5] >>> print ' b= ', b b= [1, 2, 3, 4, [' A ', ' B ', ' C '], 5] >>> print ' c= ', c c= [1, 2, 3, 4, [' A ', ' B ', ' C ']] >>> print ' d= ', d d= [1, 2, 3, 4, [' A ', ' B ']]

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.