★★★★[Reprint]python Study Note one: Data type conversion ★★★★

Source: Internet
Author: User

An int function can
(1) Converting a numeric string into an integer that conforms to a mathematical format
(2) Convert floating-point numbers to integers, but simply rounding, not rounded.

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
Ten print EE
11

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
Ten print EE

The STR function converts numbers into characters

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
Ten print ee #result = 124.3

★★★★[Reprint]python Study Note one: Data type conversion ★★★★

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.