python学习day002--语言元素

变量和类型

整型 int
浮点型 float
字符串 string " " 或’ ’

"""
使用变量保存数据和进行变量的加减乘除运算
"""
a=321
b=12
print(a+b)#333
print(a-b)#309
print(a*b)#3852
print(a/b)#26.75

"""
对变量的类型进行检查:type()
"""
x="hello,world"
y=100
i=12.3
j=True
print(type(x))#<class 'str'>
print(type(y))#<class 'int'>
print(type(i))#<class 'float'>
print(type(j))#<class 'bool'>

可以使用Python中内置的函数对变量类型进行转换。
int():将一个 数值或字符串 转换成整数,可以指定进制。
float():将一个 字符串 转换成浮点数。
str():将指定的对象转换成字符串形式,可以指定编码。
chr():将整数转换成该编码对应的字符串(一个字符)。
ord():将字符串(一个字符)转换成对应的编码(整数)。
input()是获取
print()函数输出利用字符串的占位符规则
%d是整数的占位符,
%f是小数的占位符,
%s是字符串的占位符。
%%表示百分号。
字符串之后的%后面跟的变量值会替换掉占位符然后输出到终端中


a = int(input('a = '))
b = int(input('b = '))
print('%d + %d = %d' % (a, b, a + b))
print('%d - %d = %d' % (a, b, a - b))
print('%d * %d = %d' % (a, b, a * b))
print('%d / %d = %f' % (a, b, a / b))
print('%d // %d = %d' % (a, b, a // b))
print('%d %% %d = %d' % (a, b, a % b))
print('%d ** %d = %d' % (a, b, a ** b))

运算符

运算符 描述
[],[:] 下标,切片
** 指数
// 整除
is ;is not 身份运算符
in ;not in 成员运算符
not;and;or 逻辑运算符
"""
练习1:将华氏温度转化为摄氏温度
"""
t=float(input('temperatrue:'))
u=(t-32)/1.8
print('%.1f华氏度=%.1f摄氏度'%(t,u))
"""
2.输入半径,输出面积和周长
"""
r=float(input('r:'))
s=3.14*r*r
l=2*3.14*r
print('面积是%.2f,周长是%.2f'%(s,l))

"""
3.判断是不是闰年
"""
year=int(input('year:'))
is_leap=year%4==0 and year%100!=0 or year%400==0
print(is_leap)
发布了56 篇原创文章 · 获赞 4 · 访问量 3182

猜你喜欢

转载自blog.csdn.net/qq_43720551/article/details/105293668