#Python day1python
標籤: pythonlinux
[TOC]
##1.Hellow World程序
###1)Hellow World
在linux裏建立一個叫hellow.py的文件。ide
文件內容:spa
#!/usr/bin/env python print("Hellow World!")在linux中須要指定解釋器
#!/usr/bin/env python
意思是到整個linux系統中找名爲python的環境變量。code
給予文件執行權限,使用./hellow.py執行文件。orm
輸出結果:遊戲
Hellow World!Python經常使用轉義符:內存
轉義字符 | 具體描述 |
---|---|
\n | 換行 |
\r | 回車 |
\v | 縱向製表符 |
\t | 橫向製表符 |
\" | " |
\\ | \ |
\(在行尾時) | 續行符 |
\a | 響鈴 |
\b | 退格(Backspace) |
\000 | 空 |
<br />
<br />字符串
<br />
<br />get
##2.變量
###1)什麼是變量?
變量用於存儲在計算機程序中引用和操做的信息。它們還提供了一種用描述性名稱標記數據的方法,這樣咱們的程序就能更清晰地被讀者和咱們本身理解。將變量看做保存信息的容器是頗有幫助的。它們的惟一目的是在內存中標記和存儲數據。而後能夠在整個程序中使用這些數據。
###變量定義的規則:
字母、數字或下劃線
的任意組合['and', 'as', 'assert', 'break', 'class', 'continue', 'def', 'del', 'elif', 'else', 'except', 'exec', 'finally', 'for', 'from', 'global', 'if', 'import', 'in', 'is', 'lambda', 'not', 'or', 'pass', 'print', 'raise', 'return', 'try', 'while', 'with', 'yield']
###什麼是常量?
常量:定義後,不會變的量,好比π是3.14...
python定義常量,變量名要大寫好比:
PIE = "em"
<br />
###2)變量的賦值:
name = "byh" name2 = name print(name,"-----",name2) name = "em" print(name,"-----",name2)
print:輸出字符
<br/>
輸出結果:
byh ----- byh em ----- byh
Process finished with exit code 0
>###爲何第二個name2輸出的是 byh,而不是em呢? * 由於: - name2只是經過name找到byh的,name2≠name - name = em只是重新定義了name,name2仍是經過第一個name變量找到byh <br/> ###註釋: 當行註釋|「#」標識當行 ---------|--------------- 多行註釋|''' 或 """ 都表示多行註釋 ###3)三元運算
a = 1
b = 5
c = 9
d = a if a > b else c
print(d)
d = a if a < b else c
print(d)
輸出結果:
9 #若是a > b條件不成立,結果就是c的值
1 #若是a < b條件成立,結果就是a的值
<br> <br> --- <br> <br> ##3.用戶輸入 ###1)簡單用戶輸入
username = input("username:")
password = input("password:")
print("Hellow",username)
>輸入密碼時,若是要密碼不可見,須要使用`getpass`模塊中的getpass方法:
import getpass
username = input("username:")
password = getpass.getpass("password:")
print("Hellow",username)
<br /> ###2)用戶輸入,輸出調用變量 >方法一:
name = input("name:")
age = int(input("age:"))
print(type(age))
job = input("job:")
salary = int(input("salary:"))
print(type(salary))
info = '''
-------- info of %s--------
Name:%s
Age:%d
Job:%s
Salary:%d
''' %(name,name,age,job,salary)
print(info)
%s|表明string字符串 ---|--- %d|表明整數 %f|表明有小數點的數 int()|強制轉換類型爲integer str()|強制轉換類型爲string print(type(salary))|輸出字符串類型 >輸出結果:
name:em
age:19
<class 'int'>
job:IT
salary:1234567
<class 'int'>
-------- info of em--------
Name:em
Age:19
Job:IT
Salary:1234567
Process finished with exit code 0
<br/> >方法二:
#Author:byh
name = input("name:")
age = int(input("age:"))
print(type(age))
job = input("job:")
salary = int(input("salary:"))
print(type(salary))
info = '''
-------- info of {_name}--------
Name:{_name}
Age:{_age}
Job:{_job}
Salary:{_salary}
''' .format(_name=name,
_age=age,
_job=job,
_salary=salary)
print(info)
>輸出結果:
name:em
age:19
<class 'int'>
job:IT
salary:1234567
<class 'int'>
-------- info of em--------
Name:em
Age:19
Job:IT
Salary:1234567
Process finished with exit code 0
<br/> >方法三:
#Author:byh
name = input("name:")
age = int(input("age:"))
print(type(age))
job = input("job:")
salary = int(input("salary:"))
print(type(salary))
info = '''
-------- info of {0}--------
Name:{0}
Age:{1}
Job:{2}
Salary:{0}
''' .format(name,name,age,job,salary)
print(info)
>輸出結果:
name:em
age:19
<class 'int'>
job:IT
salary:1234567
<class 'int'>
-------- info of em--------
Name:em
Age:em
Job:19
Salary:em
Process finished with exit code 0
<br/> <br/> --- <br/> <br/> ##4.表達式if ... else ###場景一 : 用戶登錄驗證
_username = "byh"
_password = "123"
username = input("username:")
password = input("password:")
if _username == username and _password == password:
print("Welcome user {name} login...".format(name=_username))
else:
print("Invaild username or password!")
elif|若是這個條件不成立,那麼下個條件是否成立呢 ---|--- >輸出結果:
#驗證成功輸出
username:byh
password:123
Welcome user byh login...
#驗證失敗輸出
username:em
password:123
Invalid username or password!
###場景二 : 猜年齡遊戲
_myage = 22
myage = int(input("myage:"))
if myage == _myage:
print("yes, it is")
elif myage > _myage:
print("should be smaller..")
else:
print("should be more big..")
>輸出結果
myage:17
should be more big..
myage:23
should be smaller..
myage:22
yes, it is
<br/> <br/> --- <br/> <br/> ##5.while循環 ###1)簡單while循環
count = 0
while True:
print("count:",count)
count +=1
if count == 1000:
break
>當這個條件成立時:True(永遠爲真),若是沒有定義 `break`條件結束循環,則會一會循環下去。 <br/> ###2)while循環猜年齡遊戲
#實現用戶能夠不斷的猜年齡,最多3次機會,繼續按如意鍵,退出按「n」
_myage = 22
count = 0
while count < 3:
myage = int(input("myage:"))
if myage == _myage:
print("yes it is.")
break
elif myage > _myage:
print("should be smaller...")
else:
print("should be more big...")
count +=1
if count == 3:
countine_config = input("do you want to keep gussing?")
if countine_config !="n":
count = 0
break|結束當前整個循序 ---|--- continue|跳出本次循環,進入下次循環 count|添加計數 >輸出結果:
#繼續猜:
myage:1
should be more big...
myage:2
should be more big...
myage:3
should be more big...
do you want to keep gussing?
myage:12
should be more big...
myage:22
yes it is.
Process finished with exit code 0
#不猜退出:
myage:1
should be more big...
myage:2
should be more big...
myage:3
should be more big...
do you want to keep gussing?n
Process finished with exit code 0
<br/> <br/> --- <br/> <br/> ##6.for循環 ###1)簡單for循環
#0,10是範圍,1是增數
for i in range(0,10,1):
print("number",i)
>輸出結果:
number 0
number 1
number 2
number 3
number 4
number 5
number 6
number 7
number 8
number 9
Process finished with exit code 0
###2)for循環猜年齡遊戲
#用戶最多猜3次
_myage = 22
for i in range(3):
myage = int(input("myage:"))
if myage == _myage:
print("yes it is.")
break
elif myage > _myage:
print("should be smaller..")
else:
print("should be more big..")
else:
print("you have tried too many times")
>輸出結果:
myage:1
should be more big..
myage:2
should be more big..
myage:3
should be more big..
you have tried too many times
Process finished with exit code 0
##7.做業 ###1)做業三:多級菜單 * 三級菜單 * 可一次選擇進入各子菜單 * 使用知識點:列表、字典
#Author:byh
#定義三級字典
data = {
'北京':{
"昌平":{
"沙河":["oldboy","test"],
"天通苑":["宜家","大潤發"],
},
"朝陽":{},
"海淀":{},
},
'山東':{
"德州":{},
"青島":{},
"濟南":{},
},
'廣東':{
"東莞":{},
"廣州":{},
"佛山":{},
},
}
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(i2) choice2 = input("選擇進入2:") if choice2 in data[choice]: #判斷輸入的層在不在 while not exit_flag: for i3 in data[choice][choice2]: #打印第三層 print(i3) choice3 = input("選擇進入3:") if choice3 in data[choice][choice2]: #判斷輸入的層在不在 for i4 in data[choice][choice2][choice3]: #打印最後一層 print(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