1. 程式人生 > >python小白學習之路--01

python小白學習之路--01

continue while True for count

為了堅持而堅持(這話說了自己不信)
作為一個不懂編程的桌面,在技術的路上越走越遠,嚴重到了找工作都很難的階段,很心酸。。。作為一個幹啥啥不行,吃啥啥有夠,韓劇看不夠,年紀又不小的我來說,在進步很難,不知路又走到哪就跑偏了,為了找到好工作而學習,至少希望可以升級做個小運維也是很滿足的。
沒有野心的人,想在現在生存,比有野心的人還要艱難。知足沒有常樂,知足只是在後退而已!
網上學習python 對於我這樣沒組織沒紀律的人,貌似起不到神馬作用,忽略我沒錢報培訓班,還是硬著頭皮,希望靠著大神的視頻讓我有所進步吧!
視頻中python 入門第一篇結束後,要求寫博客,我是假裝聽老師的話,然後過來自我安慰。
沒啥語言功底,數學一般邏輯性不強,字跡潦潦草草,寫出的東西也只有自己能看。
python01
小白入門的第一天
了解python,
神馬是python,是蟒蛇。
為啥python要用.py 結尾,為了讓其他人知道你寫的這是個python 腳本,老師推薦最好使用pycharm腳本編輯器,下了個試用版,免費使用30天。
python 版本 3.6 (最好學習使用3.0 版本,2.0 過時了)

字符編碼
ASCII 編碼

  1. 輸出“Hello world” (這是個儀式)
    變量名要求:a, 不能是數字開頭 b,除了“_”以外不能包含其他特殊字符 c,包含大小寫,數字,下劃線 d,變量名要簡單易懂表達所代表的東西 e,關鍵字不能成為變量名(and ,as,assert break class continue def del elif ………..)

定義變量 name = "Hello World"
print (name)

2.註釋多行:(‘‘‘ ‘‘‘)
‘‘‘
多行註釋,也可以打印多行
‘‘‘
打印多行:
例:
name = ‘alex‘
msg=‘‘‘
print ("my name is :" , {_name} )
name = "paoche ge"
‘‘‘.format(_name=name)
print (msg)
輸出結果:
print ("my name is :" , alex )
name = "paoche ge"

3.用戶輸入(input)和格式化輸出

例:用戶輸入(input)
username = input("type your username:")
print(username)
格式化輸出
a,例:字符串拼接(‘‘‘ + ‘‘‘)
username = input ("username is :")
age = input("age is :")
job = input("job is:")
salary=input("salary is:")
info1=‘‘‘
------info1 is ‘‘‘ + username ‘‘‘-----------
username is : ‘‘‘+ username ‘‘‘
age is : ‘‘‘ +age ‘‘‘
job is : ‘‘‘+job ‘‘‘
salary is : ‘‘‘ + salary
print (info1)

b, 字符串拼接(‘‘‘ %s ‘‘‘) ,s= string 字符串
username = input ("username is :")
age = input("age is :")
job = input("job is:")
salary=input("salary is:")
info2=‘‘‘
------info2 is %s-----------
username is : %s
age is : %s
job is : %s
salary is : %s
‘‘‘%(username,username,age,job,salary)
print (info2)

c, 字符串拼接(‘‘‘ %s ‘‘‘) s= string %d=整數 int=強制為數字?
username = input ("username is :")
age = int(input("age is :"))
job = input("job is:")
salary=input("salary is:")
info3=‘‘‘
------info3 is %s-----------
username is : %s
age is : %d
job is : %s
salary is : %s
‘‘‘%(username,username,age,job,salary)
print (info3)

d, 字符串拼接(使用排序.format)
username = input ("username is :")
age = input("age is :")
job = input("job is:")
salary=input("salary is:")
info4=‘‘‘
------info4 is {0}-----------
username is : {0}
age is : {1}
job is : {2}
salary is : {3}
‘‘‘.format(username,age,job,salary)
print (info4)

e, 字符串拼接(.format)
username = input ("username is :")
age = input("age is :")
job = input("job is:")
salary=input("salary is:")
info5=‘‘‘
------info5 is {_username}-----------
username is : {_username}
age is : {_age}
job is : {_job}
salary is : {_salary}
‘‘‘.format(username=_username,
age=_age,
job=_job,
salary=_salary)
print (info5)

以上的輸出(print)結果是:

**/Library/Frameworks/Python.framework/Versions/3.6/bin/python3.6 "/Users/a1/PycharmProjects/untitled3/day1/user input.py"
username is :alex
your job is:IT
your age is:23
salary is :232323


