Weekend, but I'm not fit to have a weekend ah, 7.9 awake, continue to look at Python, yesterday downloaded a tool pycharm, this tool and idea is a family. Use almost, today continue to look at the foundation.
Write format: Python is particularly sensitive to capitalization, and you need to be careful when writing
Syntax: A statement that begins with an indent # is a comment that is visible to the person and can be arbitrary, and the interpreter ignores the comment. Each of the other lines is a statement, and when the statement ends with a colon : , the indented statement is treated as a block of code.
Indentation has pros and cons. The advantage is that you are forced to write the formatted code, but there is no rule that indents are a few spaces or tabs. In accordance with established management, you should always stick to the indentation of 4 spaces.
Data types and variables: Refer to the Liaoche teacher's works
Integer
Python can handle integers of any size, including, of course, negative integers, which are represented in the program in the same way as mathematically, for example:,,, 1 100 , and so on -8080 0 .
Because the computer uses binary, it is sometimes convenient to use hexadecimal notation for integers, and hexadecimal is 0x represented by prefixes and 0-9,a-f, for example: 0xff00 ,, and 0xa5b4c3d2 so on.
Floating point number
Floating-point numbers, which are decimals, are called floating-point numbers because, when represented by scientific notation, the decimal position of a floating-point number is variable, for example, 1.23x109 and 12.3x108 are exactly equal. Floating-point numbers can be written in mathematical notation, such as,, 1.23 3.14 , and -9.01 so on. But for very large or very small floating-point numbers, it must be expressed in scientific notation, the 10 is replaced with E, 1.23x109 is 1.23e9 , or 12.3e8 , 0.000012 can be written 1.2e-5 , and so on.
Integers and floating-point numbers are stored inside the computer in different ways, and integer operations are always accurate (is division accurate?). Yes! ), and the floating-point operation may have rounding errors.
String
A string is ‘ any text enclosed in single or double quotation marks " , such as ‘abc‘ , and "xyz" so on. Note that ‘‘ or "" itself is just a representation, not part of a string, so the string is ‘abc‘ only a , b c this 3 characters. If it ‘ is also a character, it can be "" enclosed, such as the character that contains it,,, the space, the "I‘m OK" I ‘ m O K 6 characters.
What if the inside of a string contains ‘ and contains both " ? Can be identified by an escape character \ , such as:
‘I\‘m \"OK\"!‘
The string content represented is:
I‘m "OK"!
Boolean value
Boolean values are exactly the same as Boolean algebra, with a Boolean value of only two values, either, or, True False True False in Python, you can directly use True , represent a False boolean value (note case), or you can calculate it by Boolean:
Boolean values can be used and , or and not operations.
andOperations are and operations, and only all are True , the result of the and operation is True :
orAn operation is or an operation, as long as one of them is True , the result of the or operation is True :
notAn operation is a non-operation, which is a single-mesh operator that turns True into False False True :
Null value
A null value is a special value in Python, None denoted by. Nonecannot be understood as 0 , because 0 it is meaningful, and None is a special null value.
In addition, Python provides a variety of data types, such as lists, dictionaries, and also allows you to create custom data types
Variable
The concept of variables is basically the same as the equation variables in junior algebra, but in computer programs, variables can be not only numbers, but also arbitrary data types.
Variables are represented by a variable name in the program, and the variable name must be a combination of uppercase and lowercase English, a number and a number, and _ cannot begin with a digit.
In Python, the equals sign = is an assignment statement that assigns any data type to a variable, the same variable can be repeatedly assigned, and can be a variable of different types
This type of variable itself is called Dynamic language , which corresponds to static language . Static languages must specify the variable type when defining the variable, and if the type does not match, an error is given. For example, Java is a static language, and assignment statements are as follows (//for comments):
int a = 123; // a是整数类型变量a = "ABC"; // 错误:不能把字符串赋给整型变量
This is why dynamic languages are more flexible than static languages.
Constant
So-called constants are immutable variables, such as the usual mathematical constants π is a constant. In Python, constants are typically represented in all uppercase variable names:
In Python, there are two types of division, one of which is / :
>>> 10 / 33.3333333333333335
/The division evaluates to a floating-point number, even if it is exactly divisible by two integers, and the result is a floating-point number:
>>> 9 / 33.0
There is also a division that is // called a floor divide, where the division of two integers is still an integer:
There is no size limit for python integers, and integers for some languages are limited by size depending on their storage length, for example, the range of Java to 32-bit integers is limited to -2147483648 - 2147483647 .
Python also has no size limit for floating-point numbers, but it is represented inf (infinitely large) beyond a certain range.
Self-Study Python notes (iii)