Python Learning Notes

Source: Internet
Author: User

Display Type

Python 3.6.1 (V3.6.1:69C0DB5, Mar, 17:54:52) [MSC v.1900 32bit (Intel)] on Win32type" Help","Copyright","credits" or "License"  forMore information.>>>ImportTypes>>>type ([])<class 'List'>>>> Type (1)<class 'int'>

The above try to operate in the REPL, if you want to display the type in the source file, add print

Import Types Print  = 123; Print (Type (a))

Common data structures

List

List is variable-length, can add elements dynamically, element type does not require uniform, orderly, repeatable

Create an empty list in two ways

Alist == List ()

Create a list with elements directly

alist = ['one', ' 3 ', True];

Append element at end

Alist.append ('four'#  API list.append (obj)

Add an element at the specified index location

' One point five ' # API List.insert (index, obj)

Querying an element at an index location

Element1 == alist[2]print(element1)print(element2)

Modify an element of an index location

' three '

Delete the element at an index location and use it when you know the index location

Alist = [1, 2, 3, 4= Alist.pop (1)  #  Delete API, parameter to delete the position, return value deleted element print(a)   #  2print#[1, 3, 4]alist.pop ()  #  remove end when parameter is not given  print#  [1, 3]

Remove an element without knowing the index position

Alist = [1, 2, 2, 3]alist.remove (#  Delete API, parameter is the element that you want to delete, if the element value is duplicated, only the top is deleted. No return value for print#[1, 2, 3]

Get list length

size = Len (alist)
Print (size)

Traverse List

 for inch alist:     Print # V is the element value

You get the index when you walk through it.

 for inch Enumerate (alist):     Print # idx is an index, Val is the element value

Reference: Http://stackoverflow.com/questions/522563/accessing-the-index-in-python-for-loops

The element that gets a value has several

' a ' ' a ','b']print#2print( List.count ('a'#2

Get maximum Element

Llist = [1, 2, 3, 4#  APIprint#  4

Mixed Type list Use this API with caution, such as int and STR mixed error

list = [1, 2, 3, 4,'a']max=max (list)Print(max) Run error Traceback (most recent call last): File"c:\Users\myname\Documents\pythonporjects\test.py", Line 5,inch<module>Max=max (list) TypeError:'>'  notSupported between instances of'Str'  and 'int'

Tuple

Once the elements of a tuple are determined to be immutable and can only be queried, the tuple itself cannot add or remove elements. element types do not require uniform, orderly, repeatable

Create an empty tuple in two different ways

Atuple == tuple ()

An empty tuple is not of much use so it is common to create elements that initialize a tuple

' a ' ' b ' )print(atuple)

Only one element of the tuple should be like this

Atuple = (1,)print(atuple)

Querying elements

' a ' ' b ' )print(atuple[0])print(atuple[1])

If you try to modify the element value, you will get an error

Atuple[0] = 0Traceback (most recent call last):  "c:\Users\myname\Documents\pythonporjects\ test.py" in <module>    ='tuple'  not support item assignment

Get the tuple length

t = (1, 2, 3= len (t) #apiprintln (size)

The element that gets a value has several

' a ' ' a ','b')print#  2print( T.count ('a'#  2

Get the largest element, refer to the list section

Tuple unpacking

t = (1, 2= tprint#  1print#  2

Reference Http://stackoverflow.com/questions/10867882/tuple-unpacking-in-for-loops

Set

Set is similar to a set in mathematics where elements are unordered and elements are not repeatable

There is only one way to create an empty set

Aset = set ()

Traversal set, because unordered so there is no index

 for inch aset:     Print (EL)

Iterate over a set that grows in duration

Http://stackoverflow.com/questions/28584470/iterating-over-a-growing-set-in-python

Create an empty dictionary, two ways

Adict == Dict ()

Traverse Dictionary

Traverse Key Only

 for inch adict:     Print # k

Traverse Key and value

 for inch Adict.items ():     Print (Key, value)

Reference Http://stackoverflow.com/questions/3294889/iterating-over-dictionaries-using-for-loops-in-python

Time and date processing

#!/usr/bin/python#-*-coding:utf-8-*-ImportTimea=time.time ()Print("timestamp", a) a= Time.strftime ("%y-%m-%d%h:%m:%s", Time.localtime ())Print(" now is", a) a=time.localtime () year=A.tm_yearPrint(" Year is", year)

More reference Python date and Time | Rookie Tutorial Http://www.runoob.com/python/python-date-time.html

Reference

Http://www.runoob.com/python/python-tutorial.html

http://www.liaoxuefeng.com/wiki/0014316089557264a6b348958f449949df42a6d3a2e542c000

Http://stackoverflow.com/questions/tagged/python

Https://www.dotnetperls.com/python

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.