python_fullstack-小知识点

Python 小知识点

一、=、==、is、id()

1、=(赋值)

a = 'yang'
print(a)
>>> yang

2、==(比较值是否相等)

a = 'yang'
b = 'yang'
print(a == b)
>>> True

3、is(比较内存地址是否相等)

a = 666
b = 666
print(a is b)
>>> False

4、id(内存地址)

a = 666
b = 888
print(id(a))
print(id(b))

>>> 6049680
>>> 10522416

二、小数字池

  1. 数字,字符串 小数据池

    • 数字的范围 -5 – 256
  2. 字符串:

    • 不能有特殊字符
    • s*20 还是同一个地址,s*21以后都是两个地址

三、python3 编码

1、知识回顾

ascii
            A : 00000010  8位 一个字节

unicode     A : 00000000 00000001 00000010 00000100 32位  四个字节
            中:00000000 00000001 00000010 00000110 32位  四个字节


utf-8      A :  00100000 8位 一个字节
          中 :  00000001 00000010 00000110 24位 三个字节


gbk        A : 00000110  8位 一个字节
         中  : 00000010 00000110 16位 两个字节
1、各个编码之间的二进制,是不能互相识别的,会产生乱码。
2、文件的储存,传输,不能是unicode(只能是utf-8 utf-16 gbk,gb2312,asciid等)

2、python3中的编码

  1. string类型在内存中是用Unicode编码
  2. bytes类型:
英文:
str 
    表现形式: s = 'yang'
    编码方式:unicode
bytes
    表现形式:b'yang'
    编码方式:除unicode外的其他编码

中文:
str 
    表现形式: s = '中国'
    编码方式:unicode 
bytes
    表现形式:b'\xe4\xb8\xad\xe5\x9b\xbd'
    编码方式:除unicode外的其他编码

3、python3种str类型与bytes类型的互相转换

① str ——>bytes
# encode()方法,编码

s = 'yang'
print(s,type(s))
print(s.encode('utf-8'),type(s.encode('utf-8')))

>>> yang <class 'str'>
>>> b'yang' <class 'bytes'>

# -------------------------------------------------
c = '中国'
print(c,type(c))
print(c.encode('utf-8'),type(c.encode('utf-8')))

>>> 中国 <class 'str'>
>>> b'\xe4\xb8\xad\xe5\x9b\xbd' <class 'bytes'>
① bytes ——>str
# decode()方法,解码

s = b'yang'
print(s,type(s))
print(s.decode('utf-8'),type(s.decode('utf-8')))

>>> b'yang' <class 'bytes'>
>>> yang <class 'str'>

# -------------------------------------------------
c = b'\xe4\xb8\xad\xe5\x9b\xbd'
print(c,type(c))
print(c.decode('utf-8'),type(c.decode('utf-8')))

>>> b'\xe4\xb8\xad\xe5\x9b\xbd' <class 'bytes'>
>>> 中国 <class 'str'>

四、shebang符号(#!)

这里写图片描述
参考资料:
https://en.wikipedia.org/wiki/Shebang_(Unix)

1.作用

  1. 如果脚本文件中没有#!这一行,那么它执行时会默认用当前Shell去解释这个脚本(即:$SHELL环境变量)。

  2. 如果#!之后的解释程序是一个可执行文件,那么执行这个脚本时,它就会把文件名及其参数一起作为参数传给那个解释程序去执行。

  3. 如果#!指定的解释程序没有可执行权限,则会报错“bad interpreter: Permission denied”。
    如果#!指定的解释程序不是一个可执行文件,那么指定的解释程序会被忽略,转而交给当前的SHELL去执行这个脚本。

  4. 如果#!指定的解释程序不存在,那么会报错“bad interpreter: No such file or directory”。
    注意:#!之后的解释程序,需要写其绝对路径(如:#!/bin/bash),它是不会自动到$PATH中寻找解释器的。

  5. 当然,如果你使用”bash test.sh”这样的命令来执行脚本,那么#!这一行将会被忽略掉,解释器当然是用命令行中显式指定的bash。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值