python學習筆記1—python的基本資料型別

HughieSung發表於2019-02-16

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
```
`
hello world
hello world
hello world
`
>>> """
hello world
hello world
hello world
"""
`
hello world
hello world
hello world
`
>>> """hello world
hello world
hello world"""
`hello world
hello world
hello world`
>>> print("""hello world
hello world
hello world""")
hello world
hello world
hello world
>>> print(```hello world
hello world
hello world```)
hello world
hello world
hello world
>>> print("hello world
hello world
hello world")
hello world
hello world
hello world
>>> """hello world
hello world"""
`hello world
hello world`
>>> `hello
SyntaxError: EOL while scanning string literal
>>> `hello
world`
`helloworld`

4)轉義字元(特殊的字元)

無法“看見”的字元
與語言本身語法有衝突的字元
  • n 換行
  • ` 單引號
  • t 橫向製表符
  • n 換行
  • r 回車
>>> print(`hello 
 world`)
hello 
 world
>>> print("`hello 
 world`")
`hello 
 world`
>>> print("`hello \n world`")
`hello 
 world`
>>> print(`hello \n world`)
hello 
 world
>>> print(`c:
orthwind
orthwest`)
c:
orthwind
orthwest
>>> print(`c:\northwind\northwest`)
c:
orthwind
orthwest
>>> print(r`c:
orthwind
orthwest`)
c:
orthwind
orthwest
>>> 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`

相關文章