One, variable
Variable definition: Variables is used to store infomation to referrenced and manipulated in a computer program.
Intermediate operation results for stored program runs
Variables are represented by a variable name in the program
The variable name must be a combination of case English, numeric, and _, and cannot start with a number
Sensitive to case
Recommended small hump nomenclature, such as MyFirstName, mylastname
Keyword cannot be declared as a variable
In Python, equals = is an assignment statement that can assign any data type to a variable, the same variable can be repeatedly assigned, and can be a variable of different types
Myfisrtname = "Jonathan" Mylastname = "Ni" |
Do not equate an equal sign of an assignment statement with a mathematical equal sign. For example, the following code:
Mathematically Understanding x = x + 2 is not true. In the program, the assignment statement evaluates the expression x + 2on the right, obtains the result, and assigns it to the variable x.
Since the value before X is 8, the value of x becomes tenafter the value is re-assigned.
It is important to understand the representation of variables in computer memory, such as the following declaring variables and assigning values.
The Python interpreter did two things:
A string of "ABC" was created in memory;
A variable named A is created in memory and points to "ABC".
A constant is an immutable quantity, usually expressed in all uppercase variable names.
Summarize:
Inside a computer, any data is considered an "object", and variables are used to point to these data objects in a program, and assigning values to variables is to associate data with variables.
Ii. Types of data
A computer is a machine that can do mathematical calculations. Computer programs take for granted the various values. In addition to numerical values, the computer can also handle text, graphics, audio, video, Web pages, etc.
Various data. Different data types need to be defined.
1. Digital
Integer: The representation method is the same as the mathematical notation, such as 1, -100, 0, and so on. or 16 binary representation,0xffffff, 0xabcd , etc.
Floating point number: That is, decimal, such as 0.99, -1.25, 88.88 and so on. Or scientific notation means 1.23e8, 1.2e-8 , etc.
Plural: consists of the real and imaginary parts, the general form is x + YJ, such as (-5+4j), etc.
Numbers have no size limit and are represented as inf(Infinity) beyond a certain range
2. String
strings are arbitrary text enclosed in single quotes ' or double quotes, such as ' abc ', 'XYZ ', and so on. ' or ' itself is just a representation, not part of a string,
For example ' ABC ' has only a,b, C, 3 characters.
If ' itself is also a character, then you can use "", such as "I m OK" contains the characters are I, ', M, Space, O, k these 6 characters.
The inside of a string contains both ' and contains ' that can be identified with the escape character \. such as ' I\ ' m \ "ok\"! ', means I ' m "OK"!
Escape characters can escape many characters, such as \ n for line breaks,\ t for tabs, \ \ For characters \
If there are many line breaks inside the string, the format of "... " can be used to represent multiple lines of content.
String Common functions
"Jonathan" . Strip () >>> "Jonathan" "; Jonathan;; ". strip (";" ) >>> "Jonathan" |
Parameter: chars--Removes the character specified by the tail of the string
Return value: Returns a new string that is generated by removing the character of the string specified by the tail. 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--Specifies the retrieved string
Beg--Start index, default is 0
End--ends the index, which defaults to the length of the string
Return value: Throws an exception if the containing substring returns the starting index value.
Python does not support single character types, and one character is also used as a string in Python.
To access substrings, you can use square brackets to intercept strings
"Jonathan" [0] >>> "J" # Subscript starting from 0 "Jonathan" [0:3] >>> "Jon" # Gu Tou regardless of tail "Jonathan" [-1] >>> "n" # Countdown First "Jonathan" [-3:-1] >>>"Ha" # Gu Tou regardless of tail "Jonathan"[-3:0] >>> " # There is no forward interception from the back "Jonathan[-3:] >>>" Han " # interpreter recognized as the third from the bottom up to the last of all characters "Jonathan" [] >>> Invalid syntax # Interpreter cannot distinguish between a single character or a range character "Jonathan" [:] >>> "Jonathan" # Interpreter recognized as all characters "Jonathan" [0:len ("Jonathan")] >>> "Jonathan" "Jonathan" [-len ("Jonathan"):] >>> "Jonathan" "Jonathan"[:: -2] >>> "Jnta" # The last one represents step 2 |
"Jon" + "Nathan" >>> "Joanthan" # Meet +, the interpreter needs to re-request memory |
Name = "Jonathan" Print ("My name is %s."% ( Name) >>> "My name is Jonathan." |
3. Boolean type
The Boolean value is exactly the same as the Boolean algebra, with a Boolean value of only True,False Two values (note case), Boolean values can be evaluated with and , or, and not
The and operation is with an operation, and only all is true, and the result of the and operation is true
An OR operation is a or operation, as long as one of them is true, the or operation results in true:
The not operation is a non-operation, it is a single-mesh operator that turns true to False, false to true
Boolean values are often used in conditional judgments
if age >= : Print ("adult") Else: Print ("Teenager") |
4, null value
The null value is a special value in Python, denoted by None . None cannot be understood as 0, because 0 is meaningful, and none is a special null value.
5. List
Lists are the most common data types in Python and can be used to make the most convenient storage modifications to the data.
Definition List names = ["Jon", "Alex", "Tom", "Catherine"]
To access the elements in the list by subscript, the index is counted starting at 0
Names[0] >>> "Jon" NAMES[-1] >>> "Catherine" |
Working with a list
Names[0] = "Jonathan" # Modify Element Names.append ("Jim") >>> ["Jonathan", "Alex", " Tom", "Catherine" , "Jim"] # The last attachment element in the list Names.insert (2, "Linda") >>> ["Jonathan", "Alex", "Linda" , "Tom", "Catherine", "Jim"] # Insert element in place labeled 2, move after other elements Names.remove ("Linda") >>> ["Jonathan", "Alex", "Tom", "Catherine" , "Jim"] # Delete the first retrieved element del names[4] >>> ["Jonathan", "Alex", "Tom", "Catherine"] # Delete element by subscript Names.index ("Alex") >>> 1 # return subscript Names.sort () >>> ["Alex", "Catherine", "Jonathan", "Tom"] # By ASCII positive order Names.reverse () >>> ["Tom", "Jonathan", "Catherine", "Alex"] # element inversion Names.count ("Tom") >>> 1 # Returns the number of occurrences |
Third, the operation
1. Arithmetic operations
2. Comparison operation
3. Assignment operation
4. Logical operation
5. Member arithmetic
6. Identity operation
7, bit arithmetic