Marco-python-class notes 12-python Core data type and type display conversion

Source: Internet
Author: User

Python's core data type (built-in)
Number: Int,long,float,complex (plural), bool (Boolean)
Character: Str,unicode
Listing: List
Dictionary: Dict
Tuples: Tuple
Files: File
Other types: Collection (set), Frozenset (frozen set), class type, None
Other file class Tools: Pipes (Pipeline), FIFOs (FIFO pipeline), sockets (socket)


Type conversions:
Built-in function to complete non-string conversion to character type
STR (): Similar to print output
Repr (): Indicates the exact value
Format (): Converting in a specific format
Digital conversions:
Int (): Convert to Integer
Float (): Convert to floating point
Convert to Integer type:
str2 = ' 45 '
Type (STR2)
Str
Transformation:
num3 = Int (str2)
Type (STR2)
Int
Print num3
45
List conversions
List (s): Convert string S to list
STR2 = "Hello World"
num3 = List (str2)
Print num3
Output:
[' H ', ' e ', ' l ', ' l ', ' o ', ' ', ' W ', ' o ', ' r ', ' L ', ' d ']
Tuple conversions
Tuple (s): Converts a string s to a tuple
STR2 = "Hello World"
NUM4 = tuple (str2)
Print Num4
Output:
(' H ', ' e ', ' l ', ' l ', ' o ', ' ', ' W ', ' o ', ' r ', ' L ', ' d ')
Collection conversions
Set (s): Converts the string s to a collection
STR2 = "Hello World"
NUM5 = Set (STR2)
Print NUM5
Output:
Set ([' ', ' e ', ' d ', ' H ', ' l ', ' o ', ' r ', ' W '])
Converting to a collection will place the same letters in the same order, and there are no specific sequences;
Dictionary conversions
Dict (d): Creating a Dictionary
D must be a tuple sequence of key-value pairs
Creates a dictionary based on the specified key value pair;
D1 = [(' A ', 1), (' B ', 2)]
NUM1 = dict (D1)
Print NUM1
Output:
{' A ': 1, ' B ': 2}

Marco-python-class notes 12-python Core data type and type display conversion

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.