《python语言程序设计》_第三章(数字函数、字符串和对象)

时间:2023-02-19 09:42:38

3.2_常见的Python函数

(1) abs()函数 求绝对值

(2) max(x1,x2,x3,....)求最大值

(3) min(x1,x2,x3,....)求最小值

(4) pow 返回a的b次方,类似a**b

(5) round(x) 返回最接近x的整数,如果x与两个整数接近程度相同,则返回偶数值

(6) round(x,n) 保留小数点后的n位浮点数(其实就是保留小数点后的n位数)

数学函数里面的函数:

(1)  fabs(x) 将x看作一个浮点数,返回它的绝对值

(2)  ceil(x) x向上取最近的整数,然后返回这个整数

(3)  floor(x) x向下取最近的整数,然后返回这个整数

(4)  exp(x) 返回幂函数e的x次方

(5)  log(x) 返回x的对数(注意,以e为底,x为正数)

(6)  log(x,a) 以a为底的x的对数

(7) sqrt(x) 返回x的平方根值

(8)  sin(x) 返回x的正弦值

(9)  asin(x) 返回x的弧度值

(10)  cos(x) 返回x的余弦值,x是角度的弧度值

(11)  acos(x) 返回acos的弧度值

(12) tan(x) 返回tan(x)的值,x是角度的弧度值

(13) degrees(x) 将x从弧度转换成角度

(14) radians(x) 将x从角度转换成弧度

程序清单3-1

import math
print("exp(1.0)=",math.exp(1))
print("log(2.78)=",math.log(2.78))
print("log(10,10)=",math.log(10,10))
print("sqr(4.0)=",math.sqrt(4.0))
print("sqr(4)=",math.sqrt(4))
print("sin(PI/2)=",math.sin(math.pi / 2))
print("degrees(1.57)=",math.degrees(1.57))
print("radians(90)=",math.radians(90))
#逗号前面的说明,原原本本读取,逗号后面的是函数,返回是函数的值

结果:

exp(1.0)= 2.718281828459045
log(2.78)= 1.0224509277025455
log(10,10)= 1.0
sqr(4.0)= 2.0
sqr(4)= 2.0
sin(PI/2)= 1.0
degrees(1.57)= 89.95437383553924
radians(90)= 1.5707963267948966

《python语言程序设计》_第三章(数字函数、字符串和对象)

程序清单2:

《python语言程序设计》_第三章(数字函数、字符串和对象)(已知公式)

知道三个点的坐标,求三角形的三个角

程序:

import math
x1,y1,x2,y2,x3,y3 = eval (input("Enter three points:"))
a=math.sqrt((x2-x3)**2+(y2-y3)**2)
b=math.sqrt((x1-x3)**2+(y1-y3)**2)
c=math.sqrt((x1-x2)**2+(y2-y1)**2)
#计算a,b,c的长度
A=math.degrees(math.acos((a**2-b**2-c**2)/(-2*b*c)))
B=math.degrees(math.acos((b**2-a**2-c**2)/(-2*a*c)))
C=math.degrees(math.acos((c**2-b**2-a**2)/(-2*b*a)))
print("The three angles are",round(A*100)/100.0,round(B*100)/100.0,round(C*100)/100.0)
#round(A*100)/100.0 为了保留两位浮点数,相当于round(A,2),不知道过程转换?

结果:

Enter three points:1,1,6.5,1,6.5,2.5
The three angles are 15.26 90.0 74.74

3.3_字符串和字符

(1)双引号 " "和单引号 ' '都可以括住一个字符串,但是单引号习惯括住单个字符和空值,双引号括住一串字符串

3.3.1_ASCII码

把一个字符映射成对应的二进制被称为字符编码

3.3.2_同一码

3.3.4_转义序列

如果想要输入带有引号的字符串,直接编写这段字符串就不会得到想要的结果

比如显示:He said,:"John's program is easy to read"

