Python 100Days 学习记录 Day1~5
学习网址:https://github.com/jackfrued/Python-100-Days
Day1 初始python
安装包下载地址https://www.python.org/downloads/
Day2 编辑器
Day3 python语言中的变量
一、常用数据类型
1.整型(int)
print(0b100) # 二进制整数 print(0o100) # 八进制整数 print(100) # 十进制整数 print(0x100) # 十六进制整数
2.浮点型(float)
print(123.456) # 数学写法 print(1.23456e2) # 科学计数法
3.字符串型(str)
字符串是以单引号或双引号包裹起来的任意文本,比如'hello'和"hello"
4.布尔型(bool)
布尔型只有True、False两种值
二、变量命名
1.规则:变量名由字母、数字和下划线构成,数字不能开头;大小写敏感;变量名不要跟 Python 的关键字重名。
2.惯例:变量名通常使用小写英文字母,多个单词用下划线进行连接;受保护的变量用单个下划线开头;私有的变量用两个下划线开头。
三、type() 和类型转换
print(type(a)) # <class 'int'>
int():将一个数值或字符串转换成整数,可以指定进制。float():将一个字符串(在可能的情况下)转换成浮点数。str():将指定的对象转换成字符串形式,可以指定编码方式。chr():将整数(字符编码)转换成对应的(一个字符的)字符串。ord():将(一个字符的)字符串转换成对应的整数(字符编码)。
"""
变量的类型转换操作
Version: 1.0
Author: 骆昊
"""
a = 100
b = 123.45
c = '123'
d = '100'
e = '123.45'
f = 'hello, world'
g = True
print(float(a)) # int类型的100转成float,输出100.0
print(int(b)) # float类型的123.45转成int,输出123
print(int(c)) # str类型的'123'转成int,输出123
print(int(c, base=16)) # str类型的'123'按十六进制转成int,输出291
print(int(d, base=2)) # str类型的'100'按二进制转成int,输出4
print(float(e)) # str类型的'123.45'转成float,输出123.45
print(bool(f)) # str类型的'hello, world'转成bool,输出True
print(int(g)) # bool类型的True转成int,输出1
print(chr(a)) # int类型的100转成str,输出'd'
print(ord('d')) # str类型的'd'转成int,输出100
Day4 python语言中的运算符
|
运算符
|
描述
|
|---|---|
[]、[:] |
索引、切片
|
** |
幂
|
~、+、- |
按位取反、正号、负号
|
*、/、%、// |
乘、除、模、整除
|
>>、<< |
右移、左移
|
& |
按位与
|
^、` |
`
|
is、is not |
身份运算符
|
in、not in |
成员运算符
|
=、+=、-=、*=、/=、%=、//=、**=、&=、|=、^=、>>=、<<=、 |
赋值运算符
|
"""
海象运算符
Version: 1.0
Author: 骆昊
"""
# SyntaxError: invalid syntax
# print((a = 10)) #运行会看到SyntaxError: invalid syntax错误信息
# 海象运算符
print((a := 10)) # 10
print(a) # 10
"""
将华氏温度转换为摄氏温度,占位符打印
Version: 1.0
Author: 骆昊
"""
f = float(input('请输入华氏温度: '))
c = (f - 32) / 1.8
print('%.1f华氏度 = %.1f摄氏度' % (f, c))
print(f'{f:.1f}华氏度 = {c:.1f}摄氏度')
Day5 分支结构
match-case 分支
status_code = int(input('响应状态码: '))
match status_code:
case 400 | 405: description = 'Invalid Request'
case 401 | 403 | 404: description = 'Not Allowed'
case 418: description = 'I am a teapot'
case 429: description = 'Too many requests'
case _: description = 'Unknown Status Code'
print('状态码描述:', description)
浙公网安备 33010602011771号