Python3 菜鸟教程 笔记1

菜鸟教程传送门 https://www.runoob.com/python3/

环境:Windows + python-3.7.4

$ 查看python版本
python -V ,注意:大写的V

例如以下是 helloworld.py 的内容
1)执行 python helloworld.py , "#!/usr/bin/python3" 被忽略,相当于注释
2)执行 ./helloworld.py , "#!/usr/bin/python3"  指定解释器的路径

#!/usr/bin/python3
print("Hello, World!")

$ 查看python保留字
>>> import keyword
>>> keyword.kwlist

$ 多行语句 -> 行末用反斜杠(\),注意: [], {}, 或 () 中的多行语句,不需要使用反斜杠(\)

$ 同一行中使用多条语句 -> 语句之间使用分号(;)分割

$ 不换行输出
print 默认输出是换行的,如果要实现不换行需要在变量末尾加上 end="":
[例]
>>> print("Good");print("Evening")
Good
Evening
>>> print("Good",end="");print("Evening",end="")
GoodEvening
>>> 

$ import 与 from...import
1)将整个模块导入,例如:import time,在引用时格式为:time.sleep(5)
2)将整个模块中全部函数导入,例如:from time import *,在引用时格式为:sleep(5)
3)将模块中特定函数导入,例如:from time import sleep,在引用时格式为:sleep(5)
4)将模块换个别名,例如:import time as abc,在引用时格式为:abc.sleep(5)

 

$ Python3 的六个标准数据类型
不可变数据(3 个):Number(数字)、String(字符串)、Tuple(元组)
可变数据(3 个):List(列表)、Dictionary(字典)、Set(集合)

$ 变量的对象类型
>>> a, b, c, d = 20, 5.5, True, 4+3j
>>> print(type(a), type(b), type(c), type(d))
<class 'int'> <class 'float'> <class 'bool'> <class 'complex'>

还可以用 isinstance 来判断:
>>> isinstance('aaa',str)
True
>>>

注意:isinstance 和 type 的区别  -> type()不会认为子类是一种父类类型; isinstance()会认为子类是一种父类类型。

 

$ 列表切片实现 翻转字符串

将下文保存为test.py,运行得到输出  Honey and Bee

  def reverseWords(input):
     
    # 通过空格将字符串分隔符,把各个单词分隔为列表
    inputWords = input.split(" ")
 
    # 翻转字符串
    # inputWords[-1::-1] 第一个参数 -1 表示最后一个元素;第二个参数为空,表示移动到列表末尾;第三个参数为步长,-1 表示逆向
    inputWords=inputWords[-1::-1]
 
    # 重新组合字符串,以空格分隔
    output = ' '.join(inputWords)
     
    return output
 
if __name__ == "__main__":
    input = 'Bee and Honey'
    rw = reverseWords(input)
    print(rw)

$ 构造包含0个或1个元素的元组
tup1 = ()    # 空元组
tup2 = (2,) # 一个元素,需要在元素后添加逗号

$ 集合
可以使用大括号 {} 或者 set() 函数创建集合(可接收字符串、元组、列表)
注意:创建一个空集合必须用 set() 而不是 {},因为 {} 是用来创建一个空字典。

>>> a=set('Today is Thursday.')       #字符串
>>> b=set(('Today','is','Thursday'))      #元组
>>> c=set(['Today','is','Thursday'])       #列表
>>> a
{'o', 'y', 'r', 's', 'u', 'h', 'T', ' ', 'd', 'i', '.', 'a'}
>>> b
{'is', 'Today', 'Thursday'}
>>> c
{'is', 'Today', 'Thursday'}
>>> 

 

$ 构造函数 dict() 构建字典

[例] 1)
>>> dict([('Mon',1),('Tue',2),('Wed',3)])
{'Mon': 1, 'Tue': 2, 'Wed': 3}

注意:键值对用元组,以下列表/元组 包列表也可以,但是 集合包列表就不可以(集合中的元素必须是不可变类型)

>>> dict1=dict([[1,111],[2,222]])
>>> dict1
{1: 111, 2: 222}
>>> dict2=dict({[1,111],[2,222]})
Traceback (most recent call last):
  File "<pyshell#132>", line 1, in <module>
    dict2=dict({[1,111],[2,222]})
TypeError: unhashable type: 'list'

>>> dict3=dict(([1,111],[2,222]))
>>> dict3
{1: 111, 2: 222}
>>> 

2)

>>> dict(Jan=1,Feb=2,Mar=3)
{'Jan': 1, 'Feb': 2, 'Mar': 3}

3)

>>> {x: x**2 for x in (2, 4, 6)}
{2: 4, 4: 16, 6: 36}

创建空字典使用 { }。

$ 遍历字典
1)遍历字典的键:

>>> def printkey(dict):  # dict 是一个字典对象
    for k in dict:
        print(k)
>>> dict1=dict([('a',1),('b',1)])
>>> printkey(dict1)
a
b
>>> 

2)遍历字典的键值:

>>> for k in dict1:
    print(k,':',dict1[k])

    
a : 1
b : 1
>>> 
>>> for k in dict1:
    print(k,end=':')
    print(dict1[k])

    
a:1
b:1
>>>
>>> for k,v in dict1.items():
    print(k,':',v)

    
a : 1
b : 1
>>> 

$ Python成员运算符

运算符描述实例
in如果在指定的序列中找到值返回 True,否则返回 False1在 [1, 2, 3, 4, 5 ] 中,返回 True
not in如果在指定的序列中没有找到值返回 True,否则返回 False6不在 [1, 2, 3, 4, 5 ] 中,返回 False

 

 

 

 

 

$ Python身份运算符

运算符描述实例
isis 是判断两个标识符是不是引用自一个对象

x is y, 类似 id(x) == id(y) , 如果引用的是同一个对象则返回 True,否则返回 False

>>> a=1
>>> b=1
>>> a is b
True

>>> a=[1,2]
>>> b=[1,2]
>>> a is b
False

is notis not 是判断两个标识符是不是引用自不同对象

x is not y , 类似 id(a) != id(b)。如果引用的不是同一个对象则返回结果 True,否则返回 False。

>>> a = (1,2,3)
>>> b = a[:]
>>> b is not a
False
>>> 
>>> a = [1,2,3]
>>> b = a[:]
>>> b is not a
True
>>> 

 

 

 

 

 

 

 

 

 

 

 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值