首页 > 编程语言 > 详细

Python 1.1数字与字符基础

时间:2019-02-18 20:11:02      阅读:269      评论:0      收藏:0      [点我收藏+]

# 基础数字操作

# -*- coding:UTF-8 -*-

"""  min() max() sum() abs() len() 
     math.pi math.e, math.sin math.sqrt   math.pow()     
"""  
import math, random
a = 1
b = 2
c = ( a + b ) * a - b**2
d =  c**2 + math.pow(a, b)
f = float(b)
print( a, b, c, c % b,  d / c, d // c , f) # /真除 //截断除法

print( random.choice( [1, 2, 3, 4, 5] ), random.random(), random.randint(1, 10) )

""" 数字格式进制
    十进制 八进制 十六进制 二进制 
     int(), oct(), hex(), bin()    
"""
figs = [ 64, 0o11, 0x11, 0b11]
print(figs)

# eval -> input str transform into digit字符串转数字
print( eval: , eval(64), eval(0b111), eval(0x140) )
# int -> str turn to num on base基于进制进行字符串数字转换
print( int:  , int(64), int(100, 8), int(40, 16) )

# 格式化输出‘%o, %x, %b‘ % (64, 64, 64)
print( format printf 64-> ,{0:o}, {1:b}, {2:x}.format(64, 64, 64) )
digit = int( input("input a digit:") )
print( "{} in different literal:".format( digit, oct(digit),          hex(digit),  bin(digit) ) )
         
  
# b_len(x) == x.bit_length()  二进制位数
def b_len( num ):
    return len( bin(num) ) - 2

""" from decimal import Decimal
    deciaml.getcontext().prec = 4 precision
    dNum = Decimal(‘0.1‘) + Decimal(‘1.3‘)
    temparory prec set
"""
# deciaml 小数
importm deciaml
with decimal.localcontext() as ctx:
    ctx.prec = 2
    dnum = deciaml.Decimal(2.123) / deciaml.Decimal(7) 
    
# fraction 分数
from fraction import Fraction
a_frac = Fraction(1, 10) + Fraction(1, 10) - Fraction(2, 10)
a_frac = Fraction(1, 3) + Fraction( 5, 14)
print(a_fracf)

def singleNumber(nums):
    """ find single number 
    type   nums: List[int]
    retype     : int
    """ 
    numSet = set(nums)
    for num in numSet:
        if nums.count(num) == 1:
            return num
    else:
        print("We can‘t found it!")
            
print( singleNumber([4,1,2,1,2]) )

# Python 基础字符

1. 字符串 " " and ‘ ‘ and """ """     转义字符 \n  \t  \‘  \\   原始字符串 raw_str-> r"\now" "\\n"
2. s.find(‘name‘)   s.rstrip()移除空格   s.replace("me","you")    s,split(‘,‘)分隔
3. s.isdigit()   s.isspace()   s.islower   s.endswith(‘spam‘)   s.upper()
4.   for x in s: print(x)迭代   ‘spam‘ in s     [c for c in s]    map( ord ,s )映射

s1="your" 
s2=" name" 
s = s1 + s2  
print( s1 * 2, s1[1] ,s[:] , s[0:-`] ,s[::-1])

# 字符串常量不可修改,不可变类型 
title = "spam" 
print("lens:", len(s) ) 
# 新建字符对象,headline变量指向此对象
headline = "knight" + " glory " + "sword" 
print("Meaning " "of" "programming")

home = "this\‘ is our new home\n"
# 格式化字符->"%s meets %s" % (s1, s2)  "{0} and {1}".format(s1, s2)
hotspot = "%s have no idea how %s i was." % ( "you", "worried" )
hitbox = "That is %d %s bird!" % ( 1, dead )
textline = "{name} wants to eat {food} \n".format( name="Bob", food="lasagna")
print( home , textline, hotspot)

test = random.choice( [apple, pear, banana, orange] )
test += str(123) # str(object)--> string
for animal in ["dog", "cat", "piglet"]:
    print( "%s is a mammal" % animal )
    
alpha = []
c = a  # chr() ord()->trans char into int [ASCII]
for i in range(26):
    alpha.append(  chr( ord(c)+i ) )
print(alpha)

 

Python 1.1数字与字符基础

原文:https://www.cnblogs.com/justLittleStar/p/10397327.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!