Python基础学习代码之数字

# import math
# print coerce(1L, 134)
# """数据类型转换"""
# print coerce(0.4, 123)
# print coerce(0j, 234)
# print divmod(125, 5)
# """除法运算"""
# print divmod(4, 3)
# print pow(2, 4)
# """密"""
# print round(3)
# print round(3.666666, 3)
# print round(3.045, 1)
# print round(3.7777)
# """浮点型四舍五入"""
# for i in range(5):
    # print round(math.pi, i)
    
    
"""
for eachnum in (.2, 0.7, 1.2, 1.7, -.2, -0.7, -1.2, -1.7):
    print "int(%0.1f)\t+%0.1f" % (eachnum, int(eachnum))
    print "floor(%0.1f)\t+%0.1f" % (eachnum, math.floor(eachnum))
    print "round(%0.1f)\t+%0.1f" % (eachnum, round(eachnum))
    print "-" * 20
    """

def func1(num1, num2):
    return num1 * num2

import sys

def func2():
    num = int(raw_input(‘input your number:‘))
    if (num >= 90) and (num <= 100):
        return "A:90~100"
    elif (num >= 80) and (num <= 89):
        return "B:80~89"
    elif (num >= 70) and (num <= 79):
        return "C:70~79"
    elif (num >= 60) and (num <= 69):
        return "D:60~69"
    elif 0 <= num < 60:
        return "F:0~60"
    else:
        sys.exit()

def func3():
    year = int(raw_input(‘input your year:‘))
    if (year % 4 == 0) and (year % 100 != 0) or (year % 4 == 0) and (year % 100 == 0):
        return ‘%d yun year‘ % year
    else:
        return ‘%d is not yun year‘ % year
        sys.exit()

def func4():
    result = []
    number = int(raw_input(‘input your number:‘))
    meifen = [25, 10, 5, 1]
    for i in meifen:
        result.append(number/i)
        number = number % i

    return "25 cert is %d,10 cert is %d,5 cert is %d,1 cert is %d"            % (result[0], result[1], result[2], result[3])

def func5():
    oper = ["*", "-", "+", "/", "%", "**"]
    exp = raw_input(‘input exp:‘)
    for op in oper:
        if exp.find(op) > -1 and exp.count(op) < 2:
            numbers = exp.split(op)
            lists = []
            for i in numbers:
                lists.append(i)
            n1 = int(lists[0])
            i = op
            n2 = int(lists[1])
    if i == "*":
        return n1 * n2
    elif i == "-":
        return n1 - n2
    elif i == "+":
        return n1 * n2
    elif i == "/":
        return n1 / n2
    elif i == "%":
        return n1 % n2
    elif i == "**":
        return n1 ** n2

def func6():
    num1 = int(raw_input(‘input:‘))
    num2 = int(raw_input(‘input:‘))
    if num1 % num2 == 0:
        return True
    else:
        return False

def func7():
    data = raw_input(‘input time:‘)
    data = data.split(":")
    hours = int(data[0])
    total = hours * 60 + int(data[1])
    return total

import random

def func8():
    rand = random.randint(1, 100)
    lists = []
    for i in range(rand):
        brand = random.randint(0, (pow(2, 31) - 1))
        lists.append(brand)
    lists.sort()
    return lists
print(func8())
时间: 2024-08-10 08:03:58

Python基础学习代码之数字的相关文章

Python基础学习代码之执行环境

class C(object):     def __call__(self, *args, **kwargs):         print "I'm callable! called with args:\n",args c = C() c('a',1) single_code = compile("print 'hello,world!'",'','single') exec(single_code) eval_code = compile('100*3','

Python基础学习代码之变量和类型

foo = 'abc' for i in range(len(foo)):     print "%d , %s" % (i, foo[i]) print [x ** 2 for x in range(5) if not x % 2] try:     f = open('e:\XIN_370_logic.tx', 'r')     for eachline in f:         print eachline     f.close() except IOError, e:   

Python基础学习代码之序列

str1 = 'abced' for i in range(-1, -len(str1), -1) + [None]:     print str1[:i] s, t = 'abc', 'def' print zip(s, t) for i, t in enumerate(str1):     print i, t print isinstance('foo', str) import string def checkid():     alphas = string.letters + '_'

Python基础学习代码之映像集合

def func1():     dict1 = {}     dict2 = {'name':'earth','port':80}     return dict1,dict2 def func2():     return dict((['x',1],['y',2])) def func3():     adict = {}.fromkeys(['x','y'],23)     return adict def func4():     alist = {'name':'earth','po

Python基础学习代码之条件和循环

def func1():     alist = ['Cathy','Terry','Joe','Health','Lucy']     for i in  range(-1,-len(alist)-1,-1):         print i,alist[i] def func2():     alist = ['Cathy','Terry','Joe','Health','Lucy']     for i,name in enumerate(alist):         print '%d

Python基础学习代码之文件和输入输出

import os ls = os.linesep def func1():     filename = raw_input('enter file name:')     f = open(filename,'w')     while True:         alline = raw_input("enter a line ('.' to quit):")         if alline != '.':             f.write("%s%s&quo

Python基础学习代码之错误和异常

def func1():     try:         return float('abc')     except ValueError,e:         print e def func2():     try:         astr = 'abc'         float(astr)     except ValueError:         astr = None     return astr def func3():     try:         astr = 

Python基础学习代码之函数和函数式编程

def func1():     print 'hello world' res = func1() print type(res) def func2():     return ['xyz',10000,-98] atuple = func2() x,y,z = func2() print x,y,z def func3():     return 'xyz',1000,-98 x,y,z = func3() print x,y,z def func4():     return ['xyz

Python基础学习代码之面向对象编程

class  AddrBookEntry(object):     'address book entry class'     def __init__(self,nm,ph):         self.name = nm         self.phone = ph         print 'created instance for:',self.name     def updatephone(self,newph):         self.phone = newph