In this article, input and output, data type, and variables are described in detail. in this article, input and output, data types, and variables are described in detail.
>>> Print ('Hello World') # print note that no space exists before the print
Print outputs multiple characters, which are connected with commas (,) in the middle, and replaced with spaces in the final output.
Name = input ()
In the interactive dialog box, the name variable is saved in the input content.
Python basics
Any programming language has its own syntax. the compiler or interpreter is responsible for converting the code conforming to the syntax into machine code and then letting the CPU execute the code. Python is interpreted rather than compiled.
Python uses indentation.
a=100if a >=0: print(a)else: print(-a)
When a statement ends with a colon (:), the indented statement is considered as a code block.
I think it is the specific logic part.
Indentation has advantages and disadvantages. The advantage is that you are forced to write formatted code, but there is no rule on whether the indent is a few spaces or tabs. According to the conventions, the indentation with four spaces should always be used.
There is too much debate over tabs and four spaces on the internet. it depends on what the company needs.
The disadvantage of indentation is that the "copy-paste" function is invalid, which is the most pitfall. When you refactor the code, the code pasted in the past must be re-checked to check whether the indentation is correct. In addition, it is difficult for IDE to format Python code like formatting Java code.
The Python program is case-sensitive. If an error occurs, the program reports an error.
Python uses indentation to organize code blocks. follow the conventions and stick to the indentation with four spaces.
Note: You need to set the tabs of all text editors to 4 spaces.
Data type
As the name suggests, a computer is a machine that can be used for mathematical computation. Therefore, a computer program can naturally process various numerical values. However, computers can process a variety of data, such as text, graphics, audio, video, and web pages. different data types need to be defined. In Python, the following types of data can be processed directly:
Integer
Python can process integers of any size, positive integers, negative integers such as 1000,-, 0
The computer uses binary, which is more convenient to represent integers in hexadecimal notation, 0x
Floating point number
A floating point is also a decimal point. it is called a floating point because the decimal point of a floating point is variable according to the scientific notation. for example, 1.23x109 and 12.3x108 are completely equal.
Now we know that the original floating point is like this.
Floating point numbers can be written in mathematics, such as 1.23, 3.14,-9.01, and so on. However, large or small floating point numbers must be represented by scientific notation, replacing 10 with e. 1.23x109 is 1.23e9, or 12.3e8, 0.000012 can be written as 1.2e-5.
Integers and floating-point numbers are stored in different ways in the computer. integers are always accurate, and floating-point numbers have rounding errors.
String
A string is any text enclosed by single quotation marks or double quotation marks, such as 'ABC' and "xyz. Note that ''or" "is only a representation, not a part of the string. Therefore, the string 'ABC' contains only 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.
What if the string contains both 'and? It can be identified by escape characters \, for example:
'I \' m "OK "! '
In this case, ''is used eventually. after testing," "is also acceptable.
The most commonly used escape characters are \ n, with line breaks
\ Represents the character \
Python also allows R'' to indicate that internal strings are not escaped by default.
>>>print(r'\\\t\\')
\ T \
There are many line breaks inside the string. If \ n is used in a line, it will be inconvenient to read. in Python, there are "..."
print('''line1line2line3''')
It must be distinguished from comments of multiple lines.
"""
Multi-line comment
Multi-line comment
Multi-line comment
"""
There is also a situation of multi-line comments
'''
'''
This is common
Boolean value
Boolean values are only True and False. note the case sensitivity.
Boolean values can also be calculated using and, or, not.
Null value
None is a special value, not 0, because 0 is meaningful.
Python data types include lists and dictionaries. you can also create custom data types.
Variable
In a computer, a variable can be a number or any data type.
A variable is 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.
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, for example:
A = 123 # a is an integer print (a) a = 'abc' # a becomes a string print ()
This variable type is not fixed. the language is called a dynamic language. the types of variables such as Java and c are fixed, such as int;
More flexible dynamic language
Variable Representation in computer memory:
For example, a = 'abc'
The Python interpreter does two things.
The 'ABC' string is created in the memory;
Create a variable named a in the memory and point it to 'ABC'
You can also assign a value to variable B. This operation actually points variable B to the data pointed to by variable.
Constant
Constants cannot be changed, for example, π. in Python, constants are usually represented by variable names in uppercase.
PI = 3.1415926
In fact, PI is still a variable, and Python does not have any mechanism to ensure that PI will not be changed. Therefore, it is a habit to use all uppercase variable names to represent constants, if you must change the PI value of the variable, no one will stop you.
Python has two division methods, which are different from Python2.
The division is calculated as a floating point/
Division is an integer //
In python2, all operations are divisible unless you replace the number of operations with a floating point number.
The above are detailed descriptions of input and output, data types, and variables in Python. For more information, see other related articles in the first PHP community!