练习51:

题目:

学习使用 按位与(&) 。

分析:

0&0=0; 0&1=0; 1&0=0; 1&1=1。

程序:

if __name__ == '__main__':
a = 8
b = a & 4
print('a & b = %d' % b)
b &= 2
print('a & b = %d' % b)

输出结果:

a & b = 0
a & b = 0

练习52:

题目:

学习使用 按位或(|) 。

分析:

0|0=0; 0|1=1; 1|0=1; 1|1=1

程序:

if __name__ == '__main__':
a = 12
b = a | 3
print('a | b is %d' % b)
b |= 7
print('a | b is %d' % b)

输出结果:

a | b is 15
a | b is 15

练习53:

题目:

学习使用 按位异或(^) 。

分析:

0^0=0; 0^1=1; 1^0=1; 1^1=0

程序:

if __name__ == '__main__':
a = 12
b = a ^ 3
print('The a ^ 3 = %d' % b)
b ^= 7
print('The a ^ b = %d' % b)

输出结果:

The a ^ 3 = 15
The a ^ b = 8

练习54:

题目:

取一个整数 a 从右端开始的 4~7位。

分析:

可以这样考虑:

(1) 先使a右移4位。

(2) 设置一个低4位全为1,其余全为0的数。可用(0<<4)

(3) 将上面二者进行 与运算(&)。

程序:

if __name__ == '__main__':
a = int(input('input a number:\n'))
b = a >> 4
c = ~(~0 << 4)
d = b & c
print('%o\t%o' % (a, d))

输出结果:

input a number:
23
27 1

练习55:

题目:

学习使用 按位取反(~)。

分析:

0=1;1=0;

(1) 先使a右移4位。

(2) 设置一个低4位全为1,其余全为0的数。可用(0<<4)

(3) 将上面二者进行&运算。

程序:

if __name__ == '__main__':
a = 234
b = ~a
print('The a\'s 1 complement is %d' % b)
a = ~a
print('The a\'s 2 complement is %d' % a)

输出结果:

The a's 1 complement is -235
The a's 2 complement is -235

练习56:

题目:

画图,学用circle画圆形。   

程序:

if __name__ == '__main__':
from Tkinter import * canvas = Canvas(width=800, height=600, bg='yellow')
canvas.pack(expand=YES, fill=BOTH)
k = 1
j = 1
for i in range(0, 26):
canvas.create_oval(310 - k, 250 - k, 310 + k, 250 + k, width=1)
k += j
j += 0.3 mainloop()

练习57:

题目:

画图,学用line画直线。

程序:

if __name__ == '__main__':
from Tkinter import * canvas = Canvas(width=300, height=300, bg='green')
canvas.pack(expand=YES, fill=BOTH)
x0 = 263
y0 = 263
y1 = 275
x1 = 275
for i in range(19):
canvas.create_line(x0,y0,x0,y1, width=1, fill='red')
x0 = x0 - 5
y0 = y0 - 5
x1 = x1 + 5
y1 = y1 + 5 x0 = 263
y1 = 275
y0 = 263
for i in range(21):
canvas.create_line(x0,y0,x0,y1,fill = 'red')
x0 += 5
y0 += 5
y1 += 5 mainloop()

练习58:

题目:

画图,学用rectangle画方形。   

程序:

rectangle(int left, int top, int right, int bottom)。
if __name__ == '__main__':
from Tkinter import *
root = Tk()
root.title('Canvas')
canvas = 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()
root.mainloop()

练习59:

题目:

画图,综合例子。  

分析:

利用for循环控制100-999个数,每个数分解出个位,十位,百位。

程序:

if __name__ == '__main__':
from Tkinter import * canvas = Canvas(width=300, height=300, bg='green')
canvas.pack(expand=YES, fill=BOTH)
x0 = 150
y0 = 100
canvas.create_oval(x0 - 10, y0 - 10, x0 + 10, y0 + 10)
canvas.create_oval(x0 - 20, y0 - 20, x0 + 20, y0 + 20)
canvas.create_oval(x0 - 50, y0 - 50, x0 + 50, y0 + 50)
import math B = 0.809
for i in range(16):
a = 2 * math.pi / 16 * i
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.create_oval(x0 - 60, y0 - 60, x0 + 60, y0 + 60) 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')
mainloop()

练习60:

题目:

计算字符串长度。  

程序:

s = 'strlen'
print(len(s))

输出结果:

6

