3.python 基本类型

  1. 代码是现实世界在计算机世界中的映射
  2. 写代码是现实世界中的事物用计算机语言来描述
  3. 基本数据类型
  4. number:数字,整数int,小数,浮点数float(单精度float,双精度double)
  5. `>>> 1
    1

type(1)
<class ‘int’>

type(-1)
<class ‘int’>

type(1.1)
<class ‘float’>

type(1.11111)
<class ‘float’>

1+0.1
1.1

type(1+0.1)
<class ‘float’>

type(1+1)

SyntaxError: invalid character in identifier

type(1+1)
<class ‘int’>

type(1+0.1)
<class ‘float’>

  1. type(1*1)
    <class ‘int’>

type(1*1.0)
<class ‘float’>

type(2/2)
<class ‘float’>

type(2//2)
<class ‘int’>

`

  1. 双斜杠是整除,单斜杠是除法
  2. 10进制,2进制,8进制,16进制
  3. 如何区分不同进制的数字
  4. `>>> 0b10 //2进制
    2

0b11
3

0o10 //8进制
8

0o11
9

0o12
10

0o13
11

0x10 //16进制
16

0x11
17

0x1f
31

11
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'
>>> 

布尔类型和复数

  1. bool表示真和假
  2. 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({123})
     
SyntaxError: invalid character in identifier
>>> bool({})
False
>>> bool({123})
SyntaxError: invalid character in identifier
>>> bool(None)
False
>>> 

字符串单引号与双引号

  1. 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'       ///原始字符串
>>> 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值