您的位置:首页 > 编程语言 > Python开发

Python3 学习笔记1_基础语法_20180209

2018-02-09 21:55 573 查看
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# 学习网站:www.runoob.com

#****************************************************
#                   Python3 教程                      *
#****************************************************

#查看python版本: python3 -V

print("hello, World!");     #   hello, World!

#****************************************************
#                   Python3 基础语法                    *
#****************************************************

#============
#   编码
#============
# 默认情况下,python源码文件以UTF-8编码,所有字符串都是unicode字符串,当然
# 你也可以为源码文件指定不同的编码 本文件第2行即为示例。

#============
#   标识符
#============
# 1. 第一个字符必须是字母表中字母或下划线'_'
# 2. 标识符的其他部分由字母,数字和下划线组成。
# 3. 标识符对大小写敏感
# 4. 在python3中,非ascii标识符也是允许的了
# 5. 用于标识变量,函数等。

#============
#   保留字
#============
"""
>>> import keyword
>>> keyword.kwlist
['False', 'None', 'True', 'and', 'as', 'assert', 'break',
'class', 'continue', 'def', 'del', 'elif', 'else', 'except',
inally', 'for', 'from', 'global', 'if', 'import', 'in', 'is',
lambda', 'nonlocal', 'not', 'or', 'pass', 'raise', 'return',
try', 'while', 'with', 'yield']
"""

#============
#   注释
#============
# 这个是单行注释
'''
这是第一种多行注释
'''
"""
这是第二种多行注释
"""

#============
#   行与缩进
#============
"""
python最具特色的就是使用缩进来表示代码块,不需要使用大括号{}
缩进的空格数是可变的,但是同一个代码块的语句必须包含相同的缩进空格数
通常缩进一个tab,4个空格的位置。
"""

if True:
print("True")
#  print("111")
else:
print("False")

# 以上程序中print("111") 缩进不一致,会导致运行错误
# 报错:IndentationError: unindent does not match any outer indentation

#============
#   多行语句
#============
# python 通常是一行写完一条语句,但如果语句很长,我们可以使用反斜杠来实现
temp_one = 1
temp_two = 2
temp_three = 3
sum = temp_one + \
temp_two + \
temp_three

print(sum)              # 6

# 在[],{},()中的多行语句,不需要使用反斜杠
total = ['item_one', 'item_two', 'item_three',
'item_four', 'item_five']

#============
#   数据类型
#============
# python 中有四种类型:整数,长整数,浮点数和复数
"""
int(整数),如 1
long(长整数),比较大的整数
float(浮点数),如 1.23, 3E-2
complex(复数), 如 1 + 2j, 1.1 + 2.2j
"""

#============
#   字符串
#============
# 字符串是不可变的,这句话要深刻理解
# python中单引号和双引号使用完全相同。
str = 'this is a string'
print(str)                  # this is a string

str = "this is a string too"
print(str)                  # this is a string too

# 转义符'\'
str = "12 = \"1\" + \"2\""
print(str)                  # 12 = "1" + "2"

# 自然字符串(特殊字符失去意义,所见即所得),通过在字符串前加r或R
str = r"this is a line with \n"
str1 = R"this is a line with \r"
print(str)                  # this is a line with \n
print(str1)                 # this is a line with \r

# python 允许处理unicode字符串,加前缀u或U,如 u"this is an unicode string"
str = u"this is an unicode string"
print(str)                  # this is an unicode string

# 按字面意义级联字符串,如"this""is""string"会被自动转换为this is string
str = "this " "is " "string"
print(str)                  # this is string

# 使用三引号("""或''')可以指定一个多行字符串
str = """string have
many lines"""               # string have
print(str)                  # many lines
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: