Python 3 학습 노트 의 기초 강좌

참고 사이트:http://www.w3cschool.cc/python3/python3-tutorial.html
__author__ = 'Administrator'

import subprocess
cmd="cmd.exe"
begin=101
end=200
while begin<end:
    p=subprocess.Popen(cmd,shell=True,stdout=subprocess.PIPE,
                       stdin=subprocess.PIPE,stderr=subprocess.PIPE)
    p.stdin.write("ping www.baidu.com
".encode()) p.stdin.close() p.wait() print ('execution result:%s'%p.stdout.read())
__author__ = 'Administrator'
#ide  PyCharm  Python3    
#  Python   
import keyword
print(keyword.kwlist)
#         
a,b,c,d=20,5.5,True,4+3j
print(type(a),type(b),type(c),type(d))
#   
s = 'Yes,he doesn\'t'
print(s,type(s),len(s))
#        
print('str'+'ing', 'my'*3)
#Python   2     
word = 'Python'
print(word[0],word[5])
print(word[-1],word[-6])
#     
word = 'ilovepython'
print(word[1:5])
print(word[:])
print(word[-10:-6])

#List(  )   
#          、           。             :
a = ['him', 25, 100, 'her']
print(a)
#list  ,  ,  ,  
a = [1, 2, 3, 4, 5]
print(a[2])
print(a + [6, 7, 8])
a[0]=22
print(a)
#  
print(a[2:5])
#  
a[2:5]=[]
print(a)
#  (tuple)     ,               。        
a = (1991, 2014, 'physics', 'math')
print(a, type(a), len(a))
#  (set)            。
student = {'Tom', 'Jim', 'Mary', 'Tom', 'Jack', 'Rose'}
print(student)   #           
print('Rose' in student)
#    
a = set('abracadabra')
b = set('alacazam')
print(a)
print(a - b)# a b   
print(a | b) # a b   
print( a & b) # a b   
#http://www.w3cschool.cc/python3/python3-data-type.html

#         (mapping type),         :     。            ,       ,          。
dic = {}  #      
tel = {'Jack':1557, 'Tom':1320, 'Rose':1886}
print(tel)
print(tel['Jack']) #      :  key  
del tel['Rose']  #        
tel['Mary'] = 4127  #        
print(tel)
print(list(tel.keys()))  #     key   list
print(sorted(tel.keys())) #  key  
print('Tom' in tel)      #     
print('Mary' not in tel) #     


__author__ = 'yunshouhu'
import base64
'''
      
      
'''
"""
       
       
"""
#   
print(5 ** 2)  # 5    
print(2 ** 7)  # 2 7  
print(2 ** 10)
print(2 ** 20)
#       
a,b=0,1
while b<10:
    print(str(b),end=',')
    a,b=b,a+b
#base64     
print("")
s="     nihao"
a=base64.b64encode(s.encode(encoding="utf-8"))
print(a.decode())
print(base64.b64decode(a).decode())

age=int(input("age of the dog:"))
print()
if age<0:
    print("this can hardly be true!")
elif age==1:
    print("about 14 humen years")
elif age==2:
    print("about 22 human years")
elif age>2:
    human=22+(age-2)*5
    print("human years:",human)
else:
    print("hehe")
#input("press return ")
#while  
n = 100
sum = 0
counter = 1
while counter <= n:
    sum = sum + counter
    counter += 1
print("Sum of 1 until %d: %d" % (n,sum))

#for  
languages = ["C", "C++", "Perl", "Python"]
for x in languages:
    print(x)
#break
edibles = ["ham", "spam","eggs","nuts"]
for food in edibles:
    if food == "spam":
        print("No more spam please!")
        break
    print("Great, delicious " + food)
else:
    print("I am so glad: No spam!")
print("Finally, I finished stuffing myself")
#  
for i in range(0, 10) :
	print(i)
for i in range(0, 10, 3) :
	print(i)
#pass       
pass  #        (Ctrl+C)
__author__ = 'yunshouhu'

#  
def hellword():
    print("helloworld ok!")
hellword()
def area(width,height):
    return width*height;
def print_welcome(name):
    print("Welcome ",name)
w=4
h=5
print("width=",w," height=",h," area=",area(w,h))
#         
a = 4  #     
def print_func1():
    a = 17 #     
    print("in print_func a = ", a)
def print_func2():
    print("in print_func a = ", a)
print_func1()
print_func2()
print("a = ", a)
#      
def parrot(voltage, state='a stiff', action='voom', type='Norwegian Blue'):
    print("-- This parrot wouldn't", action, end=' ')
    print("if you put", voltage, "volts through it.")
    print("-- Lovely plumage, the", type)
    print("-- It's", state, "!")
parrot(1000)                                          # 1 positional argument
parrot(voltage=1000)                                  # 1 keyword argument
parrot(voltage=1000000, action='VOOOOOM')             # 2 keyword arguments
parrot(action='VOOOOOM', voltage=1000000)             # 2 keyword arguments
parrot('a million', 'bereft of life', 'jump')         # 3 positional arguments
parrot('a thousand', state='pushing up the daisies')  # 1 positional, 1 keyword

#       
def arithmetic_mean(*args):
    sum = 0
    for x in args:
        sum += x
    return sum
print(arithmetic_mean(45,32,89,78))
print(arithmetic_mean(8989.8,78787.78,3453,78778.73))
print(arithmetic_mean(45,32))
print(arithmetic_mean(45))
print(arithmetic_mean())
#         
from collections import deque
queue = deque(["Eric", "John", "Michael"])
queue.append("Terry")           # Terry arrives
queue.append("Graham")          # Graham arrives
print(queue.popleft())                 # The first to arrive now leaves
print(queue.popleft())                # The second to arrive now leaves
print(queue)                          # Remaining queue in order of arrival
#        
knights = {'gallahad': 'the pure', 'robin': 'the brave'}
for k, v in knights.items():
    print(k, v)

좋은 웹페이지 즐겨찾기