day01
Python注释
-
注释分为单行注释和多行注释。
'''单行注释''' """ 多 行 注 释 """
-
注释不参与程序的执行
变量
变量名命名要求
- 变量名不能是系统关键字
- 变量名区分大小写
- 变量名只能由数字,字母,下划线组成,不能以数字开头
变量名命名规范
1.见名知意。
2.驼峰命名法
a.大驼峰命名法:变量中每个单词的首字母要大写。(FirstName)
b.小驼峰命名法:变量中从第二个单词开始首字母大写。(firstName)
from keyword import kwlist
print(kwlist)
type方法
a = 213123
print(a + 1)
b = 10.1
c = True
d = 'abc'
e = [1, 2, 3] # 列表(list)
f = (1, 2, 3) # 元组(tuple)
g = {1, 2, 3} # 集合(set)
h = {'name': '张三'} # 字典(dict)
# tupe:查看数据类型
print(type(a), type(b), type(c), type(d), type(e), type(f), type(g), type(h))
输入和输出
输入的结果输出以后一定是一个字符串
输出的结果默认换行(end = ‘\n’),其中(end = '\t’是间隔4个空格的间距(Tab))
print(10, 10 + 100, 10 < 30, '111112',end='\t')
sep = ’😊‘是起输出结果之间的间距的作用
print(10, 10 + 100, 10 < 30, '111112',sep='🙂')
格式化输出
name = input('请输入姓名:')
age = int(input('请输入年龄:'))
str1 = '小明今年20岁'
分两种方法输出:
-
str2 = '{}今年{}岁'.format(name,age)
-
str3 = f'{name}今年{age}岁'
千分位符’,’
num = 1231231123123455
str4 = '{:,}'.format(num)
b = '{:.2%}'.format(67/100)# '.'表示用小数点隔开,2的意思是保留两位小数,%表示转换成百分数
from datetime import datetime
times = datetime(2022,6,21,15,33,43)
t = '{:%Y:%m-%d %H.%M.%S}'.format(times)
print(t)
'<‘左对齐,’>‘右对齐,’^'居中对齐
"""
*
**
***
"""
"""
*
**
***
"""
print('{:<3}'.format('*'))
print('{:<3}'.format('**'))
print('{:<3}'.format('***'))
运算符
运算符包括:数学运算,赋值运算,比较运算,逻辑运算符,位运算等等·
- 数学运算符:+,-,*,/,//(整除),**(幂运算),%(取余)
print(2 + 3)
print(2 - 3)
print(2 * 3)
print(2 / 3)
print(2 // 3)# 向小取整
print(2 % 3)
print(2 ** 3)
# 幂运算涉及到开方结果都是浮点型
print(4 ** 0.5)
- 逻辑运算符:and(逻辑与运算),or(逻辑或运算),not(逻辑非运算)
print(True and True)
print(False and True)
print(False and False)
print(True or True)
print(False or True)
print(False or False)
print(not True)
例子:判断一个年份是否是闰年
t = int(input('输入年份'))
# t = 2000
print(not (t % 4 and t % 100) or not (t % 400))
# 通过输出True判断是闰年,通过False判断是平年