Python: 学习之路(3) - 基本数据类型: Numbers, Strings

学习一门新的编程语言,最基础的部分大同小异。基本上均包括这几个方面: 数据类型、常量与变量、操作符、函数、判断语句、循环语句...等等。...

本文也主要对这几个方面做简要介绍。在此之前,先了解下Python与其他高级编程语言不大一样的地方。

  1. Python中没有main()函数的概念,从第一行开始,逐行解释执行。
  2. 关于代码块,我的大括号呢?

Python使用缩进划分代码块,而不使用大括号。实际上,Python程序员更喜欢称之为"代码组",而不是"代码块"。总之,大家了解下就可以了,二者在Python中一般是指同一含义。

代码组很容易识别,因为它们总是缩进的,。需要特别注意的是: 不要在Python中混用制表符(Tab)和空格!!!

Python是按照缩进的空格数来解释代码的,如果使用Tab键来缩进,可能会带来潜在的问题。如果习惯使用Tab键,建议在编辑器配置Tab键替换为4个空格

。对于Python程序员来说,这是约定俗成的做法。

事实上,大括号在Python中也是有用的,不过不是用来区分代码块,而是用来分隔数据,如:字典类型的数据。



下面进入正文部分,目前知识有限,后续如有新的有用的知识点,再进行补充。

  • Numbers
  • Strings
  • 常量与变量

其他复杂类型后续单独更新~



  • Numbers 数值

int 整数, 比如: 1,20,1000, -3, 0

0x前缀表示16进制数, 比如:0x12=18, 0xff = 255

0o前缀表示8进制数,比如0o12=10, 0o77 = 63

float浮点数, 比如: 1.2, -4.5, 科学计数法表示: 1.23x10^9 存储为:1.23e9,0.000015表示为:1.5e-5

bool 布尔型,值是True 或 False

complex复数

  • Strings 字符串

以单引号'或双引号"括起来的任意文本,比如: ‘abc’ , "abc", 两种都是正确的,字符串都只有'a', 'b', 'c'这三个字符。

字符串输出时,注意使用转义字符'\'.

<code>>>> 'I\'m OK.'  //使用转义字符\,输出单引号'
"I'm OK."
>>> "I'm OK." //或者使用双引号
"I'm OK."
>>> "Tom\tMary\tJane\tPeter"  
'Tom\tMary\tJane\tPeter'
>>> print("Tom\tMary\tJane\tPeter") //在print函数里,\n才生效
Tom	Mary	Jane	Peter
>>> >>> "Tom\nMary\nJane\nPeter"
'Tom\nMary\nJane\nPeter'
>>> print("Tom\nMary\nJane\nPeter") //在print函数里,\n才生效
Tom
Mary
Jane
Peter
>>> #seperate long strings in multiple lines  //#+string开头的表示注释
>>> print("Lily and Lucy are good friends. They are in the same class.")
Lily and Lucy are good friends. They are in the same class.
>>>#使用三组单引号'''...'''或者三组双引号"""..."""来进行拆行、分行输出。
>>> print("""Lily and Lucy are good friends.
They are in the same class.""")
Lily and Lucy are good friends.
They are in the same class.
>>> print('''Lily and Lucy are good friends.
They are in the same class.''')
Lily and Lucy are good friends.
They are in the same class.
>>> print('''Lily
Lucy
Mary''')
Lily
Lucy
Mary
>>> /<code>

读取字符串长度:

<code>>>> len('Hello')
5
>>> len("world!")
6/<code>

字符串里的字符的索引关系如下:

Python: 学习之路(3) - 基本数据类型: Numbers, Strings

取自Python manual手册中的示例

举例说明:

<code>>>> word = 'Python'
>>> print(word)
Python
>>> word
'Python'
>>> word[0]
'P'
>>> word[2]
't'
>>> word[5]
'n'
>>> word[6]
Traceback (most recent call last):
  File "", line 1, in 
    word[6]
IndexError: string index out of range
>>> word[-1]
'n'
>>> word[-3]
'h'
>>> word[-5]
'y'
>>> word[-6]
'P/<code>

字符串的其他操作可以help(str)查看. 或者查看manual手册。

<code>>>> help(str)
Help on class str in module builtins:

class str(object)
 |  str(object='') -> str
 |  str(bytes_or_buffer[, encoding[, errors]]) -> str
 |  
 |  Create a new string object from the given object. If encoding or
 |  errors is specified, then the object must expose a data buffer
 |  that will be decoded using the given encoding and error handler.
 |  Otherwise, returns the result of object.__str__() (if defined)
 |  or repr(object).
 |  encoding defaults to sys.getdefaultencoding().
 |  errors defaults to 'strict'.
 |  
 |  Methods defined here:
    __contains__(self, key, /)
 |      Return key in self.
 |  
 |  __eq__(self, value, /)
 |      Return self==value.
 |  
 |  __format__(self, format_spec, /)
 |      Return a formatted version of the string as described by format_spec.
 |  
 |  __len__(self, /)
 |      Return len(self).
 |  
 |  __sizeof__(self, /)
 |      Return the size of the string in memory, in bytes.
 | .........//and so on/<code>
  • 常量与变量

这里必须介绍下变量了。

顾名思义,与数学中的概念一致。变量可以通过变量名访问,其值时可以改变的。常量的值不能被改变的。在C/C++中,我们一般用全大写字母来定义常量。

而在Python中,不支持const, 不支持#define, enum, 其实并没有严格意义上的常量。大多数人所以为的Python中的常量,基本上都是可以修改的。

几个常用的内置常量了解下即可,比如:布尔值 True和False, 可以赋值给bool类型的变量;空值None,经常用于表示空值,给 None 赋值是非法的并会引发 SyntaxError。

下面我们来认识下变量。

Python中的变量不需要预声明,在第一次使用变量时,变量就会立即存在。Python变量从所赋对象的类型得到自己的类型信息。

举例说明:

<code>>>> a=1
>>> a
1
>>> b=c=a
>>> b
1
>>> c
1
>>> a=3
>>> a
3
>>> b
1
>>> c
1
 >>> name = 'May'
>>> print(name)
May
>>> alias = name
>>> print(alias)
May/<code>

先定义了一个变量a, 给它赋值为整数1, 然后用"b=c=1“, 给b和c赋值,都指向1这个对象。然后修改了a的值为3,结果是,只有a的值变了,b和c的值没变,仍为1.

第二个例子,定义了一个对象name, 用字符串"May"赋值。

这是因为:Python中一切皆对象,此对象并不是C++/Java中的对象,有所不同的是,Python不需要创建类,才能创建对象。所以,Python基于对象,但不是纯粹的面向对象。

在上例中,"May", 1, 3 皆是对象。在定义变量a的时候,它指向对象1,后来修改a的值,指向了对象3, 而b,c所指的对象并没有发生改变,所以仍然指向对象1, 值没变。

Python: 学习之路(3) - 基本数据类型: Numbers, Strings


相关链接:

上一篇:Python: 学习之路(2) - 起步

下一篇:Python学习之路(4) - 基本数据类型: Lists, Tuples


Never too late to learn...to be continued...


分享到:


相關文章: