Data type conversions in Python

Source: Internet
Author: User
Tags ord set set

1, List turn str

Suppose you have a list named Test_list, and the converted Str is named TEST_STR

The conversion method:

Test_str = "". Join (Test_list)

Example:

It is important to note that this method requires that the element in the list be of character type, and if integer type, it needs to be converted to the character type and then to the STR type.

2. STR goto LIST

Suppose you have a str named Test_str, and the converted list is named Test_list

The conversion method:

Test_list=list (TEST_STR)

Example:

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 Number Type Conversion
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
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

Basic data types
1: Although variables in Python do not need to be declared, they must be assigned when used
1. Shaping variables
2. Floating-point variables
3. Character type
2: You can assign a value to multiple variables, or you can assign values to multiple variables
There are 6 standard data types in 3:python3
*number (digital)
*true=1
*false=0
* Numeric Division (/) always returns a floating-point number to get the integer using//Operator
* When blending calculations, Python converts the shaping to a floating-point number
*string (String)
* strings are enclosed in ' or ', using \ to escape special strings
* If you do not want the backslash to escape, you can prefix the string with an R to indicate the original string
* Index value starts at 0, 1 is the beginning of the end
* Plus + is the string's connector, the asterisk * denotes the copy of the current string, followed by the number of copies
*list (list)
The *list is written between square brackets, and the elements are separated by commas.
* As with strings, lists can be indexed and sliced
*list can be connected using the + operator
The elements in the *list can be changed.
*tuple (meta-group)
* Tuples are similar to lists, except that elements of tuples cannot be modified, and tuples are written in parentheses. Separating elements with commas
* Tuples can also be indexed and sliced, in the same way
* Note the special syntax rules for constructing tuples that contain 0 or 1 elements
* Tuples can also be spliced with the + operator
*sets (Collection)
*set is a sequence that does not need to be repeated, and the basic function is to test the membership and remove duplicate elements
Dictionary (dictionary)
* Dictionary is a type of mapping, the dictionary is identified with {}, and it is an unordered build (key): Value pair Collection
* Built (key) must use immutable types. Built in the same dictionary (key) must be unique
* Create an empty dictionary using {}
4: Conversions between types
*int (x,base=10) x string or number, base binary number, default decimal floating point to Integer
*float integer conversion to floating-point
Convert *complex to plural
*STR (10) converting an object to a string
*repe () Converts an object to an expression string
*REPR (Dict) converts an object to an expression string
*eval (str) is used to calculate a Python expression that is valid in a string, returning an object
*tuple (listi) Convert a list to a tuple
*list () converting tuples to lists
*set Conversion Collection

‘‘‘
Print ('------------------1----------------') a=100# shaping variable b=100.0# floating-point variable c= ' zifuxing ' #字符串print (a,b,c) print ('-------- -------------2------------------') A=b=c=1print (a,b,c) a,b,c=1,2,3print (a,b,c) print ('--------------------3------ -------------') print (' number ') A,b,c=20,5.5,true#type can query the data type that the variable refers to print (type (a), type (b), type (c)) # You can also use Isinstance to determine that # type () does not consider a subclass to be a parent class type #isinstance () would consider a subclass to be a parent class type print (' String string ') str1= ' Zifuchuan ' Print (str1[0:- 1]) #输出第一个到倒数第二个print (str1[0]) #输出第一个字符串print (Str1[2:5]) #输出第三个到第五个字符串print (str1[2:]) #输出第三个后面所有的字符串print (str1*2) # Output String 2 times print (str1+ ' Test ') #链接字符串print (' list ') listp=[' abc ', 768,2.33, ' Liebiao ', 70.2]tinylist=[123, ' Liebiao ']print ( LISTP) #输出完整列表print (listp[0]) #输出列表的第一个元素print (Listp[1:3]) #输出第二个元素到第三个元素print (listp[2:]) #输出第三个元素开始的所有元素print ( tinylist*2) #输出两次列表print (listp+tinylist) #链接两个列表 # The element in the variable list a=[1,2,3,4,5,6]a[0]=9a[2:5]=[13,14,5]a[2:5]=[]# You can delete the specified element print (' Tuple tuples, use the same as above but cannot modify the elements ') print (' Set set ') student={' Tom ', ' Jim ', ' Mary ', ' Tom ', ' Jack ', ' Rose '}print ( Student) #输出集合, duplicate elements are automatically removed if ' R 'The OSE ' in Student:print (' Rose is in the set ') Else:print (' Rose is not in the set ') #set可以进行集合运算a =set (' Abra ') b=set (' Alac ') print (a) #set可以去重复所 A|b print (a&b) #a和b的交集print (a^b) #a和b不同时存在的元素print (' Dictionary dictionary ') with output AH A,b,rprint (A-B) #a和b #a和b的差print tinydict={' name ': ' Runoob ', ' Code ': ' 1 ', ' site ': ' www.runoob.com '}print (tinydict) #输出完整的字典print (Tinydict.keys ()) # Output all Build print (Tinydict.values ()) #输出所有的值print ('----data type conversion--------') print (int (3.6)) #浮点数转换为整数print (float (1)) # Integers are converted to floating-point print (float (' 123 ')) #字符串转为浮点数print (complex) #整数为复数print (complex (' 1 ')) #字符串为负数dict ={' Runoob ': ' Runoob.com ', ' Google ': ' goole.com '}print (str (dict)) I=int (() print (str (i)) print (Repr (dict)) X=7print (eval (' 3*x ')) # Can manipulate strings listi=[' Google ', ' Taobao ', ' Runoob ', ' Baidu ']print (tuple (Listi)) tpo=tuple (Listi) t= (' 1 ', ' 2 ', ' 3 ') Print (list ( T) print (TPO) x=set (' Runoob ') y=set (' Google ') print (x, y)

Data type conversions in 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.