Exemplo n.º 1
0
# 引入模块
from abstest import my_abs
result = my_abs(111)
print(result)
Exemplo n.º 2
0
from abstest import my_abs
print(my_abs(-89))
print('hello')
Exemplo n.º 3
0
# -*- coding: utf-8 -*-
from abstest import my_abs
import math

print(abs(-10))
print(max(1, 2, 3, -1, 2, 4, 100))
print(hex(100))

# 自定义函数
# def my_abs(x):
#     if x >= 0:
#         return x
#     else:
#         return -x

print(my_abs(-100))


# 定义两个返回的函数
def move(x, y, step, angle=0):
    nx = x + step * math.cos(angle)
    ny = y - step * math.sin(angle)
    return nx, ny


# 使用函数
x, y = move(100, 100, 60, math.pi / 6)

print(x, y)

Exemplo n.º 4
0
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

# 在Python中,定义一个函数要使用def语句,依次写出函数名、括号、括号中的参数和冒号:,然后,在缩进块中编写函数体,函数的返回值用return语句返回。
# 如果没有return语句,函数执行完毕后也会返回结果,只是结果为None。
# return None可以简写为return
from abstest import my_abs
print('01.', my_abs(-100))
# import的用法在后续模块一节中会详细介绍。

# 如果想定义一个什么事也不做的空函数,可以用pass语句:


def nop():
    pass


# 实际上pass可以用来作为占位符,比如现在还没想好怎么写函数的代码,就可以先放一个pass,让代码能运行起来。

# import math语句表示导入math包,并允许后续代码引用math包里的sin、cos等函数。
import math
# 返回多个值


def move(x, y, step, angle=0):
    nx = x + step * math.cos(angle)
    ny = y - step * math.sin(angle)
    return nx, ny


# 同时获得返回值
Exemplo n.º 5
0
from abstest import my_abs
from abstest import a
from functools import reduce
print(my_abs(-23))
a()

L = list(range(100))
print(L[2:7])

d = {'a': 1, 'b': 2, 'c': 3}
for key in d:
    print(key)

for x, y in [(1, 1), (2, 4), (3, 9)]:
    print(x, y)


def f**k(x):
    return 2 * x


r = map(f**k, [1, 2, 3, 4, 5, 6, 7, 8, 9])
print(list(r))


def add(x, y):
    return x + y


print(reduce(add, [12, 23, 34]))
Exemplo n.º 6
0
import math #import math 语句表示导入 math 包,并允许后续代码引用 math包里的 sin、cos 等函数
def move(x, y, step, angle = 0):
    nx = x + step * math.cos(angle)#横坐标变换
    ny = y - step * math.sin(angle)#纵坐标变换
    return nx, ny
print('\n'*3)
print(move(2, 4, 3, angle=4))
print(move(4, 1, 2, angle=0))
x, y = move(100, 100, 60, math.pi / 6)
r = move(100, 100, 60, math.pi / 6)#Python 的函数返回多值其实就是返回一个 tuple,但写起来更方返回一个 tuple 可以省略括号
print(x, y)#返回一个 tuple 可以省略括号?
print(r)
from abstest import my_abs #用from abstest import my_abs来导入my_abs()函数
print(my_abs(-3))

Exemplo n.º 7
0
print(max(2, 3, 1, 9))
print(max(s))
print(min(s))

print(int('223'))
print(bool(8), bool(-2), bool(0))
print(str(288))

help(hex)
print(hex(11))

#   自定义函数



print('自定义的绝对值函数', my_abs(-8))
# print('报错', my_abs('3'))
nop()

#   多返回值函数返回的实质上是一个tuple, 多个变量可以同时接收一个tuple, 按位置赋给对应的值
def move(x, y, step, angle=0):
    nx = x + step * math.cos(angle)
    ny = y - step * math.sin(angle)
    return nx, ny

x, y = move(100, 100, 60, math.pi / 6)
print(x, y)
print(move(100, 100, 60, math.pi/6))


