Python基礎(chǔ)語法了解一下(一)

  • 數(shù)據(jù)類型
  • 容器類型
  • 強制類型轉(zhuǎn)換
  • 標識符
  • 變量賦值

數(shù)據(jù)類型 Base Types: integer, float, bollean, string, bytes

數(shù)據(jù)類型

1、整型 int: 123 , 0 , -121
2、浮點型 float: 2.5 , 0.0 , -1.2
3、布爾類型 bool: False True
4、字符串 str: "ab" , "a\nb" , "I'm" "a\tb\tc"
5、字節(jié)類型 bytes: b"toto\xfe\775"

容器類型:list[], tuple(), str bytes(), dict{:}, set()

容器類型
#list數(shù)組
[1,3,5,7,"9"][:2]
[1, 3]
#數(shù)組函數(shù)
list((1,3,5,7,"9"))[:2]
[1, 3]
#tuple元組
(1,2,"3",)[1]
2
#元組函數(shù)
tuple((1,2,"3",))[1]
2
#dict字典
{"apple":1,"pear":2,"orange":3}
{'apple': 1, 'pear': 2, 'orange': 3}
#字典函數(shù)
dict(apple=1,pear=2,orange=3)
{'apple': 1, 'pear': 2, 'orange': 3}
#set集
{"a","b",1,2}
{1, 2, 'a', 'b'}
#集函數(shù)
set({"a","b",1,2})
{1, 2, 'a', 'b'}

強制類型轉(zhuǎn)換 Conversions

強制類型轉(zhuǎn)換
int("123")
123
#二進制轉(zhuǎn)換成十進制
int("0101",2)
5
#六進制轉(zhuǎn)換成十進制
int("12",16)
18
int(12.3)
12
float("12345")
12345.0
round(12.345,1)
12.3
#bool(x):
#False for null x, empty container x, None or False x; 
#True for other x
bool(0)
False
bool(None)
False
bool([])
False
str("abc")
'abc'
#code ? char
chr(35)
'#'
ord("@")
64
ord("%")
37
repr("one")
"'one'"
bytes([72,9,64])
b'H\t@'
list("abcde")
['a', 'b', 'c', 'd', 'e']
list((1,2,3,4,5))
[1, 2, 3, 4, 5]
dict([("apple",1),("pear",2)])
{'apple': 1, 'pear': 2}
dict(apple=1,pear=2)
{'apple': 1, 'pear': 2}
set(["apple","pear"])
{'apple', 'pear'}
#separator str and sequence of str → assembled str
"@".join(["name","net.com"])
'name@net.com'
#str splitted on whitespaces → list of str
"this is an apple".split()
['this', 'is', 'an', 'apple']
#str splitted on separator str → list of str
"this is an apple".split("a")
['this is ', 'n ', 'pple']
#str splitted on separator str → list of str
[int(x) for x in ('1','2',3,3.3)]
[1, 2, 3, 3]
[int(x) for x in ['1','2',3,3.3]]
[1, 2, 3, 3]

標識符 Identifiers 用作變量,函數(shù),模塊,類......名稱

標識符

標識符必須以字母(大小寫均可)或者" _ "開頭,接下來可以重復0到多次(字母|數(shù)字|" _ ") a…zA…Z_ followed by a…zA…Z_0…9

  • diacritics allowed but should be avoided
  • 禁止使用關(guān)鍵字(python內(nèi)部已經(jīng)使用了的標識符)(language keywords forbidden)
  • 區(qū)分大小寫 (lower/UPPER case discrimination)
  • 沒有長度限制

變量賦值 Variables assignment =

變量賦值
#"變量名稱"="賦值"
a=120
a=b=50
x,y,z=12,11,10
x+=3
y/=2
z%=3
n=None
#remove name x
del x
#values swap
y,z=z,y
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 一、Python簡介和環(huán)境搭建以及pip的安裝 4課時實驗課主要內(nèi)容 【Python簡介】: Python 是一個...
    _小老虎_閱讀 6,313評論 0 10
  • 在C語言中,五種基本數(shù)據(jù)類型存儲空間長度的排列順序是: A)char B)char=int<=float C)ch...
    夏天再來閱讀 3,993評論 0 2
  • 一、PyCharm的基本使用1.1、注釋:為了方便自己或者其他人查看單行注釋:用 # 號單行注釋多行注釋: 用 ...
    IIronMan閱讀 9,072評論 3 18
  • Lua 5.1 參考手冊 by Roberto Ierusalimschy, Luiz Henrique de F...
    蘇黎九歌閱讀 14,235評論 0 38
  • 十七、鐘校尉奉命請賢,嵇中散得罪小人 249年高平陵事件兩年以后,251年九月七日,魏國太傅司馬懿在洛陽病逝。 子...
    襄楚閱讀 1,053評論 0 2

友情鏈接更多精彩內(nèi)容