python笔记 基础语法·第8课 【乘法口诀表,占位符,添加函数extend,排序函数sort,平均数函数mean,格式化输出format】

这篇博客详细介绍了Python的基础语法,包括使用三种方式实现九九乘法口诀表,占位符的使用,extend函数添加元素到列表,sort函数对列表排序,mean函数计算平均数,以及format函数的格式化输出。还展示了如何计算考试成绩的平均数并找出低于平均分的成绩。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

九九乘法口诀表

方式一

print('1 X 1 = 1')
print('1 X 2 = 2  2 X 2 = 4')
print('1 X 3 = 3  2 X 3 = 6  3 X 3 = 9')
print('1 X 4 = 4  2 X 4 = 8  3 X 4 = 12  4 X 4 = 16')
...
...

方式二

%占位符的使用
常见的占位符有:
%d 整数
%f 浮点数
%s 字符串
%x 十六进制整数
使用的时候不知道写什么的地方直接使用 %s 进行代替,语句的末尾加上 %() 括号里面直接填写内容即可(字符串加上引号,中间用“,”分割)
示例:

 'Hello, %s' % 'world'
'Hello, world'
'Hi, %s, you have $%d.' % ('Michael', 1000000)
'Hi, Michael, you have $1000000.'

for i in range(1,2):
    print( '%d X %d = %d' % (i,1,i*1) ,end = '  ' )
print('') 

for i in range(1,3):
    print( '%d X %d = %d' % (i,2,i*2) ,end = '  ' )
print('') 

for i in range(1,4):
    print( '%d X %d = %d' % (i,3,i*3) ,end = '  ' )
print('') 

for i in range(1,5):
    print( '%d X %d = %d' % (i,4,i*4) ,end = '  ' )
print('')

for i in range(1,6):
    print( '%d X %d = %d' % (i,5,i*5) ,end = '  ' )
print('') 

for i in range(1,7):
    print( '%d X %d = %d' % (i,6,i*6) ,end = '  ' )
print('')

for i in range(1,8):
    print( '%d X %d = %d' % (i,7,i*7) ,end = '  ' )
print('') 

for i in range(1,9):
    print( '%d X %d = %d' % (i,8,i*8) ,end = '  ' )
print('') 

for i in range(1,10):
    print( '%d X %d = %d' % (i,9,i*9) ,end = '  ' )
print('') 

方式三

for i in range(1,10):
    for j in range(1,i+1):
        print( '%d X %d = %d' % (j,i,i*j),end = '  ' )
    print('  ')

添加函数 extend();排序函数

添加

extend() 函数用于在列表末尾一次性追加另一个序列中的多个值(用新列表扩展原来的列表)
示例:

aList = [123, 'xyz', 'zara', 'abc', 123];
bList = [2009, 'manni'];
aList.extend(bList)
print(aList)
输出结果为:[123, 'xyz', 'zara', 'abc', 123, 2009, 'manni']

排序

sort() 函数用于对原列表进行排序,如果指定参数,则使用比较函数指定的比较函数。

list1 =  [91, 95, 97, 99]  
list2 =  [92, 93, 96, 98]
list3 =list1
list3.extend(list2)
list3.sort()
print(list3)
输出结果为:[91, 92, 93, 95, 96, 97, 98, 99]

平均数

numpy.mean函数功能:求取均值
这个函数的参数不少,但是常用的就两种,一种是直接对整个数组求平局,另外一个是加上axis参数对不同的轴进行平均运算
第一种,如果直接调用mean函数计算,则计算所有元素的平均值,只输出一个数
示例1:

import numpy as np      # 导入 numpy库,下面出现的 np 即 numpy库
scores1 =  [91, 95, 97, 99, 92, 93, 96, 98]  
scores2 = []
average = np.mean(scores1)        # 一行解决。
print('平均成绩是:{}'.format(average))

第二种,加上axis参数的计算,可以理解为横轴和纵轴,或者x和y轴,mean函数会保留对应轴的结构计算平均值
mean(matrix,axis=0) 其中 matrix为一个矩阵,axis为参数
以m * n矩阵举例:
axis 不设置值,对 mn 个数求均值,返回一个实数
axis = 0:压缩行,对各列求均值,返回 1
n 矩阵
axis =1 :压缩列,对各行求均值,返回 m *1 矩阵
示例2:

import numpy as np
num1 = np.array([[1,2,3],[2,3,4],[3,4,5],[4,5,6]])
now2 = np.mat(num1)
now2
matrix([[1, 2, 3],
    [2, 3, 4],
    [3, 4, 5],
    [4, 5, 6]])
>>> np.mean(now2) # 对所有元素求均值
3.5
>>> np.mean(now2,0) # 压缩行,对各列求均值
matrix([[ 2.5, 3.5, 4.5]])
>>> np.mean(now2,1) # 压缩列,对各行求均值
matrix([[ 2.],
    [ 3.],
    [ 4.],
    [ 5.]])

格式化输出format()函数

format() 函数,相对基本格式化输出采用‘%’的方法,format()功能更强大,该函数把字符串当成一个模板,通过传入的参数进行格式化,并且使用大括号‘{}’作为特殊字符代替‘%’
使用方法由两种:b.format(a)和format(a,b)。
1.基本用法
  (1)不带编号,即“{}”
  (2)带数字编号,可调换顺序,即“{1}”、“{2}”
  (3)带关键字,即“{a}”、“{tom}”
示例1

print('{} {}'.format('hello','world'))  # 不带字段
		 hello world
 print('{0} {1}'.format('hello','world'))  # 带数字编号
		 hello world
print('{0} {1} {0}'.format('hello','world'))  # 打乱顺序
		 hello world hello
print('{1} {1} {0}'.format('hello','world'))
		 world world hello
print('{a} {tom} {a}'.format(tom='hello',a='world'))  # 带关键字
		 world hello world

2.进阶用法
(1)< (默认)左对齐、> 右对齐、^ 中间对齐、= (只用于数字)在小数点后进行补齐
(2)取位数“{:4s}”、"{:.2f}"等
示例2

print('{} and {}'.format('hello','world'))  # 默认左对齐
		 hello and world
print('{:10s} and {:>10s}'.format('hello','world'))  # 取10位左对齐,取10位右对齐
		 hello      and      world
print('{:^10s} and {:^10s}'.format('hello','world'))  # 取10位中间对齐
 		  hello    and   world   
 print('{} is {:.2f}'.format(1.123,1.123))  # 取2位小数
 		 1.123 is 1.12
 print('{0} is {0:>10.2f}'.format(1.123))  # 取2位小数,右对齐,取10位
		  1.123 is       1.12

考试成绩平均数计算

import numpy as np # 导入 numpy库,下面出现的 np 即 numpy库

scores1 =  [91, 95, 97, 99, 92, 93, 96, 98]
scores2 = []
average = np.mean(scores1)  # 一行解决。
print('学生的分数为:')
print(scores1)
print('平均成绩是:{}'.format(average))
for score in scores1:
    if score < average:
        scores2.append(score)
        continue  # 少于平均分的成绩放到新建的空列表中,然后继续判断。
print(' 低于平均成绩的有:{}'.format(scores2))  

#下面展示一种NumPy数组的操作。

socres3 = np.array(scores1)
print(' 低于平均成绩的有:{}'.format(socres3[socres3<average]))

#输出内容如下:
学生的分数为:
[91, 95, 97, 99, 92, 93, 96, 98]
平均成绩是:95.125
低于平均成绩的有:[91, 95, 92, 93]
低于平均成绩的有:[91 95 92 93]

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值