python3基础语法(1)

1. 标识符

  • 第一个字符必须是字母表中字母或下划线 _ 。
  • 标识符的其他的部分由字母、数字和下划线组成。
  • 标识符对大小写敏感。
  • python3允许中文标识符,但不建议使用

2. 保留字

import keyword
print(keyword.kwlist)

打印结果:['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']
 

3. 注释

单行注释:#

多行注释:’‘’ ‘’‘或者“”“ ”“”

#单行注释
print('hello world')
'''
多行注释
第一行
第二行
第三行
'''
print('hello')
"""
第四行
第五行
第六行
"""
print('world')

4. 多行语句

Python 通常是一行写完一条语句,但如果语句很长,我们可以使用反斜杠 \ 来实现多行语句

one = 1
two = 2
three = 3
totol = one + \
    two + \
    three
print(totol)

5. 数字类型

python中数字有四种类型:整数、布尔型、浮点数和复数。

  • int (整数), 如 1, 只有一种整数类型 int,表示为长整型,没有 python2 中的 Long。
  • bool (布尔), 如 True。
  • float (浮点数), 如 1.23、3E-2
  • complex (复数), 如 1 + 2j、 1.1 + 2.2j

6. 字符串

  • 字符串使用单引号或者双引号括起来
  • 使用三引号('''或者""")可以指定多行字符串
  • 转义符\
  • 使用r可以让反斜杠不发生转义
  • 字符串可以用+运算符连接字符串,*运算符重复字符串
  • 字符串有两种索引方式:从左往右0开始,从右往左-1开始
  • python中字符串不能改变
  • python中没有单独的字符类型,一个字符就是长度为1的字符串
  • 字符串的截取语法:变量[头下标:尾下标:步长],步长省略时默认为1
str1 = 'str1'
str2 = "str2"
str3 = '''hello
world'''
str4 = 'hello\nworld'
str5 = r'hello\nworld'
str6 = str1 + str2
str7 = str1*3
str8 = str5[0:5] #hello
str9 = str6[-1:-6:-1] #2rts1

7. 等待用户输入

input()函数,输入enter结束输入

input("请输入你的名字:")

8. print输出

print() 默认输出是换行的,如果要实现不换行需要在变量末尾加上 end=""

name = 'cyn'
print('你的名字是:',name)
print('hello',end=" ")
print('world')

9. 同一行显示多条语句

不同语句用分号隔开即可

print('hello',end=' ');print('world')

10. import与from...import

在 python 用 import 或者 from...import 来导入相应的模块。

import导出整个模块;from...import从模块中导出我们需要的函数

将整个模块(somemodule)导入,格式为: import somemodule

从某个模块中导入某个函数,格式为: from somemodule import somefunction

从某个模块中导入多个函数,格式为: from somemodule import firstfunc, secondfunc, thirdfunc

将某个模块中的全部函数导入,格式为: from somemodule import *

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值