Python學習day_01

day 1

Python開始

Python的解釋器

  • cpython官方標準。動態的,邊執行,邊解釋。
  • ipython
  • jpython,他是一種完整的語言,他是一個Python語言在Java中的完全實現。
  • PYPY ,他是用Python寫的解釋器,速度比cpychon快。
  • ironpython
    注意:在一般編程過程中,大寫代表常量,而小寫則代表變量
x = 2
y = 3
z = x 
x =5
結果:x = 5,z = 2

Python中的數據類型

網址:http://www.yiibai.com/python/python_basic_operators.html

這裏寫圖片描述

字符編碼(默認是ascall存)

Python中使用的是Unicode字符,在Python中ord()可以查看ascall值。
在存儲字符的過程中,默認是使用ascall進行存儲字符,如果這樣寫就是使用Unicode進行存儲。

>>> a = u'lala'
>>> type(a)
<type 'unicode'>

>>> name =”飛天”
>>> print name 
飛天
>>> name
'\xe9\xa3\x9e\xe5\xa4\xa9’   
#這是utf-8(ascall是utf-8的一個子集),他靈活存儲漢字和字母,當你是漢字的時候,使用3個字節,如果是英文就用一個字節。\xe9\xa3\x9e這就是飛字。存儲在內存的過程是使用Unicode,因爲不能同時使用兩種字符集。在硬盤中存儲是使用硬盤中,就是使用utf-8,因爲能節省空間Unicode不管是漢字還是字母都是使用2個字節。

在Unicode轉化成utf-8中間需要一個轉換過程,轉換過程就如下

name = u“飛天”
name.encode(‘utf-8’)  #轉化成utf-8
#解開
b =name.encode(‘utf-8’)  #這是一個將他轉化成字符串類型的
name.decode(‘utf-8’) #重新轉化成Unicode

支持中文,在代碼首頁加入:* coding:utf-8 *#有四種方式,詳見博客。

導入模塊

有好幾種方式:

  • import sys
    這樣調用的話,就是要加上你模塊的名稱例如:sys.argv.
  • from sys import argv
    這樣就直接 argv進行調用
  • import multiprocessing as multi
    將模塊取一個別名,用來代替繁瑣的模塊名稱
  • form sys import * #不建議使用
    把這個模塊所有的方法都導進來,也是直接使用

流程控制

  • if … elif …else #永遠只有一個結果
  • for i in range(1,100).
  • while …else

練習

#猜年齡
name = raw_input("Please input  your name:")
info = 'This is var will be print out'
age = input("Age:")
job = raw_input("Job:")
real_age = 29
for i in range(1,10):
    if age > 29:
        print "think smaller!"
    elif age == 29:
        print '\033[32;1mGOOD! 10RMB!\033[0m'
    else:
        print "think bigger"
    print"you still get %s shots" %(10-i)
    age = input("Age:")
lala =  """
   Name:%s
    Age:%s
    Job:%s
"""%(name,age,job)
print "-----------------"
#輸出你想打印的數值
count = 0
print_num = input("which loop do you want to be print out ?")
while count < 10000:
    if  count == print_num:
        choice = raw_input("Do you want to continue the Loop?(y/n)")
        if choice == "n":
            break
        else :
#這裏使用一個while循環也可以。
#while print_num <= count
    #   print_num = input("which loop do you want to be print out ?")
#   print print "%d已經過了,sx" %(count)
       print_num = input("which loop do you want to be print out ?")
           if print_num < count:
               print "%d已經過了,sx" %( print_num)
               print_num = count

    else :
        count +=1
        print 'Loop',count
else:
    print "Loop",count

作業

#編寫登錄接口
-   輸入密碼和用戶名
-   認證成功後顯示歡迎信息
-   認錯三次後鎖定
#代碼
import  sys
retry_limit = 3
retry_count = 0
account_file = "accounts.txt"
lock_file = "account_lock.txt"
while retry_count < retry_limit:  #只要不超過3次及不斷循環
    username = raw_input("\033[32;1mUsername:\033[0m")
    lock_check = file(lock_file,)
    #當用戶輸入用戶名後,打開LOCK文件以檢查此用戶已經LOCK
    for line in lock_check.readlines():
        line = line.split()
        if username == line[0]:
            sys.exit('033[31!1mUsername %s os locked!\033[0m' %username)
    password = raw_input('\033[32;1mPassword:\033[0m')
    f = file (account_file,'rb')
    match_flag =False
    for line in f.readlines():
        user,passwd = line.strip('\n').split()
        #去掉每行多餘的,並把這一行按空格分成兩列,分別賦值user,passwd兩個變量
        if username == user and passwd == password:
            print "Match!",username
            match_flag = True
            break
    f.close()
    if match_flag == False:
        print  "usrname"
        retry_count +=1
    else :
        print "welcome log the system!"
else:
    print "your account is locked!"
發佈了32 篇原創文章 · 獲贊 2 · 訪問量 8438
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章