发牌SO 发表于 2018-8-11 11:44:09

Python学习入门笔记-基础知识

Python 2.7.3 (default, Apr 10 2012, 23:31:26) on win32  
Type "copyright", "credits" or "license()" for more information.
  
>>> 2 ** 3
  
8
  
>>> -3 **2
  
-9
  
>>> (-3) ** 2
  
9
  
>>> 1111111111111111111111111111111111111111111111111111111111111
  
1111111111111111111111111111111111111111111111111111111111111L
  
>>> 11111111111111111111111111111111111111l
  
11111111111111111111111111111111111111L
  
>>> 2324324523423432432432432L * 2132132143432423423432L
  
4955767028159349056163576467822347272465546624L
  
>>> 0X86
  
134
  
>>> 01010101010101010101
  
18300341342965825L
  
>>> x = 5
  
>>> x % 4
  
1
  
>>> x *9
  
45
  
>>> x+1
  
6
  
>>> y= x+1
  
>>> y
  
6
  
>>> print x+y
  
11
  
>>> z =x +y
  
>>> z
  
11
  
>>> input("the meaning of life: ")
  
the meaning of life: 34
  
34
  
>>> x = input("x: ")
  
x: 9
  
>>> y =input("y: ")
  
y: -9
  
>>> print x+y
  
0
  
>>> if 1 == 2:print 'One equals two '
  
>>> if 1 == 2: print 'One equals two '
  
>>>
  
>>> if 1 ==1 : print 'One equals one '
  
One equals one
  
>>> if time % 60 == 0: print 'On the hour '
  
Traceback (most recent call last):
  
File "<pyshell#29>", line 1, in <module>
  
if time % 60 == 0: print 'On the hour '
  
NameError: name 'time' is not defined
  
>>> pow(2.9)
  
Traceback (most recent call last):
  
File "<pyshell#30>", line 1, in <module>
  
pow(2.9)
  
TypeError: pow expected at least 2 arguments, got 1
  
>>> pow(2 9)
  
SyntaxError: invalid syntax
  
>>> pow(2,9)
  
512
  
>>> pow(2, 2+1)
  
8
  
