Python3语法学习小记

您所在的位置:网站首页 python语句分为 Python3语法学习小记

Python3语法学习小记

2024-07-17 15:03| 来源: 网络整理| 查看: 265

基础语法:

1.可以使用斜杠( \)将一行的语句分为多行显示,例如:

z = 100 + \ 20

语句中包含[], {} 或 () 括号就不需要使用多行连接符。

2.单引号、多引号都可以使用,但是要注意前后对应,不能混搭。三引号(''' or """)可用来做多行注释,也可用来表示跨多行段落。

y = '''home is in sy''' 变量类型

Python中的变量不需要声明,变量的赋值操作既是变量声明和定义的过程。

字符串:

1.获取一段子串,使用变量[头下标:尾下标],+ 是字符串连接运算符,* 是重复操作。

str[2:5] # 字符串中第三个至第五个之间的字符串 str[2:] # 从第三个字符开始的字符串 print(str * 2) # 输出字符串两次 print(str + "TEST") # 输出连接的字符串

2.更新字符串:将第几位之后的字符进行更改(不是连接)。

str[:4] + "python"

3.字符串成员运算符:

in:如果字符串中包含给定的字符返回 True

not in: 如果字符串中不包含给定的字符返回 True

if x not in a: print("x is in a[]") else: print("x is not in a[]")

4.字符串有许多内建函数https://www.w3cschool.cn/python/python-strings.html

列表List

列表用[ ]标识,列表的数据项不需要具有相同的类型。与字符串的索引一样,列表索引从0开始,使用方括号的形式截取字符。也可使用 + 和 *

a = ['abc', 123, 'joy', 'ooo'] a[2] = 'Alex' a.append('990') del a[1] print(a[2:])

列表同样也有很多函数和方法:https://www.w3cschool.cn/python/python-lists.html

元组Tuple

元组用"()"标识。内部元素用逗号隔开。但是元素不能二次赋值,相当于只读列表,但可以对元组进行连接组合。与列表用法类似

Tuple = ('max', 'edit', 299) print(Tuple[0:1]) 字典Dictionary

字典是无序的对象集合。 字典当中的元素是通过键来存取的,字典用"{ }"标识。字典由索引(key)和它对应的值value组成。键必须是唯一的,但值则不必。值可以取任何数据类型,但键必须是不可变的。

Dict = {'is': 'my python', '4': 90} print(Dict) print(Dict.keys()) 逻辑运算符

and 、or 、not与c语言中&&、||、!的用法类似

if语句 if 判断条件: 执行语句 elif 判断条件2: 执行语句2 else: 执行语句3

python 并不支持 switch 语句,所以多个条件判断,只能用 elif 来实现。注意冒号。

while循环 while 判断条件: 执行语句

else 中的语句会在循环正常执行完的情况下执行.

while x > 40: print("x is %d now" % x) x -= 10 else: print("one is end") for循环

for循环可以遍历任何序列的项目,如一个列表或者一个字符串。

for letter in 'Python': print('当前字母 :', letter)

for … else ,for 中的语句和普通的没有区别,else 中的语句会在循环正常执行完(即 for 不是通过 break 跳出而中断的)的情况下执行.

函数

函数代码块以def关键词开头,后接函数标识符名称和圆括号()。

任何传入参数和自变量必须放在圆括号中间。圆括号之间可以用于定义参数。

函数的第一行语句可以选择性地使用文档字符串—用于存放函数说明。

def display1(str1): print(str1 * 3)   return

使用关键字参数允许函数调用时参数的顺序与声明时不一致,即乱序传参。

匿名函数:使用lambda表达式:

sum2 = lambda arg1, arg2: arg1 + arg2 print('Value of total : ', sum2(10, 20)) print("Value of total : ", sum2(20, 20)) 文件I/O

从键盘输入使用input()函数:

str1 = input("what do u want to say:") print("u want to say:", str1)

打开关闭写入读取文件:

fo = open("foo.txt", "w") fo.write("\nthis is a dream.") fo.close() fo = open("foo.txt", "r") string = fo.read() position = fo.tell() print(string, "\n", position)

还有很多文件方法:https://www.w3cschool.cn/python/file-methods.html

异常处理

try/except语句用来检测try语句块中的错误,从而让except语句捕获异常信息并处理。

try: fo = open("f.txt", "w") fo.write("\nmay be this is a dream.") except IOError: print("ERROR:can't find the file or read it.") else: print("written content in the file successfully") fo.close() fo = open("foo.txt", "r") string = fo.read() position = fo.tell() print(string, "\n", os.getcwd()) # 获取文件地址函数

try-finally 语句无论是否发生异常都将执行最后的代码。可以使用except语句或者finally语句,但是两者不能同时使用。else语句也不能与finally语句同时使用.

try: fh = open("testfile", "r") fh.write("This is my test file for exception handling!!") finally: print("Error: can\'t find file or read data")


【本文地址】


今日新闻


推荐新闻


    CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3