Python Learning Summary 7: Variable type

Source: Internet
Author: User
Tags python list

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

assigning values to variables

Variables in Python do not need to be declared, and the assignment of variables is the process of declaring and defining variables.

Counter =    #  Assignment integer variable miles = 1000.0   #  floating-point "John  "    #  string

assigning values to multiple variables

Python allows you to assign values to multiple variables at the same time, such as:

A = b = c = 1   #  creates an integer object with a value of 1 and three variables allocated to the same memory space. 
Standard data Types

There are several types of data that can be stored in memory.

Python has five standard data types:

    • Numbers (digital)
    • String (String)
    • List (lists)
    • Tuple (tuple)
    • Dictionary (dictionary)
python numbers

Numeric data types are used to store numeric values. They are immutable data types, which means that changing the numeric data type assigns a new object.

When you specify a value, the number object is created.

var1 = 1= 10
python string

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

The Python string list has 2 order of values:

    • Left-to-right index starts at default 0 , with a maximum range of 1 less string lengths
    • Right-to-left index starts with default -1 , the maximum range is the beginning of the string

If you want to get a substring, you can use the variable [head subscript: Tail subscript], you can intercept the corresponding string, where the subscript is starting from 0, can be positive or negative, subscript can be null to take the head or tail.

str ='Hello world!'PrintStr#Output Full StringPrintSTR[0]#the first character in the output stringPrintStr[2:5]#A string between the third and fifth in the output stringPrintStr[2:]#outputs a string starting from the third characterPrintSTR * 2#output String two timesPrintSTR +"TEST"     #string for output connection

The above instance output:

Hello world! Hllollo world! Hello world! Hello world! Hello world! TEST
python list

The list of worthy partitions can also be used to variable [head subscript: Tail subscript], you can intercept the corresponding list

Left-to-right index starts with default 0, right-to-left index default-1, subscript can be empty to take the head or tail

List = ['ABCD', 786, 2.23,'John', 70.2]tinylist= [123,'John']PrintList#Output Complete listPrintLIST[0]#the first element of the output listPrintList[1:3]#outputs the second to third elementPrintList[2:]#outputs all elements from the third start to the end of the listPrintTinylist * 2#output List two timesPrintList + tinylist#print a list of combinations

The above instance output:

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

The tuple is identified with a "()". The inner elements are separated by commas. However, an element cannot be assigned two times, which is equivalent to a read-only list.

Tuple = ('ABCD', 786, 2.23,'John', 70.2) Tinytuple= (123,'John')PrintTuple#Output Full tuplePrintTUPLE[0]#the first element of an output tuplePrintTuple[1:3]#outputs the second to third elementPrintTuple[2:]#outputs all elements from the third start to the end of the listPrintTinytuple * 2#output tuple two timesPrintTuple + tinytuple#Print a group of tuples

The above instance output:

('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 meta dictionary

The Dictionary (dictionary) is the most flexible built-in data structure type in addition to the list of unexpected python. A list is an ordered combination of objects, and a dictionary is a collection of unordered objects.

The difference between the two is that the elements in the dictionary are accessed by keys, not 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'}Printdict[' One']#the value of the output key is ' one 'PrintDICT[2]#the value of the output key is 2PrintTinydict#output a complete dictionaryPrintTinydict.keys ()#Output All keysPrintTinydict.values ()#Output All Values

The above instance output:

This isOnethis istwo{'Dept':'Sales','Code': 6734,'name':'John'}['Dept','Code','name']['Sales', 6734,'John']
python data type conversions

Sometimes, we need to convert the data-built type into the data type, and you just need to use the data type as the function name.

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

function Description

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 number

Complex (real [, Imag])

Create a complex number

STR (x)

Convert an object x to a string

REPR (x)

Convert an object x to an expression string

eval (str)

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

Tuple (s)

Converting a sequence s to a tuple

List (s)

Convert the sequence s to a list

Set (s)

Convert to mutable Collection

Dict (d)

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

Frozenset (s)

Convert to immutable Collection

Chr (x)

Converts an integer to one character

UNICHR (x)

Converts an integer to a Unicode character

Ord (x)

Converts a character to its integer value

Hex (x)

Converts an integer to a hexadecimal string

Oct (x)

Converts an integer to an octal string

Python Learning Summary 7: Variable type

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.