標籤:python
一、Python資料類型
字串、數字、元組(tup)、list(列表)、dict(字典)
1.數字操作:
1.1四則運算:+ - * / %(求餘)
print 2+24print 1+2*49print 1.0/2.00.5print 2%42
2.字串操作:
2.1.字串:用單引號或者雙引號引起來
print ‘hello‘ hello
2.2.字串拼接:用+號將多個字串拼接起來
print ‘hello ‘+‘world‘hello world
2.3.字串轉義:在Python中字串使用單引號或雙引號括起來,如果字串內部出現了需要轉義的字元(例如:"和‘本身),只需要在字元前添加逸出字元:\即可
常用逸出字元:
>>> print ‘My name\‘s lilei‘My name‘s lilei
4.變數:變數值可以是字串、數字、list、dict、tup
x = ‘hello world‘print xhello worldage = 20print age20
5.擷取使用者輸入:raw_input()#python內建函數
#代碼x = raw_input(‘please input a string: ‘)print ‘hello ‘+x#輸入world輸出 hello world
6.字串格式化:
常用類型:
%s:字串
%d:整型
%f:浮點型
%x:十六進位
%%:表示%本身
name = ‘liuyang‘age = 26#數字和字串不能直接相加#26為數字,不能與字串相連,所以我們要將類型轉換成字串print ‘hello ‘+name+‘,and i am ‘+str(age)+‘ years old‘hello liuyang,and i am 26 years old#字串格式化%s 引入變數 後邊%結尾意思是左邊字串結束了現在要代入變數)print ‘hello %s,and i am %s years old‘ %(name,age)hello liuyang,and i am 26 years old#用%s換成%d預留位置,代表一個數字>>>print ‘hello %s,and i am %d years old‘ % (name,age)hello liuyang,and i am 26 years old
7.資料類型
int:整型
str:字串
float:浮點型
bool:布爾型
x = raw_input(‘print input a number: ‘)y = raw_input(‘print input a number2: ‘)print int(x)+int(y)#輸入x 3 輸入y 4#輸出 7
8.Python內建函數strip():
.strip()#消除字串兩邊空格
#預設是空格,可以是別的字元
.strip(‘\n‘)
#消除分行符號
.rstrip()#消除字串右邊空格
.lstrip()#消除字串左邊空格
.upper()大寫轉小寫
.upper().lower() 小寫轉大寫
name = ‘ liuyang ‘print name.strip()liuyangprint name.lstrip()liuyang print name.rstrip() liuyangprint name.strip().upper()LIUYANGprint name.strip().upper().lower()liuyang
9.Python內建函數strip():
type()
判斷資料類型
name = ‘liuyang‘print type(name)str
10.流程式控制制
10.1.布爾值:True,False
True:表示真
False:表示假
4>3Truenot 0True5>6False
10.2.邏輯運算:and or not
#A and B 就是A 和B都是TRUE的時候才是TRUE,其它情況都是false
#A or B 就是A 和B只要有一個是TRUE就是TRUE,條件至成立一個即為TRUE
#not A如果A是FALSE返回TRUE,如果A 是True返回FALSE
註:‘and’的優先順序高於‘or’
10.3.if else判斷:
if 條件判斷(真/假):條件為真執行if後面語句
else:#條件為假執行else語句
條件為假的情況:
#Null 字元串
#空列表
#數字0
#空字典都是
#none
if ‘‘: print ‘123‘else: print ‘1231343‘1231343#if not 0: print ‘add‘else: print ‘dd‘add#list = []>>>if list: print ‘list不為空白為真‘else: print ‘aaa‘ aaa#if dict: print ‘123132‘else: print ‘af‘ af
10.4.while迴圈:
#while 條件成立:
# 如果情況是TRUE,代碼持續執行;不成立即為False
#while迴圈直到情況是false才會退出
#while True一直迴圈,break手動終止
break和continue都是針對for迴圈和while迴圈的
break:跳出迴圈
#continue:跳過本次迴圈
i = 0
while i<20:
print i
i = i + 1
#
>>>name = ‘‘
# 定義name為空白,while判斷name非空是True,代碼執行,
# name空為假,條件判斷FALSE退出print name
while not name:
name=raw_input(‘input your name‘)
print ‘hello ‘+name
10.5.for 迴圈序列:專門針對list,dict,以後用到比較多的流程式控制制
for name in [‘2‘,‘3‘,‘4‘]: print name2 3 4
練習1:輸入一個數字,不為0時列印所有數位和,如果資料輸入是0直接退出
#/usr/bin/python#coding=utf-8sum = 0while True:num = int(raw_input(‘input a number‘))if num == 0:breakelse:sum = sum + int(num)print sum執行指令碼:輸入23 22 22 輸入0 退出指令碼,列印輸入數字總和67
練習2:本金是10000,年利率是3.25%,求多少錢後,存款能翻翻
#/usr/bin/python#coding=utf-8money=10000lixi=0.0325y=0#如果money小於20000條件為true則迴圈一直執行,flase跳出迴圈 即money大於20000也就是本金翻翻while money <= 20000:money=money*(1+lixi)y = y + 1print money,y#執行指令碼20210.6986788存款翻翻 22年
練習3:列印最大的兩個值
#/usr/bin/python#coding=utf-8unsort_num = [1,2,3,444,555,3234,35,65535,65535,21]#print unsort_nummax_num1 = 0max_num2 = 0for i in unsort_num:if i > max_num1:max_num2 = max_num1max_num1 = ielif i > max_num2:max_num2 = iprint "max num is:%s,second max num is: %s " %(max_num1,max_num2)#列印結果max num is:65536 ,second max num is 65535
練習4:使用者登入,驗證使用者及密碼,密碼輸入錯誤三次退出登入
name = raw_input(‘input your name: ‘).strip()if name == ‘liuyang‘:count = 0while count<3:passwd = raw_input(‘input your passwd: ‘)if passwd == ‘123456‘:print‘User %s login sucessfully !‘%(name)breakelse:print ‘Wrong passwd,please input again !‘count +=1print ‘passwd wrong three times,the count %s is locked !‘%(name)else:print "User %s not exists,please confirm !"%(name)
練習5:輸入一個數字,與50比較,大於50提示輸入數字太大,小於50,提示輸入數字太小,三次機會,超過三次,返回輸入錯誤
最佳化前:
#/usr/bin/python#coding=utf-8compare_num = 50count = 0while count<=3:input_num = int(raw_input(‘input a number: ‘))if input_num == compare_num:print ‘輸入數字正確‘breakelif input_num > compare_num:print ‘輸入的數字%s大於%s,還有%d次機會‘%(input_num,compare_num,3-count)else:print ‘輸入的數字%s小於%s,還有%d次機會‘%(input_num,compare_num,3-count)count +=1#最佳化後##/usr/bin/python#coding=utf-8import syscompare_num = 50count = 0while count<=3:input_num = raw_input(‘input a number: ‘)try:num = int(num)except Exception,e:print ‘輸入非法‘sys.exit()time = 3-countif input_num > compare_num:print ‘輸入的數字太大,還有%s次機會‘%(time)elif input_num < compare_num:print ‘輸入的數字太小,還有%s次機會‘%(time)else:print ‘輸入數字正確‘breakcount +=1
#引申:
exit():是Python系統函數 import sys #匯入系統函數
break 與exit() exit是直接退出python指令碼,break是退出當前迴圈
異常檢查處理
try:
1/0 #執行語句,並返回執行結果
except Exception,e:
print "語法錯誤"
python第一天