【python自学】(一)-----基本概念

常量

python中数有4种类型:

整数

长整数

浮点数:例如52.3E-4表示52.3 * 10-4。

复数:(-5+4j)

字符串

  • 使用单引号('):所有的空白,空格和制表符都照原样保留。如 'Quote me on this';
  • 使用双引号("):作用和使用单引号相同;
  • 使用三引号('"):可以指示一个多行的字符串。可以在三引号内自由使用单引号和双引号。例如:
'''This is a multi-line string. This is the first line.
This is the second line.
"What's your name?," I asked.
He said "Bond, James Bond."
'''
  • 转义符:对于'What's your name?'的写法在python中需要写作'What\'s your name?';同时在行末使用一个\相当于下一行继续,并非写作新的一行,例如:
"This is the first sentence.\
This is the second sentence."

相当于:

"This is the first sentence. This is the second sentence."
  • 自然字符串

如果不需要如转义符那样特别处理的字符串,那么需要指定一个自然字符串,自然字符串通过给字符串加上前缀r或者R来指定。例如r"Newlines are indicated by \n"

  •  Unicode字符串

Unicode是书写国际文本的标准方法。若使用中文或者其他文本语言,需要在字符串前加上前缀U或者u。例如: u"这是一个中文字符串"; 

  • 注意

字符串是不可变的;如果将两个字符串相邻放着,会被python自动级联,例如:'What\'s' 'your name?'会被自动转为"What's your name?";

变量

标识符命名:变量在命名时需要遵循标识符命名的规则。

数据类型

通常是数或者字符串,也可以是自己构建的类

对象

 

 

猜你喜欢

转载自blog.csdn.net/m0_38103546/article/details/81293966