I. If Else1.if StatementsIf expression://Note If there is a colon after, there must be Statement (s)//4 spaces relative to if indent Note: Python uses indentation as a method of grouping its statements, and it is recommended to use 4 spaces2. Example:1 "[email protected] python-scripts]# cat 11.py#!/usr/bin/python#coding =utf-8if 1: //In Python 1 is true (true), 0 is False (fluse) print "Hello python" //when it is true output "Hello python" Else:print "Hello Linux" //otherwise output "Hell Linux" Run as follows:[[email protected] python-scripts]# python 11.pyHello python2 "[email protected] python-scripts]# cat 11.py#!/usr/bin/python#coding =utf-8if 0: The print "Hello python" statement is executed only when the condition is set, otherwise print "Hell Linux" is executedprint "Hello python"Else:print "Hello Linux"Run as follows:[[email protected] python-scripts]# python 11.pyHello Linux 3 "[email protected] python-scripts]# cat 11.py#!/usr/bin/python#coding =utf-8if 1 < 2://1 less than 2 is established, the following statement is executedprint "Hello python"Else:print "Hello Linux"The results of the operation are as follows:[[email protected] python-scripts]# python 11.pyHello python4 "[email protected] python-scripts]# cat 11.py#!/usr/bin/python#coding =utf-8a = (' B ', +)if ' B ' in a:print "Hello python"Else:print "Hello Linux"Run as follows:[[email protected] python-scripts]# python 11.pyHello python5 "[email protected] python-scripts]# cat 11.py#!/usr/bin/python#coding =utf-8if not 1 > 2: // negateprint "Hello python"Else:print "Hello Linux"Run as follows:[[email protected] python-scripts]# python 11.pyHello python6 "[email protected] python-scripts]# cat 11.py#!/usr/bin/python#coding =utf-8if not 1 > 2 and 1==1:print "Hello python"Else:print "Hello Linux"[[email protected] python-scripts]# python 11.pyHello python3.if Statement Exercise: Here is the input [email protected] python-scripts]# cat 12.py#!/usr/bin/python#coding =utf-8sorce = input("Please input a num:") if Sorce >=:print "A"print "Very good"elif sorce >=80:print ' B 'print ' good 'elif sorce >=70:print ' Pass 'Else:print ' not pass '4.int Usagein [1]: Int (' + ') //30 is a string, and it becomes a number by int Out[1]:In [2]: type (int (') )out[2]: int //type is shaped 5.if Practice, here with Raw_input [email protected] python-scripts]# cat 12.py#!/usr/bin/python#coding =utf-8sorce = int(raw_input("Please input a num:")) //Use raw_input here to turn the quoted numbers into plastic if Sorce >=:print "A"print "Very good"elif sorce >=80:print ' B 'print ' good 'elif sorce >=70:print ' Pass 'Else:print ' not pass '
Python Basic 2.1 If Process Control (i)