Python基础知识学习笔记
学习的是python3.0往后的版本
打印hello world,单引号/双引号都可以,但是必须有括号
print('hello world')
print("hello world")
Python有5种数据类型
+加法运算
有些浮点数数在计算机中是无法精确表示的,只能近似表示,因此用浮点数表示,会有精度损失,例如
算术运算符
例子
引入模块
实际操作一下
关系运算符
逻辑运算符
判断2019年是不是闰年
(2019%4==0 and 2019%100!=0) or (2019%400==0)
运算符的优先级
增量运算符
标识符规则
输入 input
input函数
功能:读取键盘输入,将所有输入作为字符串看待
语法:input([prompt]), [prompt]是提示符,最好提供
举例,可以看到,变量a是以字符串的形式存储在内存中的
a=input("in:")
输出print
这个开头就知道了,现在考虑如何把多个变量输出到一行呢, 用逗号 ,
看个例子
选择分支结构
score=68
gender="lady"
if score>=60:
if gender=='lady':
print("yes")
else:
print('no')
多分支结构
score=68
if score>=90:
print('A')
else:
if score>=80:
print('B')
else:
if score>=70:
print('C')
else:
if score>=60:
print('D')
else:
print('E')
其实python是有elif的,上面程序可以这样写,省的缩进会弄错
score=68
if score>=90:
print('A')
elif score>=80:
print('B')
elif score>=70:
print('C')
elif score>=60:
print('D')
else:
print('E')
看了例子,判断篮球比分是否安全
points=int(input('Leading points:'))
have_ball=input('the leading team has ball:(yes/no)')
seconds=int(input('the remaining seconds:'))
points-=3
if have_ball=='yes':
points+=0.5
else:
points-=0.5
if points<0:
points=0
points**=2
if points>seconds:
print('safe')
else:
print('not safe')
程序运行结果:
循环结构
while循环
c=0
while c<5:
print('hello')
c+=1
for循环
求e的值
e=1
factorial=1
for i in range(1,100):
factorial*=i
e+=1.0/factorial
print(e)
求pi的值
pi=0
sign=1
divisor=1
for i in range(1,10000000):
pi+=sign/divisor
sign*=-1
divisor+=2
pi*=4
print('pi is:',pi)
n=27
for n in range(1,10):
while n!=1:
if n%2==0:
n/=2
else:
n=n*3+1
print(n)
求平方根
x=2
low=0.0
high=x
guess=(low+high)/2
while abs(guess**2-x)>1e-4:
if guess**2>x:
high=guess
else:
low=guess
guess=(low+high)/2
print(guess)
判断素数
import math
n=10
for i in range(2,int(math.sqrt(n))+1):
if n%i==0:
print('the number is not a prime')
break
else:
print('the number is a prime')
打印前50个素数
import math
n=2
count=0
while count<50:
for i in range(2,int(math.sqrt(n))+1):
if n%i==0:
break
else:
print(n)
count+=1
n+=1
判断回文数
num=12321
num_p=0
num_t=num
while num!=0:
num_p=num_p*10+num%10
num=num/10
if num_t==num_p:
print('ok')
else:
print('no')
函数
def sum(start,stop):
sum=0
for i in range(start,stop+1):
sum+=i
return sum
运行结果
global
x=1
def fun():
global x
x=2
fun()
print(x)
同时判断回文数和素数
num=121
def is_palin(num):
num_p=0
num_t=num
while num!=0:
num_p=num_p*10+num%10
num=num/10
if num_t==num_p:
return True
else:
return False
def is_prime(num):
for i in range(2,int(math.sqrt(n))+1):
if n%i==0:
return False
else:
return True
if is_palin(num) and is_prime(num):
print('ok')
else:
print('no')
打印月份日历
def is_leap_year(year):
if year%4==0 and year%100!=0 or year%400==0:
return True
else:
return False
def get_num_of_days_in_month(year,month):
if month in (1,3,5,7,10,12):
return 31
elif month in(4,6,9,11):
return 30
elif is_leap_year(year):
return 29
else:
return 28
def get_total_num_of_day(year,month):
days=0
for y in range(1800,year):
if is_leap_year(y):
days+=366
else:
days+=365
for m in range(1,month):
days+=get_num_of_days_in_month(m)
return days
def get_start_day(year,month):
return (3+get_total_num_of_day(year,month))%7
def get_month_english_name(month):
if month==1:
return 'January'
elif month==2:
return 'February'
elif month==3:
return 'March'
elif month==4:
return 'April'
elif month==5:
return 'May'
elif month==6:
return 'June'
elif month==7:
return 'July'
elif month==8:
return 'August'
elif month==9:
return 'September'
elif month==10:
return 'October'
elif month==11:
return 'November'
else:
return 'December'
year=2019
month=1
start_weekday=get_start_day(year,month)
print("%20s" %get_month_english_name(month),end=" ")
print("%10d" %year)
print("---------------------------------------------")
print(" Sun Mon Tue Wed Thu Fri Sat ")
for i in range(0,start_weekday):
print(" ",end="")
round=start_weekday
days_of_this_month=get_num_of_days_in_month(year,month)
for j in range(1,days_of_this_month+1):
print(" %3d" %j,end="")
round+=1
if round%7==0:
print("\n")
print("\n")
程序运行结果:
递归函数
阶乘
def p(n):
if n==0 or n==1:
return 1
else:
return n*p(n-1)
print(p(3))
斐波那契数列
def fib(n):
if n==1 or n==2:
return 1
else:
return fib(n-1)+fib(n-1)
print(fib(5))
汉诺塔
def hanoi(n,A,B,C):
if n==1:
print('Move',n,'from',A,'to',C)
else:
hanoi(n-1,A,C,B)
print('Move',n,'from',A,'to',C)
hanoi(n-1,B,A,C)
hanoi(4,'Lef','Mid','Right')