Python variable type

Source: Internet
Author: User
Tags python list

PythonVariable Type

The value that the variable is stored in memory. This means that there is a space in memory when creating variables.

Based on the data type of the variable, the interpreter allocates the specified memory and determines what data can be stored in memory.

Therefore, variables can specify different data types, which can store integers, decimals, or characters.

Assigning values to variables

Variables in Python do not need to be declared, and the assignment of variables is the process of declaring and defining variables.

Each variable is created in memory and includes information about the identity, name, and data of the variable.

Each variable must be assigned before it is used, and the variable will not be created until the variable is assigned.

The equals sign (=) is used to assign a value to a variable.

The left side of the equals sign (=) operator is a variable name, and the right side of the equals sign (=) operator is the value stored in the variable.

Assigning values to multiple variables

Python allows you to assign values to multiple variables at the same time. For example:

A = b = c = 1

The above example creates an integer object with a value of 1 and three variables allocated to the same memory space.

You can also specify multiple variables for multiple objects. For example:

" John "

For the above example, two integer objects 1 and 2 are assigned to variables A and B, and the string object "John" is assigned to variable C.

Standard data types
Numbers (numeric) string (string) list (list) Tuple (tuple) Dictionary (dictionary)
Python numbers

Numeric data types are used to store numeric values.

They are immutable data types, which means that changing the numeric data type assigns a new object.

When you specify a value, the number object is created:

var1 = 1= 10

You can also use the DEL statement to delete some object references.

The syntax for the DEL statement is:

