※python学习第二天※

目录

1、将华氏温度转换为摄氏温度(F = 1.8C + 32)

2、输入半径计算圆的周长和面积

3、判断是否是闰年

4、运算符的使用

5、字符串常用操作

6、使用变量保存数据并进行操作

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

8、练习格式化输出

9、检查变量的类型

10、类型转换


1、将华氏温度转换为摄氏温度(F = 1.8C + 32)

输入:
f = float(input('请输入华氏温度: '))
c = (f - 32) / 1.8
print('%.1f华氏度 = %.1f摄氏度' % (f, c))

输出:
请输入华氏温度: 127
127.0华氏度 = 52.8摄氏度

2、输入半径计算圆的周长和面积

输入:
import math

radius = float(input('请输入圆的半径: '))
perimeter = 2 * math.pi * radius
area = math.pi * radius * radius
print('周长: %.2f' % perimeter)
print('面积: %.2f' % area)


输出:
请输入圆的半径: 5
周长: 31.42
面积: 78.54

3、判断是否是闰年

输入:
year = int(input('请输入年份: '))
# 如果代码太长写成一行不便于阅读 可以使用\或()折行
is_leap = (year % 4 == 0 and year % 100 != 0 or
           year % 400 == 0)
print(is_leap)

输出:
请输入年份: 2021
False

4、运算符的使用

输入:
a = 5
b = 10
c = 3
d = 4
e = 5
a += b
a -= c
a *= d
a /= e
print("a = ", a)

flag1 = 3 > 2
flag2 = 2 < 1
flag3 = flag1 and flag2
flag4 = flag1 or flag2
flag5 = not flag1
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

5、字符串常用操作

输入:
str1 = 'hello, world!'
print('字符串的长度是:', len(str1))
print('单词首字母大写: ', str1.title())
print('字符串变大写: ', str1.upper())
# str1 = str1.upper()
print('字符串是不是大写: ', str1.isupper())
print('字符串是不是以hello开头: ', str1.startswith('hello'))
print('字符串是不是以hello结尾: ', str1.endswith('hello'))
print('字符串是不是以感叹号开头: ', str1.startswith('!'))
print('字符串是不是一感叹号结尾: ', str1.endswith('!'))
str3 = str1.title()
print(str3)

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

6、使用变量保存数据并进行操作

输入:
a = 321
b = 123
print(a + b)
print(a - b)
print(a * b)
print(a / b)
#取整除 - 向下取接近除数的整数
print(a // b)
print(a % b)
#** 幂 - 返回a的b次幂
print(a ** b)

输出:
444
198
39483
2.6097560975609757
2
75
199580904170858944588683464608694075062943107082809027605498347841561605181074274426665986623764972439807786764739130193391688887305693295698132008415537889753720415793877902074665765736230030622222862498385818118244547668141388643750880896837470534556730014314212951333550122949806119583030226121714710874561

7、将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 = 9
b = 9
18
0
81
1.0
1
0
387420489

8、练习格式化输出

输入:
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))

输出:
a = 5
b = 5
5 + 5 = 10
5 - 5 = 0
5 * 5 = 25
5 / 5 = 1.000000
5 // 5 = 1
5 % 5 = 0
5 ** 5 = 3125

9、检查变量的类型

输入:
a = 100
b = 1000000000000000000
c = 12.345
d = 1 + 5j
e = 'A'
f = 'hello, world'
g = True
print(type(a))
print(type(b))
print(type(c))
print(type(d))
print(type(e))
print(type(f))
print(type(g))

输出:
<class 'int'>
<class 'int'>
<class 'float'>
<class 'complex'>
<class 'str'>
<class 'str'>
<class 'bool'>

10、类型转换

输入:
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)
print(type(a))
print(b)
print(type(b))
print(c)
print(type(c))
print(d)
print(type(d))
print(e)
print(type(e))
print(f)
print(type(f))
print(g)
print(type(g))
print(h)
print(type(h))
print(i)
print(type(i))
print(j)
print(type(j))
print(k)
print(type(k))
print(m)
print(type(m))

输出:
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'>

我是花花,祝自己也祝您变强了~ 

猜你喜欢

转载自blog.csdn.net/m0_52711790/article/details/120990623