python基础语法
注释的方法
- 单行注释:#
#python
- 多行注释:六个单引号/六个双引号
'''
python
'''
"""
python
"""
- 注释快捷键(添加/删除注释):ctrl+/(?)
单引号和双引号的区别
无本质区别,但是在一行代码中需要多次用到单双引号的时候不能都使用单引号/双引号
eg:
正确的写法
print("姓名:'cindy'")
错误的写法:
print("姓名:"cindy"")
标识符
- 作用:作为变量、函数、类、模块以及其他对象的名称
- 命名规则:只能数字,字母和下划线并且不能用数字开头,并且标识符命名不能有python关键字
python关键字:[‘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’]
python关键字的查看方法:import keyword print(keyword.kwlist)
输入和输出
输出(print):
- 单个输出:
print('hello python')
- 多个输出(一般自动换行)
print('hello python')
print('hello world')
结果:
D:\python3.7\python.exe D:/Pycharm_workspace/test_cindy/app_homework/test.py
hello python
hello world
Process finished with exit code 0
- 多个输出不换行
print('hello python',end='')
print('hello world')
结果:
D:\python3.7\python.exe D:/Pycharm_workspace/test_cindy/app_homework/test.py
hello pythonhello world
Process finished with exit code 0
输入(input):
输入的是字符串
name=input('姓名:')
age=input('年龄:')
gender=input('性别:')
print(name,age,gender)
变量
命名规则:只能数字,字母和下划线并且不能用数字开头
命名风格(常用的三种如下):
-
全小写加下划线(单词之间加下划线)
eg.account_name -
大驼峰
eg.AccountName -
小驼峰
eg.accountName