This article describes how to use Python for automated development-Introduction to variables, data types, and calculation methods
I. variables
Variable definition: Variables are used to store infomation to referrenced and manipulated in a computer program.
Used to store the intermediate calculation result of a program running.
Variables are represented by a variable name in the program.
The variable name must be a combination of uppercase and lowercase English letters, numbers, and _, and cannot start with a number.
Case Sensitive
We recommend that you use the camper method, such as myFirstName and myLastName.
The keyword cannot be declared as a variable.
In Python, equal sign = is a value assignment statement, which can assign any data type to a variable. the same variable can be assigned multiple times and can be of different types.
MyFisrtName = "Jonathan" MyLastName = "Ni" |
Do not equate the equals sign of the value assignment statement with the equals sign of mathematics. For example, the following code:
Mathematical understanding of x = x + 2 is not true. In the program, the value assignment statement calculates the expression x + 2 on the right, returns 10, and then assigns the variable x.
Since the value before x is 8, after the value is re-assigned, the value of x becomes 10.
It is very important to understand the representation of variables in computer memory. for example, the following statements declare variables and assign values.
The Python interpreter does two things:
A "ABC" string is created in the memory;
Create a variable named a in the memory and point it to "ABC ".
A constant is a variable that cannot be changed. it is usually represented by a variable name in uppercase.
Summary:
In a computer, any data is regarded as an "object", and variables are used to point to these data objects in a program. assigning values to variables is to associate the data and variables.
II. Data types
A computer is a machine that can be used for mathematical computation. Computer programs naturally need to process various numbers. In addition to numeric values, computers can also process text, graphics, audio, video, webpages, etc.
Various data. Different data types must be defined.
1. number
Integer: indicates that the method is the same as that in mathematics, such as 1,-100, 0, and 1000. Or in hexadecimal notation, 0 xffffff, 0xabcd, etc.
Floating point: that is, decimal places, such as 0.99,-1.25, and 88.88. Or use scientific notation to indicate 1.23e8, 1.2e-8, etc.
Plural: it consists of a real number and a virtual number. generally, it is in the form of x + yj, such as (-5 + 4j ).
There is no limit on the number size. if the number exceeds a certain range, it is expressed as inf (infinitely large)
2. string
A string is any text enclosed by single quotation marks or double quotation marks, such as 'ABC' and "XYZ. 'Or "is only a representation, not part of a string,
For example, 'ABC' only contains three characters: a, B, and c.
If 'is also a character, it can be enclosed by "". for example, "I'm OK" contains the characters I,', m, space, O, k.
The character string contains both 'and can be identified by escape characters. For example, 'I \' m \ "OK \"! ', Indicating I'm "OK "!
Escape characters can escape many characters. for example, \ n indicates line breaks, \ t indicates tabs, and \ indicates characters \
If a string contains many line breaks, the format of '''... ''' can be used to represent multiple lines of content.
Common string functions
"Jonathan". Strip ()>>> "Jonathan" "; Jonathan ;;".Strip (";")>>> "Jonathan" |
Parameter: chars -- remove the specified character from the start or end of the string
Return value: return the new string generated by removing the specified character from the start or end of the string. The original string remains unchanged.
Parameter: str -- string
Return value: string length
"Jonathan". index ("J") >>> 0 "Jonathan". index ("J", 1) >>>Substring not found "Jonathan". index ("an") >>> 6 |
Parameters:
Str -- specify the string to be retrieved
Beg -- start index. the default value is 0.
End -- end index. the default value is the string length.
Returned value: if a substring is included, the system returns the starting index value. Otherwise, an exception is thrown.
Python does not support the single character type. a single character is also used as a string in Python.
Access the substring and use square brackets to intercept the substring.
"Jonathan"[0]>>> "J" # subscript starts from 0 "Jonathan"[0: 3]>>> "Jon" # ignore the tail "Jonathan"[-1]>>> "N" # Last "Jonathan"[-3:-1]>>> "Ha" # ignore tail "Jonathan"[-3: 0]>>> "" # No truncation from the back to the front "Jonathan[-3:]>>> "Han" # The interpreter identifies all characters from the last to the last "Jonathan"[]>>>Invalid syntax# The interpreter cannot tell whether a single character is a specific interval character. "Jonathan"[:]>>> "Jonathan" # The interpreter recognizes all characters "Jonathan"[0:Len ("Jonathan")]>>> "Jonathan" "Jonathan"[-Len ("Jonathan"):]>>> "Jonathan" "Jonathan"[:-2]>>> "Jnta" # The last digit represents step 2. |
"Jon" + "nathan"> "Joanthan" # encounter +, the interpreter needs to apply for memory again |
Name = "Jonathan" Print ("My name is % s." % (name) >>> "My name is Jonathan ." |
3. Boolean
The Boolean value is exactly the same as that of Boolean algebra. a Boolean value is only True or False (case sensitive ).And, or,AndNotOperation
AndOperation and operation, only True for all,AndThe result is True.
OrOperation is or, as long as one of them is True,OrThe result is True:
NotThe operation is not an operation. it is a single-object operator that converts True to False and False to True.
Boolean values are often used in condition judgment.
IfAge> = 18: Print("Adult ") Else: Print("Teenager ") |
4. null value
A null value is a special value in Python, expressed as None. None cannot be understood as 0, because 0 is meaningful, and None is a special null value.
5. list
The list is the most common data type in Python. you can use the list to conveniently store and modify data.
Definition list names = ["Jon", "Alex", "Tom", "Catherine"]
Access the elements in the list by subscript. the subscript starts counting from 0.
Names [0]> "Jon" Names [-1]> "Catherine" |
Operate the list
Names [0] = "Jonathan" # modifying elements Names. append ("Jim") >>> ["Jonathan", "Alex", "Tom", "Catherine ","Jim"] # Add an element at the end of the list Names. insert (2, "Linda") >>> ["Jonathan", "Alex ","Linda", "Tom", "Catherine", "Jim"] # insert an element in the place marked as 2. move the other elements Names. remove ("Linda") >>> ["Jonathan", "Alex", "Tom", "Catherine", "Jim"] # delete the first Retrieved element Del names [4] >>> ["Jonathan", "Alex", "Tom", "Catherine"] # delete elements by subscript Names. index ("Alex") >>> 1 # returns the subscript Names. sort () >>> ["Alex", "Catherine", "Jonathan", "Tom"] # sort by ASCII forward Names. reverse () >>> ["Tom", "Jonathan", "Catherine", "Alex"] # element inversion Names. count ("Tom") >>> 1 # return the number of occurrences |
III. operation
1. Arithmetic operations
The above is a detailed description of the variables, data types, and calculation methods using Python automated development. For more information, see other related articles in the first PHP community!