튜토리얼에 따라python3.3 배우기
5407 단어 python3
'''
# =============================================================================
# FileName: test.py
# Desc:
# Author: Anduo
# Email: [email protected]
# HomePage: http://anduo.iteye.com
# Version: 0.0.1
# LastChange: 2013-05-15 20:47:32
# History: python version is 3.3
# =============================================================================
'''
radius = 10
pi = 3.14
area = pi*radius**2
print("the area is", area)
# 3.1.2 Strings examples
'spam eggs' # print 'spam eggs'
'doesn\'t' # print "doesn't"
' "Yes," he said .' # print ' "Yes," he said . '
hello = "This is a rather long string containing
\
several lines of text just as you would do in C.
\
Note that whitespace at the beginning of the line is\
significant."
print(hello)
word = 'Hello' + 'A'
print(word) # result is 'HelpA'
'<' + word * 5 + '>'
# '<HelpAHelpAHelpAHelpAHelpA>'
'str' 'ing'
# 'string'
'str'.strip() + 'ing'
# 'string'
# +---+---+---+---+---+
# | H | e | l | p | A |
# +---+---+---+---+---+
# 0 1 2 3 4 5
#-5 -4 -3 -2 -1
# 3.1.4 lists
a = ['spam', 'eggs', 100, 1234]
# 3.2 first steps towards programming
a, b = 0, 1
while b < 10:
print(b, end=',')
a, b = b, a+b
# 4.1 if statements
x = int(input('Please enter an integer:'))
if x < 0:
x = 0
print('Negative changed to zero')
elif x == 0:
print('zero')
elif x == 1:
print('Single')
else:
print('More')
#end
# 4.2 for statements
words = ['cat','window','defenestrate']
for w in words:
print(w, len(w))
for w in words[:]: # Loop over a slice copy of the entire list.
if len(w) > 6:
words.insert(0,w)
# 4.3 The range() Function
for i in range(5):
print(i)
range(5, 10)
#5 through 9
range(0, 10, 3)
#0, 3, 6, 9
range(-10, -100, -30)
#-10, -40, -70
a = ['Mary','had', 'a', 'little', 'lamb']
for i in range(len(a)):
print(i,a[i])
# 4.4 break and continue Statements,and else Clauses on Loops
for n in range(2, 10):
for x in range(2,n):
if n % x == 0:
print(n,'equals',x,'*',n//x)
break
else:
# loop fell through without finding a factor
print(n,'is a prime number')
fom num in range(2, 10):
if num % 2 == 0:
print("Found an even number", num)
continue
print("Found a number", num)
# 4.5 pass statements
while True:
pass # Busy-wait for keyboard interrupt (Ctrl + C)
# This is commonly used for creating minimal classes:
class MyEmptyClass:
pass
def initlog(*args):
pass # Remember to implement this!
# 4.6 Defining Function
def fib(n): # write Fibonacci series up to n
"""Print a Fibonacci series up to n."""
a, b = 0, 1
while a < n:
print(a, end=' ')
a, b = b, a + b
print()
fib(2000)
def fib2(n):# return Fibonacci series up to n
"""result a list containing the Fibonacci series up to n."""
result = []
a, b = 0, 1
while a < n:
result.append(a)
a, b = b, a+b
return result
# 4.7
def ask_ok(prompt, retries=4, complaint='Yes or no, please!'):
while True:
ok = input(prompt)
if ok in ('y', 'ye', 'yes'):
return True
if ok in ('n', 'no', 'nop', 'nope'):
return False
retries = retries - 1
if retries < 0:
raise IOError('refusenik user')
print(complaint)
def concat(*args, sep="/"):
return sep.join(args)
4.7.5 Lambda Forms
def make_incrementor(n):
return lambda x: x + n
#--------------------------------#
# #
# by Anduo 2013-05-13 #
#--------------------------------#
# 【tutorial.pdf】 ,
#python , 。
# python , , .
# Fibonacci 。 。
def fib(n): # write Fibonacci series up to n
"""Print a Fibonacci series up to n."""
a, b = 0, 1
while a < n:
print(a, end=' ')
a, b = b, a + b
print()
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Jupyter 공식 DockerHub에 대한 메모에 기재되어 있다. base-notebook minimal-notebook scipy-notebook tensorflow-notebook datascience-notebook pyspark-notebook all-s...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.