Variables in Python

Source: Internet
Author: User

variable Assignment: variable assignment does not require a declaration type, each variable must be assigned before it is used, and the variable will not be created until the variable is assigned.

Multiple variable assignment: a=b=c=1 a,b,c value is 1

Specify multiple variables A, b, C = 1, 2 for multiple objects, "John" A value is 1, b value is 2, and C value is "John"

data type : Python has 5 data types numbers numbers, string strings, list lists, tuple tuples, dictionary dictionary numeric data classes are immutable data types, changing digital data The type assigns a new object.

There are 4 types of numbers: int, long, float, complex (plural, real and imaginary are floating-point)

Summary : Data types are divided into digital and non-digital.

Digital type includes integral type, long integer type, floating point type, plural type;

Non-numeric types include strings, lists, tuples, and dictionaries;

Non-digital similarities: You can use slices, links (+), repetition (*), value (a[]) and other related operations;

Different points of non-digital type:

Lists can be assigned directly, tuples cannot be assigned values, and dictionaries are assigned values in the Dict[k]=v way.

Delete : You can use Del to delete a reference to an object. Grammar del Var1[,var2[,var3[....,varn]]

VAR1 = 1 The Number object is created when the specified value is #
VAR2 = 10

del var deletes a reference to a single object
Del var_a, var_b Delete references to multiple objects

string : There are two values in order from left to right index starting from 0, maximum range is string length-1

The right-to-left index starts at 1, and the maximum range is the beginning of the string

string interception : variable [head subscript: Tail subscript] subscript empty to take the head or tail

The result of str = ' Ilovepython ' s[1:5] is love. with head not including tail

Print str # output full string
Print Str[0] # The first character in the output string
Print Str[2:5] # The string between the third and fifth in the output string
Print str[2:] # Outputs a string starting from the third character
Print str * 2 # output String two times Hello world! Hello world!
Print str + "TEST" # Output concatenated string Hello world! TEST

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

List : The most common type of composite data is Python, which uses the most frequent data types and the list is identified with [].

The list can contain characters, numbers, strings, lists (that is, nested)

interception of lists: methods are the same as string interception. variable [head subscript: Tail subscript] subscript empty to take the head or tail

list = [' Runoob ', 786, 2.23, ' John ', 70.2]
Tinylist = [123, ' John ']

Print List # output complete listing [' Runoob ', 786, 2.23, ' John ', 70.2]
Print List[0] # The first element of the output list Runoob
Print List[1:3] # outputs the second to third element [786, 2.23]
Print list[2:] # Outputs all elements from the third start to the end of the list [2.23, ' John ', 70.2]
Print tinylist * 2 # Output List two times [123, ' John ', 123, ' John ']
Print List + tinylist # printing combinations [' Runoob ', 786, 2.23, ' John ', 70.2, 123, ' John ']

tuples : Tuples are similar to lists, with () identifiers, inner elements separated by commas, but tuples cannot be assigned two times, equivalent to read-only lists

the interception of tuples : Methods and strings, as well as list methods. variable [head subscript: Tail subscript] subscript empty to take the head or tail

tuple = (' Runoob ', 786, 2.23, ' John ', 70.2)
Tinytuple = (123, ' John ')

Print Tuple # output complete tuple (' Runoob ', 786, 2.23, ' John ', 70.2)
Print Tuple[0] # The first element of an output tuple Runoob
Print Tuple[1:3] # outputs the second to third element (786, 2.23)
Print tuple[2:] # Outputs all elements from the third start to the end of the list (2.23, ' John ', 70.2)
Print Tinytuple * 2 # Output tuple two times (123, ' John ', 123, ' John ')
Print tuple + tinytuple # Printing Group of tuples (' Runoob ', 786, 2.23, ' John ', 70.2, 123, ' John ')

list allows updates, tuples do not allow updates: tuple = (' Runoob ', 786, 2.23, ' John ', 70.2)
list = [' Runoob ', 786, 2.23, ' John ', 70.2]
Tuple[2] = 1000 # tuples are illegal to apply
List[2] = 1000 # is a legitimate application in the list

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

The difference is that the elements in the dictionary are accessed by keys rather than by offsets.

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

Dict = {}
dict[' One ' = "This is one"
DICT[2] = "This is the"

tinydict = {' name ': ' John ', ' Code ': 6734, ' dept ': ' Sales '} ortinydict = {' name ': ' John ', ' Code ': 6734, 2: ' Sales '} or Dict = {1: ' This ', 2: ' Is ', 3: ' Dictionary ', 4:4}

Print dict[' One ' # output key for ' One ' value this is one
Print dict[2] # output key is a value of 2 this is the
Print Tinydict # Output full dictionary {' dept ': ' Sales ', ' Code ': 6734, ' name ': ' John '}
Print Tinydict.keys () # Output all keys [' dept ', ' Code ', ' name ']
Print tinydict.values () # Output all values [' Sales ', 6734, ' John ']

Conversion of data types : Simply use the data type as a function name.

All data types in Python are classes and can be viewed by type () to see data types such as N=1 type (n)

or Isinstance to judge.such as a = 111
Isinstance (A, int)
Returns True

Difference: type () does not consider a subclass to be a parent class type.
Isinstance () considers a subclass to be a parent class type.

Class A:
... pass
...
>>> class B (A):
... pass
...
>>> Isinstance (A (), a)
True
>>> type (A ()) = = A
False
>>> isinstance (B (), A)
True
>>> type (B ()) = = A
False

Variables in Python

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.