Home IT Linux Windows Database Network Programming Server Mobile  
  Home \ Programming \ Python variable type     - phpinfo (): like phpinfo as a Python script (Programming)

- Linux alpine use the command line to access Gmail (Linux)

- Linux user management (Linux)

- Ubuntu Linux use MAC binding against ARP attacks (Linux)

- Redis logging system (Database)

- Ubuntu 14.10 / 14.04 / 12.04 virtual users to install the printing software Boomaga (Linux)

- Java proxy mode (Programming)

- DRBD switchover (Server)

- Valgrind * not * leak check tool (Linux)

- Install and use automated tools Ansible in CentOS 7 (Linux)

- Use NTFS-3G to mount NTFS partitions under Linux U disk and removable hard disk (Linux)

- Linux modify environment variables method (Linux)

- Oracle Character Set Summary (Database)

- TNS-03505 name could not be resolved (Database)

- NFS installation process under the CentOS (Linux)

- Nodejs command-line program development tutorial (Programming)

- Security: Unix operating system intrusion tracking Strikes Back (Linux)

- Objective-C basic program structure (Programming)

- Install Python 3.3.4 under CentOS 6.4 (Linux)

- Multi-core CPU, multi-threading and parallel computation (Linux)

  Python variable type
  Add Date : 2018-11-21      
  Variable values stored in memory. This means that will open up a space in the memory when you create a variable.

Based on the data type of the variable, the interpreter will allocate the specified memory, and decide what data can be stored in memory.

Thus, the variables can specify different data types, these variables can store integer, decimal, or character.

Variable assignment

Python variables need not be declared variable assignment both declared and defined process variable.

Each variable is created in memory, including all logos, names and data of variable information.

Each variable must be assigned before use, variable assignment after the variable will be created.

Equal sign (=) is used to assign values to variables.

Equal sign (=) operator on the left is a variable name, an equal sign (=) operator on the right is the value stored in the variable. E.g:

#! / Usr / bin / python
# - * - Coding: UTF-8 - * -

counter = 100 # integer variable assignment
Float miles = 1000.0 #
name = "John" # string

print counter
print miles
print name
The above example, 100,1000.0 and "John" are assigned to the counter, miles, name variable.

The above program will output the following results:

A plurality of variable assignment

Python allows you to assign multiple variables simultaneously. E.g:

a = b = c = 1
The above examples, create an Integer object, and a value of 1, three variables are assigned to the same memory space.

You can also specify multiple variables into multiple objects. E.g:

a, b, c = 1, 2, "john"
The above examples, two integer objects 1 and 2 is assigned to the variable a and b, string object "john" is assigned to the variable c.

Standard data types

Stored data can be a variety of types in memory.

For example, person.s age as a value stored in his or her address is stored in alphanumeric characters.

Python has some standard used to define the type of operation, and they may be for each of them the storage method.

Python has five standard data types:

Numbers (Digital)
String (String)
List (list)
Tuple (tuple)
Dictionary (dictionary)
Python figures

Numeric data type is used to store values.

They are immutable data types, which means changing the numeric data type will be assigned a new object.

When you specify a value, Number object is created:

var1 = 1
var2 = 10
You can also use the del statement to delete some object references.

del statement syntax is:

del var1 [, var2 [, var3 [...., varN]]]]
You can delete single or multiple objects by using the del statement. E.g:

del var
del var_a, var_b
Python supports four different types of values:

int (a signed integer)
long (long integers [can also represent octal and hexadecimal])
float (float)
complex (complex)

Some examples of 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
You can also use a long integer lowercase "L", but it is recommended that you use an uppercase "L", to avoid the number "1" confusion. Python uses "L" to display the long integer.
Python also supports complex numbers, complex numbers by the real and imaginary parts, you can use a + bj, or complex (a, b) that the real part and an imaginary part b is a floating-point
Python strings

String or strings (String) is a string of characters consists of numbers, letters, underscores thereof.

Usually referred to as:

s = "a1a2 ... an" (n> = 0)
It is a programming language indicates the data type of text.

