好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

Python3基础语法注意点

文章内容参考了菜鸟教程

数字(Number)类型 python中数字有四种类型:整数、布尔型、浮点数和复数。 int (整数), 如 1, 只有一种整数类型 int,表示为长整型,没有 python2 中的 Long。

字符串(String) python中单引号等同双引号使用。 转义符:,可以用来转义,使用r可以让反斜杠不发生转义。。 如 r"this is a line with \n" 则\n会显示,并不是换行。 Python中的字符串不能改变。

实例

!/usr/bin/python3

str=‘hello world!‘

print(str) # 输出字符串 print(str[0:-1]) # 输出第一个到倒数第二个的所有字符 print(str[0]) # 输出字符串第一个字符 print(str[2:5]) # 输出从第三个开始到第五个的字符 print(str[2:]) # 输出从第三个开始的后的所有字符 print(str * 2) # 输出字符串两次 print(str + ‘Python‘) # 连接字符串

print(‘------------------------------‘)

print(‘Pytho\n hello world!‘) # 使用反斜杠()+n转义特殊字符 print(‘Pytho\n hello world!‘) # 在字符串前面添加一个 r,表示原始字符串,不会发生转义

输出结果: hello world! hello world h llo llo world! hello world!hello world! hello world!Python ------------------------------ Pytho hello world! Pytho\n hello world!

空行 函数之间或类的方法之间用空行分隔,表示一段新的代码的开始。类和函数入口之间也用一行空行分隔,以突出函数入口的开始。 空行的作用在于分隔两段不同功能或含义的代码,便于日后代码的维护或重构。 空行也是程序代码的一部分。

等待用户输入

!/usr/bin/python3

input("\n") 以上代码中 ,"\n"在结果输出前会输出一个新的空行。一旦用户按下 enter 键时,程序将退出。

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

!/usr/bin/python3

x="a" print( x, end=" " )

import 与 from...import 在 python 用 import 或者 from...import 来导入相应的模块。 将整个模块(somemodule)导入,格式为: import somemodule 从某个模块中导入某个函数,格式为: from somemodule import somefunction 从某个模块中导入多个函数,格式为: from somemodule import firstfunc, secondfunc, thirdfunc 将某个模块中的全部函数导入,格式为: from somemodule import *

查看更多关于Python3基础语法注意点的详细内容...

  阅读:18次