- 代码是现实世界在计算机世界中的映射
- 写代码是现实世界中的事物用计算机语言来描述
- 基本数据类型
- number:数字,整数int,小数,浮点数float(单精度float,双精度double)
- `>>> 1
1
type(1)
<class ‘int’>type(-1)
<class ‘int’>type(1.1)
<class ‘float’>type(1.11111)
<class ‘float’>1+0.1
1.1type(1+0.1)
<class ‘float’>type(1+1)
SyntaxError: invalid character in identifier
type(1+1)
<class ‘int’>type(1+0.1)
<class ‘float’>
-
type(1*1)
<class ‘int’>
type(1*1.0)
<class ‘float’>type(2/2)
<class ‘float’>type(2//2)
<class ‘int’>`
- 双斜杠是整除,单斜杠是除法
- 10进制,2进制,8进制,16进制
- 如何区分不同进制的数字
- `>>> 0b10 //2进制
2
0b11
30o10 //8进制
80o11
90o12
100o13
110x10 //16进制
160x11
170x1f
3111
11`
>>> bin(10) //bin将其他进制转化为2进制
'0b1010'
>>> bin(0o7)
'0b111'
>>> bin(0xe)
'0b1110'
>>>
>>> int(0b111) //int将其他进制转化为10进制
7
>>> int(0o77)
63
>>> int(888)
888
>>> hex(888) //hex将其他进制转化为16进制
'0x378'
>>> hex(0x777)
'0x777'
>>>
>>> oct(0b111) //oct将其他进制转化为8进制
'0o7'
>>> oct(0x777)
'0o3567'
>>>
布尔类型和复数
- bool表示真和假
- complex表示复数
>>> True
True
>>> False
False
>>> type(True)
<class 'bool'>
>>> type(False)
<class 'bool'>
>>> int(True)
1
>>> int(False)
0
>>> bool(1)
True
>>> bool(0)
False
>>>
>>> bool('abc')
True
>>> bool('')
False
>>> bool([1,2,3])
True
>>> bool([])
False
>>> bool({1,2,3})
SyntaxError: invalid character in identifier
>>> bool({})
False
>>> bool({1,2,3})
SyntaxError: invalid character in identifier
>>> bool(None)
False
>>>
字符串单引号与双引号
- str字符串,
>>> 'hello , world'
'hello , world'
>>> '1'
'1'
>>> 1
1
>>> type(1)
<class 'int'>
>>> type('1
SyntaxError: EOL while scanning string literal
>>> type('1')
<class 'str'>
>>>
>>> "let's go"
"let's go"
>>> 'let"s go'
'let"s go'
>>> 'let\'s go' //转义字符
"let's go"
>>>
多行字符串
>>> '''
hello world
hello world
'''
'\nhello world\nhello world\n'
>>> //对于phthon,每行可插入79个字符
>>> """
hello
hello
hello
"""
'\nhello\nhello\nhello\n' //三引号,或者三个双引号表示回车换行
>>>
>>> print("""hello world\nhello world\n""")
hello world
hello world
>>>
转义字符
转义字符是一种特殊的字符,用以表示1.无法看见的字符
有一些与语言语法有冲突的字符需要用到转义字符
\n 表示换行
’ 单引号
\t 横向制表符
\r 回车
原始字符串
>>> print('hello \n world')
hello
world
>>> // \n是转义字符
>>> print('hello \\n world ')
hello \n world
>>>
>>> 'let 's go'
SyntaxError: invalid syntax
>>> 'let \'s go'
"let 's go"
>>> // 加一个反斜杠,
>>> print('c:\northwind\northwest')
c:
orthwind
orthwest
>>> print('c:\\northwind\\northwest')
c:\northwind\northwest
>>>
>>> print(r'c:\northwind\northwest')
c:\northwind\northwest
>>> //加r,得到一个原始字符串,所见即所得
>>> print(r'let's go')
SyntaxError: invalid syntax
>>> //字符串必须成对出现
字符串运算
>>> 'hello'+'world'
'helloworld'
>>> 'hello'*3
'hellohellohello'
>>> 'hello'*'world'
Traceback (most recent call last):
File "<pyshell#7>", line 1, in <module>
'hello'*'world'
TypeError: can't multiply sequence by non-int of type 'str'
>>>
>>> 'hello world'[0]
'h'
>>> 'hello world'[5]
' '
>>> 'hello world'[-1]
'd'
>>> 'hello world'[-6]
' '
>>>
>>> 'hello world'[0]
'h'
>>>
>>> 'hello world'[6]
'w'
>>> 'hello world'[-5]
'w'
>>>
>>> 'hello world'[0:4]
'hell'
>>> 'hello world'[0:5]
'hello'
>>>
>>> 'hello world'[0:-1]
'hello worl'
>>>
>>> 'hello world'[6:11]
'world'
>>> 'hello world'[6:15]
'world'
>>> 'hello world'[6:-1]
'worl'
>>> 'hello world'[6:]
'world'
>>>
>>> 'hello python java C# javascript php ruby'[6:]
'python java C# javascript php ruby'
>>>
>>> "hello python java c# javascript php ruby"[-4:]
'ruby'
>>>
>>> r'C:\windows'
'C:\\windows'
>>> R'C:\windows'
'C:\\windows' ///原始字符串
>>>