Python基础语法
创建项目
创建project时路径不要出现中文或空格一类的符号
PC结构界面


注释
-
程序是由代码和注释组成,注释是对某一部分代码做的解释
-
注释不参与程序执行,仅仅起解释作用
注释的两种形式:单行注释、多行注释
-
单行注释:使用#将某一行内容进行注释,#放到某行开头,单行注释快捷键:
ctrl(command) + / -
多行注释:使用三引号(三对单引号
'''或三对双引号""")进行多行注释'''''' """"""
变量
-
什么是变量
变量指代存储的而数据。
-
变量的命名
-
变量命名基本规则:
- 变量名不能是系统关键字。
- 变量名只能由数字、字母、下划线组成,不能以数字开头。
- 变量名区分大小写。
-
变量命名规范:
- 见名知意。
- 驼峰命名法
- 大驼峰命名法:变量中每个单词的首字母都大写。(FirstName)
- 小驼峰命名法:变量中从第二个单词开始首字母大写。(firstName)
-
-
查看系统关键字
from keyword import kwlist print(kwlist) # ['False', 'None', 'True', 'and', 'as', 'assert', 'async', 'await', 'break', 'class', 'continue', 'def', 'del', 'elif', 'else', 'except', 'finally', 'for', 'from', 'global', 'if', 'import', 'in', 'is', 'lambda', 'nonlocal', 'not', 'or', 'pass', 'raise', 'return', 'try', 'while', 'with', 'yield'] (版本3.8) FirstName = 10 print(FirstName) # 10 -
变量保存的数据
变量可以保存任意类型的数据
-
Python中需要学的数据类型
数字(整型、浮点型、复数)、字符串、布尔(True、False)、列表、元组、集合、字典等
Type方法
type: 查看数据类型
整型(int)、浮点型(float)、布尔(bool)、字符串(str)# 列表(list)、元组(tuple)、集合(set)、字典(dict)
a = 2837490288402845
print(a + 1)
b = 10.1
c = True
d = 'abc'
e = [1, 2, 3]
f = (1, 2, 3)
g = {1, 2, 3}
h = {'name': '张三'}
print(type(a)) # <class 'int'>
print(type(b)) # <class 'float'>
print(type(c), type(d), type(e), type(f), type(g), type(h))
# <class 'bool'> <class 'str'> <class 'list'> <class 'tuple'> <class 'set'> <class 'dict'>
输入和输出
-
输入 -
inputnum = input('Enter a num:') print(num, type(num)) # num <class 'str'>输入的结果输出以后一定是一个字符串
-
输出 -
printprint能够将程序中任意数据打印出来,将数据规范的打印出来,供程序员辨别数据。
\n(换行):转义字符 - 具有特殊含义的字符\t(制表符tab)- 结束字符串 —
end='\n'(默认自动换行,可以不写) - 分隔字符串 —
sep=' '(默认一个空格,可以不写)
print(10, 10 + 100, 10 < 30, '1234') # 10 110 True 1234 # 结束字符串 --- end='\n' print(10, 10 + 100, 10 < 30, '1234', end='\n') print(123) """ 10 110 True 1234 123 """ print(10, 10 + 100, 10 < 30, '1234', end='') print(123) """ 10 110 True 1234123 """ print(10, 10 + 100, 10 < 30, '1234', end='\t') print(123) """ 10 110 True 1234 123 """ # 分隔字符串 --- sep=' '(默认一个空格,可以不写) print(10, 10 + 100, 10 < 30, '1234', sep='♥') """ 10♥110♥True♥1234 """sep、end必须放到print的末尾
格式化输出
-
format()方法 - 配合{}向一个文本中任意传参
format中数据的个数需要和{}的数量保持一致。
name = input('请输入姓名:') age = input('请输入年龄:') str1 = '小明今年20岁' print(name, '今年', age, '岁', sep='') str2 = '{}今年{}岁'.format(name, age) print(str2) name = '张三' age = 20 '{}今年{}'.format(name, age)格式化输出的结果的数据据类型一定是字符串(字符串:使用引号引起来的一堆符号的合集)
-
f-字符串
str3 = f'{name}今年{age}岁' print(str3) -
千分位符
,:,- 表示将传进来的数据每隔三位使用,分隔一次num = 389272983748921 a = '{:,}'.format(num) print(a) # 一般会应用于银行余额存款等 -
数字格式化 - 百分比格式
:.2%
4.0.4565 --> 45.65%
b = '{:.2%}'.format(67/100)
print(b)
-
时间 2022-6-21 15:28:40
2022-6-21 15:28:40 %Y-%m-%d %H-%M-%S - 表示时分秒 from datetime import datetime time_ = datetime(2022, 6, 21, 15, 33, 40) print(time_) c = '{:%Y-%m-%d %H-%M-%S}'.format(time_) print(c) -
对齐
<- 左对齐>- 右对齐^- 居中对齐:>n- 将数据使用空格拓展成长度为N的字符串,并且右对齐:0>n- 将数据使用0拓展为长度为n的字符串并且右对齐
""" * ** *** """ """ * ** *** """ print('*') print('**') print('***') """ * ** *** """ print('{:>3}'.format('*')) print('{:>3}'.format('**')) print('{:>3}'.format('***')) """ * ** *** """ print('{:♥>3}'.format('*')) print('{:0>3}'.format('**')) print('{:0>3}'.format('***')) """ ♥♥* 0** *** """
Python运算符
数学运算符、赋值运算符、比较运算符、逻辑运算符、位运算符…
数学运算符
| 数学运算符 | 名称 |
|---|---|
+ | 加 |
- | 减 |
* | 乘 |
/ | 除 |
% | 取余、取模 |
// | 整除 |
** | 幂运算 |
-
+、-、*中,如果运算对象全部是整数,运算结果为整型;如果运算类型有浮点数,则输出结果位浮点型 -
/,不管运算对象是什么类型,运算结果类型均为浮点数print(5 / 2, 6 / 2) # 2.5 3.0 -
//- 整除:向小取整,任何除法运算得到的结果均为整数(int)print(2 // 3) # 0 -
%- 取余print(5 % 2) # 1 print(2 % 3) # 2 -
**- 幂运算:涉及到开方结果均为浮点型(float)print(2 ** 2) # 4 print(4 ** 0.5) # 2.0 print(4 ** 2.5) # 32.0 print(4 ** -0.5) # 0.5
逻辑运算符
| 逻辑运算符 | 名称 |
|---|---|
and | 逻辑与运算 |
or | 逻辑或运算 |
not | 逻辑非运算 |
逻辑运算结果均为布尔值
-
and- 条件1 and 条件2,条件1和条件2都成立时,整体条件成立。反之,条件不成立。
中断:and左边条件不成立时,and右边条件无需再判断。print(True and True) # True print(True and False) # False print(False and False) # Falsetips: 复制快捷键 ctrl + d 直接复制到下一行 无需选择整体 只需选中需要复制的那一行的任意位置
-
or- 条件1 or 条件2,条件1和条件2至少有一个成立,整体成立。都不成立时,整体才不成立。
中断:or左边条件成立时,or右边条件无需再判断。print(True or True) # True print(False or True) # True print(False or False) # False -
not- 取反,反义词print(not True)
练习
判断闰年:
1. 能被4整除但是不能被100整除。
2. 能被400整除。
地球公转:365天5小时40+分钟。
# 练习:判断一个年份是否时闰年(0=False,非0均为True)
# bool() - 能够将数据转为布尔值
year = 2004
print((not year % 4 and year % 100) or not year % 400)
print(bool(not year % 4 and year % 100) or not year % 400)
print(False or False) # False
- `not` - 取反,反义词
```python
print(not True)
练习
判断闰年:
1. 能被4整除但是不能被100整除。
2. 能被400整除。
地球公转:365天5小时40+分钟。
# 练习:判断一个年份是否时闰年(0=False,非0均为True)
# bool() - 能够将数据转为布尔值
year = 2004
print((not year % 4 and year % 100) or not year % 400)
print(bool(not year % 4 and year % 100) or not year % 400)
本文详细讲解了Python初学者必备的基础知识,包括创建项目时的路径规范、注释的作用及不同类型的注释,以及变量的概念、命名规则和常见数据类型。通过实例演示了如何定义和操作变量,以及输入输出、格式化输出和运算符的使用。
4787

被折叠的 条评论
为什么被折叠?