a, b = (3, 5)
Exemplo n.º 8
0
from abstest import my_abs

my_abs(-9)
Exemplo n.º 9
0
    print(1 + 1 + 3)
else:
    print('不好啊')

print(uuid.uuid1())

# hex()
# n1 = 255
# n2 = 1000

print(hex(255))


# 0xff

print(my_abs(-128))
print(power2(16))
print(power(15,2))
print(power(4,3))

# 输出2的平方根
print(math.sqrt(2))
print(math.sin(math.pi/2))

enroll('LC','LC1')

print(fact(5))

# 切片
L = ['Michael', 'Sarah', 'Tracy', 'Bob', 'Jack']
Exemplo n.º 10
0
from abstest import my_abs
n1 = 255
n2 = 1000
print(hex(n1))
print(hex(n2))

print('my_abs(10)', my_abs(10))
print('my_abs(-10)', my_abs(10))


def calc(*numbers):
    sum = 0
    for n in numbers:
        sum = sum + n * n
    return sum
print(calc())
print(calc(1, 2, 3, 4))


def add_end(L=[]):
    L.append('END')
    return L

print(add_end())
print(add_end())


def add_end1(L=None):
    if L is None:
        L = []
    L.append('END')
Exemplo n.º 11
0
print(abs(-10))
print(max(1, 5, -15, 8))
print(int('123'))
#123
print(int(12.34))
#12
print(float("12.34"))
#12.34
print(float("12"))
#12.0
print(str(100))
print(str(12.34))
print(bool(1))
#True
print(bool(''))
#False
print(hex(1))
#0x1

from abstest import my_abs
print(my_abs(-222))
#调用外部函数
Exemplo n.º 12
0
from abstest import my_abs
print(my_abs(200))
print(my_abs(-200))
Exemplo n.º 13
0
s = set([1, 2, 3, 4, (1, 2, 3)])
s

d = {(1, ):10}
print(d)

print(str(hex(110)) + '\n' + str(hex(120)))

## 定义函数
def my_abs( x ):
    if x >= 0:
        return x
    else:
        return -x

print(my_abs(-99))

from abstest import my_abs
print(my_abs(-110))

def my_abs2(x):
    if not isinstance(x, (int, float)):
        raise TypeError('bad operand type~')
    if x >= 0:
        pass
    else:
        return
print(my_abs2('a'))

## 位置函数
def power(x, n):
Exemplo n.º 14
0
from abstest import my_abs

my_abs(-10)
Exemplo n.º 15
0
from abstest import  my_abs,move,quadratic,power,power1,enroll,add_end,calc,person,person1
import math
#测试my_abs
print(my_abs(-20))
print(my_abs(99))
#print(my_abs('a'))

#测试move
x,y=move(100,100,60,math.pi/6)#返回的是一个tuple,可以使用多个变量接收,按位置赋值给变量
print(x,y)

z=move(50,50,30,math.pi/6)#返回tuple,在语法上返回一个tuple可以省略括号
print(z)

#测试二元一次方程式跟求解
print(math.sqrt(2))
print('quadratic(2, 3, 1) =', quadratic(2, 3, 1))
print('quadratic(1, 3, -4) =', quadratic(1, 3, -4))
print('quadratic(1, 3, -4) =', quadratic(4, 1, 1))
print('quadratic(1, 3, -4) =', quadratic(1, 2, 1))

if quadratic(2,3,1)!=(-0.5,-1.0):
    print('测试失败')
if quadratic(1,3,-4)!=(1.0,-4.0):
    print('测试失败')
else:
    print('测试成功')
#print('quadratic(b, 2, 1) =', quadratic(1, 'c', 1))

