Python試験_3回目

59211 ワード

-
python   11    
      :(70 )
1.         ?         (2 )
2.    tuple、list、dict   ,       (3 )
3.     (generator)      ,        generator(3 )
4.    lambda  /   (2 )
5.a=10
  b=20
  def test(a,b):
     print(a,b)
  c = test(b,a)
  print(c)
       ,      a,b,c     ?   ?(4 )
 
6.     @property      ,          (4 )
 
7.d={'k1':'v1','k2':[1,2,3],('k','3'):{1,2,3}}(4 )
        :
  1)       value    key(2 )
  2)      key     ,      value 。(2 )
  3)d[('k','3')]   value         (1 )
8.     @wrapper   ,  a()        ,       (2 )
def wrapper(func):
    def inner(*arg, **kwargs):
        func(*arg, **kwargs)
    return inner
 
@wrapper
def a(arg):
    print(arg)
 
a()
9.     7th_questions,     'T'    (5 )
10.          ,          (8 )
 
11   10           ,      (5 )
 
12.     ,           (5 )
def f1():
    print('funcname is f1')
 
def f2():
    print('funcname is f2')
    return 1
 
def f3(func1):
    ll = func1()
    print('funcname is f3')
    return ll
 
print(f3(f2))
 
13.                ?(2 )
 
14.         2017/10/01 18:08:15   (3"2017-11-18 17:43:43"         
 
15.               ?(1)
                ?(2)
 
16     Python static method(    ) class method(   )(2)
 
 
17.     __new__   __init__          (218.      :1、2、3、4,                    ?    ?(   )(5)
 
19.     test2  ,                   ,          if,else    ,            
  :
        url>>>:login
         ----20.           (  random)   
 
 
      (30 )
 
1.    、  、   、         (2 )
2.            ?(3 )
3.      :(5 )
    class Person:
        def __init__(self,name,age):
            self.name = name
            self.age = age
 
    1)   10      (22)   age    name(34.   cs  (15 )
    1)             ,      (10 )
 
                  police
                    ,   ,  ,  
                   ,        police
 
 
                  terrorist
                    ,   ,  ,  
                   ,        terrorist
 
    2)       ,    ,      ,    (2 )
 
    3)                    ,             (3 )
 
5    (10 )
 
