【Python-100-Days】Day02 变量、比较运算符和逻辑运算符

练习:

 1. 赋值运算符和复合赋值运算符

a = 10
b = 3
a += b        # 相当于:a = a + b, a = 10 + 3 = 13
a *= a + 2    # 相当于:a = a * (a + 2), a = 13 * (13 + 2)
print(a)      # a = 195

运行结果:

195

2. 将华氏温度转换为摄氏温度
F = 1.8C + 32 

print可以进行格式化处理,%.1f是占位符,由一个float型变量来替换它,.1f表示保留小数点后1位有效数字。还可以用print(f'xxxx')来指定格式,类似于String.format()。{f:.1f}{c:.1f}表示输出时会由f和c这两个变量来替换两个占位符。

f = float(input('Please input Fahrenheit: '))
c = (f - 32) / 1.8
print('%.1f Fahrenheit is %.1f Celsius' % (f, c))
print(f'{f:.1f} Fahrenheit = {c:.1f} Celsius')

运行结果: 

Please input Fahrenheit: 33
33.0 Fahrenheit is 0.6 Celsius
{f:.1f} Fahrenheit = {c:.1f} Celsius

3. 输入半径计算圆的周长和面积

这里用到了math库,math.pi就是π。 

import math

radius = float(input('Please input radius: '))
perimeter = 2 * math.pi * radius
area = math.pi * (radius ** 2)

print('perimeter is: %.2f' % perimeter)
print('area is: %.2f' % area)

运行结果:

Please input radius: 8
perimeter is: 50.27
area is: 201.06

4. 输入年份 如果是闰年输出True 否则输出False 

改了一下题目,如果是闰年输出今年是闰年,如果不是闰年输出今年不是闰年。加个if语句进行判断。

year = int(input('Please input year: '))
is_leap = (year % 4 == 0 and year % 100 != 0) or year % 400 == 0

if(is_leap): print('%d is a leap year' % year)
else: print('%d is not a leap year' % year)

运行结果: 

Please input year: 2022
2022 is not a leap year
Please input year: 2000
2000 is a leap year

5. 运算符的使用 

a = 5
b = 10
c = 3
d = 4
e = 5
a += b # a = 5 + 10 = 15
a -= c # a = 15 - 3 = 12
a *= d # a = 12 * 4 = 48
a /= e # a = 48 / 5 = 9.6
print("a = ", a)

flag1 = 3 > 2 # True
flag2 = 2 < 1 # False
flag3 = flag1 and flag2 # False
flag4 = flag1 or flag2 # True
flag5 = not flag1 # False
print("flag1 = ", flag1)
print("flag2 = ", flag2)
print("flag3 = ", flag3)
print("flag4 = ", flag4)
print("flag5 = ", flag5)
print(flag1 is True)
print(flag2 is not False)

运行结果:

a =  9.6
flag1 =  True
flag2 =  False
flag3 =  False
flag4 =  True
flag5 =  False
True
False

6. 字符串常用操作 

str1 = 'hello, world!'
print('字符串的长度是:', len(str1)) #13 空格也算
print('单词首字母大写: ', str1.title()) # Hello, World!
print('字符串变大写: ', str1.upper()) # HELLO, WORLD!
# str1 = str1.upper()
print('字符串是不是大写: ', str1.isupper()) #True
print('字符串是不是以hello开头: ', str1.startswith('hello')) #False
print('字符串是不是以hello结尾: ', str1.endswith('hello')) #False
print('字符串是不是以感叹号开头: ', str1.startswith('!')) #False
print('字符串是不是一感叹号结尾: ', str1.endswith('!')) #True
str2 = '- \u87d2\u86c7'
str3 = str1.title() + ' ' + str2.lower()
print(str3)

运行结果: 

字符串的长度是: 13
单词首字母大写:  Hello, World!
字符串变大写:  HELLO, WORLD!
字符串是不是大写:  False
字符串是不是以hello开头:  True
字符串是不是以hello结尾:  False
字符串是不是以感叹号开头:  False
字符串是不是一感叹号结尾:  True
Hello, World! - 蟒蛇

7. 使用变量保存数据并进行操作 


a = 321
b = 123

print(a + b) #加法
print(a - b) #减法
print(a * b) #乘法
print(a / b) #除法
print(a // b) #整除
print(a % b) #取余
print(a ** b) #指数 a^b

运行结果:

444
198
39483
2.6097560975609757
2
75
199580904170858944588683464608694075062943107082809027605498347841561605181074274426665986623764972439807786764739130193391688887305693295698132008415537889753720415793877902074665765736230030622222862498385818118244547668141388643750880896837470534556730014314212951333550122949806119583030226121714710874561

8. 将input函数输入的数据保存在变量中并进行操作 


a = int(input('a = '))
b = int(input('b = '))

print(a + b) #加法
print(a - b) #减法
print(a * b) #乘法
print(a / b) #除法
print(a // b) #整除
print(a % b) #取余
print(a ** b) #指数 a^b

运行结果:

a = 2
b = 3
5
-1
6
0.6666666666666666
0
2
8

9. 格式化输出

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))

print('%d + 2 * %d = %d' % (a, b, a + 2 * b))

运行结果:

a = 8
b = 65
8 + 65 = 73
8 - 65 = -57
8 * 65 = 520
8 / 65 = 0.123077
8 // 65 = 0
8 % 65 = 8
8 ** 65 = 50216813883093446110686315385661331328818843555712276103168
8 + 2 * 65 = 138

10. 检查变量的类型 

a = 100
b = 1000000000000000000
c = 12.345
d = 1 + 5j
e = 'A'
f = 'hello, world'
g = True
print(type(a)) #int
print(type(b)) #int
print(type(c)) #float
print(type(d)) #complex
print(type(e)) #str
print(type(f)) #str
print(type(g)) #bool

运行结果:

<class 'int'>
<class 'int'>
<class 'float'>
<class 'complex'>
<class 'str'>
<class 'str'>
<class 'bool'>

11. 类型转换

a = 100
b = str(a)
c = 12.345
d = str(c)
e = '123'
f = int(e)
g = '123.456'
h = float(g)
i = False
j = str(i)
k = 'hello'
m = bool(k)
print(a) #100
print(type(a)) #int
print(b) #100
print(type(b)) #str
print(c) #12.345
print(type(c)) #float
print(d) #12.345
print(type(d)) #str
print(e) #123
print(type(e)) #str
print(f) #123
print(type(f)) #int
print(g) #123.456
print(type(g)) #str
print(h) #123.456
print(type(h)) #float
print(i) #false
print(type(i)) #bool
print(j) #Flase
print(type(j)) #str
print(k) #hello
print(type(k)) #str
print(m) #True
print(type(m)) #bool

运行结果:

100
<class 'int'>
100
<class 'str'>
12.345
<class 'float'>
12.345
<class 'str'>
123
<class 'str'>
123
<class 'int'>
123.456
<class 'str'>
123.456
<class 'float'>
False
<class 'bool'>
False
<class 'str'>
hello
<class 'str'>
True
<class 'bool'>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值