18 19 20 py if statement comparison operator assertions
Keywords:
Big Data
Hadoop
Python
shell
less
The fourth lesson Conditional statement( if,else and elif)
# coding:utf-8
python The language is defined by indented code blocks
# Conditional statements (if, else, and elif)
'''
if logic_expression:
statement1
statement2
statement3
... ...
statementn
elif logic_expression:
statement1
statement2
... ...
statementn
else:
statement1
statement2
... ...
statementn
otherstatement
'''
n = 3
if n == 3: # In python, 4 spaces or 1 tab are used by default
print("n == 3")
print("hello world")
# Indents that belong to the same code block must be the same
print("------------------")
w = 2
if w == 1:
print("w ==1")
else: #Don't forget that there's a colon behind 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("Please enter your name:")
if name.startswith("B"): #This represents the prefix of the string, that is, it starts with B, which is a method
print("Name to B Start")
elif name.startswith("F"):
print("Name to F Start")
elif name.startswith("T"):
print("Name to T Start")
else:
print("First name starts with another letter")
//This part of the code is written in 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 "Your Name:$name"
elif [ $name == "mjh" ];then
echo "Your Name:$name"
else
echo "Your name is other"
fi
[hadoop@dev-hadoop-test03 majihui_test]$ sh a.sh
pls input you name:majihui
//Your name: majihui
[hadoop@dev-hadoop-test03 majihui_test]$ sh a.sh
pls input you name:mjh
//Your name: mjh
[hadoop@dev-hadoop-test03 majihui_test]$ sh a.sh
pls input you name:sdd
//Your name is other
[root@localhost if]# Cat ifjiaab5.sh [if there is a bug in this script, do you want to judge whether both parameters are positive integers] the method of judging positive integers is explained above!
#!/bin/bash
if [ $1 -lt $2 ];then
echo "$1<$2"
elif [ $1 -eq $2 ];then
echo "$1=$2"
else
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
---------------------------------------------------------------------
//Lesson 5 if conditional statement nesting code block
//Here is the teacher's code
# coding:utf-8
name = input("What's your name?")
if name.startswith("Bill"):
if name.endswith("Gates"):
print("Welcome Bill Gates Sir")
elif name.endswith("Clinton"):
print("Welcome, Mr Clinton")
else:
print("Unknown name")
print("hello world")
elif name.startswith("Plum"):
if name.endswith("Ning"):
print("Welcome to Miss Li Ning")
else:
print("Unknown name")
else:
print("Unknown name")
---------------------------
//Let's write a code ourselves
# coding:utf-8
name = input("Please input A B C Three letters")
if name.startswith("A"): #Indicates A
if name.endswith("a"): #That ends with a
print("What you entered is Aa")
elif name.endswith("b"):
print("What you entered is Ab")
else:
print("I don't know what you're typing")
print("hello world") #This code means that whatever it is, it will output hello world
elif name.startswith("Plum"):
if name.endswith("Ning"):
print("Welcome to Miss Li Ning")
else:
print("Unknown name")
else:
print("Unqualified input")
---------------------------------------
//Lesson 6 comparison operators
# coding:utf-8
# Comparison operator
'''
x == y x Be equal to y
x < y x less than y
x > y x greater than y
x <= y x Less than or equal to y
x >= y x Greater than or equal to y
x != y x Not equal to y
x is y x and y Is the same object
x is not y x and y Not the same object means x y Created by two completely different classes
x in y x yes y Members of the container, y It's a list.[1,2,3,4],1 in y,10 in y
x not in y x No y Members of the container
//The value after comparison of all comparison operators is boolean type******
shell in
//Integer binary comparison operator
//Comparator used in [] comparator used in (()) and [[]] description
-eq == equal The abbreviations of are equal
-ne != not equal Unequal
-gt > greater than
-ge >= Greater than or equal to
-lt < less than
-le <= Less than or equal to
'''
print("Hello" == "Hello") #The value is True
print("hello" == "Hello") #A value of flash indicates that python is strictly case sensitive
# print("hello" = "Hello") #If we only use an equal sign =, we will report the error directly
print(10 == 20) #The value is false
print("hello" > "Hello") #A value of true indicates that the value of the lower case h ASC code is greater than h
print("hello" > "hello world") #If the value is false, it means that the hello prefix is the same, so the comparison degree is obvious that the length of the back is greater than the length of the front
list = [1,2,3,4,5] # This is a list.
print(1 in list) #The value is true
print(10 in list) #The value is false
print(10 not in list) #The value is true
print("--------------------------------")
# Applying comparison operators to our conditional statements
x = 40
y = 30
s1 = "Hello"
s2 = "World"
if x < y:
print("x < y")
else:
print("x >= y")
# Or logic or this is similar to shell general||
# and logic is similar to the&&
shell The following are:
//Logical operators
//Logical operator used in [] description of logical operator used in [[]]
-a [and And] && ""And" are true, then true
-o [or Or] || "Or "if one of the two ends is true, it is true
! ! "No is true on the contrary
'''
and True and True == True
or False or False == False
'''
if x < y and s1 < s2:
print("Meeting conditions")
elif not s1 > s2:
print("Basically meet the conditions")
else:
print("Not satisfied with conditions")
Lesson 7 Assertions
//The literal interpretation of an assertion is that after a certain condition is satisfied, the whole assertion is broken
Assertions are equivalent to conditional statements + throwing exceptions
#Assertions
'''
if not condition
crash program
If the conditions are not met, run an exception / / it is mainly used in the development mode of TDD
TDD (Test Driven Development) ා the normal development mode of test driven development is to conduct black box and white box test after the development is completed
#The process of TDD is the opposite. Before I write a program, I specified this block. For example, our program involves x y z
For example, my program must meet the requirements of x > 20 y < 10 z = = 50 to be successful. As long as you change the conditions, my program will make him make mistakes artificially
x y z
x > 20
if x <= 20:
Throw exception
y < 10
z == 50
'''
value = 20
Assert value > 10 ා this output is true but there is no output at the output station
value = 4
Assert value > 10 ා if he doesn't meet the conditions, he will run out of an exception, which is a pile of code errors.
The error code is:
/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 <module>
assert value > 10
AssertionError
Process finished with exit code 1
Posted by berbbrown on Fri, 03 Jan 2020 16:59:05 -0800