データ分析入門_PART 1 python基礎構文_CH 05条件及びループ文

27697 ワード

'''
【  5.1】      :if  

Python                   (True  False)         。

if     :
        ……
else:
        ……
    
**         

  3 : if else    :if: elif: else:1 :   2    3and or     (a and b)or(c and d)
'''
'
【 5.2】 :if

Python (True False) 。

if :
……
else:
……

**

:if: elif: else: 1 : 2
'
# if       1  () 2:  
#3    
# if         ,          
# Python       :               

#       
age = 12 
if age<18:
    print('18  ')
18  
#    input()
#         ,input()           ,
#    string  ,          int float   

score=input('     ')
print(type(score))
print('     '+score)
     88

     88
#       :if-else
flag=False #  python False True       
name=input('     ')
if name=='boss':
    flag=True
    print('welcome boss')
else:
    print(name+'not authorized')
     boss
welcome boss
#       :if-elif-...-else
num = int(input('    '))
if num == 3:
    print('boss')
elif num == 2:
    print('user')
elif num == 1:
    print('worker')
else: 
    print('not authorized')
    3
boss
#         :or  and  not 

#       0~10  
#     : hello
num = int(input('    '))
if(num<10 and num>0):
    print('hello')

#         0   10
#     : undefine
num = int(input('    '))
if(num>10 or num<0):
    print('undifined')

#       0~5  10~15  
#           
#     : undefine
num = int(input('    '))
if(num>0 and num<5)and(num>10 and num<15):
    print('hello')
else:
    print('undifined')
    5
hello
    5
    5
undifined
'''
   
①              :      ,           60 ,     ,  60 ,      
②            :      ,        、    、        
'''
#①
score=int(input('please input your score'))
if (score>=60):
    print('pass')
else:
    print('fail')
please input your score66
pass
#②
numstr=input('please input your num')
if (numstr.isnumeric()):
    num=int(numstr)
    if(num==66):
        print('yes')
    elif(num!=66):
        print('no')
else:
    print('worong input')
please input your num66
yes
'''
【  5.2】      :for  

for             ,            。

  

  :        for          
     for            for       or       
                 i
eg
for i in range(5):
  range(5)=【0,1,2,3,4】
for   【】
i      0,1,2,3,4

  4 :1for   ,          i 2for  ; 3for    4    +()
 3  if  
'''

'
【 5.3】 :for

for , 。



: for
for for or
i
eg
for i in range(5):
range(5)=【0,1,2,3,4】
for 【】
i 0,1,2,3,4

4 :1for , i 2for ()3for ; 4for
if
'
#    "hello world"5    ?
for i in range(5):
    print('hello world')
hello world
hello world
hello world
hello world
hello world
#   for    、  

#   list
lst=list(range(10))
for i in lst :
    print(i)

#     ,    ,          or  ,     dic    dic.keys dic.values or  
age={'tom':44,'jim':56,'sam':99}
for name in age.keys():
    print('age equals',age.get(name))
0
1
2
3
4
5
6
7
8
9
age equals 44
age equals 56
age equals 99
#      ,  :      3   

#     
for i in range(2):
    for j in range(3):
        print(i,j)
0 0
0 1
0 2
1 0
1 1
1 2
"""
   
①         , for         
②  for         ,       
③       ,     key value
④  input        n,  hello world n 
⑤        ,    :  a,  n,  d,  s,         input()  
⑥     ["a", "b", "c"],[1,2,3], for           ,    
"""
#①②③④  

#⑤
n=int(input('input n'))
a=int(input('input a'))
d=int(input('input d'))
s=0
for i in range(n):
    num=a+i*d
    s+=num
print('result is ',s)
input n5
input a5
input d5
result is  75
#⑥
#     dic                     dict(listofcouplist)
a=["a","b","c"]
b=[1,2,3]
lst=[]
for i in range(len(a)):
    lst.append([a[i],b[i]])
dic=dict(lst)
print (dic)

{'a': 1, 'b': 2, 'c': 3}
'''
【  5.3】      :while  

               

            ,    、   (null)    true。

      false ,    。

  4  1              ,  break continue 2   3   4    +()
    if for  
'''

'
【 5.4】 :while



, 、 (null) true。

false , 。

4 1 2 3 4 , break continue
'
#       
count=0
while count<9:
    print(count)
    count=count+1
print('bye')
#       :            true,           
#       true,         
#          !!
var = 1
while var == 1 :  
    num = input("Enter a number  :")
    print( "You entered: ", num)
print( "Good bye!")

# while-else  
#    if-else     
count = 0
while count < 5:
    print(count, " is  less than 5")
    count = count + 1
else:
    print(count, " is not less than 5")

'''
【  5.4】        

break:             ,        

continue:               ,     continue         ,           

pass:pass    ,   ,                           

'''
# break  
#break          
s=0
n=1
while n>0:
    s=s+n
    n=n+1
    if n==20:
        break
print(s)

#    ,break        
for i in range(5):
    for j in range(5):
        if j>2:
            break
        print(i,j)
190
0 0
0 1
0 2
1 0
1 1
1 2
2 0
2 1
2 2
3 0
3 1
3 2
4 0
4 1
4 2
# continue  
# continue       Python  !!!         !!!,           。
s=0
for i in range(50):
    if i%2==0:
        continue
    print(i)
1
3
5
7
9
11
13
15
17
19
21
23
25
27
29
31
33
35
37
39
41
43
45
47
49
# pass      
#        
def func1():
    pass