初識Python

Program01  基本的輸出

做爲一種語言的學習,他的儀式感很重要python

print("Hello World!") 

這就是python3裏面的 Hello World! 打印輸出函數

Program02  註釋符號的使用

name = "你好,世界"
msg = """
print(name)
666
777
888
"""
name2 = name+"111"
print(name)
print(name2)
print("My name is",name,"Hello world")
print(msg)

輸出結果:學習

你好,世界
你好,世界111
My name is 你好,世界 Hello world

print(name)
666
777
888
'''
在python3中 三個連續的引號是多行註釋
'''

此程序中能夠學到:python3中 ''' 表示多行註釋,若是將註釋掉的內容賦值給一個變量,測試

print一下是能夠打印輸出的,單行註釋使用的是 #spa

Program03  多種方式打印我的信息

name = input("name:")
age = int(input("age:")) # integer 將字符串轉化爲整數型
print( type(age), type( str(age)))
job = input("job:")
salary = input("salary:")
info = '''
-------- info of %s--------
Name: %s
age: %d
job: %s
salary: %s
'''%(name,name,age,job,salary)
print(info)

輸出結果:rest

name:XiaoBai
age:11
<class 'int'> <class 'str'>
job:Student
salary:1000

-------- info of XiaoBai--------
Name: XiaoBai
age: 11
job: Student
salary: 1000
name = input("name:")
age = int(input("age:")) # integer 將字符串轉化爲整數型
print( type(age), type( str(age)))
job = input("job:")
salary = input("salary:")
info = '''
-------- info of %s--------
Name: %s
age: %d
job: %s
salary: %s
'''%(name,name,age,job,salary)

info2 = '''
-------- info of {_name} -------- 
# 此處下劃線僅僅是爲了區別name 這一個變量
Name: {_name}
age: {_age}
job: {_job}
salary: {_salary}
'''.format(_name = name,
_age = age,
_job = job,
_salary = salary)

print(info2)

輸出結果:code

name:xiaobai
age:11
job:Student
salary:1000orm

-------- info2 of xiaobai --------
# 此處下劃線僅僅是爲了區別name 這一個變量
Name: xiaobai
age: 11
job: Student
salary: 1000blog

name = input("name:")
age = int(input("age:")) # integer 將字符串轉化爲整數型
job = input("job:")
salary = input("salary:")
info = '''
-------- info of %s--------
Name: %s
age: %d
job: %s
salary: %s
'''%(name,name,age,job,salary)

info3 = '''
-------- info3 of {0} --------
Name: {0}
age: {1}
job: {2}
salary: {3}
'''.format(name,age,job,salary)
print(info3) 
# 打印整段,%s 是段內調用段外的字符串
# 在此方法中輸入時,format()中的name只寫了一次!

輸出結果:字符串

name:xiaobai
age:11
job:Stu
salary:1000

-------- info3 of xiaobai --------
Name: xiaobai
age: 11
job: Stu
salary: 1000

總結:

'''
時間 2018年2月6日21:08:12
目的 學習python輸入、輸出

