三目运算:

>>> 1 if 5>3 else 0
1
>>> 1 if 5<3 else 0
0

深浅拷贝:

一、数字和字符串

对于 数字 和 字符串 而言,赋值、浅拷贝和深拷贝无意义,因为其永远指向同一个内存地址。

import copy
# ######### 数字、字符串 #########
n1 = 123
# n1 = "i am alex age 10"
print(id(n1))
# ## 赋值 ##
n2 = n1
print(id(n2))
# ## 浅拷贝 ##
n2 = copy.copy(n1)
print(id(n2))
22219144
# ## 深拷贝 ##
n3 = copy.deepcopy(n1)
print(id(n3))
22219144

二、其他基本数据类型

对于字典、元祖、列表 而言,进行赋值、浅拷贝和深拷贝时,其内存地址的变化是不同的。

1、赋值

赋值,只是创建一个变量,该变量指向原来内存地址,如:

n1 = {"k1": "wu", "k2": 123, "k3": ["alex", 456]}
n2 = n1

python——杂货铺

2、浅拷贝

浅拷贝,在内存中只额外创建第一层数据

import copy
  
n1 = {"k1": "wu", "k2": 123, "k3": ["alex", 456]}
  
n3 = copy.copy(n1)

python——杂货铺

3、深拷贝

深拷贝,在内存中将所有的数据重新创建一份(排除最后一层,即:python内部对字符串和数字的优化

import copy
  
n1 = {"k1": "wu", "k2": 123, "k3": ["alex", 456]}
  
n4 = copy.deepcopy(n1)

python——杂货铺

函数扩展:发送邮件实例

def emil(p):
    import smtplib
    from email.mime.text import MIMEText
    from email.utils import formataddr
  
  
    msg = MIMEText('邮件内容', 'plain', 'utf-8')
  # From 后面的邮箱地址一定要跟发送邮箱地址相同,不然会认证失败 msg[
'From'] = formataddr(["武沛齐",'wptawy@126.com']) msg['To'] = formataddr(["走人",'424662508@qq.com']) msg['Subject'] = "主题" server = smtplib.SMTP("smtp.126.com", 25) server.login("wptawy@126.com", "邮箱密码") server.sendmail('wptawy@126.com', [p,], msg.as_string()) server.quit email("244591052@qq.com")

加密(hashlib)

import hashlib
# 设置自己的加密字符串,防止他人撞库破解
hash = hashlib.md5(bytes('sfa',encoding='utf-8'))
# 设置加密的密码
hash.update(bytes('123',encoding="utf-8"))
print(hash.hexdigest())
# b72a6beb24eef36c247274014dd4ade3
import hashlib

def md5(arg):
    hash = hashlib.md5(bytes('licheng', encoding='utf-8'))
    # 设置加密的密码
    hash.update(bytes(arg, encoding="utf-8"))

    return hash.hexdigest()

def register(user,pwd):
    with open("db",'a',encoding="utf-8") as f:
        temp = user + "|" + md5(pwd)+ "\n"
        f.write(temp)

def login(user,pwd):
    with open("db","r",encoding="utf-8") as f:
        for line in f:
            u, p = line.strip().split("|")
            if u == user and p == md5(pwd):
                return True


choose = input("1.登录\n 2.注册")
if choose == "2":
    user = input("用户名:")
    pwd = input("密码:")
    register(user,pwd)
elif choose == "1":
    user = input("用户名:")
    pwd = input("密码:")
    judge = login(user,pwd)
    if judge:
        print("登陆成功")
    else:
        print("登录失败")
实例:登录密码加密保存

相关文章:

  • 2021-11-03
  • 2021-12-27
  • 2021-12-18
  • 2021-08-19
  • 2021-05-04
  • 2021-08-30
猜你喜欢
  • 2021-11-22
  • 2022-01-22
  • 2022-01-23
  • 2022-01-09
  • 2021-10-05
相关资源
相似解决方案