Python[基础]笔记回顾(一)

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>

return
true
false
Command1
'while'-Condition
Command2
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,20,1,21,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.不能与**“保留字”**同名

andasassertbreakclasscontinuedefdelifelifelse
exceptFalsefinallyforfromgloballambdaimportNoneinis
notorpassraiseTruenonlocalreturnYieldwhiletrywith

p y t h o n 保留字表 ( 部分 ) python保留字表(部分) python保留字表(部分)

(三)变量

<变量Var>=<表达式>

(四)常量(不存在)

Python无法定义常量

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值