Python基礎学習-2時間で手に入るPython
暇な時にPythonを勉強します.全部で2時間もかかって、Pythonの基本文法を無から有まで習得しました.ついでに泡立ち、速列、二分検索と書きました.注釈が詳しくて、注釈ですぐにPythonを覚えることができます.
コードディスプレイ
以下のコードはPythonの一部の基礎文法を紹介している.例えば、コードブロックフォーマット、演算法則、if else forなどの基本演算、三目演算、注釈、参照、配列などの基本文法である.
コードは次のことをしました
コードクリップを参照
# _*_ coding: utf8 _*_
# python hello world
# Java pyhon。
# def 。 :
"""
python {}.
import
"""
import random
import math
def helloWorld() :
# print ,
print "helloWorld",
print "helloWorld"
#
arr = ["Today", "is", "a", "good", "day"]
# for , Java
for i in arr:
print i,
# .
print "-" * 100
# , 。
print bubble(arr)
# python ,
# ,
arr = range(8)
for i in xrange(0,len(arr)):
# phthon ,
# 0~10000
arr[i] = math.ceil(random.random() * 10000)
print arr
find = 1
arr[0] = find
search_index = binary_search(quick(arr, 0, len(arr) - 1), find)
print search_index
# python
str = ((search_index != -1) and ("Find it at %d" % (search_index)) or ("Didn`t find"))
print str
print "*insert_sort*" * 10
arr = range(10)
for i in xrange(0,len(arr)):
arr[i] = math.ceil(random.random() * 10000)
print arr
print insert_sort(arr)
print "*binary_insert_sort*" * 8
arr = range(10)
for i in xrange(0,len(arr)):
arr[i] = math.ceil(random.random() * 10000)
print arr
print binary_insert_sort(arr)
#
def bubble(arr):
size = len(arr)
for x in xrange(0, size):
for y in range(0, size):
if arr[x] > arr[y]:
tp = arr[x]
arr[x] = arr[y]
arr[y] = tp
return arr
#
def quick(arr, start, end):
if start >= end:
return
# index ,
index = quick_partition(arr, start, end)
#
quick(arr, start, index - 1)
#
quick(arr, index + 1, end)
return arr
#
'''
:
, end、 start、 end - 1······
, , , start >= end。 end 。
,
'''
i = 1
def quick_partition(arr, start, end):
key = arr[start]
#
global i
j = 1
while start < end:
# , 。
# end ,end , 。
while arr[end] >= key and end > start:
end = end - 1
arr[start] = arr[end]
# , 。
# start ,start , 。
while arr[start] <= key and end > start:
start = start + 1
arr[end] = arr[start]
# python print C
print " %d , %d , start=%d: end=%d, key=%d, :%s" % (i, j, start, end, key, arr)
j = j + 1
# , start>=end , 。
arr[end] = key
i = i + 1
return end
#
def binary_search(arr, search):
# 。
left = 0
right = len(arr) - 1
while left <= right:
middle = (left + right) / 2
print middle
if arr[middle] > search:
right = middle - 1
elif arr[middle] < search:
left = middle + 1
else :
return middle
#
def insert_sort(arr) :
if len(arr) < 2:
return arr
##
for i in xrange(1, len(arr)):
search = arr[i]
pos = i
j = i - 1
while j >= 0:
if arr[j + 1] < arr[j]:
arr[j + 1] = arr[j]
pos -= 1
else:
break
# python j--
arr[pos] = search
j -= 1
else:
# python for break else
print "Jump out!"
return arr
#
def binary_insert_sort(arr) :
if len(arr) < 2:
return arr
for i in xrange(1, len(arr)):
search = arr[i]
start = 0
end = i - 1
while start <= end:
middle = (start + end) / 2
if arr[middle] >= search:
end = middle - 1
else:
start = middle + 1
# python for 。
j = i - 1
while j > end:
arr[j + 1] = arr[j]
j -= 1
arr[end + 1] = search
return arr
if __name__ == '__main__':
helloWorld()
出力結果は一定ではなく、毎回異なります.出力を参照:
helloWorld helloWorld
Today is a good day ----------------------------------------------------------------------------------------------------
['is', 'good', 'day', 'a', 'Today']
[7340.0, 8957.0, 9774.0, 1592.0, 7897.0, 9892.0, 8787.0, 2144.0]
1 , 1 , start=0: end=0, key=1, :[1, 8957.0, 9774.0, 1592.0, 7897.0, 9892.0, 8787.0, 2144.0]
2 , 1 , start=2: end=7, key=8957, :[1, 2144.0, 9774.0, 1592.0, 7897.0, 9892.0, 8787.0, 9774.0]
2 , 2 , start=5: end=6, key=8957, :[1, 2144.0, 8787.0, 1592.0, 7897.0, 9892.0, 9892.0, 9774.0]
2 , 3 , start=5: end=5, key=8957, :[1, 2144.0, 8787.0, 1592.0, 7897.0, 9892.0, 9892.0, 9774.0]
3 , 1 , start=2: end=3, key=2144, :[1, 1592.0, 8787.0, 8787.0, 7897.0, 8957.0, 9892.0, 9774.0]
3 , 2 , start=2: end=2, key=2144, :[1, 1592.0, 8787.0, 8787.0, 7897.0, 8957.0, 9892.0, 9774.0]
4 , 1 , start=4: end=4, key=8787, :[1, 1592.0, 2144.0, 7897.0, 7897.0, 8957.0, 9892.0, 9774.0]
5 , 1 , start=7: end=7, key=9892, :[1, 1592.0, 2144.0, 7897.0, 8787.0, 8957.0, 9774.0, 9774.0]
3
1
0
0
Find it at 0
*insert_sort**insert_sort**insert_sort**insert_sort**insert_sort**insert_sort**insert_sort**insert_sort**insert_sort**insert_sort*
[7347.0, 448.0, 5447.0, 3529.0, 8954.0, 4314.0, 6423.0, 2680.0, 3495.0, 8583.0]
Jump out!
[448.0, 2680.0, 3495.0, 3529.0, 4314.0, 5447.0, 6423.0, 7347.0, 8583.0, 8954.0]
*binary_insert_sort**binary_insert_sort**binary_insert_sort**binary_insert_sort**binary_insert_sort**binary_insert_sort**binary_insert_sort**binary_insert_sort*
[8702.0, 7143.0, 710.0, 2101.0, 549.0, 726.0, 1398.0, 6767.0, 7489.0, 5456.0]
[549.0, 710.0, 726.0, 1398.0, 2101.0, 5456.0, 6767.0, 7143.0, 7489.0, 8702.0]