python3 study notes - the basic data types

python3 basic data types are six kinds .
Including digital type Number, string type String, tuple type Tuple.
And a list of type List, dictionaries Dictionary, collection type Set.
Where the value has the type Number int, float, bool, complex four types.

此处回忆一下Java的内置基本数据类型。java的基本数据类型有三类八种。
包括数值类型,字符类型,布尔类型。
数值类型有byte,short,int,long,float,doube。
字符类型char,布尔类型boolean。

Number (Digital)

>>> a, b, c, d = 20, 5.5, True, 4+3j
>>> print(type(a), type(b), type(c), type(d))
<class 'int'> <class 'float'> <class 'bool'> <class 'complex'>

String (String)

python string may include a single or double quotes.
Taken string syntax is as follows:
the index value to the starting value 0, -1 from the end of the start position.
Here Insert Picture Description
Plus sign + is string connector, the asterisk * indicates a copy of the current string.
note

1. Python 没有单独的字符类型,一个字符就是长度为1的字符串。
2. Python中的字符串有两种索引方式,从左往右以0开始,从右往左以-1开始。
3. Python中的字符串不能改变。向一个索引位置赋值,比如word[0] = 'm'会导致错误。
此处回忆一下Java的字符串类型,Java字符串类型是引用类型,也是不可变的。
比如String s = "hello";  s = s+"world"; 此时在这段代码中,s原先指向一个String对象,内容是"Hello",然后我们对s进行了+操作,那么s所指向的那个对象是否发生了改变呢?答案是没有。这时,s不指向原来那个对象了,而指向了另一个String对象,内容为"Hello world!",原来那个对象还存在于内存之中,只是s这个引用变量不再指向它了。

Tuple (tuple)

Tuple written in parentheses (), the elements separated by commas. Tuple elements can not be changed.
Tuple element type may not be the same.
Similarly tuple string, can be indexed and labeled index from 0 at the start, -1 position from the end of. It may also be taken.
In fact, we can put a string of yuan as a special group. Because they are not variable.

>>>tup = (1, 2, 3, 4, 5, 6)
>>> print(tup[0])
1
>>> print(tup[1:5]) # 打印第2到第5个元素
(2, 3, 4, 5)
>>> tup[0] = 11  # 修改元组元素的操作是非法的
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'tuple' object does not support item assignment
>>>

Tuple structure contains zero or one element of the special, so there is some additional syntax rules:

tup1 = ()    # 空元组
tup2 = (20,) # 一个元素,需要在元素后添加逗号

List (list)

  1. List written in brackets [], the elements separated by commas.
  2. Type elements in the list may not be identical, which supports numbers, strings may even contain a list (called nesting).
  3. And like strings, and indexing the list can also be taken after the list is taken returns a new list containing required elements.
    Here Insert Picture Description
    And Python strings are not the same, the elements in the list can be changed:
>>>a = [1, 2, 3, 4, 5, 6]
>>> a[0] = 9
>>> a[2:5] = [13, 14, 15]
>>> a
[9, 2, 13, 14, 15, 6]
>>> a[2:5] = []   # 将对应的元素值设置为 [] 
>>> a
[9, 2, 6]

Python list may receive interception third parameter, the parameter is the role of the step taken, the following examples in index position 1 to 4 and the index step size is set to 2 (a spaced position) string taken:
Here Insert Picture Description
Here Insert Picture Description
If the third parameter reverse reading negative values. -1 indicates all reversed.
To be continued.

Published 68 original articles · won praise 25 · views 40000 +

Guess you like

Origin blog.csdn.net/sinat_32336967/article/details/104953760