Python string list are two kinds of values in order:

From left to right index defaults to zero, the maximum range is less string length 1
From right to left starting at -1 index default, the maximum range is the beginning of a string
If you want to get some real substring, you can use the variables [under the header: Last subscript], you can intercept the corresponding string, where the subscript 0 is measured from the beginning, which can be positive or negative, under mark can be empty means to take the head or tail.

such as:

s = 'ilovepython'
s [1: 5] is the result of love.

When using a colon-separated strings, python returns a new object that contains the results in order to offset the contents of this continuous identification, beginning on the left is included under the border.

The above results include the s [1] the value of l, the maximum range is not taken to include the border is s [5] the value of p.

Plus sign (+) is the string concatenation operator, the asterisk (*) is repeated operations. The following examples:

#! / Usr / bin / python
# - * - Coding: UTF-8 - * -

str = 'Hello World!'

print str # Output the full string
print str [0] # output string of the first character
# String output string third to fifth between: print str [5 2]
print str [2:] # output from the beginning of the third character string
print str * 2 # twice the output string
print str + "TEST" # output connection string
Examples of the above output:

Hello World!
llo World!
Hello World! Hello World!
Hello World! TEST
Python list

List (list) is the most frequently used Python data types.

You can complete the list data structure to achieve most of the collection class. It supports characters, numbers, strings and even contains a list (called nesting).

List with a [] logo. Python is the most common type of complex data. See the code to understand.

List segmentation deserve to be used in variable [under the header: Last subscript], you can intercept the appropriate list, from left to right by default index of 0, default index from right to left starting at -1, the subscript may blank means to take the head or tail.

Plus sign (+) is a list of the concatenation operator, the asterisk (*) is repeated operations. The following examples:

#! / Usr / bin / python
# - * - Coding: UTF-8 - * -

list = [ 'abcd', 786, 2.23, 'john', 70.2]
tinylist = [123, 'john']

a complete list of output print list #
print list [0] # the first element of the list output
print list [1: 3] # output of the second to the third element
print list [2:] # output all elements from the beginning to the end of the third list
print tinylist * 2 # output list twice
print list + tinylist list # print portfolio
Examples of the above output:

[ 'Abcd', 786, 2.23, 'john', 70.2]
[786, 2.23]
[2.23, 'john', 70.2]
[123, 'john', 123, 'john']
[ 'Abcd', 786, 2.23, 'john', 70.2, 123, 'john']
Python tuple

Tuple is another data type, similar to the List (list).

Tuple with "()" logo. Internal elements separated by commas. But the second element can not be assigned, the equivalent of read-only list.

#! / Usr / bin / python
# - * - Coding: UTF-8 - * -

tuple = ( 'abcd', 786, 2.23, 'john', 70.2)
tinytuple = (123, 'john')

print tuple # output complete tuple
print tuple [0] # the first element of the output tuple
print tuple [1: 3] # output of the second to the third element
print tuple [2:] # output all elements from the beginning to the end of the third list
print tinytuple * 2 # output tuple twice
print tuple + tinytuple # tuple combination of print
Examples of the above output:

( 'Abcd', 786, 2.23, 'john', 70.2)
(786, 2.23)
(2.23, 'john', 70.2)
(123, 'john', 123, 'john')
( 'Abcd', 786, 2.23, 'john', 70.2, 123, 'john')
The following is a tuple invalid because tuples are not allowed to update. The list is updated to allow:

#! / 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 # tuple is illegal Application
list [2] = 1000 # list is legitimate applications
Python element dictionary

Dictionary (dictionary) is in addition to python than among the list of the most flexible type of built-in data structures. The list is ordered binding objects, dictionaries are unordered collections of objects.

The difference between the two is: among the elements of the dictionary is accessed by a key, rather than through an offset access.

Dictionary use "{}" logo. Dictionary by the index (key) and its corresponding value value components.

#! / Usr / bin / python
# - * - Coding: UTF-8 - * -

