1.装Python
2.装Jupyter Notebook
3.学习Python 100例
Python 100例:https://www.runoob.com/python/python-100-examples.html
所给100例子为Python2 ,因此在Python3中有所改动,其中常用如下:
- print“ ”——>print(“ ”)
- if x=7——>if x==7
- from Tkinter import *——>from tkinter import
print ("dub dance, world!")
# In[2]:
print ("hello bishies!")
# In[3]:
x=6
y=7
print (x+y)
# In[6]:
###
for i in range(1,5):
for j in range(1,5):
for k in range(1,5):
if( i != k ) and (i != j) and (j != k):
print (i,j,k)
# In[7]:
##ep3
for i in range(1,85):
if 168 % i == 0:
j = 168 / i;
if i > j and (i + j) % 2 == 0 and (i - j) % 2 == 0 :
m = (i + j) / 2
n = (i - j) / 2
x = n * n - 100
print(x)
# In[8]:
##ep5
l = []
for i in range(3):
x = int(input('integer:\n'))
l.append(x)
l.sort()
print (l)
# In[9]:
##ex 7
a = [1, 2, 3]
b = a[:]
print (b)
# In[10]:
##ex14
def reduceNum(n):
print('{} ='.format(n))
if not isinstance(n, int) or n <= 0 :
print ('请输入一个正确的数字 !')
exit(0)
elif n in [1] :
print ('{}'.format(n))
while n not in [1] : # 循环保证递归
for index in range(2, n + 1) :
if n % index == 0:
n=n // index # n 等于 n/index
if n == 1:
print (index )
else : # index 一定是素数
print( '{} *'.format(index)),
break
reduceNum(90)
# In[11]:
##22
for i in range(ord('x'),ord('z') + 1):
for j in range(ord('x'),ord('z') + 1):
if i != j:
for k in range(ord('x'),ord('z') + 1):
if (i != k) and (j != k):
if (i != ord('x')) and (k != ord('x')) and (k != ord('z')):
print( 'order is a -- %s\t b -- %s\tc--%s' % (chr(i),chr(j),chr(k)))
# In[12]:
##27
def output(s,l):
if l==0:
return
print (s[l-1])
output(s,l-1)
s = input('Input a string:')
l = len(s)
output(s,l)
# In[13]:
##30
a = int(input("请输入一个数字:\n"))
x = str(a)
flag = False
for i in range(len(x)//2):
if x[i] == x[-i - 1]:
flag = True
break
if flag:
print ("%d 是一个回文数!" % a)
else:
print ("%d 不是一个回文数!" % a)
# In[14]:
##38
if __name__ == '__main__':
a = []
sum = 0.0
for i in range(3):
a.append([])
for j in range(3):
a[i].append(float(input("input num:\n")))
for i in range(3):
sum += a[i][i]
print (sum
)
# In[16]:
##50
import random
#生成 10 到 20 之间的随机数
print (random.uniform(10, 20))
# In[25]:
##52
if __name__ == '__main__':
a = 77
b = a |3
print( 'a | b is %d' % b)
b |= 7
print ('a | b is %d' % b)
# In[27]:
##55
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)
# In[33]:
##57
if __name__ == '__main__':
from tkinter import *
canvas = Canvas(width=300, height=300, bg='black')
canvas.pack(expand=YES, fill=BOTH)
x0 = 263
y0 = 263
y1 = 275
x1 = 275
for i in range(4):
canvas.create_line(x0,y0,x0,y1, width=1, fill='white')
x0 = x0 - 5
y0 = y0 - 5
x1 = x1 + 5
y1 = y1 + 5
x0 = 263
y1 = 275
y0 = 263
for i in range(2):
canvas.create_line(x0,y0,x0,y1,fill = 'red')
x0 += 5
y0 += 5
y1 += 5
mainloop()
# In[35]:
##59
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()
# In[40]:
##62
Str = 'abcdefg'
Str2 = 'cde'
print (Str.find(Str2))
# In[42]:
##77
if __name__ == '__main__':
s = ["man","woman","girl","boy","sister"]
for i in range(len(s)):
print (s[i])
# In[44]:
##78
if __name__ == '__main__':
person = {"li":18,"wang":50,"zhang":20,"sun":52}
m = 'li'
for key in person.keys():
if person[m] < person[key]:
m = key
print ('%s,%d' % (m,person[m]))
# In[45]:
##87
if __name__ == '__main__':
class student:
x = 0
c = 0
def f(stu):
stu.x = 20
stu.c = 'c'
a= student()
a.x = 3
a.c = 'a'
f(a)
print (a.x,a.c)
# In[47]:
##95
from dateutil import parser
dt = parser.parse("Aug 28 2015 12:00AM")
print (dt)
# In[50]:
#96
if __name__ == '__main__':
str1 = input('请输入一个字符串:\n')
str2 = input('请输入一个子字符串:\n')
ncount = str1.count(str2)
print (ncount)
# In[52]:
#98
if __name__ == '__main__':
fp = open('test.txt','w')
string = input('please input a string:\n')
string = string.upper()
fp.write(string)
fp = open('test.txt','r')
print (fp.read())
fp.close()
# In[53]:
#100
i = ['a', 'b']
l = [1, 2]
print (dict([i,l]))
4.写Python小程序
1.确定质数
#!/usr/bin/env python
# coding: utf-8
# In[23]:
def reduceNum(n):
mmm=0
print('{} =\n1*'.format(n))
if not isinstance(n, int) or n <= 0 :
print ('请输入一个正确的数字 !')
exit(0)
elif n in [1] :
print( '{}'.format(index)),
while n not in [1] : # 循环保证递归
for index in range(2, n+1 ) :
if n % index == 0:
n=n // index # n 等于 n/index
if n == 1:
print ( index )
else : # index 一定是素数
mmm=mmm+1;
print( '{}*'.format(index)),
break
if mmm==0:
print( "是素数"),
else:
print( "不是素数"),
a = int(input("请输入一个数字:"))
reduceNum(a)
2.查找计数文本中某字母出现个数。
if __name__ == '__main__':
fp = open('test.txt','w')
string = input('please input a string:\n')
string = string.upper()
fp.write(string)
fp = open('test.txt','r')
print (fp.read())
fp.close()
注意:所打开文件需要和py文件在同一文件夹下;