python经典练习100例(51~70)

目录

51. 按位与 &

52. 按位或 |

53. 按位异或 ^

54. 取一个整数a指定位

55. 按位取反

56. 用circle画圆形

57. 用line画直线

58. 用rectangle画方形

59. 综合画图例子

60. 计算字符串长度

61. 打印杨辉三角形

62. 查找字符串

63. 画椭圆

64. 利用ellipse 和 rectangle 画图

65. 一个最优美的图案

66. 按大小排序

67. 最大的与第一个元素交换,最小的与最后一个元素交换

68. 移动数组元素

69. 有n个人围成一圈,顺序排号

70. 求一个字符串的长度


51. 按位与 &

题目:学习使用按位与 &

# coding: UTF-8

a = 7 # 十进制
b = 5
c = a & b
print '%d & %d = %d' % (a, b, c)

print '##################'
a = 077 # 八进制
b = a & 3
print '%d & 3 = %d' % (a, b)
b &= 7
print '%d & 7 = %d' % (a, b)


print '################'
a = 0x77
b = a & 8
print '%x & 8  = %o' % (a, b)
b = a & 15
print '%d & 15 = %d' % (a, b)

print '#############'
a = 1
b = 7
c = (a << 3) + b
print a, b, c

52. 按位或 |

题目:学习使用按位或 |

# coding: UTF - 8

a = 5
b = 2
c = a | b
print '%d | %d = %d' %(a,b,c)

53. 按位异或 ^

题目:学习使用按位异或 ^

# coding: UTF-8

a = 5
b = 4
c = a^b
print '%d ^ %d = %d' %(a,b,c)

54. 取一个整数a指定位

题目:取一个整数a从右端开始的4〜7位。

# coding: UTF-8

a = input("plz input a num a:")
b = (a >> 4) & 0x15
print b

print "##############"
b = (a >> 4) & (~(~0 << 4))
print b

55. 按位取反

题目:学习使用按位取反

# coding: UTF-8

print ~5
print ~7

print '%x , %d, %o' %( ~0, ~0, ~0)
print bin(0)
print bin(~0 & 0b1111111111111111)

a = 234
b = ~a
print b
a = ~a
print a

56. 用circle画圆形

题目:画图,学用circle画圆形。

# coding: UTF-8

# python 常用GUI库: Tkinter、 wxPython、 Jython

# Tkinter是python的标准GUI库。python使用Tkinter可以快速的创建GUI应用程序。
# Tkinter是内置在python的安装包中、IDLE也是用Tkinter编写,对于简单的图形界面Tkinter应付自如。
# python3.x 导Tkinter包时需小写T: import tkinter

'''
创建一个GUI程序:
1. 导入Tkinter模块
2. 创建控件
3. 指定这个控件的master, 即这个控件属于哪一个
4. 告诉GM(geometry manager)有一个控件产生了
'''

if __name__ == '__main__':
    import Tkinter

    # Canvas 画布控件;显示图形元素如线条或文本
    # w = Canvas(master, option = value, ...)  master父容器; option可选项,即该按钮的可设置属性,用键=值的形式设置。
    canvas = Tkinter.Canvas(width = 800, height = 600, bg = 'green')

    # Tkinter控件有特定的几何状态管理方法,管理整个控件区域组织,一下是Tkinter公开的几何管理类:包、网格、位置
    # pack() 包装; grid() 网格; place() 位置;
#    canvas.pack(expand = Tkinter.YES, fill = Tkinter.BOTH)
    canvas.pack()

    k = 10
    j = 5
    x0 = 400 #圆心横坐标
    y0 = 300 #圆心纵坐标
    for i in range(16):
        # Canvas组件支持以下标准选项: arc-创建一个扇形; image-创建图像; line-创建线条; oval-创建圆; polygon-创建一个至少三个边的多边形
        # oval = canvas.create_oval(x0, y0, x1, y1, options)
        canvas.create_oval(x0-k, y0-k, x0+k, y0+k, width = 2)
        k += j
        j += 2

    canvas.mainloop() #进入消息循环

结果如下:

python经典练习100例(51~70)_第1张图片

57. 用line画直线

题目:画图,学用line画直线。

# coding: UTF-8

if __name__ == '__main__':
    import Tkinter
    canvas = Tkinter.Canvas(width=1080, height=720, bg = 'gray')
    canvas.pack()
    x0 = 200
    y0 = 160
    x1 = 200
    y1 = 400
    # line = canvas.create_line(x0, y0, x1, y1, ..., xn, yn, options)
    canvas.create_line(x0,y0,x1,y1)

    x0 = 400
    y0 = 160
    x1 = 400
    y1 = 400
    for i in range(50):
        canvas.create_line(x0,y0,x0,y1,width=1,fill='red')
        x0 = x0-5
        y0 = y0-5
        x1 = x1+5
        y1 = y1+5

    x0 = 400
    y0 = 160
    x1 = 400
    y1 = 400
    for i in range(51):
        canvas.create_line(x0,y0,x0,y1,fill='red')
        x0 += 5
        y0 += 5
        y1 += 5
    canvas.mainloop()