5(1class Base:
    def f1(self):
        self.f2()
 
    def f2(self):
        print('...')
 
class Foo(Base):
    def f2(self):
        print('9999')
 
obj = Foo()
obj.f1()
 
  1:      self    ?(2 )
  2:         (3 )
 
 
5(2class JustCounter:
   __secretCount = 0
 
   def count(self):
       self.__secretCount += 1
       print(self.__secretCount)
 
class Bars(JustCounter):
 
    def count(self):
        print(self.__secretCount)
 
 
counter1 = JustCounter()
counter2 = Bars()
 
counter1.count()
counter2.count()
print (counter1.__secretCount)
 
  1:  counter1.count()    ?(2 )
  2:         (3 )
 
 
 
 
     (20 ):
         init    :
    class Person:
        def __init__(self,name,age,sex,weight):
            self.name = name
            self.sex = sex
            self.age = age
            self.weight = weight
       100 person   ,
          obj1,obj2 name sex    
     obj1.name==obj2.name and obj1.sex==obj2.sex
                  ,        100   ,  100       。
      :
          Person         

7th_questionsファイルの内容は次のとおりです.
One year like any old other year
In a week like any week
Monday lying down, half asleep
People doing what people do
Loving,working,and
getting through no portraits on the walls Of Seventh Avenue
Then Tuesday came and went
Like a helicopter overhead
The letter that she left, cold addressed in red
Tuesday came and went one
One September
When will she come again
The thing about memories
They're sure and bound to fade
Except for the stolen souls Left upon her blade
Is Monday coming back That's what Mondays do
They Turn and Turn around
Afraid to see it through
Then Tuesday came and went
Like a helicopter overhead
The letter that she left, cold addressed in red
Tuesday came and went one
One September
When will she come again
Tuesday came and went one
One September, when?
Cold and dressed in red
How could I forget
Tuesday came and went
Like a helicopter overhead
Will she come again?

test2.pyファイルの内容は次のとおりです.
#! /usr/bin/env python
# -*- coding: utf-8 -*-
# __author__ = "Meet"
# Date: 2018/4/26
 
def login():
    print("")
 
 
def logout():
    print("")
 
 
def home():
    print("")
 
 
def article():
    print("        ")
 
 
def editor():
    print("")
 
 
def add():
    print("")
 
 
def delete():
    print("")
 
 
def admin():
    print("")

 
答え
python   11    
      :(70 )
1.         ?         (2 )
 
    
       
 
r,    
w,    
a,       
 
 b  
       
 
rb     
wb     
ab       
 
 
2.    tuple、list、dict   ,       (3 )
 
tuple  index,len,count  ,    
    tuple    ,   
list      ,  li = [],
    li.append('name')
    li.pop['name']
    li['name'] = 'eva'
    print(li['name'])
    list    ,      
 
dict       ,  dic = {}
    dic['name'] = 'alex'
    dic.pop('name')
    dic['name'] = eva
    print(dic['name'])
    dict    ,key    ,key     
 
 
3.     (generator)      ,        generator(3 )
 
   :          
      yield          ,       
     __next__    ,       
 
   generator  ,      ,         。  return     
 
generator  
def func():
    print(1)
    yield
 
4.    lambda  /   (2 )
 
lambda                        。
       ,           
  :
    = lambda   :   
 
       ,     
      ,                
            
 
  :
lambda x:x*2
        
def func(x):
    return x*2
 
5.a=10
  b=20
  def test(a,b):
     print(a,b)
  c = test(b,a)
  print(c)
       ,      a,b,c     ?   ?(4 )
 
    a, b, c    20,10,None
            test(b,a)     a,b   20,10
          ,a,b   20,10,   20,10
          return,  c None
         20,10,None
 
 
6.     @property      ,          (4 )
 
property        ,           ,           
 
  :
class A():
    def __init__(self,name):
        self.name = name
 
    @property
    def age(self):
        return 20
 
a = A('alex')
print(a.name,a.age)  #  name age
#    alex 20
 
7.d={'k1':'v1','k2':[1,2,3],('k','3'):{1,2,3}}(4 )
        :
  1)       value    key(2 )
 
d={'k1':'v1','k2':[1,2,3],('k','3'):{1,2,3}}
for i in d:
    if type(d[i]) == list:
        print(i)
 
  2)      key     ,      value 。(2 )
 
d={'k1':'v1','k2':[1,2,3],('k','3'):{1,2,3}}
for i in d:
    if type(i) == tuple:
        print(d[i])
 
  3)d[('k','3')]   value         (1 )
 
d={'k1':'v1','k2':[1,2,3],('k','3'):{1,2,3}}
print(type(d[('k','3')]))
       
 
8.     @wrapper   ,  a()        ,       (2 )
def wrapper(func):
    def inner(*arg, **kwargs):
        func(*arg, **kwargs)
    return inner
 
@wrapper
def a(arg):
    print(arg)
 
a()
 
    :
def wrapper(func):
    def inner(*arg, **kwargs):
        func(*arg, **kwargs)
    return inner
#@wrapper
def a(arg):
    print(arg)
 
a = wrapper(a)
a('1')
 
 
9.     7th_questions,     'T'    (5 )
 
with open('7th_questions',encoding='utf-8') as f:
    for i in f:
        i = i.strip()
        if i.startswith('T'):
            print(i)
 
10.          ,          (8 )
 
