Python練習-number数字とstr文字列

19693 ワード

一、number数字
1.ブールおよびNULL
2.数字の分類
3.システム機能
3.1数学機能
3.2乱数機能
二、str文字列
1.文字列の作成
2.文字列間の演算
3.文字列機能
3.1検索
3.2変換
3.3置換
3.4分割とマージ
import math
import random

"""
      :
    True 1,False 0
      if    while      
  :
             ,       ,       None
                    
"""
# 1
a = True
b = False
print(type(a))  # 
#          
print(a + 2)  # 3
print(b - 4)  # -4
print(a / 2)  # 0.5
print(b * 4)  # 0

"""
number  
"""
#   
#   :Python3.x         int,           ,     
c = 9
d = 10
e = 9
# is       , ==       【 】
print(c is d)  # False
print(c is e)  # True
print(c == d)  # False
print(c == e)  # True
#           ,      ,       
f = g = h = 15
print(f, g, h)  # 15 15 15
#           ,       
i, j = 10, 20
k, l = (30, 40)
print(i, j)  # 10 20
print(k, l)  # 30 40
# *********        ,          :***********
# 1.python     :
m = 50
n = 60
m, n = n, m
print("m =", m, ",n =", n)  # m = 60 ,n = 50
# 2.        :
temp = m
m = n
n = temp
print("m =", m, ",n =", n)  # m = 50 ,n = 60

"""
    
import math
"""
# 1.   : abs()
n1 = -89
n1 = abs(n1)
print(n1)  # 89
# 2.      : max()/min()
list1 = [9,80,90,55,3,50,777]
print(max(list1))  # 777
print(min(list1))  # 1
# 3.   :pow()
print(pow(2, 10))  # (2 10  ) 1024
# 4.        :round(m,n)     n,       
n2 = 5.563113
print(round(n2, 2))  # (          2 )  5.56
# 5.   (  )      
print(sum(list1))  # ( list1        )  1064
print(sum(list1, 50))  # ( list1       50) 1114
print(sum(list1, -50))  # ( list1       50) 1014
# 6.    
n3 = 90
n4 = 89
print(n3 > n4)  # True
print(n3 < n4)  # False
# 7.  math.ceil()             ,+1
n5 = 12.34
n6 = 13.93
print(math.ceil(n5))  # 13
print(math.ceil(n6))  # 14
# 8.  math.floor()               
print(math.floor(n5))  # 12
print(math.floor(n6))  # 13
# 9.  math.sqrt()       ,      
n7 = 9
n8 = 13
print(math.sqrt(n7))  # 3.0
print(math.sqrt(n8))  # 3.605551275463989
# 10. math.modf()                    
n9 = 12
n10 = 45.1323
print(math.modf(n9))  # (0.0, 12.0)
print(math.modf(n10))   # (0.13230000000000075, 45.0)
# 11. math.log()     
n11 = 100
print(math.log(n11))  # 4.605170185988092
print(math.log10(n11))  # 2.0
# 12.   
print(math.pi)  #    π 3.141592653589793
print(math.e)  #         2.718281828459045

"""
     
"""
# random.randint(start, end)     ,    
# random.choice(start, end, step)   ,     

"""
str   
"""
# 1.      
    # find():      ,                       ,      -1
    # rfind():      ,     -1
    # index():      ,      
    # rindex():      ,     
# 2.       
    # int():      
    # eval(): str         
    # upper():        Hello----->HELLO
    # lower():      
    # swapcase():     ,       HEllo----->heLLO
    # title():          ,     
    # capitalize():         ,     
# 3. chr()        ASC
# 4. ord()        ASC
# 5. strip():              
    # lstrip():    
    # rstrip():    
s1 = "****hello python****"
print(s1.strip("****"))  # hello python
# 6.       
# replace(old, new)
s2 = "hello java"
print(s2.replace("java", "python"))  # hello python
# maketrans()           
s3 = str.maketrans("abcde", "hello")
print(s3)  # {97: 104, 98: 101, 99: 108, 100: 108, 101: 111}
            # a-->h,b-->e,c-->l,d-->l,e-->0
# 7.    split      -->  
s4 = "a b c d"
print(str.split(s4))  # ['a', 'b', 'c', 'd']
# 8.    join     -->   
str1 = ","
s5 = ("a", "b", "c")
print(str1.join( s5 ))  # a,b,c