Python Learning---------Day2

Source: Internet
Author: User
Tags floor division

The fourth chapter introduces Python object types
Why use built-in types
Built-in objects make programs easier to write
Built-in objects are extended components
Built-in objects tend to be more efficient than custom data structures
Built-in objects are part of the language standard
Python's core data types
Digital
String
List
Dictionary
Meta-group
File
Other types (set, type, None, Boolean)
Digital
Import Math
Math.PI
MATH.SQRT (54)
Import Random
Random.random ()
Random.choice ([1,2,3,4])
String
Sequence operations
s= ' spam '
Len (s)
S[0]
S[1]
S[len (s) -1]== s[-1]
S[-2]
S[1:3] # shard operation
S[:-1]
s[:] #就是s
S+ ' xyz ' #字符串支持加号合并, a new string is synthesized from a string
S*8
The string is immutable--the value cannot be changed after its creation, for example, you cannot pass a
Changes the string, but you can always create a new string to assign the same variable name to it.
Value
Type-specific methods
S.find (' pa ') #函数返回传入子字符串的偏移量, not found in case of return-1
S.replace (' pa ', ' xyz ') #搜索并进行替换
Line= ' Aaa,bbb,ccccc,dd '
Line.split (', ')
s= ' spam '
S.upper () #大写
S.isalpha () # Judging is not the letter
Line=line.restrip () #去除换行符
Dir (S) can find all properties of an object
Ord (' A ') returns the ASCII code of a
Python allows strings to be included in single or double quotes, and it can also represent multiple lines of string in three quotation marks.
Form
Pattern matching
Import re
Match=re.match (' hellp[\t]*. *) World ", ' Hello Python World ')
Match.group (1)

Match=re.match ('/(. *)/(. *)/(. *) ', '/usr/home/lqs ')
Match.Groups ()
A list is an ordered collection of positions related to an arbitrary type of object, it does not have a fixed size, does not want a string, its
Size is Variable
l=[123, ' spam ', 1.23]
Len (L)
L[0]
L[:-1]
l+[4,5,6] #创建了一个新的列表
L # no change
L.append (' NI ')
L.pop (2)
m=[' BB ', ' AA ', ' CC '
M.sort ()
M.reverse ()
Bounds checking Python does not allow referencing elements that do not exist
Nesting
m=[[1,2,3],
[4,5,6],
[7,8,9]]
List parsing
COL2=[ROW[1] for fow in M]
[row[1] for row im M if row[1]%2==0]
Diag=[m[i][i] for i in [0,1,2]]
Doubles=[c*2 for C in ' spam ']
Dictionary
d={' food ': ' spam ', ' Qian ': 4, ' Color ': ' Pink '}
d[' food ']
d[' Qian ']+=1 dictionary with variability
a={}
a[' name ']= ' Bob '
a[' age ']=23
Re-visit nested
rec={' name ': {' first ': ' Bob ', ' last ': ' Snuth '},
' Job ': [' dev ', ' Mgr '],
' Age ': 40.5}
rec[' name ']
rec[' name ' [' Last ']
rec[' job '].append (' janitor ')
D={' A ': 1, ' B ': 2, ' C ': 3}
Ks=d>keys ()
Ks.sort ()
For key in KS:
Print key, ' = = ', D[key]

For key in Sorted (D):
Print key, ' = = ', D[key]
Iteration and optimization
Square=[x**2 for x in [1,2,3,4]]
Square=[]
For x in [1,2,3,4]:
Square.append (x**2)
Non-existent key if test
D.has_key (' F ')
If not D.has_key (' F '):
print ' missing '
Tuples are immutable sequences
T= (a)
File
F=open (' Data.txt ', ' W ')
F.write (' hello\n ')
F.write (' world\n ')
F.close ()
F=open (' Data.txt ')
Bytes=f.read ()
Print bytes
Bytes.split ()

Fifth chapter number
Numeric constants
Hybrid Type automatic upgrade
Traditional division, Floor division, and true Division
The built-in int function transforms a string of numbers into an integer, and can be defined by a second parameter to
The conversion of the number into the system
Int (' 0100 ', 8) int (' 0x40 ', 16)
Eval (s) converts an octal or hexadecimal string into a normal integer, but more overhead can cause
Security issues
Int (2.56) round (2.567) round (2.567,2)
Decimal number
From decimal Import Decimal
Decimal (' 0.1 ') +decimal (' 0.1 ') +decimal (' 0.1 ')-decimal (' 0.3 ')

The sixth chapter dynamic type Introduction
A=3
Create an object to represent the value 3
Create a variable A, if it has not yet been created
Connect the variable with the new object 3

A variable is an element of a system table that has a connection to an object
The object is a piece of memory allocated, and there is enough space to show the value that they represent
A reference is an automatically formed pointer from a variable to an object
Note issues with shared references
a=[1,2,3]
B=a
a=2

a=[1,2,3]
B=a
a[0]=2

a=[1,2,3]
b=a[:]
a[0]=2
Problem solving for dictionary shared references using the copy Deepcopy method

l=[1,2,3]
M=l
L==m TRUE
L is M TRUE
= = Tests that two referenced objects are sufficient to have the same value
The IS operator is the identity of the check object

Python Learning---------Day2

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.