18 19 20 pyif文比較演算子アサートasserts
6263 ワード
(if、else elif)
# coding:utf-8
python
# (if、else elif)
'''
if logic_expression:
statement1
statement2
statement3
... ...
statementn
elif logic_expression:
statement1
statement2
... ...
statementn
else:
statement1
statement2
... ...
statementn
otherstatement
'''
n = 3
if n == 3: # python 4 1 tab
print("n == 3")
print("hello world")
#
print("------------------")
w = 2
if w == 1:
print("w ==1")
else: # else
print("w !=1")
print("------------------")
n = 5
if n == 3:
print("n == 3")
print("xyz")
elif n == 4:
print("n == 4")
print("ddd")
elif n == 5:
print("n == 5")
print("xxx")
else:
print("n != 3")
print("abc")
name = input(" :")
if name.startswith("B"): # , B
print(" B ")
elif name.startswith("F"):
print(" F ")
elif name.startswith("T"):
print(" T ")
else:
print(" ")
shell
[hadoop@dev-hadoop-test03 majihui_test]$ cat a.sh
#!/bin/bash
read -t 6 -p "pls input you name:" name
if [ $name == "majihui" ];then
echo " :$name"
elif [ $name == "mjh" ];then
echo " :$name"
else
echo " "
fi
[hadoop@dev-hadoop-test03 majihui_test]$ sh a.sh
pls input you name:majihui
:majihui
[hadoop@dev-hadoop-test03 majihui_test]$ sh a.sh
pls input you name:mjh
:mjh
[hadoop@dev-hadoop-test03 majihui_test]$ sh a.sh
pls input you name:sdd
[root@localhost if]# cat ifjiaoben5.sh 【 bug 】 !
#!/bin/bash
if [ $1 -lt $2 ];then
echo "$1$2"
fi
[root@localhost if]# sh ifjiaoben5.sh 1 2
1<2
[root@localhost if]# sh ifjiaoben5.sh 2 1
2>1
[root@localhost if]# sh ifjiaoben5.sh 1 1
1=1
---------------------------------------------------------------------
if
# coding:utf-8
name = input(" ?")
if name.startswith("Bill"):
if name.endswith("Gates"):
print(" Bill Gates ")
elif name.endswith("Clinton"):
print(" ")
else:
print(" ")
print("hello world")
elif name.startswith(" "):
if name.endswith(" "):
print(" ")
else:
print(" ")
else:
print(" ")
---------------------------
# coding:utf-8
name = input(" A B C ")
if name.startswith("A"): # A
if name.endswith("a"): # a
print(" Aa")
elif name.endswith("b"):
print(" Ab")
else:
print(" ")
print("hello world") # hello world
elif name.startswith(" "):
if name.endswith(" "):
print(" ")
else:
print(" ")
else:
print(" ")
——————————————————————————————————————————————————————————————————————————————
# coding:utf-8
#
'''
x == y x y
x < y x y
x > y x y
x <= y x y
x >= y x y
x != y x y
x is y x y
x is not y x y x y 2
x in y x y ,y [1,2,3,4],1 in y,10 in y
x not in y x y
boolean ******
shell
[] (()) [[]]
-eq == equal
-ne != not equal
-gt >
-ge >=
-lt <
-le <=
'''
print("Hello" == "Hello") # True
print("hello" == "Hello") # flase python
# print("hello" = "Hello") # =
print(10 == 20) # false
print("hello" > "Hello") # true h H
print("hello" > "hello world") # flase hello
list = [1,2,3,4,5] #
print(1 in list) # true
print(10 in list) # false
print(10 not in list) # true
print("--------------------------------")
#
x = 40
y = 30
s1 = "Hello"
s2 = "World"
if x < y:
print("x < y")
else:
print("x >= y")
# or shell ||
# and shell &&
shell :
[] [[]]
-a 【and 】 && “ ” ,
-o 【or 】 || “ ”
! ! “ ”
'''
and True and True == True
or False or False == False
'''
if x < y and s1 < s2:
print(" ")
elif not s1 > s2:
print(" ")
else:
print(" ")
(Assertions
// ,
+
# (assertions)
'''
if not condition
crash program
// TDD
TDD(Test-driven development) #
#TDD x y z
x > 20 y < 10 z == 50 ,
x y z
x > 20
if x <= 20:
y < 10
z == 50
'''
value = 20
assert value > 10 #
value = 4
assert value > 10 # , 。
:
/Users/majihui/pycharm_work/venv/bin/python /Users/majihui/pycharm_work/test05.py
Traceback (most recent call last):
File "/Users/majihui/pycharm_work/test05.py", line 2, in
assert value > 10
AssertionError
Process finished with exit code 1