python basic grammar _2 basic data types

 
Outline
  1. Number (Digital)
  2. String (String)
  3. List (list)
  4. Tuple (tuple)
  5. Sets (collections)
  6. Dictionarys (dictionary)
  7. Data type conversion python
text
Number (Digital)
Python3 support int, float, bool, complex (plural).
In Python 3, there is only one integer type int, expressed as a long integer, no python2 of Long.
Python is used to store digital data type value.
It is not allowed to change the data type, which means that if you change the value of the digital data type, will be re-allocated memory space.
Built-in type () function can be used to query object type variable points.
The difference is that isinstance and type:
  • type () does not think that is a subclass of the parent type.
  • isinstance () will be considered sub-class is a parent class type.
Note: Python2 is no Boolean type, which represents the number 0 with False, represented by 1. True. To Python3 in the True and False is defined as a keyword, but the value 0 or 1 and, they may be digital addition.
 
note:
  • 1, Python may for multiple variable assignments, such as a, b = 1, 2.
  • 2, a variable may be directed by assigning different types of objects.
  • 3, the division value (/) always returns a floating point number, to obtain an integer // operator use.
  • 4, upon mixing calculations, Python integer will be converted into floating-point numbers.
  • Python also supports complex, the complex composed of a real part and an imaginary number part, may be used a + bj, or Complex (a, b) indicates, the real part and the imaginary part b is a floating-point
  • python 2.x inside, // is floor division, / if there is a floating-point number to get a decimal, if two integers are also in addition to floor
  • python 3.x inside, // is floor division, / are on both sides of the decimal matter is not integer.
  • In interactive mode, the last expression result is output is assigned to the variable _
  • 数学函数http://www.runoob.com/python3/python3-number.htmlsqrt(x)round(x [,n])
  • 随机函数:choice(seq)shuffle(lst)
  • 三角函数:cos(x)sin(x)
  • 数学常量:

 

 
String(字符串)
Python中的字符串用单引号(')或双引号(")括起来,同时使用反斜杠(\)转义特殊字符。
索引值以 0 为开始值,-1 为从末尾的开始位置。
变量赋值时,str='abc' 'def'打印出来是字符串,str='abc','def'打印出来是元组
加号 (+) 是字符串的连接符, 星号 (*) 表示复制当前字符串,紧跟的数字为复制的次数。实例如下:

 

 
反斜杠的使用
1、使用反斜杠(\)转义特殊字符,如果你不想让反斜杠发生转义,可以在字符串前面添加一个 r,表示原始字符串:

 

2、反斜杠(\)可以作为续行符,表示下一行是上一行的延续。也可以使用 """...""" 或者 '''...''' 跨越多行。
 

 

 

 

 
注意:
  • 1、反斜杠可以用来转义,使用r可以让反斜杠不发生转义。
  • 2、字符串可以用+运算符连接在一起,用*运算符重复。
  • 3、Python中的字符串有两种索引方式,从左往右以0开始,从右往左以-1开始。
  • 4、Python中没有单独的字符类型,一个字符就是长度为1的字符串
  • 5、Python 字符串不能被改变。向一个索引位置赋值,比如word[0] = 'm'会导致错误。
详解:
Python转义字符
Python字符串运算符
Python字符串格式化
 
      %s
 格式化字符串
      %d
 格式化整数
 

 

 
Python2.6 开始,新增了一种格式化字符串的函数 str.format(),它增强了字符串格式化的功能。
Python三引号
解决当需要一块HTML或者SQL时,这时用字符串组合,特殊字符串转义将会非常的繁琐
Unicode 字符串
在Python2中,普通字符串是以8位ASCII码进行存储的,而Unicode字符串则存储为16位unicode字符串,这样能够表示更多的字符集。使用的语法是在字符串前面加上前缀 u。
在Python3中,所有的字符串都是Unicode字符串。
Python 的字符串内建函数
 
List(列表)
List(列表) 是 Python 中使用最频繁的数据类型。
列表可以完成大多数集合类的数据结构实现。列表中元素的类型可以不相同,它支持数字,字符串甚至可以包含列表(所谓嵌套)。
和字符串一样,列表同样可以被索引和截取,列表被截取后返回一个包含所需元素的新列表。
索引值以 0 为开始值,-1 为从末尾的开始位置。
加号(+)是列表连接运算符,星号(*)是重复操作。如下实例:

 

注意:
  • 1、List写在方括号之间,元素用逗号隔开。
  • 2、和字符串一样,list可以被索引和切片。
  • 3、List可以使用+操作符进行拼接。
  • 4、List中的元素是可以改变的。
 
Tuple(元组)
元组(tuple)与列表类似,不同之处在于元组的元素不能修改。元组写在小括号( ())里,元素之间用逗号隔开。

 

 

 

注意:
  • 1、把字符串看作一种特殊的元组,元组的元素不能修改。
  • 2、元组也可以被索引和切片,方法一样。
  • 3、注意构造包含0或1个元素的元组的特殊语法规则。
  • 4、元组也可以使用+操作符进行拼接。
  • 5、虽然tuple的元素不可改变,但它可以包含可变的对象,比如list列表。
  • 6、string、list和tuple都属于sequence(序列)。
 
Set(集合)
集合(set)是一个无序不重复元素的序列。
基本功能是进行成员关系测试和删除重复元素。
可以使用大括号 { } 或者 set() 函数创建集合,注意:创建一个空集合必须用 set() 而不是 { },因为 { } 是用来创建一个空字典。

 

Dictionary(字典)
字典(dictionary)是Python中另一个非常有用的内置数据类型。
列表是有序的对象集合,字典是无序的对象集合。两者之间的区别在于:字典当中的元素是通过键来存取的,而不是通过偏移存取。
字典是一种映射类型,字典用"{ }"标识,它是一个无序的 键(key) : 值(value)对集合。
键(key)必须使用不可变类型。
在同一个字典中,键(key)必须是唯一的。

 

构造函数 dict() 可以直接从键值对序列中构建字典如下:

 

另外,字典类型也有一些内置的函数,例如clear()、keys()、values()等。
注意:
  • 1、字典是一种映射类型,它的元素是键值对。
  • 2、字典的关键字必须为不可变类型,且不能重复。
  • 3、创建空字典使用 { }。
 
Python数据类型转换
数据类型的转换,你只需要将数据类型作为函数名即可。
以下几个内置的函数可以执行数据类型之间的转换。这些函数返回一个新的对象,表示转换的值。

 

 
 
 

Guess you like

Origin www.cnblogs.com/TomBombadil/p/10977943.html