del Var1[,var2[,var3[....,varn] []]

You can delete single or multiple objects by using the DEL statement. For example:

del var del var_a, Var_b

Python supports four different numeric types:

int (signed integer) long (Long integer [also can represent octal and hex]) float (floating point) complex (plural)
Python string

A string or series (string) is a string of characters consisting of numbers, letters, and underscores.

Generally recorded as:

s="a1a2 an"(n>=0)

It is the data type that represents the text in the programming language.

The Python string list has 2 order of values:

    • Left-to-right index starts at default 0, with a maximum range of 1 less string lengths
    • Right-to-left index starts with default-1, the maximum range is the beginning of the string

If you want to get a substring, you can use the variable [head subscript: Tail subscript], you can intercept the corresponding string, where the subscript is starting from 0, can be positive or negative, subscript can be null to take the head or tail.

Like what:

' Ilovepython '

S[1:5] The result is love.

When using a colon-delimited string, Python returns a new object that contains the contiguous content identified with the offset, and the beginning of the left contains the bottom bounds.

The result above contains the value L of s[1], and the maximum range taken does not include the upper boundary, or the value p of s[5].

The plus sign (+) is a string join operator, and an asterisk (*) is a repeating operation. The following example:

#!/usr/bin/python#-*-coding:utf-8-*-Str='Hello world!'PrintStr#Output Full StringPrintSTR[0]#the first character in the output stringPrintStr[2:5]#A string between the third and fifth in the output stringPrintStr[2:]#outputs a string starting from the third characterPrintSTR * 2#output String two timesPrintSTR +"TEST" #string for output connection

The result of the above example output:

Hello world!
H
Llo
Llo world!
Hello world! Hello world!
Hello world! The TEST Python list (list) is the most frequently used data type in Python.

A list can accomplish the data structure implementation of most collection classes. It supports characters, numbers, and even strings that can contain lists (so-called nesting).

The list is identified by []. Is the most common composite data type of Python. See this code to understand.

The list of values can also be used to split the variable [head subscript: Tail subscript], you can intercept the corresponding list, from left to right index default 0, starting from right to left index default-1, subscript can be empty to take the head or tail.

The plus sign (+) is the list join operator, and the asterisk (*) is a repeating operation. The following example:

#!/usr/bin/python#-*-coding:utf-8-*-List= ['ABCD', 786, 2.23,'John', 70.2]tinylist= [123,'John']PrintList#Output Complete listPrintLIST[0]#the first element of the output listPrintList[1:3]#outputs the second to third elementPrintList[2:]#outputs all elements from the third start to the end of the listPrintTinylist * 2#output List two timesPrintList + tinylist#print a list of combinations

The result of the above example output:

[' ABCD ', 786, 2.23, ' John ', 70.2]
Abcd
[786, 2.23]
[2.23, ' John ', 70.2]
[123, ' John ', 123, ' John ']
[' ABCD ', 786, 2.23, ' John ', 70.2, 123, ' John '] Python tuples

A tuple is another data type, similar to a list.

The tuple is identified with a "()". The inner elements are separated by commas. However, an element cannot be assigned two times, which is equivalent to a read-only list.

#!/usr/bin/python#-*-coding:utf-8-*-tuple= ('ABCD', 786, 2.23,'John', 70.2) Tinytuple= (123,'John')PrintTuple#Output Full tuplePrintTUPLE[0]#the first element of an output tuplePrintTuple[1:3]#outputs the second to third elementPrintTuple[2:]#outputs all elements from the third start to the end of the listPrintTinytuple * 2#output tuple two timesPrintTuple + tinytuple#Print a group of tuples

The result of the above example output:

(' ABCD ', 786, 2.23, ' John ', 70.2)
Abcd
(786, 2.23)
(2.23, ' John ', 70.2)
(123, ' John ', 123, ' John ')
(' ABCD ', 786, 2.23, ' John ', 70.2, 123, ' John ')

The following are tuples that are not valid because tuples are not allowed to be updated. And the list is allowed to be updated:

#!/usr/bin/python#-*-coding:utf-8-*-tuple= ('ABCD', 786, 2.23,'John', 70.2) List= ['ABCD', 786, 2.23,'John', 70.2]tuple[2] = 1000#illegal application in tuplesLIST[2] = 1000#is a legitimate application in the list

The Dictionary (dictionary) is the most flexible built-in data structure type in Python, except for lists. A list is an ordered combination of objects, and a dictionary is a collection of unordered objects.

The difference between the two is that the elements in the dictionary are accessed by keys, not by offsets.

The dictionary is identified with "{}". A dictionary consists of an index (key) and a value corresponding to it.

#!/usr/bin/python#-*-coding:utf-8-*-Dict={}dict[' One'] =" This is one"dict[2] =" This is the"tinydict= {'name':'John','Code': 6734,'Dept':'Sales'}Printdict[' One']#the value of the output key is ' one 'PrintDICT[2]#the value of the output key is 2PrintTinydict#output a complete dictionaryPrintTinydict.keys ()#Output All keysPrintTinydict.values ()#Output All Values

The output is:

This is one of the ' is ' and ' dept ': ' Sales ', ' Code ': ' 6734, ' name ': ' John '} [' dept ', ' Code ', ' name '] [' Sales ', 6734, ' John '] P Ython Data type Conversions

Sometimes, we need to convert the data-built type into the data type, and you just need to use the data type as the function name.

The following several built-in functions can perform conversions between data types. These functions return a new object that represents the value of the transformation.

function Description

int (x [, Base])

Convert x to an integer

Long (x [, Base])

Convert x to a long integer

Float (x)

Convert x to a floating-point number

Complex (real [, Imag])

Create a complex number

STR (x)

Convert an object x to a string

REPR (x)

Convert an object x to an expression string

eval (str)

Used to evaluate a valid Python expression in a string and return an object

Tuple (s)

Converting a sequence s to a tuple

List (s)

Convert the sequence s to a list

Set (s)

Convert to mutable Collection

Dict (d)

Create a dictionary. D must be a sequence (key,value) tuple.

Frozenset (s)

Convert to immutable Collection

Chr (x)

Converts an integer to one character

UNICHR (x)

Converts an integer to a Unicode character

Ord (x)

Converts a character to its integer value

Hex (x)

Converts an integer to a hexadecimal string

Oct (x)

Converts an integer to an octal string

Python variable type

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.