Python 變量-循環
一、變量
- 不管什么編程語言或腳本語言 在定義變量時都有一定的規則。Python變量定義規則如下:
-
- 變量名只能是字母、數字或下划線的任意組合
- 變量名的第一個字符不能是數字
- 關鍵字不能當變量名,如下:
'and'、'as'、'assert'、'break'、'class'、'continue'、'def'、'del'、'elif'、'else'、'except'、'exce'、'finally'、'for'、'from'、'if'、'is'、'in'、'global'、'import'、'lambda'、'not'、'or'、'pass'、'print'、'raise'、'retuen'、'try'、'while'、'with'、'yield'
- 聲明變量和賦值
- 字符串變量聲明:字符串必須加引號. 單引、雙引都可以。主要含有字母都必須加引號
Name = "Yuhl1"
- 數字類型變量聲明:數字類型可不加引號
Age = 27
Name 是變量名,Yuhl1是變量值。下邊Age同等
- 注釋
- 單行注釋
# 被注是的內容
-
- 多行注釋
""" 被注釋的內容 """
二、Python 第一個程序
在linux下創建一個.py文件,如下:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
_Author_ = "Yuhl"
Google_Mail = "cnblogs.honglin@gmail.com"
# 提示前台輸入 關鍵字"input"
Google_Url = input("Please Input Google The Url:")
print(Google_Url)
print(Google_Mail)
三、循環語句
- if 語句
- 外層變量可以被內層代碼使用,內層代碼無法被外層代碼使用
user_name = input("Please input user name:")
password = input("Please input user password:")
if user_name == "Yuhl" and password == "abc123"
print("welcome %s Login successful"%user_name)
else:
print("username and password errors..")
--------------------------------------------------------------------------------------------------------------------------
my_age = 27
user_age = input("Please Input your age")
if user_age == my_age:
print("Congratulations, you got it")
elif user_age > my_age:
print("Oops,got guess bigger")
else:
print("Oops,got guess small")
- for 語句
- 注意for語言的嵌套使用
打印十個數字
for line in range(10):
print(line)
流程控制 只打印數字5后面的
for line in range(10):
if line > 5:
print(line)
for循環猜年齡游戲【只能猜三次】
Yuhl_age = 27
for line in range(3):
user_age = input("Please Input user age:")
if user_age == Yuhl_age:
print("welcome your guessed it")
elif user_age > Yuhl_age:
print("input errors larger. please again input use age")
else:
print("input errors smaller. please again input use age")
for else結合使用猜年齡游戲講解【for else】
Yuhl_age = 27
for line in range(3):
user_age = input("Please Input user age:")
if user_age == Yuhl_age:
print("welcome your guessed it")
elif user_age > Yuhl_age:
print("input errors larger. please again input use age")
else:
print("input errors smaller. please again input use age")
else:
exit("Too Many Attemots....")
PS:for else的含義是程序在正常執行的時候走else下的代碼.如果中間程序有唄break的那么就不會執行else里代碼
如果以下代碼中的if 判斷和break注視加上. 那么會執行else中的代碼. 正常執行
如果以下代碼中的if 判斷和break注視未加. 那么不會執行else中的代碼. 這就是區別. 中間截斷不執行else下數據
for i in range(10):
print(i)
if i == 5:
break
else:
print("程序正常結束")
for 循環嵌套講解01
for i in range(5):
for j in range(5):
print(i,j) # 此處pint包含3
if j > 3:
break # break跳出整個當前層循環. 只跳一層
print(i,j) # 此處pint不包含3. 邏輯問題
for 循環嵌套講解02
for i in range(10):
for j in range(10):
print(i.j) # 此處print的話包含6
if j < 5:
break # break. 外層大循環依舊被執行過. 嵌套循環中j < 5就break了. 那么下邊的print就不會被執行
continue # 跳出當前循環. 繼續下一次循環
print(i,j) # 此處print的話不包含6 邏輯問題
- while 語句
while 死循環
count = 0
while True:
print("welcome your enter while Infinite loop.....%s"%count)
count += 1
while 語句if進行流控制.只打印某一點數據
count = 0
while True:
if count == 9999
print("welcome your enter while Infinite loop.....%s"%count)
break
count += 1
while 語句. 只打印多少次
count = 0
while count < 10:
print("welcome your enter while Infinite loop.....%s"%count)
count += 1
while 語句.加if控制流
count = 0
while True:
print("welcome your came to Heaven on earth")
if count == 5:
print("What a paradise on earth it is hell on earth")
break
count += 1
while 猜年齡游戲
count = 0
YHL_age = 27
while count < 3:
"""strip() #去除空格 isdigit()判斷是不是整數 int()轉換成整數 """
input_age = input("Please input age:").strip()
if input_age.isdigit():
input_age = int(input_age)
else:
continue
if input_age == YHL_age:
print("guess correct .....")
break
elif input_age == YHL_age:
print("guess the.")
else:
print("Guess a little")
count += 1
