Home IT Linux Windows Database Network Programming Server Mobile  
  Home \ Programming \ Python variable type     - Use FirewallD build dynamic firewall (Linux)

- MySQL various log summary (Database)

- Oracle to start to solve the error ORA-27102 (Database)

- System with Windows Remote Desktop to connect Ubuntu 15.04 (Linux)

- Use ldap implement Windows Remote Desktop Ubuntu Linux (Linux)

- Setting up Linux machine through a proxy firewall (Linux)

- OpenGL shadow map (Programming)

- Linux Getting Started tutorial: How to backup Linux systems (Linux)

- Sublime Text 3 practical functions and shortcut keys used to collect (Linux)

- To install Cinnamon desktop environment on your Ubuntu 14.04 / 12.04 (Linux)

- The REVERSE function of DB2 (Database)

- VMware clone Linux find eth0 (Linux)

- Linux Getting Started tutorial: XWindow what (Linux)

- Linux Shell debugging (Programming)

- Improve WordPress performance (Server)

- IPTABLES configuration steps under Linux (Linux)

- Hibernate4 The Hello World (basic environmental structures) (Programming)

- Git 2.5 increase in the working tree, triangle improved workflow performance (Linux)

- Linux System Getting Started Learning: The Linux log (Linux)

- Ease of use "Explain Shell" script to understand Shell command (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
- Python programming style (Programming)
- Fedora 19/20 and Debian Jessie / Sid users to install FFmpeg 2.3.2 (Linux)
- RedHat yum configuration and the prompt (Linux)
- Four IDS intrusion detection tool under Linux environment (Linux)
- PostgreSQL use pgpool achieve high availability (Database)
- How to add any of a Linux distribution beautiful fonts (Linux)
- Java objects to garbage collection (Programming)
- [Android] Eclipse does not update the Android SDK Manager solution [using GoAgent] (Programming)
- Oracle 11g manually create a database (Database)
- U disk to install Windows and Ubuntu 15.04 dual system (Programming)
- Ubuntu amend resolv.conf restart failure problem (Linux)
- How to release the cache memory on Linux (Linux)
- Customize the 404 error page Nginx (Server)
- Oracle physical storage structure outline (Database)
- Linux Task Scheduler (at, crontab) (Linux)
- Testing Oracle 11g RMAN replicate database (Database)
- Bash Automated Customization Linux belongs to its own CentOS system (Linux)
- ActionContext and ServletActionContext Summary (Programming)
- Linux foundation tutorial: how to modify the host name on CentOS or RHEL 7 (Linux)
- C + + secondary pointer memory model (pointer array) (Programming)
  CopyRight 2002-2016 newfreesoft.com, All Rights Reserved.