如果直接编写,则是print("He said,:"John's program is easy to read"”),就会显示语句错误,Python判断不出双引号是字符串里面的

正确的是:

print("He said,:\"John's program is easy to read\"")

《python语言程序设计》_第三章(数字函数、字符串和对象)

注意:\紧接加上字符 构成的特殊字符称作转义序列

\n  表示换行或者行结束

\f  表示让打印机从下一页开始打印

\r  表示把光标移动的同一行的第一个位置

3.3.5_不换行打印

print(item,end="anyendingstring")      >>>显示“  itemanyendingstring     #而且不换行地显示另一个print()内的内容

比如:

程序1:
print("AAA",end='+')
print("BBB",end='--')

>>>AAA+BBB--

程序2:

import math
radius=3
print("The area is",radius*radius*math.pi,end=' ')
print("and the perimeter is",2*radius*math.pi)

>>>The area is 28.274333882308138 and the perimeter is 18.84955592153876  #如果没有设置不换行,就会自动切换成两行。

3.3.6_函数str

函数str 表示把一个数字转换成一个字符串

《python语言程序设计》_第三章(数字函数、字符串和对象)

3.3.7_字符串连续操作

程序1:

《python语言程序设计》_第三章(数字函数、字符串和对象)

程序2:

message="Welcome"+"to"+"python"
message+="and python is fun"
print(message)

结果:

>>>Welcometopythonand python is fun

程序3:

s1=input("Enter a string:") #输入一个字符串s1
s2=input("Enter a string:") #输入一个字符串s2
s3=input("Enter a string:") #输入一个字符串s3
print("s1 is"+s1)

结果:

Enter a string:321

Enter a string:123

Enter a string:231
s1 is321

3.4_研究实例:最小数量的硬币

程序清单3-4

程序:

amount=eval(input("Enter an amount,for example,11.56:"))
#输入一个值
remainingAmount=int(amount*100)
#对输入的一个值乘以100,然后取整,转换成分数,命名为remainingAmount
numberOfOneDollars=remainingAmount//100
remainingAmount=remainingAmount%100
#分数//100得到美元数(即是整数部分),命名为numberOfOneDollars。
#分数%100得到得到余数即是剩余的分数,命名为remainingAmount
numberOfQuarters=remainingAmount//25
remainingAmount=remainingAmount%25
#remainingAmount//25得到两角伍分的数,命名为numberOfQuarters。
#remainingAmount%25得到除以两角五分剩下的分数(必定小于两角五分)
numberOfDimes=remainingAmount//10
remainingAmount=remainingAmount%10
#将剩余的分数除于10得到一角硬币的个数,命名为numberOfDimes
numberOfNickels=remainingAmount//5
remainingAmount=remainingAmount%5
#将剩下不足以10分的remainingAmount除于5,得到五分硬币的个数numberOfNickels

numberOfPennies=remainingAmount

print("Your amount",amount,"consist of\n",
"\t",numberOfOneDollars,"dollars\n",
"\t",numberOfQuarters,"quaters\n",
"\t",numberOfDimes,"dimes\n",
"\t",numberOfNickels,"nickels\n",
"\t",numberOfPennies,"pennies")

结果:

Enter an amount,for example,11.56:11.56
Your amount 11.56 consist of
11 dollars
2 quaters
0 dimes
1 nickels
1 pennies

3.5_对象和方法简介

id()和type()函数获取对象信息。

lower()把数值变成了小写,upper把数值变成了大写。

程序1:

s="welcome"
s1=s.lower()
print(s1)
s2=s.upper()
print(s2)

结果:

welcome
WELCOME

#strip()被用来移除一个字符串两端的空格符。空格符有:' ' 、 \t 、  \f 、\r 、\n

程序2:

s="\t WElcome \n"
s1=s.strip() #调用strip
print("s is" ,s)
print(s1)

结果返回:WElcome

# 3..6 _格式化数字和字符串

#round( item , n )  #当保留n位小数,当第n位为0时,只是显示n-1位。比如n=2时候,14.10111显示的是14.1

程序:

amount=12618.98
interestRate = 0.0013
interest=amount * interestRate
print("Interest is",round(interest,2))

结果:16.4

程序:

amount=12618.98
interestRate = 0.0013
interest=amount * interestRate
print("Interest is",round(interest,3))

结果:

16.405

#format ( item , format_specifier)  #format_specifier格式说明符

#print(item , 域宽度.精度转换码) #域宽度指的是整个数的长度,精度指的是小数点后面可以保留多少个小数

程序

print(format(57.467657,"10.2f"))       #域宽度是10,2是精度,f是转换码,当域宽度被省略的时候,那就默认域宽度为0,宽度会根据格式化这个数所需要的宽度自己设置
print(format(12345678.923,"10.2f"))
print(format(57.4,"10.2f"))

结果:

《python语言程序设计》_第三章(数字函数、字符串和对象)

#用科学计数法格式化

程序:#加减+-和点。都算在宽度里面

print(format(57.467657,"10.2e"))
print(format(12345678.923,"10.2e"))
print(format(0.000574,"8.2e"))

结果:

《python语言程序设计》_第三章(数字函数、字符串和对象)

#格式化为百分比%,转换码%可以把一个数字格式化成百分数。n变成100*n%

程序:

print(format(57.467657,"10.2%"))
print(format(12345678.923,"10.2%"))
print(format(0.000574,"8.2%"))

结果:

《python语言程序设计》_第三章(数字函数、字符串和对象)

#3.6.4_调整格式  #一个数的格式是默认向右边对齐的,<放在格式说明符里面得到的字符串是以指定的宽度向左对齐的。

程序:

print(format(57.467657,"10.2f"))
print(format(57.467657,"<10.2f"))

结果:

《python语言程序设计》_第三章(数字函数、字符串和对象)

#3.6.5 格式化整数

# "d"格式化十进制整数、"x"十六进制整数、"0"八进制整数、"b"二进制整数 # 16进制0123456789ABCEDF代表的就是0-15

程序:

print(format(59832,"<10x"))

结果:e9b8

#3.6.5 格式化整数

程序:

print(format(59832,"10d"))
print(format(59832,"<10d"))
print(format(59832,"10x"))
print(format(59832,"<10x"))

结果:

《python语言程序设计》_第三章(数字函数、字符串和对象)

注意:格式化说明符,并不是把它格式化了,而是把格式改成指定的格式。d s o b -- 10 、16、8、2 进制 。

#3.6.6

格式化字符串

转换码s可以字符串格式化成一个指定宽度的字符串。#指定长度和对齐

程序 :

print(format('welcome to python',"20s"))
print(format("wlcome to python","<20s")) #左对齐
print(format("welcome to python",">20s"))

结果:

《python语言程序设计》_第三章(数字函数、字符串和对象)

#3.7 --绘制各种图形。

程序:

import turtle #加载画图工具
turtle.pensize(3) #设置画笔的粗细
turtle.penup() #把画笔提起来
turtle.goto(-200,-50) #把笔移动到(-100,-50)的位置,当作初始点
turtle.pendown() #放写画笔
turtle.circle(40,steps=3) #绘制一个三角形

turtle.penup() #拿起画笔
turtle.goto(-100,-50)
turtle.pendown()
turtle.circle(40,steps=4)

turtle.penup()
turtle.goto(0,-50)
turtle.pendown()
turtle.circle(40,steps=5)

turtle.penup()
turtle.goto(100,-50)
turtle.pendown()
turtle.circle(40,steps=6)

turtle.penup()
turtle.goto(200,-50)
turtle.pendown()
turtle.circle(40)

turtle.done()

结果:

《python语言程序设计》_第三章(数字函数、字符串和对象)

#3.8 绘制带颜色和带文字的图形

程序:

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import turtle
turtle.pensize(3)
turtle.penup()
turtle.goto(-200,-50)
turtle.pendown()
turtle.begin_fill() #调用begin_fii()来告诉python绘制一个填充颜色的图形。
turtle.color("red") #设置填充的颜色
turtle.circle(40,steps=3) #设置填充颜色的图形
turtle.end_fill() #完成图形颜色的填充

turtle.penup()
turtle.goto(-100,-50)
turtle.pendown()
turtle.begin_fill()
turtle.color("blue")
turtle.circle(40,steps=40)
turtle.end_fill()

turtle.penup()
turtle.goto(200,-50)
turtle.pendown()
turtle.begin_fill()
turtle.color("purple")
turtle.circle(40)
turtle.end_fill()

turtle.color("green") #设置颜色是绿色
turtle.penup() #画笔拿起
turtle.goto(-100,50)
turtle.pendown()
turtle.write("Cool Colorful Shapes",font=("times",18,"bold"))
#画笔填充字符串,设置字体为times,大小为18,粗体
turtle.hideturtle()
turtle.done

结果

《python语言程序设计》_第三章(数字函数、字符串和对象)

《python语言程序设计》_第三章(数字函数、字符串和对象)的更多相关文章

  1. 全国计算机等级考试二级教程-C语言程序设计&lowbar;第14章&lowbar;结构体、共用体和用户定义类型

    函数的返回值是结构体类型 #define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<stdlib.h> struct ...

  2. 全国计算机等级考试二级教程-C语言程序设计&lowbar;第5章&lowbar;循环结构

    for循环结构的嵌套 外层循环每循环一次,内层循环会完整循环一次. 外层循环是竖. 内层循环是横. for, do...while, while的选择: 如果有固定次数,如阶乘! ,判断素数,用 fo ...

  3. 全国计算机等级考试二级教程-C语言程序设计&lowbar;第8章&lowbar;地址和指针

    面试: unsigned int *p1 = &num; int *p2 = &num; #define _CRT_SECURE_NO_WARNINGS #include<std ...

  4. 全国计算机等级考试二级教程-C语言程序设计&lowbar;第6章&lowbar;字符型数据

    #include <stdio.h> main() { char c; char d; c = ; d = '; if (c == d) { printf("yes\n&quot ...

  5. 全国计算机等级考试二级教程-C语言程序设计&lowbar;第15章&lowbar;位运算

    位运算,不适用于实数,仅仅适用于整数.字符. C语言的位运算只能操作整数.字符,实数是指数方式表示的,不适用于位运算. #define _CRT_SECURE_NO_WARNINGS #include ...

  6. 全国计算机等级考试二级教程-C语言程序设计&lowbar;第3章&lowbar;顺序结构

    1输入两个整数给变量x和y:然后输出x和y:在交换x和y中的值后,在输出x和y. #include <stdio.h> main() { int x, y, t; printf(&quot ...

  7. 全国计算机等级考试二级教程-C语言程序设计&lowbar;第4章&lowbar;选择结构

    switch什么时候用break,什么时候不用break 调用break:一次执行一个分支,输入一个数据,对应一个级别 不调用break:连续执行多个分支 if...else 可以处理任何情况,大于小 ...

  8. 全国计算机等级考试二级教程-C语言程序设计&lowbar;第2章&lowbar;C程序设计的初步知识

    正负号与被除数一致. 3 % (-5) == 3 (-3) % 5 == -3 不用求余运算符,求出余数. int x, y; 答:x - x / y * y; const int i = 10; c ...

  9. 全国计算机等级考试二级教程-C语言程序设计&lowbar;第9章&lowbar;数组

    四维数组,可用于航天卫星,三维+时间 #define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<stdlib.h> ...

  10. 全国计算机等级考试二级教程-C语言程序设计&lowbar;第7章&lowbar;函数

    函数执行,从右到左执行 #define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<string.h> main() ...

随机推荐

  1. redis成长之路——(一)

    为什么使用redis Redis适合所有数据in-momory的场景,虽然Redis也提供持久化功能,但实际更多的是一个disk-backed的功能,跟传统意义上的持久化有比较大的差别,那么可能大家就 ...

  2. SQL函数创建错误

    [Err] 1418 - This function has none of DETERMINISTIC, NO SQL, or READS SQL DATA in its declaration a ...

  3. 再谈自主开发与企业IT管理

    前两天写<自主开发与带兵打仗>分析了一下自主开发的利与弊,得到了园内不少朋友的反馈,但我觉得还有很多东西没有交待清楚,可能有很多朋友也跟我一样在公司的IT部门,有自己的研发团队也有很多外购 ...

  4. &lpar;void&ast;&rpar;0 的理解

    例如: #define NULL ((void *)0) 用来定义无效的指针 (void *)0 就是将0强制转化为(void *)类型的指针 char *ch = (void *)0;//ch指向地 ...

  5. python学习第八天 -- 迭代

    什么是迭代? 在python语言中,迭代的意思就是通过for循环遍历一个有序/无序的集合. 迭代是通过 for x in 集合 来完成的. 前面有提到python中有哪些集合,现在我们来梳理一下: ( ...

  6. 【手打】LZW编码的C&sol;C&plus;&plus;实现

    LZW编码通过建立一个字符串表,用较短的代码来表示较长的字符串来实现压缩. LZW压缩算法是Unisys的专利,有效期到2003年,所以相关算法大多也已过期. 本代码只完毕了LZW的编码与解码算法功能 ...

  7. c&num; 读取ACCESS 数据库

    using System; using System.Collections.Generic; using System.Data.OleDb; using System.IO; using Syst ...

  8. &lbrack;翻译&rsqb;现代java开发指南 第二部分

    现代java开发指南 第二部分 第二部分:部署.监控 & 管理,性能分析和基准测试 第一部分,第二部分 =================== 欢迎来到现代 Java 开发指南第二部分.在第一 ...

  9. Python编程快速上手——让繁琐工作自动化学习笔记

    第一部分 基本语法 1.字符串不能直接和数字相加,要用str()转一下:但是可以和数字相乘,用于表示多个字符串复制:字符串不能和浮点数直接结合,字符串可以和字符串直接相加: 2.输入函数用input( ...

  10. ckeditor django admin 中使用

    ckeditor settings配置 ############ # CKEDITOR # ############ MEDIA_ROOT = os.path.join(BASE_DIR, 'medi ...