Main data types in Python
- Digital
- String
- List
- Meta-group
- Dictionary
Digital
- Integral type
- Long integer (numbers followed by L or L)
- Floating-point type (the number is appended.) Or. 0)
- Plural type
There is no need to define a type beforehand in Python, it will determine the type based on the specific value
>>> NUM1 =123>>> type(num1) < type ' int ' >>>> type(123) < type ' int ' >>>> num2 =999999999999999999999999999999999>>> type(num2) < type ' Long ' >>>> num3 =123L>>> type(num3) < type ' Long ' >>>> NUM4 =123L>>> type(num4) < type ' Long ' >>>> F1 =123.>>> type(F1) < type ' float ' >>>> F2 =123.0>>> type(f2) < type ' float ' >>>> C =3.14J>>> type(c) < type ' Complex ' >>>> C1 =3.14J>>> type(C1) < type ' Complex ' >
String
使用引号定义的一组可以包含数字、子母、符号(非特殊系统符号)的集合。
String definitions
可以使用单引号,双引号和三重引号定义
>>>STR1 =' str1 '>>>STR2 ="STR2">>>STR3 ="" " str3 " "">>>STR4 ="' str4 '>>>STR5 ="' STR5 '">>>STR6 ="'"Str6"'"File"<stdin>", line1STR6 ="'"Str6"'"^syntaxerror:invalid syntax>>>STR7 =""Str7""File"<stdin>", line1STR7 =""Str7""^syntaxerror:invalid syntax>>>Str8 =' "" Str8 "" '>>>STR9 ="'"STR9""'>>>Str10 =' "'Str10' "'File"<stdin>", line1Str10 =' "'Str10' "'^syntaxerror:invalid syntax
那么该怎么用呢?其实一般情况下区别不大,除非引号中有特殊的符号,如下:
>>> Str11 =' Let 's Go' File ' <stdin> ', line 1 str11 = ' Let' s go '^SyntaxError: Invalid syntax>>> str12 ="Let's Go">>> Str13 ="Let ' s"Go" !"File"<stdin>", line1Str13 ="Let ' s"Go" !"^SyntaxError: Invalid syntax>>> Str13 ="Let's \" go\ "! ">>>PrintStr13 Let' s "Go"!>>> str14 = ' Let\' s \ "go\"! '>>>PrintStr14 Let' s "Go"!
"\"是转移字符"\n"代表换行,通过这个就可以打印出规定格式的字符串。但是有时候,这个看起来不够直观,而且难以控制,这时候就需要三重引号啦!~
>>> """ tom:... i am jack!... I miss U... bye~~""">>> print mail tom: i am jack! I miss U bye~~>>> mail‘ tom:\n i am jack!\n I miss U\n bye~~‘
String manipulation (slices and indexes)
a[start:end:step]分别代表:开始位置,结束为止(不包括在内),和步长
>>>a=' ABCDEFG '>>>Len(a)7>>>a[1]' B '>>>a[6]' G '>>>a[7]traceback (most recent call Last): File"<stdin>", Line 1,inch<module>indexerror:stringIndex out ofRange>>>a[0][1]traceback (most recent call Last): File"<stdin>", Line 1,inch<module>indexerror:stringIndex out ofRange>>>a[0]a[1] File"<stdin>", Line 1 a[0]a[1] ^syntaxerror:invalid syntax>>>a[0]+a[1]' AB '>>>a[-1]' G '>>>a[0:1]' A '>>>a[0:7]' ABCDEFG '>>>a[:7]' ABCDEFG '>>>a[:]' ABCDEFG '>>>a[1:]' BCDEFG '>>>a[0:5]' ABCDE '>>>a[0:5:0]traceback (most recent call Last): File"<stdin>", Line 1,inch<module>valueerror:slice Step cannot beZero>>>a[0:5:1]' ABCDE '>>>a[0:5:2]' Ace '>>>a[0:5:-2]"'>>>a[5:0:-2]' Fdb '
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Middle Valley Education Python data type