Data types for Python

Source: Internet
Author: User
Tags truncated

There are six standard data types in the Python3:

Number (numeric)
String (String)
List (lists)
Tuple (tuple)
Sets (collection)
Dictionary (dictionary)

1.Number (digital)

Python3 has the following number types:

    • Integer (Int) -usually referred to as an integer or integral, is a positive or negative integer, with no decimal points. The Python3 integral type is not limited in size and can be used as a long type, so Python3 does not have a long type of Python2.
    • float (float) -floating-point type consists of integral and fractional parts, and floating-point types can also be represented using scientific notation (2.5e2 = 2.5 x 102 = 250)
    • complex numbers ((complex)) -complex numbers are composed of real and imaginary parts, and can be represented by a + BJ, or complex (a, b), where both the real and imaginary part of a complex number are floating-point types.

Python Numeric type conversions:
    • int (x) converts x to an integer.

    • float (x) converts the x to a floating-point number.

    • Complex (x) converts x to a complex number, the real part is x, and the imaginary part is divided into 0.

    • complex (x, y) converts x and y to a complex number, the real part is x, and the imaginary part is Y. X and y are numeric expressions.

Number = 235Print(Type (number))#View the type of number. ifIsinstance (number,dict):#determines whether a number is (int, float, bool, complex) type, or whether it is a (str,list,tuple,set,dict) type.     Print("is OK")Else:    Print("is no") Money= Input ("Enter Number:")ifMoney.isdigit ():#To determine if it is a number, money must be input.     Print("is OK")Else:    Print("is no")
‘‘‘
Isinstance (variable, type to be judged) #判断是否是要判断的类型
Variable. IsDigit () #判断是否为正整数

2.String (String)

The strings in Python are enclosed in single quotation marks (') or double quotation marks ("), and the special characters are escaped with a backslash (\).

The syntax for intercepting a string is as follows:

Variable [head subscript: Tail subscript]

String manipulation:

str ='Runoob'Print(str)#Output StringPrint(Str[0:-1])#outputs all characters from the first to the penultimatePrint(Str[0])#The first character of the output stringPrint(Str[2:5])#outputs the characters from the third start to the fifthPrint(str[2:])#output all characters from the beginning of the thirdPrint(STR * 2)#output String two timesPrint(str +"TEST")#Connection String

3.List (list)

A list can accomplish the data structure implementation of most collection classes. The types of elements in a list can be different, it supports numbers, and strings can even contain lists (so-called nesting).
A list is a comma-delimited list of elements written between square brackets ([]).
As with strings, lists can also be indexed and truncated, and a new list containing the required elements is returned when the list is truncated.

List = ['ABCD', 786, 2.23,'Runoob', 70.2]tinylist= [123,'Runoob'] Print(list)#Output Complete listPrint(List[0])#The first element of the output listPrint(List[1:3])#output from the second start to a third elementPrint(list[2:])#outputs all elements starting from the third elementPrint(Tinylist * 2)#output Two-time listPrint(List + tinylist)#Connection List

4.Tuple (tuple)

Tuple (tuple) is similar to a list, except that the elements of a tuple cannot be modified. Tuples are written in parentheses (()), and the elements are separated by commas.

Print (tuple)             # Output Full tuple Print (Tuple[0])          # the first element of an output tuple Print (Tuple[1:3])        # output starts from the second element to the third element Print (tuple[2:])         # outputs all elements starting from the third element Print (Tinytuple * 2)     # output two tuples Print # Connecting tuples

5.Sets (Collection)

A collection (set) is a sequence of unordered, non-repeating elements.
The basic function is to test the membership and remove duplicate elements.
You can create a collection using the curly braces {} or the set () function, note: Creating an empty collection must be set () instead of {}, because {} is used to create an empty dictionary.

Student = {' Tom ', ' Jim ', ' Mary ', ' Tom ', ' Jack ', ' Rose '}
Print (student) # output set, duplicate elements are automatically removed

# member Test
if (' Rose ' in student):
Print (' Rose in the collection ')
else:
Print (' Rose is not in the collection ')

# set can perform set operations
A = set (' Abracadabra ')
b = Set (' Alacazam ')

Print (a)
Difference set of print (A-B) # A and
The set of print (a | b) # A and B
Print (A & B) # intersection of # A and B
Print (a ^ b) # A and B do not exist in the same element

6.Dictionary (dictionary)

The Dictionary (dictionary) is another very useful built-in data type in 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.
A dictionary is a type of mapping that is identified by the dictionary with "{}", which is an unordered key (key): The value pair collection.
The key (key) must use the immutable type.
In the same dictionary, the key must be unique

Dict ={}dict[' One'] ="Www.w23.com"dict[2] ="Www.4dd.com"tinydict= {'name':'Runoob','Code': 1,'site':'www.runoob.com'}Print(dict[' One'])#the value of the output key is ' one 'Print(Dict[2])#the value of the output key is 2Print(tinydict)#output a complete dictionaryPrint(Tinydict.keys ())#Output All keysPrint(Tinydict.values ())#Output All Values

Data types for Python

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.