Process Control
Conditional judgment
if single branch : performs the appropriate action when a "condition" is established.
Syntax structure:
If condition: command
Flow chart:
Example: If 3 is greater than 2, then the output string "very good"
#!/usr/bin/env python#-*-coding:utf-8-*-if 3 > 2: print ("Very good")
if dual branch: performs an action when "condition is" or "condition is not set" to perform another operation.
Syntax structure:
If condition: command1else: command2
Flow chart:
Example: Enter a number, if the input is 5, then output you is right, enter other number, output you is wrong
#!/usr/bin/env python#-*-coding:utf-8-*-num=input (' Please input a number: ') if num = = 5: print (' You're right ') else : print (' You're wrong ')
if multi-branch: equivalent If statement nesting, performing different operations on multiple conditions.
Syntax structure:
If condition: command1elif Condition: command2 ... #可存在多个elif判断 command...else: commandn
Flow chart:
Example: Enter a number that, if it is in the range of 0-9, turns the number into binary output, and if it is in the range 10 to 19, the number is converted to a 8-input output, at 20 or greater, then to 16-input output
#!/usr/bin/env python#-*-coding:utf-8-*-num=int (input (' please input a number: ')) #input输入为str字符串格式用int转化成整数if num in Range: the #range () method is a range, a for loop detailing the print (num) #bin () method, converted to binary elif num in range (10,20): print ( Oct (num)) #oct () method, convert to octal else: print (Hex (num)) #hex () method, convert to hexadecimal
Cycle
For loop: Performs a set of command operations repeatedly based on the different values of the scalar.
Syntax structure:
For variable in collection: command
Flow chart:
Example 1: Output numbers 0 through 10 sequentially
For I in range: print (i)
Range (11) represents a range, from beginning to end, 0,1,2,3,4,5,6,7,8,9,10, a total of 11 numbers, this range Gu Tou regardless of the tail, that is, not including the largest number
Example 2: Output numbers 5 through 10 sequentially
For I in Range (5,11): print (i)
Range (5,11), which represents a range starting at 5 and ending to 11, including 5 but excluding 11
Example 3:0 to 10 numeric range, output one number at every other number
For I in Range (0,11,2): print (i)
The output is: The third number of the 0,2,4,6,8,10,range () method represents the number of steps, which is the value of the interval
Add: For+else
The instruction within the else is executed only after the for loop is completed, and the loop is not interrupted by break or continue
For I in Range (0,11,2): print (i) Else: print (' OK ') output: 0246810ok
Conditional loops
While condition loop: repeats the condition of the test instruction, and executes the corresponding command operation repeatedly, as long as the condition is true, until the condition is false. If used true as a loop condition can produce an infinite loop.
Syntax structure:
While condition: command
Flow chart:
Example: Print all the odd numbers from 0 to 100
I=1while i <=100: print (i) i = i + 2
Add: While+else
Indicates that while the other is not executed until the normal loop is finished, that is, the loop is not interrupted by break and continue, and the else instruction is not executed until it is fully looped
Example:
I=1while i <=5: print (i) i = i + 2else: print (' OK ') output: 135ok
Digital
Number types include: integer and float (that is, integers and decimals), Boolean, plural
Digital Features:
1. Only one value can be stored, no matter how long the number
2, once defined, cannot be changed, because the number is immutable (if not understood, see after the array tuple after the edge to understand)
3. Direct access via variables
Integral type
Integer types can be expressed in decimal, binary, octal, and hexadecimal notation, such as decimal 10, binary 1010, octal 12, and hexadecimal A are all integers 10.
Print (+) print (BIN) #10的十进制转换二进制print (Oct) #10的十进制转换八进制print (hex) #10的十进制转换十六进制, The 0-9 and abcdef outputs are: 100b1010 #0b表示二进制0o12 #0o表示八进制0xa #0x表示16进制
The integral type in the Python2 is the length limit, divides into the integral type and the long integer type, does not have the length limit in the Python3, therefore the number can be called the integer type again, certainly may through the long integer method the integral type Programming long integer, the long integer type number finally has an L identification.
Other common Methods (abbreviated):
Int () converted to integral type
ABS () to find absolute value
Float () conversion layer floating point type
Floating point Type
That is, decimals, such as 1.2,0.3, etc.
Floating point number scientific counting method:
Print (1.3e-3) #1.3 of 10 of the-3-square, E3 represents 10 of the 3-time Square
Boolean value
True with 1 indicates
False is denoted by 0
String
String
A string is a collection of ordered characters used to store and represent basic file information
String attributes:
1. Only one value can be stored
2, non-changeable
3. The character set is defined in order from left to right and the index is accessed sequentially from 0
Definition method: string definition, four kinds of quotation marks are defined strings
A= ' xyz ' b= "xyz" c= "xyz" "d=" "" XYZ "" "
String processing
Remove whitespace: Removes the character position before and after the string, and cannot be removed from the middle
s1= ' abc ' s2= ' **********abc******* ' s3= ' **********a**bc******* ' Print (S1.strip (')) print (S2.strip (' * ')) print ( S3.strip (' * ')) output result: ABCABCA**BC
Split: Specify character as delimiter, convert string to list
s1= ' Abc|def|ghi ' Print (S1.split (' | ')) Output results: [' abc ', ' Def ', ' Ghi ']
Length: Statistical string length, including spaces and symbols
s1= ' abc ' s2= ' **********abc******* ' s3= ' **********a**bc******* ' Print (len (S1)) print (Len (S2)) print (Len (S3)) Output results: 162022
Index: Represents the position of the character, counting from 0
s1= ' abc ' s2= ' **********abc******* ' s3= ' **********a**bc******* ' Print (s1[2]) #0位置为a, 1 positions b,2 position C
Print (s2[11]) print (s3[-8]) #-8 position is the 8th character from the rear, the first character position from the post count is-1 bit output result: CBC
Slices: match the required string fragment according to the index location
s1= ' Abcdefghi ' Print (S1[2:5]) #2, 3,4 position print (S1[2:8:2]) #匹配2, 4,6 position, second 2 for steps, every other match a print (s1[-5:-3]) #倒序 -5,- 4 Position output Result: CDECEGEF
Other common methods:
Capitalize first letter
S1= ' abc def ABC ' Print (S1.capitalize ())
Output Result:
ABC def ABC
Count the number of characters, spaces are also characters
S1= ' abc def ABC ' Print (S1.count (' a ')) print (S1.count (' A ', 0,4)) #表示0-4 position, excluding 4-bit output results: 2 #整个字符串有两个a1 #0, one
Examine the start and end strings
S1= ' abc def ABC ' Print (S1.endswith (' C ')) Print (S1.startswith (' a ')) output: Truetrue
Find if the character is contained within a string
S1= ' abc def ABC ' Print (S1.find (' B ')) Print (S1.find (' d ')) output: 1 #输出的是匹配的第一个字符的索引位置4
formatting strings
msg1= ' name:{},age:{},sex:{} ' Print (MSG1) print (Msg1.format (' Egon ', ', ' Male ')) msg2= ' name:{0},age:{1},sex:{0} ' Print (Msg2.format (' aaaaaaaaaaaaaaaaa ', ' bbbbbbbbbbbbbb ')) msg3= ' Name:{x},age:{y},sex:{z} ' Print (Msg3.format (y=18, X= ' Egon ', z= ' male ') output result: Name:{},age:{},sex:{}name:egon,age:18,sex:malename:aaaaaaaaaaaaaaaaa,age: Bbbbbbbbbbbbbb,sex:aaaaaaaaaaaaaaaaaname:egon,age:18,sex:male
Index (view index location)
S1= ' abc def ABC ' Print (S1.index (' a ')) #匹配第一个a的索引位置print (S1[s1.index (' e ')]) #调用用索引位置
Checks whether a string is a purely numeric
s1= ' 123 ' Print (S1.isdigit ()) Output result: True
Replace
msg= ' Hello Alex ' #赋值时候实际上是msg =str (' Hello Alex '), replace () method is given by STR, i.e. str.replace () print (Msg.replace (' x ', ' X ') Print (Msg.replace (' Alex ', ' Bob ')) Print (Msg.replace (' l ', ' a ')) print (Msg.replace (' l ', ' a ', 1)) Print (Msg.replace (' l ', ' A ', 2)) #第二个数字为替换的次数输出结果: Hello Alexhello bobheaao aaexhealo alexheaao Alex
Uppercase Conversion
x= ' Hello ' Print (x.upper ()) Output: Hello
Check if it's all uppercase
x= ' HELLO ' Print (x.isupper ()) Output result: True
lowercase conversions and checks
x= ' HELLO ' Print (X.lower ()) print (X.islower ()) Output: Hellofalse
Check is not a space
x= "
Start uppercase
x= ' Hello ' Print (x.title ()) Output: Hello
Check if the start is uppercase
msg= ' Hello ' Print (msg.istitle ()) Output result: True
Left-Justified and right-aligned: Numbers indicate the length of the alignment, and the asterisk behind is not enough to be filled with asterisks
x= ' abc ' Print (X.ljust (Ten, ' * ')) print (X.rjust (10, ' * ')) output: ABC**************ABC
Case Exchange
x= ' ab ' Print (x.swapcase ()) Output result: AB
Python-based Process control, digital and string processing