'''
函数的参数测试
Exemplo n.º 16
0
print(float('12.34'))
print(str(1.23))
print(str(100))
print(bool(1))
print(bool(''))

a = abs
print(a(-1))

n1 = 255
n2 = 1000
print(hex(255))
print(hex(1000))

from abstest import my_abs
print(my_abs(-2))
#print(my_abs(-2,3))
#print(my_abs('222'))
'''
pass用法
def nop():
    pass

age = 26
if age >= 18:
    pass
'''
import math


def move(x, y, step, angle=0):
Exemplo n.º 17
0
from abstest import my_abs

print('hello world')
names = ['Tom', 'Jack', 'Mary']
for name in names:
    print(name)

print(my_abs(-5))
Exemplo n.º 18
0
print 'I\'m ok.'
print 3 > 2

classmates = ['Michael', 'Bob', 'Tracy']
classmates.append("lhz")
print len(classmates)
print classmates[-1]

print max(1, 2)

from abstest import my_abs
print my_abs(-100)

L = ['Michael', 'Sarah', 'Tracy', 'Bob', 'Jack']
for i in L:
    print i


def odd():
    print('step 1')
    yield 1
    print('step 2')
    yield (3)
    print('step 3')
    yield (5)


o = odd()
next(o)
next(o)
Exemplo n.º 19
0
from abstest import my_abs

print(my_abs(int(input())))
Exemplo n.º 20
0
#! usr/bin/evn python3
# -*- coding: utf-8 -*-

from abstest import my_abs

print(my_abs(9))
print(my_abs(-10))
#print(my_abs('a'))
Exemplo n.º 21
0
print(d['Bob'])
d['aa'] = 3
print(d['aa'])
d['Jack'] = 90
print('d[\'Jack\'] is %s' % d['Jack'])
d['Jack'] = 39
print('d[\'Jack\'] is %s' % d['Jack'])
print('hh' in d)
print(d.get('jack', -1))
print('d[\'Jack\'] is %s' % d.get('Jack', -1))
#help(abs)
print(abs(-19))
n1 = 299
print(hex(n1))
from abstest import my_abs
print(my_abs(-8))


def enroll(name, gender, age=6, city='Beijing'):
    print('name:', name)
    print('gender:', gender)
    print('age:', age)
    print('city:', city)


enroll('aa', 'asd', '7', 'beijing')
enroll('xx', '89')


def calc(*numbers):
    sum = 0
Exemplo n.º 22
0
# 函数执行完毕也没有return语句时,自动return None

from abstest import my_abs
print(my_abs(-9))

# practice

# 请定义一个函数quadratic(a, b, c),接收3个参数,返回一元二次方程:
# ax2 + bx + c = 0
# 的两个解。


def quadratic(a, b, c):
    import math
    if not isinstance(a, (int, float)):
        raise TypeError('数据类型错误')
    elif not isinstance(b, (int, float)):
        raise TypeError('数据类型错误')
    elif not isinstance(c, (int, float)):
        raise TypeError('数据类型错误')
    d = b * b - 4 * a * c
    if a == 0:
        print('a值不能为0')
    x1 = (-b + math.sqrt(d)) / 2 / a
    x2 = (-b - math.sqrt(d)) / 2 / a
    return x1, x2


# 测试:
print('quadratic(2, 3, 1) =', quadratic(2, 3, 1))
print('quadratic(1, 3, -4) =', quadratic(1, 3, -4))
Exemplo n.º 23
0
print('n1 = %d = %s' % (n1,hex(n1)))
print('n2 = %d = %s' % (n2,hex(n2)))


##################test part three#################
##########define function

#def my_abs(x):
#    if x >= 0:
#       return x
#    else:
#       return -x

x = 100
y = -100
print('x =',my_abs(x))
print('y =',my_abs(y))
#my_abs('A')
nop()
x1,y1 = move(100,100,60,math.pi / 6)
print(x1,y1)

print(quadratic(2,3,1))
print(quadratic(1,3,-4))

print(power(5))
print(power(5,3))


person('Micheal',30)
person('Bob',35,city='Beijing')