Python - 1 Data Type

A numerical type:

(1), integer, Boolean, float, E notation
the PS: 1, boolean: ture (value 1), flase (value 0)
(2), a plus sign (+): join operator (left and right type needs to be consistent)

>>> temp = (1,2,3,4,5,6,7,8,9)
>>> temp = temp[:2] + temp[5:] #左右两边都是一个元组
>>> temp
(1, 2, 6, 7, 8, 9)

Multiplication (*): repetition operators

Second, the data type:

2.1 cast:

int (), float (), str () ( character)
input (print): ps: print the output will automatically wrap
a single multi-line comments:
A, the first row must declare first coding
example: # - - Coding : cp936 - -
single-line comment with a sufficient #
b, multi-line comment with the three single quotes:

>>>"""注释1
注释2
注释3"""
print("89")
2.2, Digital

(1)、

Here Insert Picture Description

(2), using a single underscore (not multiple) digital improve readability as a separator, can appear anywhere, but can not be at the beginning and end, you can not use a plurality of successive underscores

>>>1_000_000
>1000000
>>>1_2_3
>123
>>>1_2 + 3_4j
>(12+34j)
>>>1_2.3_45
>12.345

Python standard library Fraction objects support operations and scores
Here Insert Picture Description

2.3, operator precedence:

First multiplication and division, after the subtraction, the front bracket, a left binary fear afraid
exponentiation> sign> arithmetic operators> Comparison operators> Logical Operators
Logical Operators : and: "and" means or: "or" in It means not: give a contrast with the Boolean operator value

  >> not ture
ameError: name 'ture' is not defined   
        >>not True
    False  
    >>not 0
    True 
      >> not 4   
       False

2.4、语句(if、while、for)

(1) IF conditional statement :

if 条件:
   elif 条件:
   else:

Ternary operator (on if): eg, small = x if x <y else y
is equivalent to

if x<y:  
small=x  else:  
small=y  

2) the while loop : syntax:

***while*** 条件:
         条件为Ture时执行

. 3) for loop :
first introduced: *** range () *** built-in function
range ([start], stop, [step = 1])

for i in range(1, 10, 2):
    print(i)
2.5, the new function expression:

Develop new functions :
(1), of the type () : define the variable type

>>>type('asdf')
      <class 'str'>

(2), isintance (variable 1, variable 2) : Comparison of two types of variables, and outputs a boolean value

 >>>isinstance(a,str)
 False
 >>>isinstance(a,int)
 True

(3), the INPUT ( "Comment:")
output type is: Comments :( something to be entered)
(4), assertion (the Assert) : a formula to determine whether the Ture, is not the case will complain
(5) for i Range in (i equation containing PS: (a, b) represents ordered from a to b (a, b, c) represents ordered from a to b, c time interval)
(. 6), BREAK : out of small cycles;
countine : out of a cycle.
(. 7), the append : behind the array with the corresponding elements
Links: https://blog.csdn.net/chiyiwei7384/article/details/83988712
(. 8), STR (obj) function :( to the obj as a overall, returns a string) is adapted to convert the object into human-readable form.
For example, suppose we want to Zhu Rensheng happy day, might write code like this:

age=20
message="Happy " + age + "th Btrthday!"
prtnt(message)

But if you run the code, it will lead to error.

In Python, it seems, age, it means that the value could be 20, it could be a character 2 and 0.

Such as above using integers in the string, the need to explicitly as the integer string. That call str () function:

age=20
message="Happy " + str(age) + "th Birthday!"
print(message)

Output: Happy 20th Birthday!
(8), Open () : Python Open () function is used to open a file, create a file object associated method for reading and writing before they can call it
related links: HTTP: // the WWW. runoob.com/python/python-func-open.html

open(name[, mode[, buffering]])

Parameter Description:

name: a string value containing the name of the file you want to access.

mode: mode determines the open file modes: read-only, write, append and so on. See a complete list of all possible values ​​as follows. This parameter is not mandatory, the default file is read-only access mode ®.

buffering: If the value of buffering is set to 0, there will be storage. If the value of buffering take 1, the line will register to access the file. If the value is an integer greater than 1 of buffering, indicating that this is the buffer size of the storage area. If negative, the buffer size for the system default parking zone.

Here Insert Picture Description

2.6, a list (List) (typically in square brackets [] elements cohabitation), tuple (tuple) (usually with parentheses ()), the string (String [])
2.6.1 List: mark []

1 , define : List (definition list (list) name) = [variable 1, variable 2, variable 3]
2 , to the list insert elements :
(1) List (.) The append (variable 4): inserted at the end of XXX (insert) 4 variable (only a single element is added)
(2) List (.) Extend (4 variables, variables 5): XXX is inserted at the end of 4 variables and variable 5 (adding multiple variables)
(. 3) List (.) INSERT (position of elements)
the PS: additive elements can not be achieved with +

