# Python Introductory Course chapter 3rd python variables and data types

Source: Internet
Author: User

Chapter 3rd python variables and data types
3-1 data types in Python
A computer is a machine that can do mathematical calculations as the name implies, so a computer program can handle a variety of values. However, the computer can handle far more than the numerical value, but also can deal with text, graphics, audio, video, web and other kinds of data, different data, need to define different data types. In Python, there are several types of data that can be processed directly:
One, integer
Python can handle integers of any size, including, of course, negative integers, and in a Python program, integers are represented in exactly the same way as mathematical notation.
Because the computer uses binary, it is sometimes convenient to use hexadecimal notation for integers, and hexadecimal is represented by 0x prefixes and 0-9,a-f.
Second, 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.23x10^9 and 12.3x10^8 are equal. Floating-point numbers can be written in mathematical notation, such as 1.23,3.14,-9.01, and 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.23x10^9 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.
Three, string
strings are arbitrary text enclosed in ' or ', such as ' abc ', ' XYZ ', and so on. Note that the ' or ' itself is only a representation, not a part of the string, so the string ' abc ' only a,b,c these 3 characters.
Four, Boolean value
The Boolean value is exactly the same as the Boolean algebra, with a Boolean value of only true, false two, or true, or false, in Python, which can be used to indicate a Boolean value directly with True, false (note case), or by Boolean operations.
Boolean values can be operated with and, or, and not.
The and operation is associated with an operation, and only the result of all true,and operations is True.
An OR operation is an operation, as long as one of the true,or operation results is True.
The not operation is a non-operation, which is a single-mesh operator that turns true to False,false to true.
Five, 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.
In addition, Python provides a variety of data types, such as lists, dictionaries, etc., and also allows you to create custom data types.
3-2 python Print statement
The print statement can output the specified text to the screen. For example, the output of ' Hello, world ', implemented in code as follows:
>>> print ' Hello, world '
Attention:
1. When we write code in a python interactive environment,,>>> is the prompt for the Python interpreter, not part of the code.
2. When we write code in a text editor, never add >>> yourself.
The print statement can also be followed by multiple strings, separated by a comma "," that can be connected to a string of outputs:
Print prints each string sequentially, encounters a comma "," and outputs a space, so the output string is spelled like this:
Print also prints integers, or calculates the result:
At any time, we can add comments to the program. Annotations are used to illustrate the code, to yourself or others, and when the program runs, the Python interpreter ignores comments directly, so there is no comment that doesn't affect the execution of the program, but it affects whether someone can read your code.
Python comments begin with #, followed by text until the end of the line is counted as a comment
3-4 variables
In Python, the concept of variables is basically the same as the equation variables of the middle school algebra.
For example, for equation y=x*x, x is the variable. When x=2, the result of the calculation is 4, when x=5, the result of the calculation is 25.
Just in a computer program, a variable can be not only a number, but also any data type.
In a python program, a variable is represented by a variable name, and the variable name must be a combination of uppercase and lowercase English, numeric, and underscore (_) and cannot start with a number, such as:
A = 1
Variable A is an integer.
t_007 = ' T007 '
The variable t_007 is a string.
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, for example:
A = 123 # A is an integer
Print a
A = ' Imooc ' # A becomes a string
Print a
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 is an integer type variable
A = "MOOC"; Error: Cannot assign string to integer variable
This is why dynamic languages are more flexible than static languages.
Do not equate an equal sign of an assignment statement with a mathematical equal sign. For example, the following code:
x = 10
x = x + 2
If mathematically understood x = x + 2 That is not true anyway, in the program, the assignment statement first calculates the right expression X + 2, obtains the result 12, and assigns the variable x. Since the value before X is 10, the value of X becomes 12 after the value is re-assigned.
Finally, it is important to understand the representation of variables in computer memory. When we wrote: a = ' ABC ', the Python interpreter did two things:
1. Create a string of ' ABC ' in memory;
2. Create a variable named a in memory and point it to ' ABC '.
You can also assign a variable A to another variable B, which actually points the variable B to the data that the variable a points to, such as the following code:
A = ' ABC '
b = A
A = ' XYZ '
Print B
Does the last line print out the contents of variable b exactly ' ABC ' or ' XYZ '? If you understand mathematically, you will mistakenly conclude that B and a are the same and should be ' XYZ ', but actually the value of B is ' ABC ', and let us execute the code in one line, and we can see exactly what happened:
Execute a = ' abc ', the interpreter creates the string ' abc ' and variable A, and points a to ' abc ':
Annotations:
Variable this thing, every language has, each time to see all the different understanding, the deeper study, the book reads times, its righteousness self-see.
3-5 python definition string
We've explained what strings are in front of us. Strings can be represented by "or".
What if the string itself contains ' what? For example, we want to represent the string I ' m OK, at this point, you can use "" to denote:
"I ' m OK"
Similarly, if the string contains ", we can use a" to denote:
' Learn ' Python "in Imooc '
What if the string contains both ' and contains '?
At this point, you need to "escape" some special characters of the string, and the python string is escaped with \.
To represent the string Bob said "I m OK".
Because ' and ' can cause ambiguity, so we insert a \ in front of it to indicate that this is an ordinary character and does not represent the beginning of the string, so the string can be represented as
' Bob said \ ' i\ ' m ok\ '. '
Note: The escape character \ does not count toward the contents of the string.
The usual escape characters are:
\ n = line break
\ t represents a tab
\ \ means the \ character itself

Python Introductory Course chapter 3rd python variables and data types

Related Keywords:
Related Article

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

## A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

• #### Sales Support

1 on 1 presale consultation

• #### After-Sales Support

24/7 Technical Support 6 Free Tickets per Quarter Faster Response

• Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.