Python3基本语法

0,当前版本

[root@centos7 Python-3.6.0]# python
Python 3.6.0 (default, Apr  2 2017, 17:16:19) 
[GCC 4.8.5 20150623 (Red Hat 4.8.5-11)] on linux
Type "help", "copyright", "credits" or "license" for more information.

1,编码

默认情况,python3的源码文件时UTF-8,所有字符串都是unicode字符串,如果可以,指定编码:

# -*- coding:utf-8 -*-

2,标识符

  2.1 第一个字符必须是字符或者下划线

  2.2 标识符其他的部分有字母,数字,下划线

  2.3 大小写敏感

3,保留字

>>> keyword.kwlist
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'keyword' is not defined
>>> 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']

4,注视

单行注视以#开头

多行注释以三个单引号,或者三个双引号括起来

5,缩进

python必须严格执行代码缩进,所以不需要使用c等其他语言的括符,缩进只要对齐即可,并没有要求缩进符个数

>>> if True:
...     print("china");
... else:
...     print('USA');
... 
china
>>> if True:
...     print("china");
... else:
...     print('USA');
...      print('11');
  File "<stdin>", line 5
    print('11');
    ^
IndentationError: unexpected indent

6,多行语句

  使用反斜杠表示多行语句

>>> sum=1+\
...     2;
>>> print(sum);
3
>>> sum=1+\
... 2;
>>> print(sum);
3

7,数据类型

  总有四种类型,整形,长整形,浮点数和复数

    整数, 如 1

    长整数 是比较大的整数

    浮点数 如 1.23、3E-2

    复数 如 1 + 2j、 1.1 + 2.2j

8,字符串

  单引号,双引号使用完全相同,参照5缩进部分

  使用三引号('''或""")可以指定一个多行字符串

  转移符'\'

  通过在字符串前加r或R。 如 r"hello world \n" 则\n会显示,并不是换行

  处理unicode字符串,加前缀u或U

  字符串是不可变的

  按字面意义级联字符串,如"hello " "world " 会被自动转换hello world 

>>> s1="""hello
... world"""
>>> print(s1);
hello
world

9,空行

函数之间或类的方法之间用空行分隔,表示一段新的代码的开始。类和函数入口之间也用一行空行分隔,以突出函数入口的开始。

空行与代码缩进不同,空行并不是Python语法的一部分。书写时不插入空行,Python解释器运行也不会出错。但是空行的作用在于分隔两段不同功能或含义的代码,便于日后代码的维护或重构。

10,等待用户的输入

>>> s2=input("\n >")

 >china
>>> print(s2)
china

11,同一行输入多行语句

>>> import sys;x = 'hello!'; sys.stdout.write(x)
hello!6
>>> import sys;x = 'hello!!'; sys.stdout.write(x)
hello!!7

12,代码组

缩进相同的一组语句构成一个代码块,称之代码组

if、while、def和class这样的复合语句,首行以关键字开始,以冒号( : )结束,该行之后的一行或多行代码构成代码组

将首行及后面的代码组称为一个子句(clause)

13,print输出

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

>>> print(x,end="")
hello!!>>> print(s1,end="")
china>>> print()

14,命令行参数

[root@centos7 Python-3.6.0]# python -h
usage: python [option] ... [-c cmd | -m mod | file | -] [arg] ...
Options and arguments (and corresponding environment variables):
-b     : issue warnings about str(bytes_instance), str(bytearray_instance)
         and comparing bytes/bytearray with str. (-bb: issue errors)
-B     : don't write .py[co] files on import; also PYTHONDONTWRITEBYTECODE=x
-c cmd : program passed in as string (terminates option list)
-d     : debug output from parser; also PYTHONDEBUG=x
-E     : ignore PYTHON* environment variables (such as PYTHONPATH)
-h     : print this help message and exit (also --help)
。。。




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

朝闻道-夕死可矣

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值