Python study Note 1: Data Type Conversion

Source: Internet
Author: User

I. int functions can
(1) convert a numeric string in a mathematical format to an integer.
(2) convert a floating point number to an integer, but simply round it, instead of rounding it.

 

Example:

  1 aa = int("124")    #Correct
2  print "aa = ", aa #result=124
3  bb = int(123.45) #correct
4  print "bb = ", bb #result=123
5  cc = int("-123.45") #Error,Can't Convert to int
6  print "cc = ",cc
7 dd = int("34a") #Error,Can't Convert to int
8 print "dd = ",dd
9 ee = int("12.3") #Error,Can't Convert to int
10 print ee
11

2. the float function converts integers and strings into floating-point numbers.

 

Example:

  1 aa = float("124")     #Correct
2 print "aa = ", aa #result = 124.0
3 bb = float("123.45") #Correct
4 print "bb = ", bb #result = 123.45
5 cc = float(-123.6) #Correct
6 print "cc = ",cc #result = -123.6
7 dd = float("-123.34") #Correct
8 print "dd = ",dd #result = -123.34
9 ee = float('123v') #Error,Can't Convert to float
10 print ee

 

3. The str function converts a number to a character.

 

Example:

  1 aa = str(123.4)     #Correct
2 print aa #result = '123.4'
3 bb = str(-124.a) #SyntaxError: invalid syntax
4 print bb
5 cc = str("-123.45") #correct
6 print cc #result = '-123.45'
7 dd = str('ddd') #correct
8 print dd #result = ddd
9 ee = str(-124.3) #correct
10 print ee #result = -124.3

 

 

 

{Online shopping for rebates, new shopping choices}

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.