Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

Python variable type


May 10, 2021 Python2


Table of contents


Python variable type

The value of the variable stored in memory. This means that a space is opened up in memory when the variable is created.

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

As a result, variables can specify different data types, and these variables can store integers, numbers, or characters.


Variable assignment

Variables in Python do not need to be declared, and the assignment of variables is both a process of variable declaration and definition.

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

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

An equal sign is used to assign a value to a variable.

To the left of the equal sign operator is a variable name, and to the right of the equal sign operator is the value stored in the variable. For example:

#coding=utf-8
 #!/usr/bin/python
 
 counter = 100 # 赋值整型变量
 miles = 1000.0 # 浮点型
 name = "John" # 字符串
 
 print counter
 print miles
 print name

Try it out . . .


In the above examples, 100,1000.0 and "John" are assigned to the counter, miles, and name variables, respectively.

Executing the above procedure outputs the following results:

 100
 1000.0
 John

Multiple variable assignments

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

 a = b = c = 1

In the above example, an integer object is created with a value of 1 and three variables are allocated to the same memory space.

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

a, b, c = 1, 2, "john" 

In 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 type

There can be several types of data stored in memory.

For example, the person.s age is stored as a numeric number and his or her address is an alphanumeric character store.

Python has some standard types for defining operations on them and for each of them the storage methods possible.

Python has five standard data types:

  • Numbers (numbers)
  • String (string)
  • List (list)
  • Tuple (Tuple)
  • Dictionary (Dictionary)

Python numbers

The numeric data type is used to store numeric values.

They are imm changed data types, which means that changing digital data types assigns a new object.

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

var1 = 1
var2 = 10 

You can also use the del statement to remove some object references.

The syntax of the del statement is:

 del var1[,var2[,var3[....,varN]]]] 

You can delete a single or more objects by using the del statement. For example:

 del var
 del var_a, var_b 

Python supports four different numeric types:

  • int (symbolized integer)
  • long (long form (can also represent octals and heteens)
  • float (floating-point type)
  • complex (complex)
Instance

Examples of some numeric types:

Int long float complex
10 51924361L 0.0 3.14j
100 -0x19323L 15.20 45.j
-786 0122L -21.9 9.322e-36j
080 0xDEFABCECBDAECBFBAEl 32.3+e18 .876j
-0490 535633629843L -90. -.6545+0J
-0x260 -052318172735L -32.54e100 3e+26J
0x69 -4721885298529L 70.2-E12 4.53e-7j
  • Long forms can also use a small "L", but it is recommended that you use a capital "L" to avoid confusion with the number "1". Python uses "L" to display the long pattern.
  • Python also supports complex numbers, which consist of real and imaginary parts, which can be represented by a plus bj, or bycomplex (a,b), and that both real and imaginary part b of complex numbers are floating-point patterns

Python string

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

It is generally noted as:

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

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

Python's string list has 2 value orders:

  • Starting with the left-to-right index default 0, the maximum range is 1 less string length
  • Starting with the right-to-left index default -1, the maximum range is the beginning of the string

If you really want to get a sub-string, you can use the variable (head sub-label: tail sub-label), you can intercept the corresponding string, where the sub-label is from 0 onwards, can be positive or negative, the sub-label can be empty to the beginning or end.

Like what:

 s = 'ilovepython' 

The result of the s.1:5 is love.

When using a string separated by a colon, Python returns a new object, and the result contains a continuous content identified by the offset pair, with the beginning on the left containing the lower boundary.

The result above contains the value l of s[1, and the maximum range taken does not include the upper boundary, which is the value p of s[5.

A plus sign is a string connection operator, and an asterisk is a repeat. Here's an example:

 #coding=utf-8
 #!/usr/bin/python
 
 str = 'Hello World!'
 
 print str # 输出完整字符串
 print str[0] # 输出字符串中的第一个字符
 print str[2:5] # 输出字符串中第三个至第五个之间的字符串
 print str[2:] # 输出从第三个字符开始的字符串
 print str * 2 # 输出字符串两次
 print str + "TEST" # 输出连接的字符串 

The above example output results:

 Hello World!
 H
 llo
 llo World!
 Hello World!Hello World!
 Hello World!TEST

Python list

List is the most frequently used data type in Python.

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

The list is identified by . i s Python's most common composite data type. Look at this code and you'll see.

The worthy split in the list can also be used with the variable (header: tail underseal), you can intercept the corresponding list, from left to right index default 0, from right to left index default -1, the undersail can be empty to take to the end.

A plus sign is a list connection operator, and an asterisk is a repeat. Here's an example:

 #coding=utf-8
 #!/usr/bin/python
 
 list = [ 'abcd', 786 , 2.23, 'john', 70.2 ]
 tinylist = [123, 'john']
 
 print list # 输出完整列表
 print list[0] # 输出列表的第一个元素
 print list[1:3] # 输出第二个至第三个的元素 
 print list[2:] # 输出从第三个开始至列表末尾的所有元素
 print tinylist * 2 # 输出列表两次
 print list + tinylist # 打印组合的列表

The above example output results:

 ['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 metagroup

A group is another data type, similar to a List.

The group is identified by (). T he inner elements are separated by commas. However, elements cannot be assigned a secondary value, equivalent to a read-only list.

 #coding=utf-8
 #!/usr/bin/python
 
 tuple = ( 'abcd', 786 , 2.23, 'john', 70.2 )
 tinytuple = (123, 'john')
 
 print tuple # 输出完整元组
 print tuple[0] # 输出元组的第一个元素
 print tuple[1:3] # 输出第二个至第三个的元素 
 print tuple[2:] # 输出从第三个开始至列表末尾的所有元素
 print tinytuple * 2 # 输出元组两次
 print tuple + tinytuple # 打印组合的元组

The above example output results:

 ('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 not valid for the eddies, because the yuans are not allowed to be updated. 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    # 元组中是非法应用
list[2] = 1000     # 列表中是合法应用

Python dictionary

Dictionary is the most flexible built-in data structure type in Python other than lists. A list is an ordered collection of objects, and a dictionary is a collection of out-of-order objects.

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

The dictionary is identified by the word "" A dictionary consists of an index (key) and its corresponding value value.

 #coding=utf-8
 #!/usr/bin/python
 
 dict = {}
 dict['one'] = "This is one"
 dict[2] = "This is two"
 
 tinydict = {'name': 'john','code':6734, 'dept': 'sales'}
 
 
 print dict['one'] # 输出键为'one' 的值
 print dict[2] # 输出键为 2 的值
 print tinydict # 输出完整的字典
 print tinydict.keys() # 输出所有键
 print tinydict.values() # 输出所有值

The output is:

This is one
This is two
{'dept': 'sales', 'code': 6734, 'name': 'john'}
['dept', 'code', 'name']
['sales', 6734, 'john']

Python data type conversion

Sometimes we need to convert the types built into the data, the data types, you just need to use the data types as function names.

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

Function Describe

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

complex(real [,imag])

Create a complex number

str(x)

Convert object x to a string

repr(x)

Converts object x to an expression string

eval(str)

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

tuple(s)

Convert sequence s to a binary

list(s)

Convert sequence s to a list

set(s)

Convert to a variable collection

dict(d)

Create a dictionary. d Must be a sequence (key, value) group.

frozenset(s)

Convert to an immedible collection

chr(x)

Convert an integer to a character

unichr(x)

Convert an integer to a Unicode character

ord(x)

Converts a character to its integer value

hex(x)

Converts an integer to a hen string

oct(x)

Convert an integer to an octal string