python基本常识

python保留字

Python 的标准库提供了一个 keyword 模块,可以输出当前版本的所有关键字:

>>> 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']

python标识符要求

  • 第一个字符必须是字母表中字母或下划线 _。
  • 标识符的其他的部分由字母、数字和下划线组成。
  • 标识符对大小写敏感
  • 虽然在高版本中,中文名称等也允许,但是最好不用。
  • 关键字不能作任何标识符名称。
  • Python 3 的内置函数也不建议作为标识符名称。

标识符辨析

name结果
HelloChina合法
hanhong_gongyi合法
_/USA不合法,标识符中不允许出现“/”号
Eng#abc不合法,标识符中不允许出现“#”号
abc123合法
888abc不合法,标识符不允许数字开头
True不合法,使用了python关键字
__ab123_c合法
len合法,但最好不要用,为python内置函数

注释

  1. 单行注释以#开头。
  2. 多行注释可以用多个 # 号,还有 ’’'对"""对

注释举例

print("living by your wants will never make you happy.")
"""Although the world is full of suffering, it is full also of the overcoming of it。"""
print("we should strive to live by ideas and ideal.")
运行结果:
living by your wants will never make you happy.
we should strive to live by ideas and ideal.
print("Do not measure your life by what you've attained in terms of your desires.")
a = 666
b = 888
# a += 222
# b += 111
print("Measure your life by those small moments of integrity, compassion, rationality, enven self-sacrifice.")
print("show me the money!", a, b)
运行结果:
Do not measure your life by what you've attained in terms of your desires.
Measure your life by those small moments of integrity, compassion, rationality, even self-sacrifice.
show me the money! 666 888

第一个Python 程序

交互式编程

  • 交互式编程不需要创建脚本文件,是通过 Python 解释器的交互模式进来编写代码。
  • 安装python时已自带。
    在这里插入图片描述

脚本式编程

  • 通过脚本参数调用解释器开始执行脚本,直到脚本执行完毕。当脚本执行完成后,解释器不再有效。

  • Python 文件将以 .py 为扩展名。

  • 使用自带的shell打开py文件,run module即可执行。

  • 如果使用pycharm IDE,鼠标右击,选择执行即可。

使用Shell

在这里插入图片描述

使用Pycharm

在这里插入图片描述

行和缩进

  • Python 的代码块不使用大括号 {} 来控制类,函数以及其他逻辑判断。
  • 不用使用;作为一个语句的节数。
  • python 最具特色的就是用缩进来写模块。
  • 建议使用4个空格,在IDE中可以将TAB设置为4个空格。
  • 缩进的空白数量是可变的,但是同级块语句必须包含相同的缩进空白数量
print("Moonlight is spread over the capital.")
 print("The sound of pounding clothes far and near is brought by autumn wind.")
 运行结果:
     print("The sound of pounding clothes far and near is brought by autumn wind.")
    ^
IndentationError: unexpected indent
错误原因:同级的语句空格要相同,以上不一致。

多行语句

  • Python语句中一般以新行作为语句的结束符。

  • 可以使用斜杠将一行的语句分为多行显示,以增加可读性。

  • 语句中包含[], {} 或 () 括号就不需要使用多行连接符。

a = 1 +\
    2 +\
    3
print("a is:", a)
 运行结果:
 a is: 6
river = ["Changjiang",
         "Huanghe"]

a = (666, 888,
     168)

b = {"zhangsan":97, "lisi":99,
     "wangwu": 96}
     
print(river, a, b)
运行结果:
['Changjiang', 'Huanghe'] (666, 888, 168) {'zhangsan': 97, 'lisi': 99, 'wangwu': 96}

Python空行

  • 增强功能划分性。

  • 增强可读性。

  • 可用在函数之间或类的方法之间,或者想突出显示的地方。

同一行显示多条语句

c = 5; d = 6; e = 7; print("sum is:", c+d+e)
运行结果:
sum is: 18

备注:参考www.runoob.com的python部分。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Python的基本命令包括: - 打印Python版本:可以使用命令"python -?"、"python -h"或"python --help"来查看您正在运行的Python版本。 - 进入命令行:在命令行中输入"python"即可进入Python的交互式环境。 - 退出命令行:在交互式环境中,使用命令"exit()"或"quit()"退出Python的交互式环境。 - 运行Python脚本:在命令行中使用"python 文件名.py"命令来运行Python脚本,其中"文件名.py"是您要运行的Python脚本文件的名称。 - 安装Python:如果您的系统上没有安装Python,您可以使用命令"sudo apt update"和"sudo apt install python3"来安装Python 3版本。 - 设置Python解释器:如果您想将Python 3设置为默认的Python解释器,可以使用适用于您的操作系统的相应命令进行设置。例如,在某些Linux系统中,可以使用"sudo update-alternatives --install /usr/bin/python python /usr/bin/python3 1"命令来设置Python 3为默认解释器。 - 在Python交互式环境中运行代码:在Python的交互式环境中,您可以直接输入Python代码并立即执行,例如打印一条消息或进行简单的计算。 请注意,这些只是Python的一些基本命令,Python有更多的功能和命令可以用于不同的用途。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [全网最全的Python常见命令大全,建议收藏,以防备用](https://blog.csdn.net/SpringJavaMyBatis/article/details/127451276)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值