【语法01】Python基本语法

基本语法

#Python保留字
import keyword
keyword.kwlist
['False',
 'None',
 'True',
 'and',
 'as',
 'assert',
 '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']
#注释
print("Hello Python")
#注释1
'''
注释2
'''
"""
注释3
"""
Hello Python





'\n注释3\n'
#行与缩进

#缩进的空格数是可变的,但是同一个代码块的语句必须包含相同的缩进空格数

if True:
    print("True")
else:
    print("False")
True
if True:
    print("True1")
    print("True2")
else:
    print("False")
print("Ecust")
True1
True2
Ecust
#多行语句

#语句过长可以使用反斜杠(\)来实现多行语句

total = 1 + \
2 + 3

print(total)

#在[],{},()中的多行语句,不需要使用反斜杠

total1 = ['a',
          'b','c']

print(total1)
6
['a', 'b', 'c']
#数字类型
#int(整数),表示为长整型,没有long
#bool
#float
#complex 复数
#字符串
#单引号和多引号使用完全相同
#转义符'\'
#反斜杠可以用来转义,使用r可以使反斜杠不转义
print("\nEcust\n")
print(r"\nEcust\n")
#字符串可以用+运算符连接在一起
Ecust = "Ecust" + "Handsome"
print(Ecust)
#字符串有两种索引方式,从左往右从0开始,从右往左从-1开始
#字符串不能改变
#python没有单独的字符类型
#字符串的截取语法:变量[头下标:尾下标]
str = "ECUST"
print(str)
print(str[0:-1])
print(str[2:4])
print(str[2:])
print(str[:-2])
print(str * 2)
print(str + "NB")
Ecust

\nEcust\n
EcustHandsome
ECUST
ECUS
US
UST
ECU
ECUSTECUST
ECUSTNB
#用户输入
input("\n\n按下enter键后退出")
按下enter键后退出





''
#同一行显示多条语句
#可以使用分号(;)分割以使用多条语句
import sys; x = 'ECUST'; sys.stdout.write(x + '\n')
ECUST
#代码组
if expression:
    suite
elif expression:
    suite
else:
    suite
#print输出
x = 'a'
y = 'b'
print(x)
print(y)
#不换行输出
print(x,end = ' ')
print(y,end = ' ')
a
b
a b 
#将整个模块导入 import somemodule
#将某个模块中导入某个函数 from somemodule import somefunction
#将某个模块中导入多个函数 from somemodule import firstfunc,secondfunc,thirdfunc
#将某个模块中的全部函数导入 from somemodule import *

#导入sys模块
import sys
for i in sys.argv:
    print(i)
print('\n python 路径为',sys.path)

from sys import argv,path
print('path:',path)#因为已经导入path成员,所以此处引用时不需要加sys.path
G:\Anaconda3\lib\site-packages\ipykernel_launcher.py
-f
C:\Users\asus\AppData\Roaming\jupyter\runtime\kernel-129790c9-1b68-4f46-a22f-3f22c933a8d2.json

 python 路径为 ['', 'G:\\Anaconda3\\python36.zip', 'G:\\Anaconda3\\DLLs', 'G:\\Anaconda3\\lib', 'G:\\Anaconda3', 'G:\\Anaconda3\\lib\\site-packages', 'G:\\Anaconda3\\lib\\site-packages\\win32', 'G:\\Anaconda3\\lib\\site-packages\\win32\\lib', 'G:\\Anaconda3\\lib\\site-packages\\Pythonwin', 'G:\\Anaconda3\\lib\\site-packages\\IPython\\extensions', 'C:\\Users\\asus\\.ipython']
path: ['', 'G:\\Anaconda3\\python36.zip', 'G:\\Anaconda3\\DLLs', 'G:\\Anaconda3\\lib', 'G:\\Anaconda3', 'G:\\Anaconda3\\lib\\site-packages', 'G:\\Anaconda3\\lib\\site-packages\\win32', 'G:\\Anaconda3\\lib\\site-packages\\win32\\lib', 'G:\\Anaconda3\\lib\\site-packages\\Pythonwin', 'G:\\Anaconda3\\lib\\site-packages\\IPython\\extensions', 'C:\\Users\\asus\\.ipython']

基本数据类型

#多个变量赋值
a = 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值