記錄 經過input輸入的默認格式是str,能夠經過int()強制類型轉化爲int類型
經過type 能夠查詢目標元素的格式和類型
三個引號(不區分單雙)能夠註釋一整段,若是將註釋掉的內容賦值
給一個變量那麼能夠打印輸出該段內容
使用format的時候不要忘記加  .
靈活運用三種打印輸出段落,並對外部內容引用的方式。其中第二種最爲常見
並且一個{} 中只寫一個變量名稱
input("登陸用戶{m},{n}?".format(m = user_name01,n = user_name02)) correct
input("登陸用戶{m,n}?".format(m = user_name01,n = user_name02)) error
'''

Program04  判斷語句

'''
時間 2018年2月6日21:14:05
目的 if else 流程判斷
記錄 隱藏密文
並且python是強制縮進,同級會同時輸出
'''
import getpass
_username = 'abc'
_password = 'abc123'
username = input("username:")
# password = getpass.getpass("password:") 此時是密文,隱藏密碼
password = input("password:")
print(username,password)
if _username == username and _password == password: print("Worlcome user {name} login...".format(name = username)) else: print("Invalid username or password!")

輸出結果:

username:abc
password:abc123
abc abc123
Worlcome user abc login...

總結:

# if執行語句必須含有縮進,不然會報錯!

Program05 while循環語句

'''
時間 2018年2月6日22:03:14
目的 繼續測試while 循環
記錄 while 循環須要加冒號
'''
age_of_boy = 56
count = 0
while count < 3:
    guess_age = int(input("guess age:"))
    if guess_age == age_of_boy:
        print("yes,you got it.")
        break
    elif guess_age > age_of_boy:
        print("think smaller...")
    else:
        print("think bigger...")
    count += 1
    
if count == 3:
    continue_confirm = input("do you want to keep gussing?")
if continue_confirm != 'n':
    count = 0

輸出結果:

guess age:10
think bigger...
guess age:100
think smaller...
guess age:50
think bigger...
do you want to keep gussing?n

Program06 for循環語句

'''
目的 測試for 循環
記錄 今後能夠看出,python中的循環和MATLAB中的循環與構建向量有類似之處
'''
age_of_boy = 56
for i in range(3):
    guess_age = int(input("guess age:"))
    if guess_age == age_of_boy:
        print("yes,you got it.")
        break
    elif guess_age > age_of_boy:
        print("think smaller...")
    else:
        print("think bigger...")
else:
    print("you have tried too many times...have a rest")

# 特別值得注意的一點是 在python中if-elif-else語句的寫法
# break 和 continue的區別是 前者終止當前循環,後者是跳出本次循環,進入下一次循環

輸出結果:

think bigger...
guess age:70
think smaller...
guess age:60
think smaller...
you have tried too many times...have a rest 

Program07 簡易用戶登陸界面

'''
時間 2018年2月10日19:51:29
目的 製做一個用戶登陸界面,可以提示用戶輸入密碼,三次出錯鎖定
# 還有小問題是不會使用python對文件進行增刪改查的操做,這次操做的用戶名和用戶密碼最好是在文件中
'''
user_name01 = 'abc'
user_name02 = 'bcd'
user_pass01 = 'abc123'
user_pass02 = 'bcd123'
# 用於提示用戶被鎖住,沒法登陸
user_flag01 = True
user_flag02 = True
count = 0
choice = input("登陸用戶{m},{n}?".format(m = user_name01,n = user_name02))
if choice == user_name01 and user_flag01 is True:
    while count < 3:
        pass_01 = input("please input the pass word of {na}:".format(na = user_name01))
        if pass_01 == user_pass01:
            print("congratulations!you passed \t")
            break
        else:
            print("try again:")
        count += 1
    else:
        userflag01 = False
        print("you hava tried so many times,the id is locked")
elif choice == user_name02 and user_flag02 is True:
    while count < 3:
        pass_02 = input("please input the pass word of {nb}:".format(nb = user_name02))
        if pass_02 == user_pass02:
            print("congratulations!you passed \t")
            break
        else:
            print("try again:")
        count += 1
    else:
        userflag02 = False
        print("you hava tried so many times,the id is locked")

輸出結果:

登陸用戶abc,bcd?abc
please input the pass word of abc:abcabc
try again:
please input the pass word of abc:acb123
try again:
please input the pass word of abc:abc123
congratulations!you passed

Program08 字典的使用

# 三級菜單的建立
# 思路 經過字典存儲
'''
時間 2018年2月10日20:34:03
目的 建立一個三級菜單

記錄 利用函數簡化此代碼,pass不進行任何操做
總結 2018年2月11日從新編寫並運行此代碼,發現一些錯誤
在從新編寫以後,發現python是嚴格的格式對齊語言,若是同時存在while 和 for 的時候,
兩者沒有區分循環層次,誤將兩者對齊,則會報錯。在以後的一系列測試中發現,循環體的
書寫位置也會影響代碼的運行結果!
'''
data = {
    '北京':{
        "昌平":{
            "沙河":["oldboy","test"],
            "天通苑":["鏈家地產","我愛我家"]
        },
        "朝陽":{
            "望京":["奔馳","陌陌"],
            "國貿":{"CICC","HP"},
            "東直門":{"Advent","飛信"},
        },
        "海淀":{},
    },
    '山東':{
        "德州":{},
        "青島":{},
        '濟寧':{
               '兗州':["BBQ","TYZY"],
               '太白':["JNYX","FSYY"]
               },
        '濟南':{
               '歷下':["山大","山交通"],
               '長清':["山師","山工藝"]
               }
    },
}

exit_flag = False

while not exit_flag:
    for i in data:
        print(i)
    choice = input("選擇進入1>>:")
    if choice in data:
        while not exit_flag:
            for i2 in data[choice]:
                print("\t",i2)
            choice2 = input("選擇進入2>>:")
            if choice2 in data[choice]:
                while not exit_flag:
                    for i3 in data[choice][choice2]:
                        print("\t\t", i3)
                    choice3 = input("選擇進入3>>:")  
                    if choice3 in data[choice][choice2]:
                        for i4 in data[choice][choice2][choice3]:
                            print("\t\t",i4)
                        choice4 = input("最後一層,按b返回>>:")
                        if choice4 == "b":
                            pass
                        elif choice4 == "q":
                            exit_flag = True
                    if choice3 == "b":
                        break
                    elif choice3 == "q":
                        exit_flag = True
            if choice2 == "b":
                break
            elif choice2 == "q":
                exit_flag = True
       

 MYK2018年2月11日

相關文章
相關標籤/搜索