Python Learning (c) Examples of numeric types

Source: Internet
Author: User



Grandma's reported this mistake, I thought can be like in Java string plus number

Traceback (most recent):
File "./number.py", line <module>
Print ("You know:" + 2**10)
Typeerror:must is str, not int


Stepping on a hole

The first thing to emphasize is that Python is a strongly typed language, and if you connect a string and a number directly, you will get an error:
typeerror:unsupported operand type (s) for +: ' int ' and ' str '

If you want numbers and strings to be concatenated, you can convert the numbers through str into a string, and then do the connection operation.
e.g. C = str (a) + b

My mom, Python is a strong language type, so why not reload the plus pinch


Here's another point to start talking about.

Although Str (), repr () and ' operations are very similar in terms of features and functions, the fact that repr () and ' ' are doing exactly the same thing, they return an "official" string representation of an object, which means that in most cases it can be evaluated (using the built-in function eval () ) to get the object back.


Practice Script number.py


#!/usr/bin/python#suddenly the evil thought of it, Py is a good evil word#abandon python to py.#the numbers contained in Python are: integers, floating-point numbers, complex numbers, decimal numbers with fixed precision, rational numbers with numerator and denominator#supported operations are: + for addition, * for multiplication, * * for exponentiationPrint(123+222)#two numbers added directlyPrint(1.5 * 4)Print("You know:"+ str (2**10))#Note that this non-string type is to be processed with STR, or the error will bePrint("Python does not have to write semicolons, so you can write at will, you may not get used to it at first")Print(2**100)#python will help you with this situation.#print (Len (str (2 * 1000000)) # It's not a damn spill, it's interesting you can execute it, it's more time-consuming3.1415 * 2Print(3.1415 * 2)Print(Repr (3.1415 * 2))#Repr is an intrinsic representation of data .ImportMath#This is the introduction of the Python module, the first to know how to write it.Print(Math.PI)Print(Math.sqrt (85))ImportRandomPrint(Random.random ())Print(Random.choice ([1, 2, 3, 4]))#You know what you mean.#The end is about the number, so use it.

Python Learning (c) Examples of numeric types

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.