Python variable type

The value of the variable stored in memory. This means that a space is created 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.

Variable assignment

A variable assignment in Python does not require a type declaration.

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

Each variable must be assigned before it is used. The variable will be created after the variable is assigned.

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

The right side of the equal sign (=) operator is a variable name, and the right side of the equal sign (=) operator is the value stored in the variable. For example:

Instance (Python 2.0+)

#!/usr/bin/python # -*- coding: UTF-8 -*- counter = 100 # Assigning an integer variable miles= 1000.0 # float name= "John" # string print counter print miles print name

Python String

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

Generally recorded as:


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

The string list for python has two order of values:

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

If you want to implement a substring from a string, you can use [header subscript: tail subscript] to intercept the corresponding string, which is under The mark is from 0, it can be positive or negative, and the subscript can be empty to get the head or tail.

[header subscript: tail subscript] The substring obtained contains the subscript character, but does not contain the character of the tail subscript.


>>> s = 'abcdef'
>>> s[1:5] 

When a string separated by a colon is used, python returns a new object, the result contains the contiguous content identified by the pair of offsets, and the beginning of the left contains the lower bound.

The above result contains the value b of s[1], and the maximum range taken does not include the tail subscript, which is s[5] is f.

The plus sign (+) is a string concatenation operator, and the asterisk (*) is a repeat operation. The following example:...

Instance(Python 2.0+)

#!/usr/bin/python # -*- coding: UTF-8 -*- str = 'Hello World!' print str # Output full string print str[0] # The first character in the output string print str[2:5] # The first character in the output string is the string between the third and fifth in the output string.... print str[2:] # The string between the third and fifth in the output string... print str * 2 #Output string twice print str + "TEST" # Output connected string

The above example output:

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

Python tuple


tuple is another data type, similar to a List.


tuple is identified by the () tag. Internal elements are separated by commas. However, tuples cannot be assigned twice, which is equivalent to a read-only list.

Instance (Python 2.0+)

#!/usr/bin/python # -*- coding: UTF-8 -*- tuple = ( 'welookups', 786 , < /span>2.23, 'john', 70.2 ) tinytuple = (123, 'john') print tuple # Output complete tuple print tuple[0] # Output the first element of the tuple print tuple[1:3] # Output Two to three elements print tuple[2:] # Output all elements from the third to the end of the list print tinytuple * 2 # Output tuple twice print tuple + tinytuple #Print a combination of tuples

The above example output:

('welookups', 786, 2.23, 'john', 70.2)
(786, 2.23)
(2.23, 'john', 70.2)
(123, 'john', 123, 'john')
('welookups', 786, 2.23, 'john', 70.2, 123, 'john')

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

The above example output...(Python 2.0+)

#!/usr/bin/python # -*- coding: UTF-8 -*- tuple = ( 'welookups', 786 , 2.23, 'john', 70.2 ) list = [ 'welookups', 786 , 2.23, 'john', 70.2 ] tuple[2] = 1000 # Illegal application in tuple list[2] = 1000 # The list is a legitimate application

Python Dictionary

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

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

The dictionary is identified by "{ }". The dictionary consists of an index (key) and its corresponding value value.

Instance (Python 2.0+)

#!/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 Keys Is the value of 'one' print dict[2] # The value of the output key is 2 print tinydict # Export the complete dictionary print tinydict.keys() # Output all keys print tinydict.values() #output all values

The output is:


Python Data Type Conversion

Sometimes, we need to convert the type of data built in, the conversion of the data type, you only need to use the data type as the function name.

The following built-in functions can perform conversions between data types. These functions return a new object representing the converted value.

welookups is optimized for learning.© welookups 2018 -
All Right Reserved and you agree to have read and accepted our term and condition