11   10           ,      (5 )
capitalize()       ,      
upper()      
lower()      
center()    
title()           (     )
startswith()         
endswith()       
strip()        ,   (
),tab (4 \t ) lstrip() 、 、tab rstrip() 、 、tab
12. , (5 ) def f1(): print('funcname is f1') def f2(): print('funcname is f2') return 1 def f3(func1): ll = func1() print('funcname is f3') return ll print(f3(f2)) : funcname is f2 funcname is f3 1 1. print(f3(f2)), f3 , f2 2. ll = func1(), func1() f2。 f2 , 'funcname is f2', return 1, 1 ll 3. 'funcname is f3', return ll, ll 1, 1 print(f3(f2)) 4. 1 13. ?(2 ) 1. 2. , 14. 2017/10/01 18:08:15 (3"2017-11-18" 17:43:43" import time str1 = '2017-10-01 18:08:15' p = time.strptime(str1,'%Y-%m-%d %H:%M:%S') #print(p) b = time.strftime('%Y/%m/%d %H:%M:%S',p) print(b) # 2017/10/01 18:08:15 # import time str2 = "2017-11-18 17:43:43" p = time.strptime(str2,'%Y-%m-%d %H:%M:%S') print(p) : time.struct_time(tm_year=2017, tm_mon=11, tm_mday=18, tm_hour=17, tm_min=43, tm_sec=43, tm_wday=5, tm_yday=322, tm_isdst=-1) 15. ?(1) ?(2) os os.path.getsize : import os f = '7th_questions' print(os.path.getsize(f)) 16 Python static method( ) classmethod( )(2) staticmethod( ) 。 self classmethod( ) , 17. __new__ __init__ (2__new__ __init__ __new__ __init__ 。 __new__ ,__init__ 18. :1、2、3、4, ? ?( )(5) 24 n_list = [] for a in range(1,5): for b in range(1,5): for c in range(1,5): if a != b and b != c and c != a : number = '{}{}{}'.format(a,b,c) n_list.append(number) print(number) n_count = len(set(n_list)) print(n_count) # 123 124 132 134 142 143 213 214 231 234 241 243 312 314 321 324 341 342 412 413 421 423 431 432 19. test2 , , if,else , : url>>>:login ---- ! , : import test2 import sys #test2 url = input('url>>>:').strip() if hasattr(sys.modules['test2'],url): getattr(sys.modules['test2'],url)() # 20. ( random) # import random def content(ss): ss = int(ss) print(' :{}'.format(ss)) count = 0 while True: suiji = random.randint(1, int(ss)) count += 1 print(' {} {}'.format(count, suiji)) ss -= suiji if ss == 0: print(' !'.format(ss)) break else: pass content(10) (30 ) 1. 、 、 、 (2 ) : : , , 2. ?(3 ) , , 3. :(5 ) class Person: def __init__(self,name,age): self.name = name self.age = age 1) 10 (2class Person: def __init__(self, name, age): self.name = name self.age = age li = [] # for i in range(1, 11): li.append(Person('eva' + str(i), i)) # 10 2) age name(3class Person: def __init__(self, name, age): self.name = name self.age = age li = [] # for i in range(1, 11): li.append(Person('eva' + str(i), i)) # 10 p_list = [] for i in li: # print(i.__dict__) p_list.append(i.__dict__) a = sorted(p_list, reverse=True, key=lambda x: x['age']) print(a[0]['name']) # age name # eva10 4. cs (15 ) 1) , (10 ) police , , , , police terrorist , , , , terrorist class police(object): def __init__(self,name,hp,weapon,sex): self.name = name self.hp = hp self.weapon = weapon self.sex = sex self.role = 'police' def attack(self,p): if p.role == 'police': print(' ! ') class terrorist(object): def __init__(self,name,hp,weapon,sex): self.name = name self.hp = hp self.weapon = weapon self.sex = sex self.role = 'terrorist' def attack(self,p): if p.role == 'terrorist': print(' ! ') tom = police('tom',10,'gun','M') tom2 = police('tom2',10,'gun','M') jack = terrorist('jack',10,'gun','M') jack2 = terrorist('jack2',10,'gun','M') tom.attack(tom2) # jack.attack(jack2) # 2) , , , (2 ) class police(object): def __init__(self,name,hp,weapon,sex): self.name = name self.hp = hp self.weapon = weapon self.sex = sex self.role = 'police' def attack(self,p): if p.role == 'police': print(' ! ') else: print('{} {},{} !'.format(self.name,p.name,p.name)) class terrorist(object): def __init__(self,name,hp,weapon,sex): self.name = name self.hp = hp self.weapon = weapon self.sex = sex self.role = 'terrorist' def attack(self,p): if p.role == 'terrorist': print(' ! ') else: print('{} {},{} !'.format(self.name, p.name,p.name)) tom = police('tom',20,'gun','M') jack = terrorist('jack',10,'gun','M') tom.attack(jack) # : tom jack,jack ! 3) , (3 ) class Person(object): # def __init__(self,name,hp,weapon,sex): self.name = name self.hp = hp self.weapon = weapon self.sex = sex self.role = None def attack(self, p): if self.role == p.role: print(' ! ') else: print('{} {},{} !'.format(self.name,p.name,p.name)) class police(Person): # def __init__(self,name,hp,weapon,sex): super().__init__(name,hp,weapon,sex) self.role = 'police' class terrorist(Person): # def __init__(self,name,hp,weapon,sex): super().__init__(name, hp, weapon, sex) self.role = 'terrorist' tom = police('tom',20,'gun','M') tom2 = police('tom2',20,'gun','M') jack = terrorist('jack',10,'gun','M') tom.attack(jack) # tom.attack(tom2) # 5 (10 ) 5(1class Base: def f1(self): self.f2() def f2(self): print('...') class Foo(Base): def f2(self): print('9999') obj = Foo() obj.f1() 1: self ?(2 ) self 2: (3 ) :9999 : Foo, f1 。 f1 , Base 。 f1 ,def f1(self): self Foo 。 self.f2() self Foo , Foo f2 。 , 9999 5(2class JustCounter: __secretCount = 0 def count(self): self.__secretCount += 1 print(self.__secretCount) class Bars(JustCounter): def count(self): print(self.__secretCount) counter1 = JustCounter() counter2 = Bars() counter1.count() counter2.count() print (counter1.__secretCount) 1: counter1.count() ?(2 ) JustCounter() Bars(), counter1 count() count , __secretCount 1, , , print, 1 2: (3 ) : counter1.count(), , 1 counter2.count() , count , count self , __secretCount, Bars JustCounter , , 。 , 。 , 。 , (20 ): init : class Person: def __init__(self,name,age,sex,weight): self.name = name self.sex = sex self.age = age self.weight = weight 100 person , obj1,obj2 name sex obj1.name==obj2.name and obj1.sex==obj2.sex , 100 , 100 。 : Person class Person: def __init__(self,name,age,sex,weight): self.name = name self.sex = sex self.age = age self.weight = weight def __hash__(self): # return hash(self.name + self.sex) # name sex hash, 2 name sex ,age def __eq__(self, other): # if self.name == other.name and self.sex == other.sex: # name sex return True p_list = [] # # 98 #count= 0 for i in range(1,99): #count += 1 p_list.append(Person('eva' + str(i),i,'M',17.5)) #print('eva' + str(i),i,'M','17.5') # print(count) # exit() # 2 ,name sex ,age p_list.append(Person('eva49',99,'M',17.5)) #print('eva'+'99',99,'M',17.5) p_list.append(Person('eva50',100,'M',17.5)) # p_lst1 #print(len(p_list)) # , p_lst1 print(len(set(p_list))) # 98

 
転載先:https://www.cnblogs.com/dongye95/p/10473381.html