Python[基础]笔记回顾(一) By:XLY23333
## 第一章 Python的基本语法文章目录
一.程序的格式框架
(一)缩进
1.缩进按键:TAB (等同于第个空格)
for/while等之后的包括内容进行缩进
(二)注释
1.注释符号:
#text | ‘’‘text’‘’( ↩ _\hookleftarrow ↩) | “”“text”“”( ↩ _\hookleftarrow ↩) |
---|
#text
'''
text01
text02
'''
"""
text01
text02
"""
(三)续行符
1.续航符:**\ **
print("ABCDEF")
#下方代码 等同 上方代码
print("ABC"\
"DEF")
output
ABCDEF
ABCDEF
二.基本语句
(一)基本输入输出
1.输入函数 input()
< Var >=input(< Hint >)
pdcode=input("请输入您的验证码:")
print(pdcode)
print(type(pdcode))#得到变量age的变量类型为'str',即字符串型 <class 'str'>
#test_data:114514aaa
#output:114514aaa
2.返回字符串的有效表达式 eval()
< Var >=eval(intput(< Hint >))
age=eval(input("请输入您的年龄:"))
print(age)
print(type(age))#得到变量age的变量类型为'int',即整型 <class 'int'>
#test_data:19
#output:19
3.输出函数 print()
(1)print(< Var >)
(2)print(<Var_1>,<Var_2>,…,<Var_n>)
(3)print(“text01{}text02{}text03{}”.format(<Var_1>,<Var_2>,<Var_3>))
A=1
a=b=c=1
print("aaa")
print(A,"aaa",'bbb')
print("text01{}text02{}text03{}".format(a,b,c))
output
aaa
1 aaa bbb
text011text021text031
(二)分支语句
1.单分支语句
if < Condition >:
<Command_1>
if 1>0:
print("YES!!")
#output:YES!!
2.双分支语句
if < Condition >:
<Command_1>else:
<Command_2>
if 1<=0:
print("YES!!")
else:
print("NO!!!")
#output:NO!!!
3.多分支语句
if < Condition_1>:
<Command_1>elif ** <Condition_2>:**
<Command_2>
⋯ \cdots ⋯
else:
<Command_2>
ps:if-elif-else具有自上而下的先后性
zfd=eval(input("Please input the test data:"))
if zfd>0:
print("testdata>0")
elif zfd<0:
print("testdata<0")
else:
print("testdata is 0")
#test_data_1:0
#output:testdata is 0
#test_data_1:-1919.810
#output:testdata<0
#test_data_1:114514
#output:testdata>0
(三)循环语句
1.条件循环语句
while(< Condition >):
<Command_1>
<Command_2>
count=0
while(count<3):
print("count={}+1={}".format(count,(count+1)))
count=count+1
print("Now count is 3")
output
count=0+1=1
count=1+1=2
count=2+1=3
Now count is 3
2.遍历循环语句
(1)for < Var > in <遍历结构>:
< Command >
for从遍历结构中逐个提取元素并放置循环变量中运行
ls=[1,3,5,7]
for i in ls:
print(i)
output
1
3
5
7
(2)for < Var > in range(<循环次数>)
< Command >
*循环次数的简写:
写法 | (0,3) | (3) | (1,3) |
---|---|---|---|
Var值 | 0,1,2 | 0,1,2 | 1,2 |
效果 | 0~2遍历,共3次 | 0~2遍历,共3次 | 1~2遍历,共2次 |
循环次数规则:留左不留右
循环_counts=(RightNumber-LeftNumber) 右数减左数
for i in range(1,3):
print(i)
output
1
2
三.基本语法元素
(一)引用
1.import语句
import <Function_Library_1>
import <Function_Library_2>
⋯ \cdots ⋯
import <Function_Library_n>
2.from语句
from <Function_Library> import <Fuction_Name>
from time import sleep
3.功能库自定义名称
import <功能库_Name> as <Anther_Name>
import time as T
4.import&from命名方式的区别
(1)import:自定义的函数名可以和库中函数的名字一样
(2)from:程序中不能用函数库中的名字定义函数
(二)命名规则
1.数字不能开头
2.不能与**“保留字”**同名
and | as | assert | break | class | continue | def | del | if | elif | else |
---|---|---|---|---|---|---|---|---|---|---|
except | False | finally | for | from | global | lambda | import | None | in | is |
not | or | pass | raise | True | nonlocal | return | Yield | while | try | with |
p y t h o n 保留字表 ( 部分 ) python保留字表(部分) python保留字表(部分)
(三)变量
<变量Var>=<表达式>
(四)常量(不存在)
Python中无法定义常量