一:python簡(jiǎn)介
1.第一行python代碼
安裝好Python损姜,在命令行輸python嫩痰,就可以進(jìn)去python解釋器的頁(yè)面。
輸入print? "hello world"
2.python入門(mén)演示
(1)簡(jiǎn)單的數(shù)學(xué)運(yùn)算
整數(shù)相加得整數(shù)谷扣,浮點(diǎn)數(shù)相加得浮點(diǎn)數(shù)焙畔,整數(shù)與浮點(diǎn)數(shù)相加得浮點(diǎn)數(shù)谱仪。
輸入:2+2.6
輸出:4.6
(2)變量賦值
Python使用<變量名>=<表達(dá)式>的方式對(duì)變量進(jìn)行賦值玻熙。
輸入:a = 12.8
(3)字符串String
字符串的生成,單引號(hào)與雙引號(hào)是一樣的疯攒。
輸入:s = "hello python!"
? ? ? ? ? print s
輸出:“hello python!”
三引號(hào)是用來(lái)輸入多行字符串嗦随。
輸入:s = """hello
? ? ? ? ? python"""
? ? ? ? print s
輸出:hello
? ? ? ? ? python
字符串的加法。
輸入:s = "hello" + "python"
? ? ? ? ? print s
輸出:“hello python”
字符串的索引敬尺。不小于0代表從左到右枚尼。小于0代表從右到左
輸入:print s[0]
輸出:“h”
輸入:print s[-1]
輸出 :“n”
查看字符串的長(zhǎng)度。
輸入:a = len(s)
? ? ? ? ? print a
輸出:12
列表list
python中生成列表用[]
輸入:b = [2*3,'hi',8.8]
輸出 :[6,'hi',8.8]
列表加法砂吞。
輸入:b + b
輸出:[6,'hi',8.8,2*3,'hi',8.8]
列表索引署恍。
輸入:b[1]
輸出:'hi'
列表長(zhǎng)度。
輸入:len(b)
輸出:3
向列表中添加元素蜻直。
輸入:b.append(9.9)
輸出:[6,'hi',8.8,9.9]
集合Set
python中生成集合用{}盯质,集合的特點(diǎn)是不含相同元素。
輸入:c = {1,2,3,4,5,5,4}
輸出 :{1,2,3,4,5}
集合的長(zhǎng)度
輸入:len(c)
輸出:5
向集合中添加元素概而。
輸入:c.add(9)
輸出:{1呼巷,2,3赎瑰,4王悍,5,9}
集合的交(&)餐曼,并 (|)压储,差(-)鲜漩,對(duì)稱(chēng)差(^)。以對(duì)稱(chēng)差舉例集惋。
輸入:c={1宇整,2,3芋膘,4鳞青,5}
? ? ? ? ? ? d={2,3为朋,5臂拓,4,6}
? ? ? ? ? ? c^d
輸出:{1习寸,6}