This article mainly introduces the strings and comparison operators for getting started with Python. it is the basic knowledge of Python syntax. For more information, see
Python string
A String is a String of numbers, letters, and underscores.
It is generally recorded:
s="a1a2···an"(n>=0)
It is the data type that represents text in programming languages.
The python string list has two values:
- From left to right Index starts from 0 by default, and the maximum range is 1 less string length
- From right to left index start from-1 by default. The maximum range is the start of the string.
- If you want to obtain a substring, you can use the variable [header subscript: tail subscript] to extract the corresponding string, where the subscript starts from 0, it can be a positive or negative number, and the subscript can be null to get the header or tail.
For example:
s = 'ilovepython'
S [] returns love.
When a string separated by a colon is used, python returns a new object. The result contains the continuous content identified by the offset, and the start on the left contains the lower boundary.
The above result contains the Value l of s [1], and the maximum obtained range does not include the upper boundary, that is, the value p of s [5.
The plus sign (+) is a string concatenation operator, and the asterisk (*) is a repeated operation. Example:
#! /Usr/bin/python #-*-coding: UTF-8-*-str = 'Hello World! 'Print str # print str [0] # print str [2] # print str [] # print str [2] Between the third and fifth characters in the output string :] # print str * 2 # print str + "TEST" # output the connected string twice
Output result of the above instance:
Hello World!Hllollo World!Hello World!Hello World!Hello World!TEST
Python comparison operator
Assume that variable a is 10 and variable B is 20:
The following example demonstrates all Python comparison operators:
#!/usr/bin/pythona = 21b = 10c = 0if ( a == b ): print "Line 1 - a is equal to b"else: print "Line 1 - a is not equal to b"if ( a != b ): print "Line 2 - a is not equal to b"else: print "Line 2 - a is equal to b"if ( a <> b ): print "Line 3 - a is not equal to b"else: print "Line 3 - a is equal to b"if ( a < b ): print "Line 4 - a is less than b" else: print "Line 4 - a is not less than b"if ( a > b ): print "Line 5 - a is greater than b"else: print "Line 5 - a is not greater than b"a = 5;b = 20;if ( a <= b ): print "Line 6 - a is either less than or equal to b"else: print "Line 6 - a is neither less than nor equal to b"if ( b >= a ): print "Line 7 - b is either greater than or equal to b"else: print "Line 7 - b is neither greater than nor equal to b"
Output result of the above instance:
Line 1 - a is not equal to bLine 2 - a is not equal to bLine 3 - a is not equal to bLine 4 - a is not less than bLine 5 - a is greater than bLine 6 - a is either less than or equal to bLine 7 - b is either greater than or equal to b