Hello,World

In [2]:
print ("Hello, World!")
Hello, World!

数字和表达式

加法

In [3]:
2+2
Out[3]:
4
In [4]:
53672+235253
Out[4]:
288925
In [5]:
1/2
Out[5]:
0.5
In [6]:
1.0/2.0
Out[6]:
0.5
In [7]:
1/2.0
Out[7]:
0.5
In [8]:
1/2.
Out[8]:
0.5
In [11]:
from __future__ import division
1/2
Out[11]:
0.5
In [12]:
1//2
Out[12]:
0
In [13]:
1.0//2.0
Out[13]:
0.0
In [16]:
1%2
Out[16]:
1

普通除法

In [17]:
10/3
Out[17]:
3.3333333333333335

整除

In [20]:
10//3
Out[20]:
3

取余(模除)

In [22]:
10%3
Out[22]:
1

乘法

In [25]:
2 * 3
Out[25]:
6

乘方

In [29]:
2 ** 3
Out[29]:
8

十六进制

In [50]:
0xAF
Out[50]:
175

八进制

In [54]:
0o10
Out[54]:
8

二进制

In [57]:
0b11
Out[57]:
3

变量

In [60]:
x = 2
In [62]:
x * 2
Out[62]:
4
In [71]:
y = x
In [73]:
x = 1
In [75]:
y
Out[75]:
2

语句

In [64]:
2 * 2
Out[64]:
4
In [68]:
print (2*2)
4
In [76]:
input("The meaning of life:")
The meaning of life:42
Out[76]:
'42'

获取用户输入

In [77]:
x = input("x:")
x:2
In [81]:
y = input("y:")
y:3
In [85]:
x= int(x)
In [86]:
y = int(y)
In [88]:
print (x*y)
6
In [89]:
if 1==2:
    print('One equals Two')
if 1 ==1:
    print('One equals One')
        
One equals One

函数

In [90]:
2 ** 3
Out[90]:
8
In [91]:
pow(2,3)
Out[91]:
8
In [92]:
abs(-10)
Out[92]:
10
In [94]:
round(1.0/2.0)
Out[94]:
0

模块

In [97]:
import math 
math.floor(32.9)
Out[97]:
32
In [101]:
float(math.floor(32.9))
Out[101]:
32.0
In [99]:
from math import sqrt
sqrt(9)
Out[99]:
3.0
In [100]:
int(sqrt(9))
Out[100]:
3
In [106]:
import cmath
cmath.sqrt(-1)
Out[106]:
1j
In [107]:
(1+3j)*(9+4j)
Out[107]:
(-3+31j)

字符串

In [108]:
"Hello,Python"
Out[108]:
'Hello,Python'
In [109]:
'Hello,Python'
Out[109]:
'Hello,Python'
In [119]:
"let\'s go!"
Out[119]:
"let's go!"
In [111]:
"\"Hello,Pyrhon\""
Out[111]:
'"Hello,Pyrhon"'
In [120]:
x= 'Hello,Python,'
In [113]:
y= "let\'s go!"
In [117]:
print(x+y)
Hello,Python,let's go!'
In [123]:
repr("Hello,World!")
Out[123]:
"'Hello,World!'"
In [124]:
str("hello,World!")
Out[124]:
'hello,World!'

新函数

In [125]:
abs(-10)
Out[125]:
10
In [126]:
cmath.sqrt(-9)
Out[126]:
3j
In [127]:
float(3)
Out[127]:
3.0
In [128]:
help()
Welcome to Python 3.6's help utility!

If this is your first time using Python, you should definitely check out
the tutorial on the Internet at https://docs.python.org/3.6/tutorial/.

Enter the name of any module, keyword, or topic to get help on writing
Python programs and using Python modules.  To quit this help utility and
return to the interpreter, just type "quit".

To get a list of available modules, keywords, symbols, or topics, type
"modules", "keywords", "symbols", or "topics".  Each module also comes
with a one-line summary of what it does; to list the modules whose name
or summary contain a given string such as "spam", type "modules spam".

help> abs
Help on built-in function abs in module builtins:

abs(x, /)
    Return the absolute value of the argument.

help> 

You are now leaving help and returning to the Python interpreter.
If you want to ask for help on a particular object directly from the
interpreter, you can type "help(object)".  Executing "help('string')"
has the same effect as typing a particular string at the help> prompt.
In [130]:
input(x)
Hello,Python,a
Out[130]:
'a'
In [131]:
int(3.0)
Out[131]:
3
In [137]:
math.ceil(2.5)
Out[137]:
3
In [138]:
math.floor(2.5)
Out[138]:
2
In [139]:
math.sqrt(9)
Out[139]:
3.0
In [140]:
pow(2,3)
Out[140]:
8
In [145]:
repr("Hello,World!")
Out[145]:
"'Hello,World!'"
In [146]:
round(2.21323531453465)
Out[146]:
2
In [147]:
str("Hello,World~")
Out[147]:
'Hello,World~'