天天看点

beginning python v2 vs v3 chapter1(jupyter note)

python基础教程第一章笔记 v2 vs v3 by Hetland

    • In [1]:
    • In [3]:
    • In [5]:
    • In [8]:
    • In [20]:
    • In [22]:
    • In [29]:
    • In [31]:
    • In [32]:
    • In [35]:
    • In [33]:
    • In [34]:
    • In [39]:
    • In [42]:
    • In [48]:

In [1]:

if (2 == 1): print("one equals two")
if (1 == 1): print("one equals one")
           

Output:

one equals one
           

In [3]:

print(pow(2,3))
print(pow(2.5,3.5))
           

Output:

8
24.705294220065465
           

In [5]:

print(abs(-10))
print(round(1.0/2.0)) #v2
print(round(2/3)) #v3
           

Output:

10
0
1
           

In [8]:

import math
print(math.floor(32.9)) #v2
print(int(math.floor(32.9))) #v2
print(int(32.9)) #v3
print(math.ceil(32.3)) #v3
           

Output:

32
32
32
33
           

In [20]:

from math import sqrt
print(sqrt(9))
foo=math.sqrt
print(foo(25))
print(sqrt(-1))
           

Output:

3.0
5.0
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-20-9f819aa95e80> in <module>()
      3 foo=math.sqrt
      4 print(foo(25))
----> 5 print(sqrt(-1))

ValueError: math domain error
           

In [22]:

print("Try complex number:")
print((1+3j)*(9+4j)) #built in complex
import cmath  # attention! not like  from ... import ..., to avoid conflict with normal sqrt
print(cmath.sqrt(-1))
           

Output:

Try complex number:
(-3+31j)
1j
           

In [29]:

# " and '
print("Let's go; Don't do that")
print('I do means "it is yours"')
print('Let\'s go')
print("\"How are you\" says he.")
           

Output:

Let's go; Don't do that
I do means "it is yours"
Let's go
"How are you" says he.
           

In [31]:

print(repr(10000L)) #v2
           

Output:

File "<ipython-input-31-be3b11b1880f>", line 1
    print(repr(10000L)) #v2
                    ^
SyntaxError: invalid syntax
           

In [32]:

print(str(10000L))  #v2
           

Output:

File "<ipython-input-32-769bbc1defa3>", line 1
    print(str(10000L))  #v2
                   ^
SyntaxError: invalid syntax
           

In [35]:

# `x` ? 
print(`10000L`) #v2
           

Output:

File "<ipython-input-35-ac667d101e12>", line 1
    print(`10000L`)
          ^
SyntaxError: invalid syntax
           

In [33]:

print(repr(10000)) #v2
print(str(10000))  #v2
           

Output:

10000
10000
           

In [34]:

print(repr("Hello,\nWorld!"))
print(str("Hello,\nWorld!"))
           

Output:

'Hello,\nWorld!'
Hello,
World!
           

In [39]:

# triple ' or "
print('''Triple
reference
mark
''')
print("Hello, \
world")
print(1+2+\
     4+5)
# raw sring
print(r'C:\abc' '\\')
# bytes string, 256 values
print(b'Hello, world')
           

Output:

Triple
reference
mark

Hello, world
12
C:\abc\
b'Hello, world'
           

In [42]:

# the balance solution of UTF-8. Good, UTF-8 is default!
print("Hello, world!".encode("ASCII"))
print("Hello, world!".encode("UTF-8"))
print("Hello, world!".encode("UTF-32"))
print("Hællå, wørld!!".encode("UTF-8"))
print(len("How long is this?".encode("UTF-8")))
print(len("How long is this?".encode("UTF-32")))
           

Output:

b'Hello, world!'
b'Hello, world!'
b'\xff\xfe\x00\x00H\x00\x00\x00e\x00\x00\x00l\x00\x00\x00l\x00\x00\x00o\x00\x00\x00,\x00\x00\x00 \x00\x00\x00w\x00\x00\x00o\x00\x00\x00r\x00\x00\x00l\x00\x00\x00d\x00\x00\x00!\x00\x00\x00'
b'H\xc3\xa6ll\xc3\xa5, w\xc3\xb8rld!!'
17
72
           

In [48]:

x=bytearray(b"hello")  #v3
x[1]=ord(b"u")
print(x)
y=bytearray("world")
           

Output:

bytearray(b'hullo')
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-48-a9a5709bc03f> in <module>()
      2 x[1]=ord(b"u")
      3 print(x)
----> 4 y=bytearray("world")

TypeError: string argument without an encoding