>>> x= input(" 你想要的数字:“)
  

  
SyntaxError: EOL while scanning string literal
  
>>> x= input(" 你想要的数字:")
  

  
SyntaxError: invalid syntax
  
>>> x = input(" 你想要的数字: ")
  

  
SyntaxError: invalid syntax
  
>>> x= input(" 123:")
  

  
SyntaxError: invalid syntax
  
>>> x = input(" 你想要的数字: ")
  
你想要的数字: pow (2,10)
  
>>> x
  
1024
  
>>> abs(-1)
  
1
  
>>> 1/2
  
0
  
>>> round(1.0/2.0)
  
1.0
  
>>> floor (1,2)
  
Traceback (most recent call last):
  
File "<pyshell#43>", line 1, in <module>
  
floor (1,2)
  
NameError: name 'floor' is not defined
  
>>> import math
  
>>> math.floor(30.1)
  
30.0
  
>>> floor(30.9)
  
Traceback (most recent call last):
  
File "<pyshell#46>", line 1, in <module>
  
floor(30.9)
  
NameError: name 'floor' is not defined
  
>>> math.floor(23.9)
  
23.0
  
>>> int(math.floor(20.5))
  
20
  
>>> from math import floor
  
>>> floor(34.1)
  
34.0
  
>>> big=math.floor
  
>>> big(23.1)
  
23.0
  
>>> int(34.2
  
)
  
34
  
>>> from math import sprt
  
Traceback (most recent call last):
  
File "<pyshell#56>", line 1, in <module>
  
from math import sprt
  
ImportError: cannot import name sprt
  
>>> from math import sprt
  
Traceback (most recent call last):
  
File "<pyshell#57>", line 1, in <module>
  
from math import sprt
  
ImportError: cannot import name sprt
  
>>> import math
  
>>> math.sprt(2)
  
Traceback (most recent call last):
  
File "<pyshell#59>", line 1, in <module>
  
math.sprt(2)
  
AttributeError: 'module' object has no attribute 'sprt'
  
>>> from math import sqrt
  
>>> sqrt(-2)
  
Traceback (most recent call last):
  
File "<pyshell#61>", line 1, in <module>
  
sqrt(-2)
  
ValueError: math domain error
  
>>> from math import sqrt
  
>>> sqrt(4)
  
2.0
  
>>> imprt math
  
SyntaxError: invalid syntax
  
>>> import math
  
>>> cmath.sqrt(-2)
  
Traceback (most recent call last):
  
File "<pyshell#66>", line 1, in <module>
  
cmath.sqrt(-2)
  
NameError: name 'cmath' is not defined
  
>>> import cmath
  
>>> cmath.sqrt(-2)
  
1.4142135623730951j
  
>>> from math import sqrt
  
>>> from cmath import sqrt
  
>>> sqrt(3)
  
(1.7320508075688772+0j)
  
>>> from math import sqrt
  
>>> sqrt(3)
  
1.7320508075688772
  
>>> ================================ RESTART ================================
  
>>>
  
Hello, world
  
>>> ================================ RESTART ================================
  
>>>
  
What is your name? bing
  
Hello, bing !
  
>>> x= "hello"
  
>>> y= "world"
  
>>> xy
  
Traceback (most recent call last):
  
File "<pyshell#76>", line 1, in <module>
  
xy
  
NameError: name 'xy' is not defined
  
>>> x y
  
SyntaxError: invalid syntax
  
>>> x
  
'hello'
  
>>> she's mothen
  
SyntaxError: EOL while scanning string literal
  
>>>
  
>>> she\'s mother
  
SyntaxError: unexpected character after line continuation character
  
>>> "hello, world "
  
'hello, world '
  
>>> she
  
Traceback (most recent call last):
  
File "<pyshell#83>", line 1, in <module>
  
she
  
NameError: name 'she' is not defined
  
>>> 'she 's mother '
  
SyntaxError: invalid syntax
  
>>> ' she\ 's mother '
  
SyntaxError: invalid syntax
  
>>> ' she \'s mother '
  
" she 's mother "
  
>>> print ' hello world '
  
hello world
  
>>> 10000000l
  
10000000L
  
>>> print ' 10000L '
  
10000L
  
>>> print " 10000L "
  
10000L
  
>>> 10000L
  
10000L
  
>>> print 10000l
  
10000
  
>>> print repr("hello, world !")
  
'hello, world !'
  
>>> print repr(10000L)
  
10000L
  
>>> print str("hello, world !"
  
)
  
hello, world !
  
>>> temp =42
  
>>> print "the temperature is" + temp
  
Traceback (most recent call last):
  
File "<pyshell#99>", line 1, in <module>
  
print "the temperature is" + temp
  
TypeError: cannot concatenate 'str' and 'int' objects
  
>>> print "the temperature is" + `temp`
  
the temperature is42
  
>>> print "the temperature is " + `temp`
  
the temperature is 42
  
>>> name = input("what is your name ? ")
  
what is your name ? bing
  
Traceback (most recent call last):
  
File "<pyshell#102>", line 1, in <module>
  
name = input("what is your name ? ")
  
File "<string>", line 1, in <module>
  
NameError: name 'bing' is not defined
  
>>> name = input("what is your name ? ")
  
what is your name ? 1223
  
>>> name = input("what is your name ? ");
  
what is your name ? 122
  
>>> print "hello " + name "!"
  
SyntaxError: invalid syntax
  
>>> print "hello " + name " ! "
  
SyntaxError: invalid syntax
  
>>> print "hello " + name + "!"
  
Traceback (most recent call last):
  
File "<pyshell#107>", line 1, in <module>
  
print "hello " + name + "!"
  
TypeError: cannot concatenate 'str' and 'int' objects
  
>>> name = input("what is your name ? ");
  
what is your name ? 123
  
>>> print "hello " + name + "!"
  
Traceback (most recent call last):
  
File "<pyshell#109>", line 1, in <module>
  
print "hello " + name + "!"
  
TypeError: cannot concatenate 'str' and 'int' objects
  
>>> ================================ RESTART ================================
  
>>>
  
what is your name ? 123print "hello " + name + "!"
  
Traceback (most recent call last):
  
File "D:/python/test", line 1, in <module>
  
name = input("what is your name ? ")
  
File "<string>", line 1
  
123print "hello " + name + "!"
  
^
  
SyntaxError: invalid syntax
  
>>> ================================ RESTART ================================
  
>>>
  
what is your name ? 123
  
Traceback (most recent call last):
  
File "D:/python/test", line 2, in <module>
  
print "hello " + name + "!"
  
TypeError: cannot concatenate 'str' and 'int' objects
  
>>> ================================ RESTART ================================
  
>>>
  
what is your name ? "bing"
  
hello bing!
  
>>> print ''' This is a vert
  
idd
  
fdfd
  
" hello world "
  
there '''
  
This is a vert
  
idd
  
fdfd
  
" hello world "
  
there
  
>>> print " hello, \
  
world ! "
  
hello, world !
  
>>> print 'C:\\ on"
  
SyntaxError: EOL while scanning string literal
  
>>> print "C:\\ on"
  
C:\ on
  
>>> print ' hello, \n world ! '
  
hello,
  
world !
  
>>> print 'c:\nowhere'
  
c:
  
owhere
  
>>> print r'c:\nere\ndf'
  
c:\nere\ndf
  
>>> print r"this is not corrert\"
  
SyntaxError: EOL while scanning string literal
  
>>> print r"this is not corrert" "\\"
  
this is not corrert\
  
>>> print r"this is not corrert" "\"
  
SyntaxError: EOL while scanning string literal
  
>>> u'hello world !'
  
u'hello world !'
  
>>> print u'你好'
  
ÄãoÃ
  
>>> print r'你好'
  
你好
  
>>>
页: [1]
查看完整版本: Python学习入门笔记-基础知识