import math
print(math.ceil(4.1)) # 返回数字的上入整数
print(math.floor(4.9)) # 返回数字的下舍整数
print(math.fabs(-10)) # 返回数字的绝对值
print(math.sqrt(9)) # 返回数字的平方根
print(math.exp(1)) # 返回e的x次幂
import random
ran = random.random()
print(ran)
ran = random.randint(1, 20)
print(ran)
a = 'Hello '
b = 'World '
print(a + b)
a = 'Hello '
print(a * 3)
a = 'Hello '
print(a[0])
a = 'Hello '
print(a[1:4])
a = 'Hello '
print('e' in a)
print('e' not in a)
join()
以字符作为分隔符,将字符串中所有的元素合并为一个新的字符串new_str = '-'.join('hello')
print(new_str) # h-e-l-l-o
print('Hello World!')
print("Hello World!")
print('This \t is a tab')
print('I\'m going to the movies.')
print('''I'm going to the movies''')
html = '''
Friends CGI Demo
ERROR
%s
'''
print(html)
# 声明一个列表
names = ['jack', 'tom', 'tonney', 'superman', 'jay']
# 通过下标或索引获取元素
print(names[0])
print(names[1])
# 获取最后一个元素
print(names[-1])
print(names[len(names) - 1])
# 获取第一个元素
print(names[-5])
# 遍历列表,获取元素
for name in names:
print(name)
# 查询names里面有没有superman
for name in names:
if name == 'superman':
print('有超人')
break
else:
print('无超人')
# 更简单的方法,来查询names里有没有superman
if 'superman' in names:
print('有超人')
else:
print('无超人')
# 声明一个空列表
girls = []
# append(),末尾追加
girls.append('杨超越')
print(girls)
# extend(), 一次添加多个,把一个列表添加到另一个,列表合并
models = ['刘雯', '奚梦瑶']
girls.extend(models) # girls = girls + models
print(girls)
# insert():指定位置添加
girls.insert(1, '张三')
print(girls)
words = ['cat', 'hello', 'pen', 'pencil', 'ruler']
del words[0]
print(words)
words.remove('hello')
print(words)
words.pop(0)
print(words)
animals = ['cat', 'dog', 'tiger', 'snake', 'mouse', 'bird']
print(animals[2:5])
print(animals[-1:])
print(animals[-3:-1])
print(animals[-5:-1:2])
print(animals[::2])
import random
random_list = []
for i in range(10):
ran = random.randint(1, 20)
if ran not in random_list:
random_list.append(ran)
print(random_list)
# 默认升序
new_list = sorted(random_list)
print(new_list)
# 降序
new_list = sorted(random_list, reverse=True)
print(new_list)
tuple1 = ()
print(type(tuple1)) #
tuple2 = ('hello',)
print(type(tuple2)) #
print(tuple2) # ('hello',)
import random
random_list = []
for i in range(10):
ran = random.randint(1, 20)
random_list.append(ran)
print(random_list)
print(random_list[::-1]) # 反转
random_tuple = tuple(random_list)
print(random_tuple)
print(random_tuple[0])
print(random_tuple[-1])
print(random_tuple[1:-3])
print(random_tuple[::-1]) # 反转
t1 = (1, 2, 3) + (4, 5)
print(t1) # (1, 2, 3, 4, 5)
t2 = (1, 2) * 2
print(t2) # (1, 2, 1, 2)
# 定义一个空字典
dict1 = {}
dict2 = {'name': '杨超越', 'weight': 45, 'age': 25}
print(dict2['name'])
# list可以转成字典,但前提是列表中元素都要成对出现
dict3 = dict([('name', '杨超越'), ('weight', 45)])
print(dict3)
dict4 = {}
dict4['name'] = '虞书欣'
dict4['weight'] = 43
print(dict4)
dict4['weight'] = 44
print(dict4)
dict5 = {'杨超越': 165, '虞书欣': 166, '上官喜爱': 164}
print(dict5.items())
for key, value in dict5.items():
if value >= 165:
print(key, value)
result = dict5.values()
print(result)
heights = dict5.values()
total = sum(heights)
avg = total / len(heights)
print(avg)
dict6 = {'杨超越': 165, '虞书欣': 166, '上官喜爱': 164}
del dict6['杨超越']
print(dict6)
dict6.pop('虞书欣')
print(dict6)
(1)init()定义构造函数,与其他面向对象语言不同的是,Python语言中,会明确地把代表自身实例的self作为第一个参数传入
(2)创建一个实例化对象 cat,init()方法接收参数
(3)使用点号 . 来访问对象的属性。
class Animals:
def __init__(self, name):
self.name = name
print('动物对象实例化')
def eat(self):
print(self.name + '要吃东西啦')
def drink(self):
print(self.name + '要喝水啦')
cat = Animals('猫咪')
print(cat.name)
cat.eat()
cat.drink()
class Person:
def __init__(self, name):
self.name = name
print('调用父类构造函数')
def eat(self):
print('调用父类方法')
# 定义子类
class Student(Person):
def __init__(self):
print('调用子类构造方法')
def study(self):
print('调用子类方法')
s = Student() # 实例化子类
s.eat() # 调用父类方法
s.study() # 调用子类方法