Basic Python knowledge

Source: Internet
Author: User

Numbers and expressions

[Python]
>>> 2 + 3
5
>>> 1.0/2.0
0.5
>>> 1.0 // 2.0 #//
0.0
>>> 1% 2 # % understanding
1
>>> 1/2 #/understanding
0
>>> 2 ** 3
8
>>>-2 ** 3
-8
>>> (-2) ** 3
-8
>>> (-2) ** 2
4

>>> 2 + 3
5
>>> 1.0/2.0
0.5
>>> 1.0 // 2.0 #//
0.0
>>> 1% 2 # % understanding
1
>>> 1/2 #/understanding
0
>>> 2 ** 3
8
>>>-2 ** 3
-8
>>> (-2) ** 3
-8
>>> (-2) ** 2
4
Long Integer
Python can handle very large integer types:


[Python]
>>> 10000004040404040679
2017004040404040679l

>>> 10000004040404040679
2017004040404040679l:


[Python] view plaincopyprint?
>>> 10000006044545*123434958906807 + 2897898
1234350335176233855348615713l

>>> 10000006044545*123434958906807 + 2897898
1234350335176233855348615713l
Hexadecimal and octal

[Python]
>>> 0xAF
175
>>> 0127
87
>>> 0xaf+ 0127
262

>>> 0xAF
175
>>> 0127
87
>>> 0xaf+ 0127
262
Variable
The variables in python are easy to understand. A variable is basically the name of a value. For example, if x represents 3, you only need the following statement:


[Python]
>>> X = 3

>>> X = 3 after the variable is assigned a value, you can use the variable in the expression.


[Python]
>>> X * 3
9

>>> X * 3
9
Statement
The following statement:


[Python]
>>> 2*3
6
>>> Print 2*3
6

>>> 2*3
6
>>> Print 2*3
6
Get user input

[Python]
>>> Input ("The meaning of Life :")
The meaning of Life: 104
104

>>> Input ("The meaning of Life :")
The meaning of Life: 104
104 here, this example is not very useful, but proceed with the following content:
[Python]
>>> X = input ("x :")
X: 289
>>> Y = input ("y :")
Y: 237
>>> Print x * y
68493

>>> X = input ("x :")
X: 289
>>> Y = input ("y :")
Y: 237
>>> Print x * y
68493
Function

[Python]
>>> 2 ** 3
8
>>> Pow (2, 3)
8

>>> 2 ** 3
8
>>> Pow (2, 3)
8. complex expressions:


[Python]
>>> 10 + pow (2, 3*13)/3.0
183251937972.66666

>>> 10 + pow (2, 3*13)/3.0
Module 183251937972.66666
You can think of a module as importing it to Python to enhance its functionality. Use special commands to import modules. The floor function mentioned above is in the module named math:


[Python]
>>> Import math
>>> Math. floor (20.8)
20.0

>>> Import math
>>> Math. floor (20.8)
20.0 if you want to convert to an integer, you can use the int function:
[Python]
>>> Int (math. floor (20.8 ))
20

>>> Int (math. floor (20.8 ))
20 another form of using the import command:


[Python]
>>> From math import sqrt
>>> Sqrt (20)
4.47213595499958

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.