推荐一个超好用的python代码练习网址,省去你安装环境的烦恼(补充一点,这个环境不能导入包,如果想在网页上练习导入包的程序,可以去官网首页,代码示例的右上角有个图标,点进去可以在线练习,不过缺点是速度有点慢):
进入http://pythontutor.com/ ,点页面中央偏上的Live Programming
环境类似下图:(左边写代码,可以一步一步执行(下边),右边是运行时参数状态)
再推荐一个简单python代码100例,有兴趣的同学多练练。
http://www.cnblogs.com/CheeseZH/archive/2012/11/05/2755107.html
基本语法在练习的时候记得最快!下面开始练习吧!
#输入三个整数,将其按从小到大输出。
x,y,z=(int(i)for i in input().split(' '))
print(min(x,y,z),end=' ')
print(x+y+z-max(x,y,z)-min(x,y,z),end=' ')
print(max(x,y,z),end='\n')
#输入几个空格隔开的整数,输出其和。
a=(int(i)for i in input().split(' '))
print(sum(a))
#将输入的若干整数添加到数组中
data=[3,4,2,2]
i=(int(i) for i in input().split(' '))
for j in i:
data.append(j)
print(data)
#输出9*9乘法表
for i in range(1,10):
for j in range(i,10):
print (i,'*',j,'=',i*j,end=' ')
print (' ')
#素数判断
from math import sqrt
def sushu(n):
flag=1
k=int(sqrt(n))
for i in range(2,k+1):
if n%i==0:
flag=0
break
return flag
#输出100以内的素数
data=[]
for i in range(2,101):
data.append(i)
for j in data:
print (j,end=' ')
i=j
while i*j<50:
if(i*j in data):
data.remove(i*j)
i=i+1
print(' ')
print(data)
print(len(data))
首先,帖两个python官网首页代码示例,熟悉一下python3的特性: https://www.python.org
# Python 3: Simple arithmetic
>>> 1 / 2
0.5
>>> 2 ** 3
8
>>> 17 / 3 # classic division returns a float
5.666666666666667
>>> 17 // 3 # floor division
5
# Python 3: Fibonacci series up to n
>>> def fib(n):
>>> a, b = 0, 1
>>> while a < n:
>>> print(a, end=' ')
>>> a, b = b, a+b
>>> print()
>>> fib(1000)
0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987
# Python 3: List comprehensions
>>> fruits = ['Banana', 'Apple', 'Lime']
>>> loud_fruits = [fruit.upper() for fruit in fruits]
>>> print(loud_fruits)
['BANANA', 'APPLE', 'LIME']
# List and the enumerate function
>>> list(enumerate(fruits))
[(0, 'Banana'), (1, 'Apple'), (2, 'Lime')]
# Python 3: Simple output (with Unicode)
>>> print("Hello, I'm Python!")
Hello, I'm Python!
# Input, assignment
>>> name = input('What is your name?\n')
>>> print('Hi, %s.' % name)
What is your name?
Python
Hi, Python.
# For loop on a list
>>> numbers = [2, 4, 6, 8]
>>> product = 1
>>> for number in numbers:
... product = product * number
...
>>> print('The product is:', product)
The product is: 384