一、构显国际橡棋8x8棋盘

#!/usr/bin/env python3
#-*- coding:utf-8 -*-
color_0="\033[41m \033[00m"
color_1="\033[46m \033[00m"
def line(a, b):
for i in range(0,48):
if ((i // 8) % 2) == 0:
print(a, end='')
else:
print(b, end='')
for x in range(0, 24):
if ((x // 4) % 2) != 0:
line(color_0, color_1)
else:
line(color_1, color_0)
print('\n', end='')

二、求正整数平方根

#!/usr/bin/env python3
#-*- coding: utf-8 -*-
#Author: fanhui
num = input("Please input a number: ")
num_sqrt = round(float(num)**0.5, 4)
print(num, "'s sqrt is ", num_sqrt, sep='')
f@z ~ $ python3 num_sqrt.py
Please input a number: 10
10's sqrt is 3.1623

三、求负整数平方根

#!/usr/bin/python3
#-*- coding: utf-8 -*-
import cmath
num = input("Please input a negative num: ")
negative_sqrt = cmath.sqrt(float(num))
print(negative_sqrt)
f@z ~ $ python3 negative_sqrt.py
Please input a negative num: -4
2j

四、三角形面积——海轮公式:√s(s-a)(s-b)(s-c),s=(a+b+c)*1/2,a、b、c分别为三角形三边长度

#!/usr/bin/env python3
#-*- coding: utf-8 -*-
#Author: fanhui
a = float(input("Please input the first length: "))
b = float(input("Please input the second length: "))
c = float(input("Please input the third length: "))
s = (a + b + c) / 2
area = (s * (s - a) * (s -b) * (s - c)) ** 0.5
print("The triangle's area is: {:,.4f}".format(area))
f@z ~ $ python3 triangle_area.py
Please input the first length: 444
Please input the second length: 444
Please input the third length: 444
The triangle's area is: 85,362.3920

五、摄时温度转华氏温度:(X * 1.8) + 32

f@z ~ $ cat temperature.py
#!/usr/bin/env python3
#-*- coding: utf-8 -*-
temperature = float(input("Please input a Celsius: "))
F = temperature * 1.8 + 32
print("F is: {:.2f}".format(F)) f@z ~ $ python3 temperature.py
Please input a Celsius: 22.4
F is: 72.32

六、判断年份是否为闰年:被4整除且不被100整除,或能被400整除

#!/usr/bin/python3
#-*- coding: utf-8 -*-
Year = int(input("Please input a Year number: "))
if (Year % 4 == 0 and Year % 100 != 0) or (Year % 400 == 0):
print("Yes! It is a leap year.")
else:
print("No! It is not a leap year.")
f@z ~ $ python3 leap_year.py
Please input a Year number: 2100
No! It is not a leap year.
f@z ~ $ python3 leap_year.py
Please input a Year number: 2000
Yes! It is a leap year.
f@z ~ $ python3 leap_year.py
Please input a Year number: 2016
Yes! It is a leap year.

七、判断素数(即:质数)

#!/usr/bin/env python3
#-*- coding: utf-8 -*-
num = int(input("Please input a Integer:"))
if num in [1, 2]:
print("Not prime number!")
else:
for x in range(2, num):
if num % x == 0:
print("Not prime number!")
break
else:
print("Prime number!")
f@z ~/py_script $ python3 prime_number.py
Please input a Integer:1
Not prime number!
f@z ~/py_script $ python3 prime_number.py
Please input a Integer:2
Not prime number!
f@z ~/py_script $ python3 prime_number.py
Please input a Integer:3
Prime number!
f@z ~/py_script $ python3 prime_number.py
Please input a Integer:4
Not prime number!
f@z ~/py_script $ python3 prime_number.py
Please input a Integer:5
Prime number!
f@z ~/py_script $ python3 prime_number.py
Please input a Integer:6
Not prime number!

Prime Number

八、整数阶乘:正整数的阶乘等于小于或等于其自身的所有正整数的乘积;0的阶乘为1

#!/usr/bin/env python3
#-*- coding: utf-8 -*-
num = int(input("Please input a non-negative integer: "))
if num < 0:
print("Sorry! Negative integer has no factorial.")
elif num == 0:
print("0's factorial is 1")
else:
factorial = 1
for x in range(1, num + 1):
factorial *= x
print("{}'s factorial is {}".format(num, factorial))
f@z ~/py_script $ a factorial.py
Please input a non-negative integer: 0
0's factorial is 1
f@z ~/py_script $ a factorial.py
Please input a non-negative integer: -111
Sorry! Negative integer has no factorial.
f@z ~/py_script $ a factorial.py
Please input a non-negative integer: 3
3's factorial is 6

Factorial

九、九九乘法表

#!/usr/bin/env python3
#-*- coding: utf-8 -*-
for m in range(1, 10):
print('|', end='')
for n in range(1, m + 1):
print('{}*{}={:2d}|'.format(n, m, n * m), end='')
print('\n', end='')
f@z ~ $ python3 99.py
|1*1= 1|
|1*2= 2|2*2= 4|
|1*3= 3|2*3= 6|3*3= 9|
|1*4= 4|2*4= 8|3*4=12|4*4=16|
|1*5= 5|2*5=10|3*5=15|4*5=20|5*5=25|
|1*6= 6|2*6=12|3*6=18|4*6=24|5*6=30|6*6=36|
|1*7= 7|2*7=14|3*7=21|4*7=28|5*7=35|6*7=42|7*7=49|
|1*8= 8|2*8=16|3*8=24|4*8=32|5*8=40|6*8=48|7*8=56|8*8=64|
|1*9= 9|2*9=18|3*9=27|4*9=36|5*9=45|6*9=54|7*9=63|8*9=72|9*9=81|

十、斐波那契数列Fibonacci Number:第0项是0,第1项是第一个1,从第三项开始,每一项都等于前两项之和 

#!/usr/bin/env python3
#-*- coding: utf-8 -*-
num = int(input('Please input a positive integer: '))
a = 0
b = 1
while b <= num:
print(b, end=' ')
a, b=b, a+b
print('\n', end='')
f@z ~ $ python3 fibonacci.py
Please input a positive integer: 40
1 1 2 3 5 8 13 21 34

十一、阿姆斯特朗数Armstrong:如果一个n位正整数等于其各位数字的n次方之和,则称该数为阿姆斯特朗数

#!/usr/bin/python3
#-*- coding: utf-8 -*-
num = input('Please input a positive integer: ')
temp = 0
for x in range(0, len(num)):
temp += int(num[x]) ** len(num)
if int(num) == temp:
print('Your number is an Armstorng Number!')
else:
print('Your number is not an Armstrong Number!')
f@z ~/py_script $ python3 armstrong.py
Please input a positive integer: 407
Your number is an Armstorng Number!
f@z ~/py_script $ python3 armstrong.py
Please input a positive integer: 100
Your number is not an Armstrong Number!

Armstrong Number

十二、获取指定期间内的阿姆斯特朗数

#!/usr/bin/env python3
#-*- coding: utf-8 -*-
def arms(num):
temp = 0
for j in range(0, len(num)):
temp += int(num[j]) ** len(num)
if temp == int(num):
print(num) n1 = int(input('Please input a positive integer: '))
n2 = int(input('Please input another positive integer: '))
if n1 > n2:
n1, n2 = n2, n1
print('=' * 40, '\n', 'Armstrong number between {} and {}: '.format(n1, n2))
for i in range(n1, n2 + 1):
arms(str(i))
f@z ~ $ a armstrong2.py
Please input a positive integer: 500
Please input another positive integer: 100
========================================
Armstrong number between 100 and 500:
153
370
370
371
407

十三、构显汉诺塔(hanoi)移动轨迹 

#!/usr/bin/env python3
#-*- coding: utf-8 -*-
def hanoi(n, x='A', y='B', z='C'):
if n == 1:
print(x, '~>', z)
else:
hanoi(n - 1, x, z, y)
hanoi(1, x, y, z)
hanoi(n - 1, y, x, z) num = int(input('Please input a positive integer: '))
hanoi(num)

递归法

#!/usr/bin/env python3
#-*- coding: utf-8 -*-
import random
num = int(input('Please input a positive integer: '))
A = list(range(num - 1, -1, -1))
B = []
C = []
TEMP = [['A', 'B', 'C'], [A, B, C]]
while len(C) != num:
x = random.choice(TEMP[1])
X = TEMP[0][TEMP[1].index(x)]
y = random.choice(TEMP[1])
Y = TEMP[0][TEMP[1].index(y)]
if X != Y:
if (x[-1:] > y[-1:] or x == []) and y != []:
x.append(y.pop())
print(Y, '~>', X, end=' ')
elif (y[-1:] > x[-1:] or y == []) and x != []:
y.append(x.pop())
print(X, '~>', Y, end=' ')
print(C)

穷举法

十四、最大公约数算法

#!/usr/bin/env python3
#-*- coding: utf-8 -*-
def divisor(i, j):
if i > j:
i, j = j, i
for k in range(i, 0, -1):
if i % k == 0 and j % k == 0:
print("=" * 44, "\n", 'The GCD between {} and {} is: {}'.format(i, j, k))
break n_0 = int(input('Please input a positive integer: '))
n_1 = int(input('Please input another positive integer: ')) divisor(n_0, n_1)
f@z ~ $ python3 GCD.py
Please input a positive integer: 24
Please input another positive integer: 54
============================================
The GCD between 24 and 54 is: 6

十五、显示日历

>>> import calendar
>>> print(calendar.month(2016, 9))
September 2016
Mo Tu We Th Fr Sa Su
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30

十六、递归斐波那契数列

#!/usr/bin/env python3
#-*- coding: utf-8 -*-
def fibo(i):
if i < 2:
return 1
else:
return (fibo(i - 1) + fibo(i - 2)) num = int(input('A positive integer: '))
for j in range(0, num + 1):
print(fibo(j))
f@z ~ $ python3 fibo_recursion.py
A positive integer: 10
1
1
2
3
5
8
13
21
34
55
89

Python3 From Zero——{最初的意识:008~初级实例演练}的更多相关文章

  1. Python3 From Zero——{最初的意识:006~数据编码与处理}

    一.读写CSV数据: #!/usr/bin/env python3 #-*- coding=utf8 -*- import csv with open('kxtx.csv', 'rt') as f: ...

  2. Python3 From Zero——{最初的意识:002~字符串和文本}

    一.使用多个界定符分割字符串 字符串.split(',')形式只适用于单一分割符的情况:多分割符同时应用的时候,可使用re.split() >>> line = 'asdf fjdk ...

  3. Python3 From Zero——{最初的意识:000~Initial consciousness}

    http://www.liaoxuefeng.com/wiki/0014316089557264a6b348958f449949df42a6d3a2e542c000 a.编码 默认情况下,Python ...

  4. Python3 From Zero——{最初的意识:007~函数}

    一.编写可接受任意数量参数的函数:*.** >>> def test(x, *args, y, **kwargs): ... pass ... >>> test(1 ...

  5. Python3 From Zero——{最初的意识:005~文件和I/O}

    一.输出重定向到文件 >>> with open('/home/f/py_script/passwd', 'rt+') as f1: ... print('Hello Dog!', ...

  6. Python3 From Zero——{最初的意识:004~迭代器和生成器}

    一.反向迭代:reversed() >>> a [1, 2, 3, 4] >>> for x in reversed(a): ... print(x, end=' ...

  7. Python3 From Zero——{最初的意识:003~数字、日期、时间}

    一.对数值进行取整:round(value,ndigits) >>> round(15.5,-1) #可以取负数 20.0 >>> round(15.5,0) #当 ...

  8. Python3 From Zero——{最初的意识:001~数据结构和算法}

    一.从队列两端高效插入.删除元素,及保留固定数量的数据条目: collections.deque([iterable[,maxlen=N]]) a = collections.deque([1, 2] ...

  9. python3.4学习笔记(十三) 网络爬虫实例代码,使用pyspider抓取多牛投资吧里面的文章信息,抓取政府网新闻内容

    python3.4学习笔记(十三) 网络爬虫实例代码,使用pyspider抓取多牛投资吧里面的文章信息PySpider:一个国人编写的强大的网络爬虫系统并带有强大的WebUI,采用Python语言编写 ...

随机推荐

  1. 随机生成一串字符串(java)

    该随笔为开发笔记 今天在公司接手了一个项目,在看该项目老代码时,发现上一位大佬写的随机取一串字符串还不错,在此做一次开发笔记 在他的基础上我做了点改动,但是原理一样 /** * 随机取一段字符串方法1 ...

  2. DLL中使用字符串时的注意事项。

    library dll1; uses SysUtils, Classes; {$R *.res} function TESTDLL:string;stdcall; begin Result:='tes ...

  3. NX二次开发-使用MFC对话框不能用UF_UI_select等函数解决方法

    VC/MFC调用UG Dialog要进入加锁状态 加锁 UF_UI_lock_ug_access ( UF_UI_FROM_CUSTOM ); 此处为UF_UI_select的函数 解锁 UF_UI_ ...

  4. [转] bae中thinkphp的REWRITE 正确配置方法

    URL_MODEL =2下. 官方的:app.conf不能用,害人呀.. 留意以下红色部分,正则要分开来写,坑爹的 正确的配置: handlers: handlers: - expire : .jpg ...

  5. 9.3.1 The assign and deassign procedural statements

    IEEE Std 1364™-2001, IEEE Standard Verilog® Hardware Description Language The assign procedural cont ...

  6. HDU 6697 Closest Pair of Segments (计算几何 暴力)

    2019 杭电多校 10 1007 题目链接:HDU 6697 比赛链接:2019 Multi-University Training Contest 10 Problem Description T ...

  7. Spring 学习笔记 Resource 资源

    Spring Resources 概述 在日常程序开发中,处理外部资源是很繁琐的事情,我们可能需要处理 URL 资源.File 资源.ClassPath相关资源等等.并且在 java 中 Java . ...

  8. 20140604 word表格中打钩 循环右移

    1.如在在word表格中打钩 符号->其他符号->字体(wingdings2) 2.循环右移 方法1: #include<stdio.h> void move(char *s) ...

  9. python:Django 简介。

    Django是基Python的重要WEB框架. 1.安装Django Web框架 安装  pip 安装 在命令行模式 执行  [pip install django == 2.0]或者 [pip in ...

  10. APP接口测试和功能测试点