>>> number = ["小甲鱼","迷途"] 
>>>> number.append("你好啊")
>>> number ['小甲鱼', '迷途', '你好啊']
>>> number.append(7,8)        #由于append只能插入一个元素,所以报错 
Traceback (most recent call last):   File "<pyshell#3>", line 1, in <module> 

>>> number.extend([1,2]) 	#用extend在末尾插入一个数组 
>>> number ['小甲鱼', '迷途', '你好啊', 1, 2] 
>>> number.insert(1,1) 	#用insert插入一个元素 

>>> number ['小甲鱼', 1, '迷途', '你好啊', 1, 2]
 >>> number.insert(1,[1,2])	#用insert插入一个数组 
 >>> number ['小甲鱼', [1, 2], 1, '迷途', '你好啊', 1, 2] 
 >>> number.insert(0,"你好啊")	#用insert插入一个字符串(需要用(“”)) 
 >>> number ['你好啊', '小甲鱼', [1, 2], 1, '迷途', '你好啊', 1, 2] 

3 , obtaining elements:
(1) index value index : list index (position) (): Get the element to a location
list index (element, start, final) (.) : Returns the position parameter in the list
(2) XXX [3]: obtaining a third value
4 , remove elements:
(. 1) remove (element)
(2) del list [position]: delete element XXX position
del XXX: delete the entire list
(.) (3) list POP : delete and displays a list of the last element of an
element of the list to delete a location: list pop (position) (.)

>>> name = ["你","最","近","好","吗"]
>>> name.pop()
'吗'
>>> name.pop(1)
'最'
>>> 

5 , a list of slice : i.e., a list of copy
(1) list [start: final ]: Output start position to the last position between the elements (including but not including the beginning of the last)
List [:]: copy the entire list

>>> list = [1,2,3,4,5,6,7]
>>> list[5:]
[6, 7]
>>> list[:5]
[1, 2, 3, 4, 5]

(2)list[start: final: step]

>>> list = [1,2,3,4,5,6,7]
>>> list[0:4:2]
[1, 3]
>>> 

PS: When no starting position, the default is to open from 0
when no end position, the default is the last
list slicing list is the copy (copy) also said that a copy when you want to modify the original list and want to keep the original list, can be used directly slices acquired copy

. 6 , list (.) COUNT (position): calculated list an element of the number of occurrences
(.) List COUNT ( Sub [, Start [, End]]): Calculation based a table listing the number of occurrences of the
list (.) reverse (): the entire list situ flip (counter-attack)
list (.) the Sort (): the entire list from small to large sort
the Sort (fune, Key, reverse)
PS: the Sort default reverse is flase

>>> list1 = [5,4,3,2,1]
>>> list2 = list1[:]		#列表分片
>>> list3 = list1
>>> list2
[5, 4, 3, 2, 1]
>>> list3
[5, 4, 3, 2, 1]

>>> list1.sort()	# sort 整理整个列表
>>> list1		# list1 发生改变
[1, 2, 3, 4, 5]
>>> list2		# list2 由于分片(即拷贝)而不发生改变 
[5, 4, 3, 2, 1]
>>> list3		# list3 随 list1 变化而发生变化
[1, 2, 3, 4, 5]

#  sort (fune, key, reverse)
   #   PS:sort 默认reverse 为 flase
>>> list1 = [5,4,3,2,1]
>> list1.sort()
>>> list1
[1, 2, 3, 4, 5]
>>> list1.sort(reverse=True)
>>> list1
[5, 4, 3, 2, 1]

Development: a list of built-in functions highlights:
Here Insert Picture Description
Here Insert Picture Description
Here Insert Picture Description

7, tuples: flag "," (comma) and parentheses

Bring the shackles list of immutable
PS: Generally, elements which can not be changed; useful array (list), the trial is also substantially tuple

<class 'tuple'>
>>> 8*(8)	# 一个元素
64
>>> 8*(8,)	#(8,)加了逗号以后为一个元组
(8, 8, 8, 8, 8, 8, 8, 8)
>>> 

(1) may be utilized slice add elements, del deleting element
(2), the splice (+), repeating (*), a member of the operator, a logic operator ()
7 , format
(1), the format
is equivalent to a filling inside the sentence elements, but when required filling parameters (key parameter and the position parameter) as the filling point
(2), position parameter: {0}, {1}, {2} ...
keyword parameter: {A}, {b}, {c} ... ( which need guidance utilizing format "a = {}")

 >>>"{a} love {b}.{c}".format(a="I",b="FishC",c="com")
'I love FishC.com'
>>> "{0} love {b}.{c}".format("I",b="FishC",c="com")
'I love FishC.com'
>>> "{1} love {b}.{c}".format("I",b="FishC",c="com"
IndexError: tuple index out of range

  Markdown       0 字数     2 行数     当前行 2, 当前列 0

Guess you like

Origin blog.csdn.net/weixin_43901038/article/details/85172339