学习所用的视频和文档
工具
Nodepad++
使用之前需要设置格式
设置->首选项->语言->勾选替换为空格
python安装包
官网,直接搜索安装文档
centos7安装python
语法
输入输出
1.input–输入,output–输出
x=3
x=x*x #square value of x
print(x)
y=float(input("Enter a number"))
print(y*y)
2.输出结果在一行
print "Hi",
print "there"
3.range用法
1)
range(stop)
range(5)
[0,1,2,3,4]
2)
range(start, stop)
range(2,5)
[2,3,4]
3)
range(start, stop, stepSize)
range(2,10,2)
[2,4,6,8]
range返回类型
(1)
type(range(5))
运行结果
class 'range'
(2)
type(list(range(4)))
运行结果
class 'List'
4.Sum
The function sum is a built-in function that takes one list as an argument and returns the sum of that list. The items in the list must be numeric types.
条件判断
代码1
x=int(input("Enter an integer:"))
if x%2 == 0:
print("")
print('Even')
else:
print(" ")
print("ODD")
print("Done with conditional")
注意:缩进
代码2
x=6
if x%2 ==0:
if x%3 ==0:
print("Divisible by 2 and 3")
else:
print("Divisible by 2 and not by 3")
elif x%3 ==0:
print("Division by 3 but not by 2")
注:elif=else if
简单算法
迭代
x=3
ans=0
itersLeft=x
while(itersLeft !=0):
ans=ans+x
itersLeft=itersLeft-1;
print(str(x)+'*'+str(x)+'='+str(ans))
for LOOP
for...in ...
Guess and Check- - -Finding a cube root of an integer
using “while”
x=int(input("Enter an integer:"))
ans=0
while ans**3<x:
ans=ans+1
if ans**3!=x :
print(str(x)+'is not a perfect cube')
else:
print("Cube root of"+str(x)+" is "+str(ans))
using “for”
x=int(input("Enter an integer:"))
for ans in range(0,abs(x)+1):
if ans**3==abs(x):
break
if(ans**3!=abs(x)):
print(str(x)+" is not a perfect cube")
else:
if x<0:
ans=-ans
print("Cube root of "+str(x)+" is "+str(ans))
浮点数的表示
二进制表示
将一个整数变为二进制数
# lecture 3.4, slide 3
num = 302
if num < 0:
isNeg = True
num = abs(num)
else:
isNeg = False
result = ''
if num == 0:
result = '0'
while num > 0:
result = str(num%2) + result
num = num/2
if isNeg:
result = '-' + result
print(result)
将一个浮点数变为二进制数
求0.375的二进制表示
步骤:
1)0.375*2^3=3
2)3的二进制数为11
3)用2^3=8来除以3
4)获得小数点和2的幂次数之间的关系
代码如下 :
# lecture 3.4, slide 4
#获取输入的浮点数
x = float(raw_input('Enter a decimal number between 0 and 1: '))
p = 0
#判定某个数是否为整数,即x*2^p是否为整数,循环结束直至为整数
while ((2**p)*x)%1 != 0:
print('Remainder = ' + str((2**p)*x - int((2**p)*x)))
p += 1
num = int(x*(2**p))
#将num这个整数用二进制数表示出来
result = ''
if num == 0:
result = '0'
while num > 0:
result = str(num%2) + result
num = num/2
#将小数点加进最后的结果中
for i in range(p - len(result)):
result = '0' + result
result = result[0:-p] + '.' + result[-p:]
print('The binary representation of the decimal ' + str(x) + ' is ' + str(result))
注意点
1.如果某个浮点数与任意的2的幂次方相乘,都不能为整数,那么PYTHON中表示出的该浮点数为一个无限接近的数,并不是该浮点数本身。
2.在测试两个浮点数是否相同是,用下面代码
abs(x-y)<0.0001,
而非
x==y
- 3.
print(0.1)
显示结果为 0.1
原因:Python的设计者进行了automaMcally round
Guess Number的方法
Approximation Methods
应用于:GUASS NUMBER中
CODE:
# lecture 3.5, slide 2
x = 25
#essilon是精确度
epsilon = 0.01
#step是步长
step = epsilon**2
numGuesses = 0
ans = 0.0
#两个循环条件必须同时满足 1.数字^2与25相差大于精确度 2.数字不大于步长
while (abs(ans**2 - x)) >= epsilon and ans <= x:
ans += step
numGuesses += 1
print('numGuesses = ' + str(numGuesses))
if abs(ans**2-x) >= epsilon:
print('Failed on square root of ' + str(x))
else:
print(str(ans) + ' is close to the square root of ' + str(x))
Bisection Search
应用于:GUASS NUMBER中
CODE:
# lecture 3.6, slide 2
# bisection search for square root
x = 12345
epsilon = 0.01
numGuesses = 0
low = 0.0
high = x
ans = (high + low)/2.0
while abs(ans**2 - x) >= epsilon:
print('low = ' + str(low) + ' high = ' + str(high) + ' ans = ' + str(ans))
numGuesses += 1
if ans**2 < x:
low = ans
else:
high = ans
ans = (high + low)/2.0
print('numGuesses = ' + str(numGuesses))
print(str(ans) + ' is close to square root of ' + str(x))
Newton-Raphson Root Finding
应用于:GUASS NUMBER中
- 原理
- CODE:
# Lecture 3.7, slide 3
# Newton-Raphson for square root
epsilon = 0.01
y = 24.0
guess = y/2.0
while abs(guess*guess - y) >= epsilon:
guess = guess - (((guess**2) - y)/(2*guess))
print(guess)
print('Square root of ' + str(y) + ' is about ' + str(guess))