python学习笔记1---python的基本数据类型

Number:数字

1)整型与浮点型

整数:int(没有short、int、long之分)
浮点数:float(python里面没有单精度和双精度之分)
>>> print('hello world')
hello world
>>> 1
1
>>> 133434
133434
>>> type(1)
<class 'int'>
>>> type(-1)
<class 'int'>
>>> type(1.1)
<class 'float'>
>>> type(1.111111111111111111)
<class 'float'>
>>> type(1+0.1)
<class 'float'>
>>> type(1+1)
<class 'int'>
>>> type(1+1.0)
<class 'float'>
>>> type(1*1)
<class 'int'>
>>> type(1*1.0)
<class 'float'>
>>> type(2/2)
<class 'float'>
>>> type(2//2)
<class 'int'>
>>> 2/2
1.0
>>> 2//2
1
>>> 1//2  //表示整除
0

2)10、2、8、16进制

>>> 10进制,2进制,8进制,16进制
SyntaxError: invalid character in identifier
>>> 0,1,2,3。。。。9,10
SyntaxError: invalid character in identifier
>>> 0,1,10
SyntaxError: invalid character in identifier
>>> 0,1。。。。7,10
SyntaxError: invalid character in identifier
>>> 0,1,2.。。。。。。。9,A,B,C,D,E,F
SyntaxError: invalid character in identifier
表示方法:
二进制(0b10)
八进制(0o10)
十六进制(0x10)
>>> 0b10
2
>>> 0b11
3
>>> 0o10
8
>>> 0o11
9
>>> 0x10
16
>>> 0x11
17
进制转换
  • 转换为二进制
>>> bin(10)
'0b1010'
>>> bin(0o7)
'0b111'
>>> bin(0xE)
'0b1110'
  • 转换为十进制
>>> int(0b111)
7
>>> int(0o77)
63
  • 转换为十六进制
>>> hex(888)
'0x378'
>>> hex(0o7777)
'0xfff'
  • 转换为八进制
>>> oct(0b111)
'0o7'
>>> oct(0x777)
'0o3567'

3)bool 布尔类型(数字类型的一种):表示真、假

>>> True
True
>>> False
False
>>> true
Traceback (most recent call last):
  File "<pyshell#2>", line 1, in <module>
    true
NameError: name 'true' is not defined
>>> false
Traceback (most recent call last):
  File "<pyshell#3>", line 1, in <module>
    false
NameError: name 'false' is not defined
>>> type(True)
<class 'bool'>
>>> type(False)
<class 'bool'>
>>> int(True)
1
>>> int(False)
0
>>> bool(1)
True
>>> bool(0)
False
>>> bool(2)
True
>>> bool(-1.1)
True
>>> bool(0b01)
True
>>> bool(0b0)
False
>>> bool('abc')
True
>>> bool('')
False
>>> bool([1,2,3])
True
>>> bool([])
False
>>> bool({1,1,1})
True
>>> bool({})
False
>>> bool(None)
False

4)complex 复数

>>> 36j
36j

str 字符串

1)单引号

>>> 'hello world'
'hello world'
>>> 'let\'s go'
"let's go"

2)双引号

>>> "hello world"
'hello world'
>>> 'let's go'
SyntaxError: invalid syntax
>>> "let's go"
"let's go"

3)三引号(多行字符串)

>>> '''
hello world
hello world
hello world
'''
'\nhello world\nhello world\nhello world\n'
>>> """
hello world
hello world
hello world
"""
'\nhello world\nhello world\nhello world\n'
>>> """hello world\nhello world\nhello world"""
'hello world\nhello world\nhello world'
>>> print("""hello world\nhello world\nhello world""")
hello world
hello world
hello world
>>> print('''hello world\nhello world\nhello world''')
hello world
hello world
hello world
>>> print("hello world\nhello world\nhello world")
hello world
hello world
hello world
>>> """hello world
hello world"""
'hello world\nhello world'
>>> 'hello
SyntaxError: EOL while scanning string literal
>>> 'hello\
world'
'helloworld'

4)转义字符(特殊的字符)

无法“看见”的字符
与语言本身语法有冲突的字符
  • n 换行
  • ' 单引号
  • t 横向制表符
  • n 换行
  • r 回车
>>> print('hello \n world')
hello 
 world
>>> print("'hello \n world'")
'hello 
 world'
>>> print("'hello \\n world'")
'hello \n world'
>>> print('hello \\n world')
hello \n world
>>> print('c:\northwind\northwest')
c:
orthwind
orthwest
>>> print('c:\\northwind\\northwest')
c:\northwind\northwest
>>> print(r'c:\northwind\northwest')
c:\northwind\northwest
>>> r'C:\Windows'
'C:\\Windows'
>>> R'C:\Windows'
'C:\\Windows'
//字符串前加上“r”以后就不是一个普通字符串,而是一个原始字符串
>>> print(r' let 's go')
      
SyntaxError: invalid syntax
//这里的单引号不是成对出现,已经不是一个字符串,加上“r”也不会是原始字符串

字符串运算 一

>>> "hello"
'hello'
>>> "world"
'world'
>>> "hello world"
'hello world'
>>> "hello"+"world"
'helloworld'
>>> "hello"*3
'hellohellohello'
>>> "hello" * "world"
Traceback (most recent call last):
  File "<pyshell#5>", line 1, in <module>
    "hello" * "world"
TypeError: can't multiply sequence by non-int of type 'str'
>>> "hello world"[0]
'h'
>>> "hello world"[3]
'l'
>>> "hello world"[4]
'o'
>>> "hello world"[5]
' '
>>> "hello world"[-1]
'd'
>>> "hello world"[-3]
'r'
//[-n]表示从字符串的末尾往前数n次得到的字符

字符串运算 二

>>> "hello world"[6]
'w'
>>> "hello world"[-5]
'w'
>>> "hello world"[0:4]
'hell'
>>> "hello world"[0:5]
'hello'
>>> "hello world"[0:-1]
'hello worl'
//这里的-1表示步长
>>> "hello world"[0:-3]
'hello wo'

字符串运算 三

>>> "hello world"[6:10]
'worl'
>>> "hello world"[6:11]
'world'
>>> "hello world"[6:20]
'world'
>>> "hello world"[6:-1]
'worl'
>>> "hello world"[6:0]
''
>>> "hello world"[6:-0]
''
>>> "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]
'hello python java c# javascript php '
>>> "hello python java c# javascript php ruby"[0:-4]
'hello python java c# javascript php '
>>> "hello python java c# javascript php ruby"[-4:]
'ruby'

相关推荐