python(四)——常量和变量、数学函数、随机数

目录

 

定义变量和常量 

打印变量类型

查看变量地址

删除变量

数字类型转换

数学函数

绝对值abs()

比较两个数的大小

获取给定序列的最大值max()和最小值min()

求X的Y次方pow()

四舍五入round()

向上取整math.ceil()和向下取整math.floor()

返回小数部分和整数部分math.modf()

开平方math.sqrt()

随机数

1.random.choice(序列),在序列中随机选取一个数

2.random.choice(range(10)),从0~9的范围内随机生成一个数

3.随机从字符串中取出一个字符

4.random.randrange(),在1-99的数值中取出1+2*n的数值,因为1+2*n,所以结果都为奇数

5.随机生成[0,1]之间的小数,random.random()

6.将链表序列随机排序

7.随机产生一个实数,范围在3-9以内

表达式

if语句


定义变量和常量 

变量:可以改变的量

常量:程序运行期间不能改变的量

在输入是如果没有定义输入量的类型,将会变成字符串来运算

n1 = input("print :")
n2 = input("print :")

print("the num(n1+n2) =",n1+n2)

在输入时定义输入量为整型,输出结果为两输入量相加

n1 = int(input("print :"))
n2 = int(input("print :"))

print("the num(n1+n2) =",n1+n2)

数字:

整数:python可以处理任意大小的整数,当然包括负整数

  • 可以连续定义变量 num1 = num2 = num3 =10
  • 交互式赋值定义变量   num1,num2 = 5,10

浮点数:由整数部分和小数部分组成,浮点数运算可能会有四舍五入的误差

复数:实数部分和虚数部分组成

打印变量类型

使用type(变量名)来打印变量类型

n = 10
print("the num =", type(n))

n = "10"
print("the num =", type(n))

查看变量地址

使用id(变量名)查看变量存储的地址

print(id(n))

删除变量

del 变量名

删除后变量无法引用

n1 = int(input("print :"))
n2 = int(input("print :"))
del n2
print("the num(n1+n2) =",n1+n2)

数字类型转换

浮点数转整型,没有四舍五入

print(int(1.1))

整数转浮点数

print(float(1))

字符串转整型

print(int("123"))

字符串转浮点数

print(float("12.3"))

转换时+/-号在前代表正负

print(int("+123"))

print(int("-123"))

数学函数

导入库import math

绝对值abs()

print(abs(-10))
print(abs(-10.5))

比较两个数的大小

在下列公式中,如果n>m则返回1,否则返回-1,如果相等则返回0

​
n=10
m=6
print((n>m)-(n<m))
print((m>n)-(m<n))

实质是Boolean类型之间的比较,true-false为1,false-true为-1

print(True-False)
print(False-True)

获取给定序列的最大值max()和最小值min()

print(max(1,3,5,7,4,6,2))
print(min(1,3,5,7,4,6,2))

求X的Y次方pow()

pow(x,y)

print(pow(3,4))

四舍五入round()

如果不在后面输入数字确定要保留几位小数,结果将精确到整数

print(round(2.343))
print(round(2.786))
print(round(2.786,2))

向上取整math.ceil()和向下取整math.floor()

import math
print(math.ceil(4.4))
print(math.floor(4.4))

返回小数部分和整数部分math.modf()

import math
print(math.modf(4.4))

开平方math.sqrt()

import math
print(math.sqrt(4))

随机数

1.random.choice(序列),在序列中随机选取一个数

print(random.choice([1,2,3,4,5,6]))

2.random.choice(range(10)),从0~9的范围内随机生成一个数

print(random.choice(range(10)))

3.随机从字符串中取出一个字符

print(random.choice("asbdsgj"))

号码匹配测试

import random
n = random.choice(range(100))+1 #随机找1~100以内的一个数
m = int(input("请输一个数值:"))
if m == n:
    print("号码匹配")
else :
    print("号码不匹配")

4.random.randrange(),在1-99的数值中取出1+2*n的数值,因为1+2*n,所以结果都为奇数

print(random.randrange(1,100,2))

5.随机生成[0,1]之间的小数,random.random()

print(random.random())

可以使用round()来确定生成随机小数的位数

print(round(random.random(),2))

6.将链表序列随机排序

list = [1,2,3,4,5]
print(list)
random.shuffle(list)
print(list)

7.随机产生一个实数,范围在3-9以内

print(random.uniform(3,9))

表达式

由变量、常量和运算符组成的式子

算术运算符: + 加    - 减    *乘        / 除         **求幂(x^y)     //取整

if语句

格式:

      if 表达式 :

             语句

      else :

             语句

如果表达式的值为0  0.0   ''    None    False,则为假,不执行下面的语句

没有学循环语句写水仙花数

153 = 1^3 + 5^3 + 3^3

import math
n = m = int(input("请输入数值: "))
a = n%10
n //= 10
b = n%10
n //= 10
c = n%10
if m == math.pow(a,3)+math.pow(b,3)+math.pow(c,3) :
    print("是水仙花数")
else:
    print("不是水仙花数")

一起学习,一起进步 -.- ,如有错误,可以发评论 

猜你喜欢

转载自blog.csdn.net/qq_36171287/article/details/89684680
今日推荐