>>> a,b,c=4,5,6
>>> a,b=1,2
>>> a,b=b,a#无需引入第三个变量
>>> print(a,b)
2,1
>>> MAX_SPEED = 120
>>> print(MAX_SPEED)
120
>>> MAX_SPEED = 140
#实际是可以改的。只能逻辑上不做修改。
>>> print(MAX_SPEED)
140
>>> a = 7/2
>>> a
3.5
>>> a = 7//2
>>> a
3
>>> a = 7%2
>>> a
1
>>> 7%4
3
>>> 2**3
8
>>> 3/0
Traceback (most recent call last):
File "", line 1, in
3/0
ZeroDivisionError: division by zero
>>> divmod(10,5)
(2, 0)
>>> divmod(10,3)
(3, 1)
>>> 3/0
Traceback (most recent call last):
File "", line 1, in
3/0
ZeroDivisionError: division by zero
>>> divmod(13,3)
(4, 1)
>>> 12
12
>>> 0b101
5
>>> 0o19
SyntaxError: invalid syntax
>>> 0o10
8
>>> 0xff
255
>>> 0xf
15
>>> 0x10
16
>>> int("456")
456
>>> int("456abc")
Traceback (most recent call last):
File "", line 1, in
int("456abc")
ValueError: invalid literal for int() with base 10: '456abc'
>>> int("456.78")
Traceback (most recent call last):
File "", line 1, in
int("456.78")
ValueError: invalid literal for int() with base 10: '456.78'
>>>
>>> googol = 10**100
>>> googol
100000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000
>>> import time
>>> b = int(time.time())
>>> b
1530168754
>>> totalMinutes = b/60
>>> totalMinutes
25502812.566666666
>>> totalMinutes = b//60
>>> totalMinutes
25502812
>>> totalHours = totalMinutes//60
>>> totalHours
425046
>>> totalDays = totalHours//24
>>> totalDays
17710
>>> totalYears = totalDays//365
>>> totalYears
48
>>>import turtle
>>>import math
#定义多个点的坐标
x1,y1 = 100,100
x2,y2 = 100,-100
x3,y3 = -100,-100
x4,y4 = -100,100
#绘制折线
>>>turtle.penup()
>>>turtle.goto(x1,y1)
>>>turtle.pendown()
>>>turtle.goto(x2,y2)
>>>turtle.goto(x3,y3)
>>>turtle.goto(x4,y4)
#计算起始点和终点的距离
>>>distance = math.sqrt((x1-x4)**2 + (y1-y4)**2)
>>>turtle.write(distance)
>>> a = True
>>> b = 3
>>> a+b
4
16.逻辑运算符
>>> a = 1000
>>> b = 1000
>>> a == b
True
>>> a is b
False
>>> id(a)
46764560
>>> id(b)
46765216
>>> c = 10
>>> d = 10
>>> c is d
True
>>> id(c)
1388831648
>>> id(d)
1388831648
>>> a = 0b11001
>>> b = 0b01000
>>> c = a|b
>>> bin(c)
#bin()可以将数字转成二进制表示
'0b11001'
>>> bin(c&b)
'0b1000'
>>> bin(c^b)
'0b10001'
>>> a = 3
>>> a<<2
#左移 1 位相当于乘以 2,左移 2 位,相当于乘以 4
12
>>> a = 8
>>> a>>1
#右移 1 位相当于除以 2
.
>>> ord('A')
65
>>> ord('高')
39640
>>> chr(66)
'B'
>>> ord('淇')
28103
>>> a = "I'm a teacher!"
>>> print(a)
I'm a teacher!
>>> b = 'my_name is "TOM"'
>>> print(b)
my_name is "TOM"
>>> resume = ''' name="gaoqi"
company="sxt"
age=18
lover="Tom"'''
>>> print(resume)
name="gaoqi"
company="sxt"
age=18
lover="Tom"
>>> c = ''
>>> len(c)
0
len()用于计算字符串含有多少字符。例如:
>>> d = 'abc 尚学堂'
>>> len(d)
6
>>> a = 'I\nlove\nU'
>>> a
'I\nlove\nU'
>>> print(a)
I
love
U
>>> print('aaabb\
cccddd')
aaabbcccddd
>>> a = 'sxt'+'gaoqi'
>>> a
'sxtgaoqi'
>>> b = 'sxt''gaoqi'
>>> b
'sxtgaoqi'
>>> a = 'Sxt'*3
>>> a
'SxtSxtSxt'
>>> myname = input("请输入名字:")
请输入名字:高淇
>>> myname
'高淇'
>>> a = 'abcdefghijklmnopqrstuvwxyz'
>>> a
'abcdefghijklmnopqrstuvwxyz'
>>> a[0]
'a'
>>> a[3]
'd'
>>> a[26-1]
'z'
>>> a[-1]
'z'
>>> a[-26]
'a'
>>> a[-30]
Traceback (most recent call last):
File "", line 1, in
a[-30]
IndexError: string index out of range
>>> a = 'abcdefghijklmnopqrstuvwxyz'
>>> a
'abcdefghijklmnopqrstuvwxyz'
>>> a[3]='高'
Traceback (most recent call last):
File "", line 1, in
a[3]='高'
TypeError: 'str' object does not support item assignment
>>> a = 'abcdefghijklmnopqrstuvwxyz'
>>> a
'abcdefghijklmnopqrstuvwxyz'
>>> a = a.replace('c','高')
'ab 高 defghijklmnopqrstuvwxyz'
>>> "abcdefg"[3:50]
'defg'
>>> a = "to be or not to be"
>>> a.split()
['to', 'be', 'or', 'not', 'to', 'be']
>>> a.split('be')
['to ', ' or not to ', '']
join()的作用和 split()作用刚好相反,用于将一系列子字符串连接起来。示例代码如下:
>>> a = ['sxt','sxt100','sxt200']
>>> '*'.join(a)
'sxt*sxt100*sxt200'
>>> a = "abd_33"
>>> b = "abd_33"
>>> a is b
True
>>> c = "dd#"
>>> d = "dd#"
>>> c is d
False
>>> str1 = "aa"
>>> str2 = "bb"
>>> str1+str2 is "aabb"
False
>>> str1+str2 == "aabb"
True
>>> "*s*x*t*".strip("*")
's*x*t'
>>> "*s*x*t*".lstrip("*")
's*x*t*'
>>> "*s*x*t*".rstrip("*")
'*s*x*t'
>>> " sxt ".strip()
'sxt'
>>> a="SXT"
>>> a.center(10,"*")
'***SXT****'
>>> a.center(10)
' SXT '
>>> a.ljust(10,"*")
'SXT*******'
>>> "sxt100".isalnum()
True
>>> "sxt 尚学堂".isalpha()
True
>>> "234.3".isdigit()
False
>>> "23423".isdigit()
True
>>> "aB".isupper()
False
>>> "A".isupper()
True
>>> "\t\n".isspace()
True
>>> a = "名字是:{0},年龄是:{1}"
>>> a.format("高淇",18)
'名字是:高淇,年龄是:18'
>>> a.format("高希希",6)
'名字是:高希希,年龄是:6'
>>> b = "名字是:
{0},年龄是{1}。{0}是个好小伙"
>>> b.format("高淇",18)
'名字是:高淇,年龄是 18。高淇是个好小伙'
>>> c = "名字是{name},年龄是{age}"
>>> c.format(age=19,name='高淇')
'名字是高淇,年龄是 19'
>>>import turtle
>>>turtle.showturtle()
>>>turtle.width(10)
>>>turtle.color("blue")
>>>turtle.circle(50)
>>>turtle.penup()
>>>turtle.goto(120,0)
>>>turtle.pendown()
>>>turtle.color("black")
>>>turtle.circle(50)
>>>turtle.penup()
>>>turtle.goto(240,0)
>>>turtle.pendown()
>>>turtle.color("red")
>>>turtle.circle(50)
>>>turtle.penup()
>>>turtle.goto(60,-50)
>>>turtle.pendown()
>>>turtle.color("yellow")
>>>turtle.circle(50)
>>>turtle.penup()
>>>turtle.goto(180,-50)
>>>turtle.pendown()
>>>turtle.color("green")
>>>turtle.circle(50)
>>>import turtle
>>>turtle.showturtle()
>>>turtle.goto(100,0)
>>>turtle.goto(100,100)
>>>turtle.goto(0,100)
>>>turtle.goto(0,0)
>>>turtle.penup()
>>>turtle.goto(120,0)
>>>turtle.pendown()
>>>turtle.goto(220,0)
>>>turtle.goto(220,100)
>>>turtle.goto(120,100)
>>>turtle.goto(120,0)
>>>turtle.penup()
>>>turtle.goto(120,-20)
>>>turtle.pendown()
>>>turtle.goto(220,-20)
>>>turtle.goto(220,-120)
>>>turtle.goto(120,-120)
>>>turtle.goto(120,-20)
>>>turtle.penup()
>>>turtle.goto(0,-20)
>>>turtle.pendown()
>>>turtle.goto(100,-20)
>>>turtle.goto(100,-120)
>>>turtle.goto(0,-120)
>>>turtle.goto(0,-20)
>>> a='我爱你'*100
>>> print(a)
我爱你我爱你我爱你我爱你我爱你我爱你我爱你我爱你我爱你我爱你我爱你我爱你我爱你我爱你我爱你我爱你我爱你我爱你我爱你我爱你我爱你我爱你我爱你我爱你我爱你我爱你我爱你我爱你我爱你我爱你我爱你我爱你我爱你我爱你我爱你我爱你我爱你我爱你我爱你我爱你我爱你我爱你我爱你我爱你我爱你我爱你我爱你我爱你我爱你我爱你我爱你我爱你我爱你我爱你我爱你我爱你我爱你我爱你我爱你我爱你我爱你我爱你我爱你我爱你我爱你我爱你我爱你我爱你我爱你我爱你我爱你我爱你我爱你我爱你我爱你我爱你我爱你我爱你我爱你我爱你我爱你我爱你我爱你我爱你我爱你我爱你我爱你我爱你我爱你我爱你我爱你我爱你我爱你我爱你我爱你我爱你我爱你我爱你我爱你我爱你
>>>
>>> "to be or not to be"[::-1]
'eb ot ton ro eb ot'
>>> 'sxtsxtsxtsxt'[::3]
'ssss'