结果如下:

python经典练习100例(51~70)_第2张图片

58. 用rectangle画方形

题目:画图,学用rectangle画方形.

# coding: UTF-8

import Tkinter

def func():
    root = Tkinter.Tk()
    root.title('Canvas')
    canvas = Tkinter.Canvas(root, width = 400, height = 400, bg = 'yellow')
    x0 = 263
    y0 = 263
    y1 = 275
    x1 = 275
    for i in range(19):
        canvas.create_rectangle(x0,y0,x1,y1)
        x0 -= 5
        y0 -= 5
        x1 += 5
        y1 += 5

    canvas.pack()
    canvas.mainloop()

canvas = Tkinter.Canvas(width = 720, height = 680, bg= 'green')
canvas.pack()

canvas.create_rectangle(260,240,460,440,width='10')

canvas.mainloop()
func()

结果如下:

python经典练习100例(51~70)_第3张图片

python经典练习100例(51~70)_第4张图片

59. 综合画图例子

题目:画图,综合例子。

# coding: UTF-8

if __name__ == '__main__':
    from Tkinter import *
    root = Tk()
    root.title('hello world')
    canvas = Canvas(root, width=1500, height=1500,bg='green')
    canvas.pack()

    x0 = 750
    y0 = 500
    canvas.create_oval(x0-50,y0-50,x0+50,y0+50)
    canvas.create_oval(x0-100,y0-100,x0+100,y0+100)
    canvas.create_oval(x0-250,y0-250,x0+250,y0+250)

    import math
    B = 4
    for i in range(16):
        a = 2*math.pi/16 * i
        # ceil() 返回数字的上整数
        x = math.ceil(x0+48*math.cos(a))
        y = math.ceil(y0+48*math.sin(a)*8)
        canvas.create_line(x0,y0,x,y,fill='red')
    canvas.create_oval(x0-300,y0-300,x0+300,y0+300)

    for k in range(501):
        for i in range(17):
            a = (2 * math.pi / 16) * i + (2 * math.pi / 180) * k
            x = math.ceil(x0 + 48 * math.cos(a))
            y = math.ceil(y0 + 48 + math.sin(a) * B)
            canvas.create_line(x0,y0,x,y,fill = 'red')
        for j in range(51):
            a = (2 * math.pi / 16) * i + (2* math.pi / 180) * k - 1
            x = math.ceil(x0 + 48 * math.cos(a))
            y = math.ceil(y0 + 48 * math.sin(a) * B)
            canvas.create_line(x0,y0,x,y,fill = 'red')
    canvas.mainloop()

python经典练习100例(51~70)_第5张图片

60. 计算字符串长度

题目:计算字符串长度。

# coding: UTF-8

st = raw_input('plz in a line: ')
a = len(st)
print a

61. 打印杨辉三角形

题目:打印出杨辉三角形(要求打印出10行如下图)

# coding: UTF-8

def print_l(l, n):
    for i in range(n):
        print l[i]

# n个list的浅拷贝的连接
a=[[0]*10]*10 #注意使用多维数组时的浅拷贝

print_l(a,10)
print '################'

for i in range(10):
    a[i][0] = 1
    a[i][i] = 1 #浅拷贝导致整个二维数组的值都为1
print_l(a,10)
print '################'


l=[]
for i in range(10):
    l.append([1]*(i+1))
print_l(l,10)

for i in range(2,10):
    for j in range(1, i):
        l[i][j] = l[i-1][j-1] + l[i-1][j]
print_l(l,10)

from sys import stdout
for i in range(10):
    for j in range(i+1):
        stdout.write(str(l[i][j]))
        stdout.write(' ')
    print

62. 查找字符串

题目:查找字符串。

# coding: UTF-8

sStr1 = 'abcdefg'
sStr2 = 'cde'
print sStr1.find(sStr2)

a1 = raw_input('plz input the first line: ')
a2 = raw_input('plz input the second line: ')

print '-1 means not in, other means str2 location in str1'
print a1.find(a2)

63. 画椭圆

题目:画椭圆。

#coding:UTF-8

import Tkinter

root = Tkinter.Tk()
root.title('椭圆')

canvas = Tkinter.Canvas(root, width=720, height=480, bg='green')
canvas.pack()

x0 = 360
y0 = 240
x = 0
y = 0
for i in range(13):
    canvas.create_oval(x0-150-x,y0-150-y,x0+150+x,y0+150+y,width=1)
    x -= 10
    y += 10
canvas.mainloop()

python经典练习100例(51~70)_第6张图片

64. 利用ellipse 和 rectangle 画图

题目:利用ellipse 和 rectangle 画图

