(eight) simple data types and variables for Python

Source: Internet
Author: User

Data type

To run a program, it is necessary to describe its algorithm first. Describing an algorithm should first describe the data to be used in the algorithm, the data is described in the form of variables or constants. Each variable or constant has a data type. There are 5 basic data types for python: integer (int), floating-point (float), character (String), Boolean (bool), null (None).

Integer

Python can handle integers of any size, and the representation in the program is exactly the same as the mathematical notation.

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 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 (We'll elaborate on the built-in approach to strings in the following chapter)

strings are arbitrary text enclosed in "or", for example ‘abc‘ , and "xyz" so on. Note that the "or" "itself is only a representation, not part of a string, so the string ‘abc‘ is a only b , c this 3 characters. If it is also a character, it can be enclosed in "", for example, the "I‘m OK" containing character is I ,,, a space, which is 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:

The string content represented is:

The escape character \ can escape a number of characters, such as a \n newline, a \t tab, and the character \ itself, so \\ the character that is represented is the \ ability to print a string in Python's interactive command line to see:

If there are a lot of characters in the string that need to be escaped, you need to add a lot \ , in order to simplify, Python also allows to use r‘‘ ‘‘ A string that represents the inside of the default is not escaped, you can try it yourself:

If there is a lot of line wrapping inside the string, \n writing in a row is not good to read, in order to simplify, Python allows ‘‘‘...‘‘‘ a format to represent multiple lines of content, you can try it yourself:

The above is entered in the interactive command line, if written as a program, that is:

Multi-line strings ‘‘‘...‘‘‘ can also be used in front of each other r , please test yourself.

Boolean valueA Boolean value is exactly the same as a Boolean algebra, with a Boolean value of only True False two values, either, True or, False in Python, you can directly use True and False represent a Boolean value (note case). It can also be computed by Boolean operations:

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 :

Boolean values are often used in conditional judgments, such as:

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, which we'll continue to talk about later. < citation 2>

Variables and constants

In a computer, a variable is used to store the various identifiers that need to be temporarily saved for data that can be changed while the program is running, a variable should have a name, and a certain storage unit in memory that holds the value of the variable in that storage unit.

Variable naming rules

The concept of identifiers is introduced first. As with other high-level languages, valid character sequences used to identify entity names such as variables, symbolic constants, functions, arrays, types, and so on are called Identifiers (identifier). Simply put, an identifier is a name. The variable name is one of the identifiers, and the name of the variable must follow the naming rules of the identifier.

Like many languages, such as the Python language and java,c++, the specified identifier can only consist of 3 characters of letters, numbers, and underscores, and the first character must be a letter or an underscore . The following list is a valid identifier and is a valid variable name:
Sum, average, total, day, month, Student_name, Tan, BASIC, li_ling

The following is an illegal identifier and variable name:
M.d.john, $442, #4, 3g64, Alex Li, C + +, zhang-ling, name, U.S.A.

Note: in Python, uppercase and lowercase letters are considered to be two different characters. As a result, sum and sum are two different variable names. In general, variable names are expressed in lowercase letters, consistent with people's daily habits to increase readability . It should be noted that the variable name cannot be the same as the Python keyword, system function name, and class name.

Variable name naming habits

When you write more and more, you will find that you define the variables will be more and more, in order to increase the readability of the code and convenient debugging, to the variable name must follow certain naming habits, you have the variable names should be able to let people at a glance about what the variable is for, for example, GetUserName know that this variable should be to get the user's name, Check_current_conn_count is to check the current number of connections, only in this way, others can see your code when you know the role of these variables, And if you put the variable name into a var1,var2,var3.....varn, someone else will scold you when they look at your code.

The definition of variable name in the premise can express its role is the most concise the better, can use a word clearly expressed as far as possible do not use two. Variables are usually named when there are so many ways of writing, you think which is the most concise, you choose which kind of it.

Checkcurrentconncount

Check_current_conn_count

Checkcurrentconncount

Bad naming:

Checkcurrentconncount

Var1 var2 Var3 Varn

Checkcurrentconncount

Defining variables

After understanding the concept and use of variables, let's define a few simple variables to see

name = ' Alex Li ' #name is a string, enclose the string in quotation marks Oh

Age = #age is an integer, integers do not enclose quotation marks, and then they become strings.

has_girlfriend = False # is a Boolean value that is generally used to make logical judgments, such as if Has_girlfriend:print ' good for you! '

Age = age + 1 # The result should be - , the arithmetic flow is the first The age +1 After the = number is calculated, and then the result is re-assigned to Age , because Age the previous value is in , after the value is re-assigned, Age value becomes .

Finally, it is important to understand the representation of variables in computer memory . When we write:

name = ' Alex '

, the Python interpreter did two things:

    1. Create a "Alex" string in memory;
    2. A variable named name was created in memory and pointed to the memory address of "Alex".

You can also assign a variable name to another variable name2, which is actually the data that points to the variable name2, for example:

Alas? Not already the name2 equals the name variable? After changing the name value, does the name2 not change? Yes, when the value of name is changed from "Alex" to "Jack", Name2 still points to the original "Alex", let's take a step-by-step analysis:

1. Define Name= "Alex", the interpreter creates the string "Alex" and the variable name, and points the name to "Alex"

2. Executing Name2=name, the interpreter creates the name2 variable and points name2 to the string that the name variable points to

3. At this point the ID built-in function to look at the two variables pointed to the memory address, the result is pointing to the same address.

4. Execute Name= "Jack", the interpreter creates a new variable "Jack" and changes the name point to "Jack".

5. Then look at the memory address of the two variables to point to the point that the name has become a new address, the "Jack" where the memory address, but Name2 still point to the original "Alex."

Now, do you understand? To sum up, when you assign a variable name to another variable name2, the interpreter simply assigns the memory address pointed to by the name variable to name2, so that name and name2 do not have a direct association . It's just that they both point to the same memory address, which is why you point the name back to a new address, and the name2 value remains the same.

Constant

As for the variables, there is also a concept is 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 :

PI = 3.14159265359 But in fact pi is still a variable, Python does not have any mechanism to ensure that PI will not be changed, so, using all uppercase variable names to represent constants is only a customary usage, and if you must change the value of the variable pi, no one can stop you.

(eight) simple data types and variables for Python

Contact Us

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.