第一章:程序设计基本方法
- python是解释型语言。
- python语言特点:语法简洁、跨平台、可扩展、开源、支持中文等。
11
(n+1)<<1 是(n+1)*2
if abs(pos())<1 如果当前位置是程序最开始的位置(就说明画完了,首尾相接了),就结束。
第二章:Python程序实例解析
上述两种写法,效果相同,不建议第二钟,个人感觉可读性差。
注意,返回的变量的值是 字符串
-
-
-
-
重点
看出来了吧,本章以上所有的讲解都是为这个例子做铺垫,比如:input()、while循环、if判断、eval()、print()格式化输出 所以重点会这个
9. 10.
这些代码大同小异,一个会了就差不多了
import turtle
turtle.setup(800, 800, 400, 400)
turtle.pencolor("black")
turtle.pensize(10)
turtle.pendown()
turtle.fd(150)
turtle.penup()
turtle.fd(30)
turtle.seth(90)
turtle.fd(30)
turtle.pendown()
turtle.fd(150)
turtle.penup()
turtle.fd(30)
turtle.seth(-180)
turtle.fd(30)
turtle.pendown()
turtle.fd(150)
turtle.penup()
turtle.fd(30)
turtle.seth(-90)
turtle.fd(30)
turtle.pendown()
turtle.fd(150)
“”“
其他需要了解的函数
turtle.circle(radius,extent=None)
例子 turtle.circle(16,180)
绘制一个半径为16像素,角度为180度的弧形
“””
第三章:基本数据类型
-
效果等价于 -
-
-
-
z.real() 提取实部 z.imag()提取虚部
- 重点
12. 重点
nowE=80
for i in range(1,11):
print("我在第%d年重%.2fkg,在月球为%.2fkg" %(i,nowE ,nowE*0.165))
nowE+=0.5
import time
scale=10
print("{0:-^14}".format("执行开始"))
for i in range(scale+1):
a,b='**' * i , '..' * (scale-i)
print("Start[{}->{}]Down!".format(a,b))
time.sleep(0.1)
print("{0:-^14}".format("执行结束"))
第四章:程序的控制结构
- 重点
from random import randint
try:
num = int(input("请输入一个1-99数字"))
except:
print("输入的内容必须为整数!")
num = int(input("请输入一个1-99数字"))
n = 1
guess=randint(1,99)
print(guess)
while (num != guess):
if num > guess:
print("遗憾,太大了")
elif num < guess:
print("遗憾,太小了")
n = n + 1
num = int(input("请输入一个0-99数字"))
print("预测了{}次".format(n))
str = input("请输入一串字符")
num = 0
word = 0
char = 0
for item in str:
i = ord(item)
if ord('9') >= i >= ord('0'):
num = num + 1
elif (ord('a') <= i <= ord('z')) | (ord('A') <= i <= ord('Z')):
word = word + 1
else:
char = char + 1
print("数字有{}个".format(num))
print("单词有{}个".format(word))
print("字符有{}个".format(char))
num1 = int(input("请输入一个数字"))
num2 = int(input("请再输入一个数字"))
greatest_common_divisor = 0
if num1 > num2:
greatest_common_divisor = num2
else:
greatest_common_divisor = num1
while (not (num2 % greatest_common_divisor == 0 and num1 % greatest_common_divisor == 0)):
greatest_common_divisor = greatest_common_divisor - 1
print("{}和{}的最大公约数是:{}".format(num1, num2, greatest_common_divisor))
least_common_multiple = 0
if num1 > num2:
least_common_multiple = num2
else:
least_common_multiple = num1
while (not (least_common_multiple % num1 == 0 and least_common_multiple % num2 == 0)):
least_common_multiple = least_common_multiple + 1
print("{}和{}的最小公倍数是:{}".format(num1, num2, least_common_multiple))
import random as random
total=1000000
win1=0
win2=0
for i in range(total):
man=random.randint(1,3)
car=random.randint(1,3)
if man==car:
win1+=1
else:
win2+=1
print("在{}次实验中:".format(total))
print("若不更改门,获胜的概率为{:.3}%.".format(win1/total*100))
print("若更改门,获胜的概率为{:.3}%.".format(win2/total*100))
第五章:函数和代码的复用
- 重点
def isOdd(num):
if(num%2==1):
return True
return False
print(isOdd(1))
print(isOdd(2))
print(isOdd(3))
print(isOdd(4))
def isNum(str):
if(type(str) == int or type(str) == float or type(str) == complex ):
return True
return False
print(isNum(1))
print(isNum(1.5))
print(isNum(complex(1,5)))
print(isNum("strs"))
def multi(*num):
result=1
for i in num:
result*=i
return result
print(multi(1,2,3,4,5))
- 重点!!!
def isPrime(num):
for i in range(num):
try:
if(i!=1 and num%i==0 and i!=num):
return False
except:
continue
return True
print(isPrime(5))
print(isPrime(9))
第六章:组合数据类型
-
元组 -
集合 -
字典,联想java里的map -
列表 -
重点
import random
items = ""
for i in range(65, 91):
items += chr(i)
items += chr(i + 32)
for i in range(48, 58):
items += chr(i)
for i in range(10):
for j in range(8):
print(random.choice(items),end="")
print("\n")
def isRepeat(items):
for i in range(len(items)):
for j in range(len(items)):
if items[i] == items[j] and i != j:
return True
return False
items = ["7", "65", "7", "32", "9"]
print(isRepeat(items))
items2 = ["17", "65", "7", "32", "9"]
print(isRepeat(items2))
def isRepeat(items):
if len(set(items))!=len(items):
return True
return False
items = ["7", "65", "7", "32", "9"]
print(isRepeat(items))
items2 = ["17", "65", "7", "32", "9"]
print(isRepeat(items2))
str = input("请输入一串字母:")
counts = {}
print(str)
for word in str:
counts[word] = counts.get(word, 0) + 1
items = list(counts.items())
items.sort(key=lambda x: x[1], reverse=True)
for i in items:
print(i)
import jieba
excludes = {"什么", "只是"}
txt = open("红楼梦.txt", "r", encoding="utf-8").read()
words = jieba.lcut(txt)
counts = {}
for word in words:
if len(word) == 1:
continue
else:
counts[word] = counts.get(word, 0) + 1
for word in excludes:
del (counts[word])
items = list(counts.items())
items.sort(key=lambda x: x[1], reverse=True)
for i in range(15):
word, count = items[i]
print("{0:<10}{1:>5}".format(word, count))
第七章:文件和数据格式化
第八章:程序设计方法论
第九章:科学计算和可视化
无
第十章:网络爬虫和自动化
总结
愿诸君都过。
|