变量
需求,计算商品总价
变量:用来存储数据的值表现形式
#苹果数量
count1 = 2
#苹果单价
price1 = 2.1
#雪糕数量
count2 = 3
#雪糕单价
price2 = 3
#榴莲数量
count3 = 1
#榴莲单价
price3 = 99
money = count1*price1+count2*price2+count3*price3
print("总价是%g 元"%money)
控制台输出结果
总价是112.2 元
标识符和关键字
%g:变量占位符
Import keywords:导入Python中的所有关键字包
#占位符
count1=2
print(count1)
print("count1=%g"%count1)
count2=5
print("count1=%g\tcount2=%g" % (count1, count2))
print("\ncount1=%g\ncount2=%g" % (count1, count2))
# 以下为打印结果
# 2
# count1=2
# count1=2 count2=5
#
# count1=2
# count2=5
# 关键字
# 大小写敏感
a=100
A=200
# 不能使用系统关键字作为标识符
# class=22
import keyword
# 将所有的关键字存储到变量result中
result = keyword.kwlist
# 打印所有的关键字
print(result)
# 求result中关键字的个数
print(len(result))
print(result.__len__())
# 以下为打印结果
# ['False', 'None', 'True', 'and', 'as', 'assert', 'async', 'await', 'break', 'class', 'continue', 'def', 'del', 'elif', 'else', 'except', 'finally', 'for', 'from', 'global', 'if', 'import', 'in', 'is', 'lambda', 'nonlocal', 'not', 'or', 'pass', 'raise', 'return', 'try', 'while', 'with', 'yield']
# 35
# 35
格式化输出与输入
Type关键字作用
Boolean、int、float
List(列表)、tuple(元组)、set(集合)、dict(字典)
格式化输出:
%g:输出小数或者整数
%d:输出整数
%f:输出小数
%s:输出字符串
height=172.6
print("身高 %gcm" % height)
print("身高 %dcm" % height)
print("身高 %fcm" % height)
# 控制台打印结果如下
# 身高 172.6cm
# 身高 172cm
# 身高 172.600000cm
# 保留小数后三位
print("身高 %.3fcm" % height)
# 身高 172.600cm
#从键盘输入可乐瓶数,计算总价格
price=2.5
count=input("请输入可乐瓶数\n")
# <class 'str'>
print(type(count))
# 键盘录入的是字符串,需要将其转成int型,不然会运算报错
count=int(count)
# TypeError: can't multiply sequence by non-int of type 'float'
print(count * price)
# 输入4得到10.0
算术运算符的使用
+:
1、求和
2、正号
3、连接两个字符串
-:
1、求差
2、负号
*:
1、求乘积
2、重复
/:
1、求商
//:
1、取整除
**:
1、求几次幂
%:
1、取余数
代码
# ------------ +的用法 --------------
a=10
b=+20
c=a+b
print("%d + %d = %d" % (a, b, c))
# 10 + 20 = 30
str1='123'
str2="456"
str3=str1+str2
print(str3)
# 123456
# ------------ -的用法 --------------
a=10
b=-10
c=a-b
print("c=%d" % c)
# c=20
# ------------ *的用法 --------------
a=3
b=6
c=a*b
print("c=%d" % c)
# c=18
# 重复将hello打印10次
print("hello" * 10)
# hellohellohellohellohellohellohellohellohellohello
print("-" * 50)
# --------------------------------------------------
print(5 / 2.0)
# 2.5
print(5 // 2)
# 2
print(5 // 2.0)
# 2.0
print(1 / 2)
# 0.5
# ------------ **的用法 --------------
print(2 ** 8)
# 256
print(8 ** -1)
# 0.125
# ------------ %的用法 --------------
print(100 % 3)
# 1
print(5 % 2)
# 1
# ------------ 其他 --------------
print(4 / 2)
print(4 // 2)
print(4.0 // 2)
# 2.0 更加精确
# 2 整除,并且都是整数,所以结果也就是整数
# 2.0 整除,有一个小数,所以结果也是小数
赋值运算符的使用
将右边的常量赋值左边的变量
1、单独的赋值
=
2、综合的赋值
+=
-=
*=
/=
//=
**=
%=
3、并列赋值
A,b=b,a
除却并列赋值,其他语法都和Java一模一样
a,b=10,20
print(a)
# 10
print(b)
# 20
c=a=b
print(a)
# 20
print(c)
# 20
a,b=a+1,a
print(a)
# 21
print(b)
# 20
注意:在Python中a=a+1,b=a这两行代码是同时运行的
实现两个数值的交换
从键盘录入两个整型数,分别打印两个值,将两个数值完成交换,打印交换之后的值
a = int(input("请输入第一个整数"))
b = int(input("请输入第二个整数"))
print("交换前:a=%d b=%d" % (a, b))
# 方式一(引入第三方变量)
# temp=a
# a=b
# b=temp
# 方式二
# a=a+b
# b=a-b
# a=a-b
# 方式三(并列运算)
a,b=b.a
# 方式四(异或)
print("交换后:a=%d b=%d" % (a, b))
A,b = b,a
注意:第二种方式容易内存溢出,不建议使用
谢谢大家,多多指教!!!