#coding: UTF-8

from Tkinter import *

root = Tk()
root.title('rectangle')

canvas = Canvas(root, width=720,height=480,bg='white')
canvas.pack()

x1 = 10
y1 = 10
x2 = 60
y2 = 35
for i in range(10):
    canvas.create_rectangle(x1,y1,x2,y2)
    x1 -= 0.8
    y1 -= 0.4
    x2 += 20
    y2 += 10
canvas.mainloop()

65. 一个最优美的图案

题目:一个最优美的图案。

#coding: UTF-8

import math

class PTS:
    def __init__(self):
        self.x = 0
        self.y = 0

points = []

def LineToDemo():
    from Tkinter import *
    sx = 400
    sy = 400
    canvas = Canvas(width=sx,height=sy,bg='white')

    AspectRatio = 0.85
    MAXPTS = 15
    h = sy
    w = sx
    xcenter = w/2
    ycenter = h/2
    radius = (h-30)/(AspectRatio * 2) - 20
    step = 360/MAXPTS
    angle = 0.0
    for i in range(MAXPTS):
        rads = angle*math.pi/180.0
        p = PTS()
        p.x = xcenter + int(math.cos(rads) * radius)
        p.y = ycenter - int(math.sin(rads) * radius * AspectRatio)
        angle += step
        points.append(p)
    canvas.create_oval(xcenter - radius,ycenter - radius, xcenter + radius,ycenter + radius)

    for i in range(MAXPTS):
        for j in range(i,MAXPTS):
            canvas.create_line(points[i].x,points[i].y,points[j].x,points[j].y)

    canvas.pack()
    mainloop()

LineToDemo()

python经典练习100例(51~70)_第7张图片

66. 按大小排序

题目:输入3个数a,b,c,按大小顺序输出。

# coding: UTF-8

l = []
for i in range(3):
    l.append(input('plz input num: '))
l.sort()
print l
l.reverse()
print l

67. 最大的与第一个元素交换,最小的与最后一个元素交换

题目:输入数组,最大的与第一个元素交换,最小的与最后一个元素交换,输出数组。

#coding: UTF-8

l = []
for i in range(5):
    l.append(input('plz input num:'))
Max = l[0]
MaxIdx = 0
Min = l[0]
MinIdx = 0
for i in range(1,5):
    if Max < l[i]:
        Max = l[i]
        MaxIdx = i
    if Min > l[i]:
        Min = l[i]
        MinIdx = i

print l
l[0], l[MaxIdx] = l[MaxIdx], l[0]
l[-1], l[MinIdx] = l[MinIdx], l[-1]
print l

68. 移动数组元素

题目:有n个整数,使其前面各数顺序向后移m个位置,最后m个数变成最前面的m个数

# coding: UTF-8

l = [1,2,3,4,5,6,7,8,9,0]
m =3
print l
lm=l[:-3]
l = l[-3:]
for i in lm:
    l.append(i)
print l, lm

def func():
    n = int(raw_input('整数 n 为:\n'))
    m = int(raw_input('向后移 m 个位置为:\n'))

    def move(array,n,m):
        array_end = array[n - 1]
        for i in range(n - 1,-1,- 1):
            array[i] = array[i - 1]
        array[0] = array_end
        m -= 1
        if m > 0:move(array,n,m)

    number = []
    for i in range(n):
        number.append(int(raw_input('输入一个数字:\n')))
    print '原始列表:',number

    move(number,n,m)

    print '移动之后:',number

func()

69. 有n个人围成一圈,顺序排号

题目:有n个人围成一圈,顺序排号。从第一个人开始报数(从1到3报数),凡报到3的人退出圈子,问最后留下的是原来第几号的那位。

# coding: UTF-8

def func(l,t):
    count = 0
    for i in range(len(l)):
        if l[i] != 0 :
            t += 1
            count += 1
        if t == 3:
            l[i] = 0
            t = 0

    if count == 1:
        return
    else:
        func(l, t)

l = [1,2,3,4,5,6,7,8]
t = 0
'''
for i in range(len(l)):
    if l[i] != 0 :
        t += 1
    if t == 3:
        l[i] = 0
        t = 0
'''
func(l, t)
print l
for i in range(len(l)):
    if l[i] <> 0:
        print '最后留下的是原来的%d号位' %(i+1)

print '******************************************'

# method2:
n = input('plz input num of person: ')
a = []
for i in range(n):
    a.append(i+1)

t=0
m=0
i=0
while m

70. 求一个字符串的长度

题目:写一个函数,求一个字符串的长度,在main函数中输入字符串,并输出其长度。

# coding: UTF -8

def func(a):
    i = 0
    for num in a:
        i+=1
    return i

def main():
    a  = raw_input('plz input a strinng: ')
    length = func(a)
    print length

if __name__ == '__main__':
    main()

 

你可能感兴趣的:(python)