if 语句
vi 2.py
#! /usr/local/bin/python
name = raw_input(“pls shuru name : “)
pwd = raw_input(“ pls shuru pwd : “ )
if name == “hu” and pwd == “123”:
print(“ you are right”)
else:
print (“ pls retype “)
:wq!
chmod +x 2.py
./2.py
-----------------
总结:if 语句 3种 情况:
if 条件:
代码块
else:
代码块
2.
if 条件:
代码块
elsf 条件:
代码块
else:
代码块
3.条件
True False
1>2 n1>n2 n1==n2
name == “XXX” and pwd == “XXX”
name == “XXXX” or pwd === “XXX”
name != “xxxx”
--------------------------------
while 一直做某件事,或者直到满足就退出
while 条件:
代码块
vi 3.py
#! /usr/local/bin/python
import time
n1 = True
while n1:
print(“1”)
time.sleep(1)
n1 = False
print(“end”)
:wq!
chmod +x 3.py
[root@tbstest hu]# ./3.py
1
end
-------------------------------------
import time
kaishi = 1
flag = True
while flag:
print(kaishi)
if kaishi == 10
flag = False
kaishi = kaishi + 1
time.sleep(3)
print(“end”)
-----------
while 循环中break 跳出所有循环,break 下面代码不在执行
kaishi = 1
while True:
print(kaishi)
if kaishi == 10:
break
kaishi = kaishi +1
continue 后面执行,执行下一次循环
跳出本次循环,继续下一次循环
while True:
print(“123”)
break
----------
while True:
print(“123”)
continue
print(“123456”)
打印除了7 的 1到10 的数字:
#! /usr/local/python
start=1
while True:
if start ==7:
start += 1
continue
print(start)
if start == 10:
break
start += 1
10.py
#! /usr/local/bin/python
sum = 0
start = 1
while start < 100:
temp = start % 2
if temp == 1:
sum = sum + start
else:
sum = sum - start
start += 1
print(sum)
~
--------------
11.py
#! /usr/local/bin/python
i=0
while i < 3:
user_name = raw_input("pls user_name: ")
pwd = raw_input("pwd : ")
if user_name == "hu" and pwd == "123":
print("yes")
break
else:
print("no")
i += 1
-------------
编码:
python 2.7
unicode utf-8 gbk
中间是 unicode
python 3+
直接 utf-8 和gbk 直接转换
原文地址:http://hutu677.blog.51cto.com/1318651/1785512