1.python variables and underlying data types

Source: Internet
Author: User
Tags abs pow

I. What is a variable?

In my understanding, the variable in the program is to create a space in memory, or you can think of variables as a container,

The operation nature of the program is a series of state changes, and the variables of this container is through memory to save the state of the program running, variable value changes constitute a program to run different results.

Two. What are the assignment methods of variables?

1. The most basic method of assignment, variable name = variable value, name = "Suhaozhi".

2. Chain assignment, a = b = c = "Suhaozhi" (variable a,b,c also corresponds to a variable value "Suhaozhi", the address space is identical)

3. Assign values to multiple variable names name,age = "Suhaozhi", "22" (variable name, variable name = variable value, variable value)

4. Variable Exchange b = b,a (variable name A and variable name b variable value will be exchanged)

5. Variable value self-increment n + = 1 (the value of each n is increased by 1, i.e. n = n + 1)


Three. Standard data type.

1. Numbers: numbers are divided into integers, long integers, Boolean values, floating-point numbers, complex numbers.

int integer: a = 1


Long integer: a = 1300000000000000

Print type (a)

<type ' Long ' >


BOOL Boolean value: Ture and False 1 and 0, this is nothing to say.


Float floating point number: The floating-point number is the decimal number in the math, in Python, if the sum of the floating-point numbers is equal to the floating-point number.

Print 5 + 1.5


2. Common functions related to numbers.

Int () Converts other numeric types to integers, such as int (3.14), and the fractional 3.14 is converted to 3.


Round () rounded, for example round (3.58) rounded to 4.0


Math.floor () An integer that is closest to the original decimal, but less than the original decimal, such as Math.floor (3.4) and the last 3.4 is converted to 3.0.

(Import math module required)


Math.ceil () and floor instead, the integer nearest to the original decimal, but greater than the original decimal, for example, Math.ceil (3.4) The last 3.4 is converted to 4.0.


Float () Converts a number to a float.


BOOL () Boolean function, as long as the value passed to the function is not NULL, not none, not 0, is true, otherwise it returns false.


ABS () obtains an absolute value, such as ABS (-1) and can calculate the absolute value of-1.

Coerce () can receive two parameters, convert two different types of numbers to the same type, and return a single ancestor.


Divmod () returns a ganso of the inclusion quotient and remainder, such as Divmod (101,10), which is 101 divided by 10, and the resulting result is (10,1) 10 + 1.

#divmod This function is very suitable for paging!


The POW () is used to calculate a number of n-times, such as POW (2,10), which is 2 of the 10-time square, which is equal to 1024.


Hex () converts a 10-digit number into a 16-based


Oct () Converts a 10 binary number to 8


Ord () converts characters to 10 binary numbers based on ASCII table


Chr () converts a 10 binary number to a character based on an ASCII table


3. About strings.

The single quote ' double quotation mark ', ' three quotes ' ' in the middle of these three symbols, are string types, and the strings are ordered.


About the string, in this need to add two points!

Both the single and double quotes of a string cannot suppress the meaning of special characters, and if you want to suppress the meaning of special characters, you need to precede the string with r!

If it is a Unicode string, u must precede r.

Here is an example of a special character:

print ' 45\4545 ' 45,5print R ' 45\4545 ' 45\4545

Some common methods for judging string types.

IsDigit () detects if all of a string is a number and returns False if it is true. (It is suddenly found that this method can also detect Roman numerals ....) But the number of Chinese characters is not supported)

Isdecimal () detects if a string only includes decimal characters, note Oh! is only a decimal string. (Roman numerals and Chinese are not supported)


IsNumeric () detects if a string is only composed of numbers (tested to find Unicode numbers, Roman numerals, man numbers can be detected)


4. Lists (list)

Lists are similar to arrays of other programming languages, defined in brackets [], where each element is a (,) comma delimiter, and can be indexed to hold a wide variety of data types.

A list can hold n values, each value is called an element, and each element in the list can be modified, and if you want to take an element from the list, the index (subscript) starts at 0.

For example:

A list is defined first, and two elements are defined in the list.

L1 = ["Suhaozhi", "Hamasaki"]

If you want to remove the first element, you will have to take the No. 0 index or the second element, start with the first index, and so on.

Print L1[0]

Suhaozhi

Print L1[1]

Hamasaki


What can a list do?

Find elements by index, add elements, delete elements, append elements at the tail, calculate list lengths, loops, inclusions, etc.

These are more commonly used in the above mentioned.

For the specific operation of the method, please look at the following special about the list, the Yuan Zu, the Dictionary of the article ~


5. Tuples (tuple)

And the list is particularly similar to a data type, the list of [] brackets replaced by () parentheses, it and the list of the biggest difference is that the elements inside the tuple is read-only non-modifiable!! In addition to modifications, deletions and the like, the list can be done by the tuple can also.


6. Dictionary (dict)

When a digital index is not useful, the data type of the dictionary comes in handy.

First of all, the characteristics of the dictionary, the dictionary is stored in the key-value pairs (key-value), key must be hash, in the dictionary must be unique, each key corresponding value is modifiable, the key in the dictionary is unordered.

How do I create a dictionary?

The first of these methods

D1 = {' name ': ' Suhaozhi ', ' age ': 22}

The second method of

D1 = dict (name= ' Suhaozhi ', age=22)

Third Kind

D1 = dict ({"Name": "Suhaozhi", "Age": 22})

Fourth type

D1 = dict ([' Name ', ' Suhaozhi '],[' age ', 22])

The four methods have the same effect.


Common operations for dictionaries include, index, add, delete, modify, loop, find all keys, find all value, length judgment.

For the specific operation of the method, please look at the following special about the list, the Yuan Zu, the Dictionary of the article ~


7. Set (SET)

Each element in the collection is a hash unique value in this set, and there is no order, just like the key in the dictionary, the purpose of the collection is to put different values together, and to perform relational operations with different sets, for example, to take the intersection, the difference set, and so on.

Each member of the collection is delimited by commas.

Create a Collection

S1 = {1,2,3,4}

Added, there is also a immutable set, Frozenset () function.

About the common methods of the collection behind there is a special article will introduce ~ ~ Here is not much to say ~


8.bytes byte type

When data is transmitted over the network, or when memory variables are stored on the hard disk, it needs to be converted to a byte type, which, if there is a B in front of the string, indicates that it is a byte type.


These are Python's common data types of simple introduction, a lot of personal understanding, some of the less right place to welcome correct ~ ~


This article is from the "Rebirth" blog, make sure to keep this source http://suhaozhi.blog.51cto.com/7272298/1905372

1.python variables and underlying data types

Related Article

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.