dict = {}
dict [ 'one'] = "This is one"
dict [2] = "This is two"

tinydict = { 'name': 'john', 'code': 6734, 'dept': 'sales'}

print dict [ 'one'] # output key is 'one' value
print dict [2] # key output value of 2
print tinydict # full output dictionary
print tinydict.keys () # Output all keys
print tinydict.values () # output all 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 built-in data type conversion, data type conversion, you only need to type the data as a function name.

Several built-in functions can perform conversions between data types. These functions return a new object that represents the converted value.

Function Description
int (x [, base])

Converts x to an integer

long (x [, base])

Converts x to a long integer

float (x)

The transition to a floating-point number x

complex (real [, imag])

Create a complex

str (x)

The object is converted to a string x

repr (x)

The object x is converted to a string expression

eval (str)

It used to calculate string valid Python expression and returns an object

tuple (s)

The sequence s into a tuple

list (s)

The sequence s is converted to a list

set (s)

Converted to variable set

dict (d)

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

frozenset (s)

Converted to immutable collection

chr (x)

Will convert an integer to a character

unichr (x)

An integer is converted to Unicode characters

ord (x)

Convert a character to its integer value

hex (x)

An integer is converted to a hexadecimal string

oct (x)

An integer is converted to an octal string

1, int function:

(1) comply with the mathematical format converting numeric strings to integers
    (2) to convert floating point to integer, but simply rounded, not rounded.

aa = int ( "124") #Correct
print "aa =", aa # result = 124
bb = int (123.45) #correct
print "bb =", bb # result = 123
cc = int ( "- 123.45") # Error, Can not Convert to int
print "cc =", cc
dd = int ( "34a") # Error, Can not Convert to int
print "dd =", dd
ee = int ( "12.3") # Error, Can not Convert to int
print ee
2, float function converts integers and strings to floating-point numbers.

aa = float ( "124") #Correct
print "aa =", aa #result = 124.0
bb = float ( "123.45") #Correct
print "bb =", bb #result = 123.45
cc = float (-123.6) #Correct
print "cc =", cc #result = -123.6
dd = float ( "- 123.34") #Correct
print "dd =", dd #result = -123.34
ee = float ( '123v') # Error, Can not Convert to float
print ee
3, str function to convert a number into a character

aa = str (123.4) #Correct
print aa #result = '123.4'
bb = str (-124.a) #SyntaxError: invalid syntax
print bb
cc = str ( "- 123.45") #correct
print cc #result = '-123.45'
dd = str ( 'ddd') #correct
print dd #result = ddd
ee = str (-124.3) #correct
print ee #result = -124.3
- AIX system file security aspects (Linux)
- To solve the Mac in question invalid BASH under configuration environment variable (Linux)
- Debian GNU / Linux service list acquisition, shutting down services or run (Linux)
- Quagga use authentication on strengthening BGP session security (Linux)
- Docker Build a Java compiler environment (Linux)
- Tsunami-udp accelerated network transmission (Linux)
- Ubuntu 12.04 installation instructions under GAMIT10.40 (Linux)
- Ubuntu 14.04 / 13.10 users how to install Ubuntu Touch core applications (Linux)
- LAMP and LNMP automated installation scripts (Server)
- To delete the directory and all specified files under the Mac (Linux)
- Java NIO The Glob mode Detailed (Programming)
- MySQL binlog automatic cleanup script (Database)
- VMware virtual machine operating system log Error in the RPC receive loop resolve (Linux)
- Shell scripts quickly deploy Tomcat project (Server)
- Strategy Games Ubuntu installation of Wesnoth 1.12.1 (Linux)
- Why do I prefer Git (Linux)
- Some problems and countermeasures Linux system calls exist (Linux)
- Use Observium to monitor your network and servers (Server)
- CentOS7 Kubernetes used on container management (Server)
- Linux upgrade GCC 4.8.1 clear and concise tutorials (Ubuntu 12.04 64-bit version as an example) (Linux)
  CopyRight 2002-2016 newfreesoft.com, All Rights Reserved.