To be a Python expert there is no secret or shortcut We need to think more and practice more not ignoring any part especially the basics Let's start with the basics of Python Like in any other programming language variables in Python are also used to identify or quote objects In practice, the variable needs to be named as the variable name The p and myString here are examples With assignment operation p will represent the floating-point number after it and myString will represent the string after it Can we randomly apply variable names in Python For example, can I apply such a variable name Nope The naming rules of variable names in Python are like in most of other high-level programming languages influenced by the C language with certain rules Here I'd like to mention another concept, called "identifier" What is an identifier indeed? It refers to a valid symbol allowed to be used as a variable name or another object name Like in the C language the naming rules of identifiers in Python are also the following three First, the first character must be a letter or the underline and the others may be letters, underline or numbers Besides, it is sensitive to upper and lower cases For example, these two variables are considered as different identifiers Although identifiers are valid in Python if they follow the three rules there are also some conventions like a variable name all in upper cases We will consider it as a symbol constant In addition, we should try to avoid any variable name with (one or two) underlines at the beginning and, in particular, variable names starting with double underlines since the underline is of special meaning to the interpreter It is a symbol used by built-in identifiers Generally we consider this type of variable names as private Let's look at another variable name Is this an English word It is actually the Chinese pinyin of the word "price" We should try to avoid pinyin since your code might become popular in the world someday and foreign friends can not understand it at all by looking at this variable We should try to make names understood by just looking at them In naming, in addition to English words or a small number of single variables we often need several words to identify the objects Then, how should words be combined Two ways of identifier naming are often adopted One is the Camel Case What is the Camel Case It means the first word is in lowercase and the first letters of the following words are in uppercase Like this, for example Is it indeed like a hump of camel? The other way is the UnderScore Case namely, to connect words with underlines like this In the same program in programming apart from the use of single words if a combination of words is needed we should try to select one way of the two instead of mixing them Apart from those rules for variable naming some combinations of symbols are not allowed they are keywords Keywords are crucial integral parts of Python There are some fixed combinations like "and", "else", "in", "while", etc Usually, in an integrated development environment they appear in different colors and fonts The codes appearing in the PPT of our course imitate the color schemes in IDLE – a standard integrated development environment of Python Variables alone often seem inadequate We often need to use operators to connect various types of formulas which also include variables and some constants, etc, together There are many types of operators in Python like arithmetic operators including mathematical power, positive and negative signs etc there are also bitwise operators, NOT comparison operators such as less than, greater than, equal as well as some logical operators All these operators can link variables and constants to form expressions Let's look at an example say, 2*PI*r Here, the operator "*" is used to link the constant with some variables and form a formula it is rightly an expression An expression must have a result of operation while an operator must follow its priority level when used For example, multiplication/division has higher priority than addition/subtraction etc We will talk about this again later when particularly discussing operation As for the result of expression we often assign it to a variable How to assign it Like in the C language the symbol "=" is also used in Python Let's have a look The assignment in Python is quite special This is an assignment to assign 3.14159 to the variable PI You having learned other languages may feel amazed since in many other languages before assignment we need to declare the type of the variable It is different in Python Python is a dynamic strongly-typed language It needs no explicit declaration Then, how is the type determined? The type is determined by the value It achieves assignment by way of reference In Python each assignment directs one reference to the corresponding memory cell Through such an assignment the variable PI acquires a reference of the memory cell where 3.14159 is Through such an assignment again the variable pi acquires another reference of the memory cell where 3.14159 is Let's do actual operations to understand the reference of variable in Python and characteristics of variable management When each object is created in Python it gets an ID which can be viewed with the function id() It is accompanied by a reference counter like x = 3.5, y = x Execute y is x, and the result is True As we know, it's because x and y refer to the same object "3.5" If we then define z = 3.5, and execute z is x what will the result be The result is False That's because the variable x and the variable z refer to two different 3.5 namely, pointing to two different 3.5 Although the values of the two 3.5 are the same they have separate own memory spaces We may view with the function of id() The results of id(x) and id(z) are not the same Then, look at the id of y As we see, it’s the same as that of x So, the execution result of y is x above is True and the result of z is x is False Next, if we define p = 3, q = 3 what would the execution result of p is q Based on our previous understanding, the result would be False Let’s execute it So strange The result is True Why is this Look at p = 256, q = 256 then The result of p is q is still True Go on: p = 257, q = 257 Execute p is q The result is surprisingly False Why is it like this It seems different from a floating-point number but also a little similar The truth is there are indeed some special points in Python Some identical small integers whose scope is [-5, 256] by default, and some literal constant strings containing only numbers, letters and underlines will reside in (be allocated to) the same memory space As these objects are frequently used this efficient way of storage may optimize the operating speed of program Got it Besides, it is required in the Python standard that, within the same statement block no additional space is allocated to the same immutable object For instance, if the variables x and y are in the same function and in the same statement block if they are both assigned as a large integer like 1000 or the same floating-point number 3.5 execute x is y, and the result is also True After we learn functions later, you may have a try Assignments in Python have rich forms For example, Python supports augmented assignment that is, put some operators and assignment symbols together like "m %= 5" in this way, the result of m%5 is assigned to the variable "m" Also, it has chained assignments like this When executed it first assigns 3.14159 to the variable PI and then goes on to assign to this variable It is similar to the continued equals sign in mathematics Moreover, it supports multiple assignment For example, there are two variables: x and y Their values are 1 and 2 respectively We write it into this form like "x, y" In Python, the comma is used to create a special data structure, called "tuple" It is such an operator A tuple is enclosed in parentheses Let's look at this Multiple assignment can be written like this "y, x = x, y" It seems strange, right? Let's look at the result, then Originally, x and y were 1 and 2 Now x and y have been changed into 2 and 1 Is that an exchange of value? If you have learned any other programming language seeing this mode of exchange would you like to cry? Our mode of exchange before was usually written like this to exchange x and y through a third variable Maybe it once occurred to you that if x could be assigned to y and y to x in form such an exchange might be possible Yes, it becomes true in Python Yes, it becomes true in Python Why can this be achieved in Python? In fact, it is because of the mode of multiple assignment For example let's look at this example "PI,r = 3.1415, 3" In its execution(The internal mechanism of execution will be discussed later) 3.14159 is assigned to the variable PI and 3 to the variable r In this way, both sides of the assignment symbol are actually tuples Sure, parentheses may be omitted a form similar to this 1 assigned to x is indeed called a statement in Python A statement is actually a line of logical codes for completely executing a task like an assignment statement or a statement like this It has completed an output How do we understand the difference between an expression and a statement? Generally, we hold that a statement is for performing a task while an expression is a specific component of a task say, 2*PI*r it is rightly an expression while assigning the operation result of this expression to such a variable as a whole forms a task thus it is a statement What I talked about just now is some basic syntax of Python You can find some small cases to practice so that you can better understand those concepts