Python3.x 基础练习题100例(51-60)的更多相关文章

  1. Python3.x 基础练习题100例(01-10)

    练习01: 题目: 有四个数字:1.2.3.4,能组成多少个互不相同且无重复数字的三位数?各是多少? 分析: 可填在百位.十位.个位的数字都是1.2.3.4.组成所有的排列后再去 掉不满足条件的排列. ...

  2. Python3.x 基础练习题100例(11-20)

    练习11: 题目: 古典问题:有一对兔子,从出生后第3个月起每个月都生一对兔子,小兔子长到第三个月后每个月又生一对兔子,假如兔子都不死,问每个月的兔子总数为多少? 分析: 兔子的规律为数列1,1,2, ...

  3. Python3.x 基础练习题100例(41-50)

    练习41: 题目: 模仿静态变量的用法. 程序: def varfunc(): var = 0 print('var = %d' % var) var += 1 if __name__ == '__m ...

  4. Python3.x 基础练习题100例(31-40)

    练习31: 题目: 请输入星期几的第一个字母来判断一下是星期几,如果第一个字母一样,则继续判断第二个字母. 分析:用情况语句比较好,如果第一个字母一样,则判断用情况语句或if语句判断第二个字母. 程序 ...

  5. Python3.x 基础练习题100例(21-30)

    练习21: 题目: 猴子吃桃问题:猴子第一天摘下若干个桃子,当即吃了一半,还不瘾,又多吃了一个第二天早上又将剩下的桃子吃掉一半,又多吃了一个.以后每天早上都吃了前 一天剩下的一半零一个.到第10天早上 ...

  6. Python3.x 基础练习题100例(81-90)

    练习81: 题目: 809??=800??+9?? 其中??代表的两位数, 809??为四位数,8??的结果为两位数,9??的结果为3位数.求??代表的两位数,及809*??后的结果. 程序: a = ...

  7. Python3.x 基础练习题100例(91-100)

    练习91: 题目: 时间函数举例1. 程序: if __name__ == '__main__': import time print (time.ctime(time.time())) print ...

  8. Python3.x 基础练习题100例(61-70)

    练习61: 题目: 打印出杨辉三角形. 程序: if __name__ == '__main__': a = [] for i in range(10): a.append([]) for j in ...

  9. Python3.x 基础练习题100例(71-80)

    练习71: 题目: 编写input()和output()函数输入,输出5个学生的数据记录. 程序: N = 5 # stu # num : string # name : string # score ...

随机推荐

  1. jQuery插件Validate

    一.导入js库 <script type="text/javascript" src="<%=path %>/validate/jquery-1.6.2 ...

  2. HDU4622 Reincarnation【SAM】

    HDU4622 Reincarnation 给出一个串,每次询问其一个子串有多少不同的子串 按每个后缀建立\(SAM\)不断往后加字符,然后记录答案,查询的时候直接用即可 //#pragma GCC ...

  3. Codeforces Round #636 (Div. 3)

    比赛链接:https://codeforces.com/contest/1343 A - Candies 题意 有一数列 x + 2x + 4x + ... + 2k-1x = n,输出 k ≥ 2 ...

  4. codeforces 292E. Copying Data

    We often have to copy large volumes of information. Such operation can take up many computer resourc ...

  5. ACM International Collegiate Programming Contest, Egyptian Collegiate Programming Contest (ECPC 2015) G. It is all about wisdom (二分,单源最短路)

    题意:有\(n\)个点,\(m\)条边,只有当你的智力值大于这条边的\(w\)才能走,问在花费不超过\(k\)的情况下,从\(1\)走到\(n\)的所需的最小智力值. 题解:这题比赛为什么没想出来呢? ...

  6. cmd控制台Windows服务相关

    1.创建服务 sc create ServerName binpath= "E:\MySql5.5\bin\mysqld.exe" 2.启动服务 sc start ServerNa ...

  7. vlc音视频开发(二)环境搭建(VS篇)

    来源:微信公众号「编程学习基地」 目录 简介 VS配置vlc开发环境 下载vlc源码 创建vlc环境 测试vlc代码 运行vlc程序 完成项目文件获取 简介 VLC 是一款自由.开源的跨平台多媒体播放 ...

  8. Linux网络文件下载

    wget 以网络下载 maven 包为例 wget -c http://mirrors.shu.edu.cn/apache/maven/maven-3/3.5.4/binaries/apache-ma ...

  9. leetcode15 三数之和 双指针

    注意题目没要求数字只能用一次 a + b + c = 0 即为 -b=a+c,同时要求数字不全为正(然后发现a+b+c就行...不过多想想没坏处嘛) 先处理特殊情况,然后 先排序 注意不重复,只需要有 ...

  10. Leetcode(83)-删除排序链表中的重复元素

    给定一个排序链表,删除所有重复的元素,使得每个元素只出现一次. 示例 1: 输入: 1->1->2 输出: 1->2 示例 2: 输入: 1->1->2->3-&g ...