Python第一天
要點(diǎn)一:ubuntu的安裝
以前使用啟動(dòng)盤(pán)安裝,涉及分盤(pán)略顯費(fèi)事,今天用了老師的虛擬機(jī)全家桶(ubuntu里pycharm和anaconda都安好的),很好吃,VMware是個(gè)好軟件。要點(diǎn)二:python基礎(chǔ)學(xué)習(xí)小木鹿
1 .創(chuàng)建一個(gè)屬于自己的代碼文件
2 . 變量與交換變量
3 . 標(biāo)識(shí)符小結(jié)
4 .格式化輸出"%"、“.format{}”
5 .判斷語(yǔ)句:if else esif——用它們寫(xiě)一個(gè)猜拳游戲
6 .whiele循環(huán)
7 .for循環(huán)與continue以及break的功能
8 . 字符串與切片
9 .字符串常見(jiàn)操作函數(shù)-
創(chuàng)建一個(gè)屬于自己的代碼文件——按這樣操作,在右邊區(qū)域修改成自己的信息即可,再次創(chuàng)建文件自帶個(gè)人信息
變量與交換變量
a=1
b=2
print(a,b)
a,b=b,a
print(a,b)
代碼效果
1 2
2 1
- 標(biāo)識(shí)符小結(jié)
1 . python、c++由字母、下劃線和數(shù)字組成,且不能以數(shù)字開(kāi)頭.python標(biāo)識(shí)符區(qū)分大小寫(xiě),常用下劃線命名
2 .java標(biāo)識(shí)符由字母、數(shù)字、美元符號(hào)$、漢字和數(shù)字組成 - %輸出、format輸出
age=19
print("你的年齡是:%s"%age)
name='司馬懿'
print('你的名字叫{}'.format(name))
你的年齡是:19
你的名字叫司馬懿
- 猜拳游戲
import random
player=input('請(qǐng)輸入:剪刀(0)石頭(1)布(2)')
player=int(player)
#生成[0,2]的隨機(jī)整數(shù)
computer=random.randint(0,2)
#獲勝的情況
if ((player==0 and computer==2) or (player==1 and computer==0) or (player==2 and computer==1)):
print("獲勝")
#平局的情況
elif (player==computer):
("平局,敢不敢再來(lái)一局")
#輸了
else:
print("輸了")
請(qǐng)輸入:剪刀(0)石頭(1)布(2)>? 1
獲勝
- while循環(huán)
1 .while 條件:
2 . 滿足執(zhí)行
#打印乘法口訣表
i=1
while i<=9:
j=1
while j<=i:
print(j,"*",i,'=',j*i,' ',end='')
j+=1
print('\n')
i+=1
1 * 1 = 1
1 * 2 = 2 2 * 2 = 4
1 * 3 = 3 2 * 3 = 6 3 * 3 = 9
1 * 4 = 4 2 * 4 = 8 3 * 4 = 12 4 * 4 = 16
1 * 5 = 5 2 * 5 = 10 3 * 5 = 15 4 * 5 = 20 5 * 5 = 25
1 * 6 = 6 2 * 6 = 12 3 * 6 = 18 4 * 6 = 24 5 * 6 = 30 6 * 6 = 36
1 * 7 = 7 2 * 7 = 14 3 * 7 = 21 4 * 7 = 28 5 * 7 = 35 6 * 7 = 42 7 * 7 = 49
1 * 8 = 8 2 * 8 = 16 3 * 8 = 24 4 * 8 = 32 5 * 8 = 40 6 * 8 = 48 7 * 8 = 56 8 * 8 = 64
1 * 9 = 9 2 * 9 = 18 3 * 9 = 27 4 * 9 = 36 5 * 9 = 45 6 * 9 = 54 7 * 9 = 63 8 * 9 = 72 9 * 9 = 81
- for循環(huán)與continue以及break的功能
1 .for 臨時(shí)變量 in 可迭代的對(duì)象:
2 .循環(huán)滿足時(shí)執(zhí)行
3 .continue表示結(jié)束本次循環(huán)緊接著開(kāi)始下一次循環(huán)
4 .break表示立刻跳出該循環(huán)
company='neusoft'
for x in company:
print("***")
if x=='s':
break
print(x)
else:
print('for循環(huán)沒(méi)有執(zhí)行break,則執(zhí)行本語(yǔ)句')
***
n
***
e
***
u
***
range函數(shù):range(起始,終止,步長(zhǎng))
- 字符串與切片
#截取一部分的操作
#對(duì)象【起始:終值:步長(zhǎng)】
name='abcdef'
print(name[2])
print(name[0:3])
print(name[2:])
print(name[3:5])
print(name[1:-1])
print(name[::2])
print(name[5:1:-2])
print(name[::-1])
c
abc
cdef
de
bcde
ace
fd
fedcba
- 字符串常見(jiàn)操作函數(shù)
1 .find()函數(shù)
def find(self, sub, start=None, end=None):
檢查某部分str是否包含在變量mystr中,如果在,返回開(kāi)始的索引值,否則返回-1
my_str='hello world neuedu and neueducpp'
index1=my_str.find('neuedu',0,18)
print(index1)
12
2 .index函數(shù)
index()跟find()一樣 只不過(guò)str不在mystr中要報(bào)一個(gè)異常
3 .count()函數(shù)
返回目標(biāo)字符串出現(xiàn)的次數(shù)
count1=my_str.count('neuedu',0,19)
print(count1)
1

python學(xué)習(xí)才剛剛開(kāi)始