--------info 5 alex------
yourname is :alex
your job is :IT
your age is :23
your salary is :232323

**

4.密碼加密
例:
username = input("username:")
password = getpass.getpass("password:")
print (username,password)


5.if 條件判斷語句()
例:
oldboy_age=55
age = input("guess oldboy age is :")

if oldboy_age == age
print("yes,you got it")
else
print("wrong ansser")

例:使用and 或是 or 連接符,滿足條件才執行
username = ‘alex‘(錯誤的寫法為 usename=alex ,表示是變量)
password = ‘123456‘
_username =input("please input your name:")
_pass=input("please input your password:")
if usename=_username and _pass=password:
print ("welcom login system")
else:
print("wrong user")

  1. while 循環語句
    while True 循環語句:表示為真則執行,反之則執行else 語句
    例:
    count = 0
    while True:
    print ("count:",count)
    count = count +1 # count +=1 每次循環加1,打印顯示循環後的數值

while True 和 if 一起循環判斷語句
例:
age= 55
while True:
guess = int(input("guess age :"))
if guess == age:
print("yes,you got it.")
elif guess < age:
print("think older.")
else:
print ("think smaler")
輸入結果如下
/Library/Frameworks/Python.framework/Versions/3.6/bin/python3.6 "/Users/a1/PycharmProjects/untitled3/day1/whiel True 循環.py"
guess age :34
think older.
guess age :56
think smaler
guess age :

加上條件:使用戶最多可以猜錯三次條件
或是while 加條件語句如(while count < 3 )
定義計數器 count = 0, count += 1 每次循環+1
跳出循環break
例:
age= 55
count = 0
while True:
if count == 3:
break
guess = int(input("guess age :"))
if guess == age:
print("yes,you got it.")
elif guess < age:
print("think older.")
else:
print ("think smaler")
count += 1
執行的結果為:
/Library/Frameworks/Python.framework/Versions/3.6/bin/python3.6 "/Users/a1/PycharmProjects/untitled3/day1/whiel True 循環.py"
guess age :23
think older.
guess age :23
think older.
guess age :23
think older.

Process finished with exit code 0


對以上的代碼做優化
例:
age = 55
count = 0
while count < 3:
guess = int(input("guess age: "))
if age == guess:
print ("yes ,you got it")
elif age < guess:
print ("think older")
else:
print ("think smaller")
count += 1

** # if count == 3:

print ("you have try too many times.")**

else:
print("you have try too many times.")
輸出結果是:
/Library/Frameworks/Python.framework/Versions/3.6/bin/python3.6 "/Users/a1/PycharmProjects/untitled3/day1/whiel True 循環.py"
guess age: 34
think smaller
guess age: 35
think smaller
guess age: 78
think older
you have try too many times.

Process finished with exit code 0


6.for 循環
a,#最基本的for 循環
for i in range(10):
print(i)
輸出結果:
0
1
2
3
4
5
6
7
8
9

b,顯示不連續的數字(0:從0 開始,10:循環10次,2:每隔2位輸出一個數字)
for i in range(0,10,2):
print (i)
輸出結果為:
/Library/Frameworks/Python.framework/Versions/3.6/bin/python3.6 "/Users/a1/PycharmProjects/untitled3/day1/for 循環.py"
0
2
4
6
8
Process finished with exit code 0
c,使用for 循環 猜年齡
例:
age = 55
for i in range (3):
guess = int(input("guess age:"))
if guess == age:
print ("yes,you got it .")
break
elif guess < age:
print ("think older")
else:
print ("think smaller")
else:
print("you have trid too many times.")
輸出結果:
/Library/Frameworks/Python.framework/Versions/3.6/bin/python3.6 "/Users/a1/PycharmProjects/untitled3/day1/for 循環.py"
guess age:34
think older
guess age:34
think older
guess age:34
think older
you have trid too many times.
Process finished with exit code 0

6.continue #跳出本次循環,執行下一次循環
while True:
for i in range(3):
user = int(input("guess age:"))
if user == age:
print("1")
elif user > age:
print(">")
elif user < age:
print("<")
con = input("do you want con?")

if con == ‘c‘:
    continue
else:
    break

age_of_oldboy = 55
count = 0
while count < 3:
age = int(input("please input age of oldboy:"))

if age_of_oldboy == age:
    print("yes ,you got it!")
    break
elif age_of_oldboy < age:
    print("think smaller")
else:
    print("think older")
    count += 1

if count == 3:
    continue_confirm = input("do you want to keep guessing..?")
    if continue_confirm  != ‘n‘:
        count = 0
        # != 不等於

python小白學習之路--01