python中的变量

**变量:**一个由程序员或用户定义的量且在程序中可能发生改变的量
#变量是所有程序的起点

变量的分类:

不可变型:无法对变量内的某一个单一元素进行修改、增加、删除
	字符串 - str - string - “变量”
	整数 - int - integer - 100
	浮点 - float - float - 3.111 1.0
	元组 - tuple - tuple - (1,2,"a","好")

可变型:可以对变量内的某一个单一或多个元素进行修改、增加、删除
	字典 - dict - dictionary - {"name":"张三","age":"33"}	
	列表 - list - list - [1,2,"张三","a"]

**赋值:**从右向左赋值

name = "张三"
name_list = ["张三","a"]

变量名字  —映射—  内存地址  —  内存存储

name —— 2131231 “张三”

变量的命名规则:

强制性规则
	1、变量开头不能是数字
	2、特殊字符不能出现在变量中( 除_ 外)
	3、变量名称不能出现空格
	4、变量名区分大小写A和a是两个变量
约定俗称的规则
	1、大驼峰 (ClassName一般用做类名)
	2、小驼峰 (defName一般用做方法名)
	3、下划线分割 (t_name一般用做变量名)
	4、不用0(零)和o(O)作为开头或结尾
import string
import random
name = 'gaobo'
sex = 'nv'
nia = 'nan'
high = 3

print("姓名%s,性别%s,年龄%s,身高%.2f" % (name, sex, nia, high))


list2 = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9", 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']
addlis = []
for i in range(8):
    qu = list2[random.randint(0, len(list2)-1)]
    print(qu)
    addlis.append(qu)
    print(addlis)
print("我的提取码:", "".join(addlis))


qu_li = "".join(random.sample((string.ascii_lowercase + string.digits), 10))

print("我的提取码:", qu_li)


'''

# name = "1"
# age = '2'
# sex = '3'
# word = '4'
# a = '5'
# print("%s"'%s''%s''%s' % (name, age, sex, word), "%s" %(a))
#
# rand = string.digits + string.ascii_letters

list1 = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9", 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']
add_list = []
for i in range(8):
    qu_1 = list1[random.randint(0, len(list1)-1)]
    add_list.append(qu_1)
print("".join(add_list))

add_l = "".join(random.sample((string.digits + string.ascii_lowercase), 8))
print(add_l)

# list = ["0","1","2","3","4","5","6","7","8","9",'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z']
# my_code = []
# for i in range(8):
#     qu_code = list[random.randint(0, len(list)-1)]
#     print(qu_code)
#     my_code.append(qu_code)
# a = ""
# print("我的提取码为", a.join(my_code) )
#
# a = ''.join(random.sample((string.digits + string.ascii_lowercase),8))
# print(a)
#
#
'''

```python
> 引用https://www.cnblogs.com/a389678070/p/9559360.html
tpl = "i am %s age %d" % ("alex", 18)
 
 tpl = "i am %(name)s age %(age)d" % {"name": "alex", "age": 18}
 
 tpl = "percent %.2f" % 99.97623
 
 tpl = "i am %(pp).2f" % {"pp": 123.425556, }
 
 tpl = "i am %.2f %%" % {"pp": 123.425556, }
 
2,Format方法:
tpl = "i am {}, age {}, {}".format("seven", 18, 'alex')
  
tpl = "i am {}, age {}, {}".format(*["seven", 18, 'alex'])
  
tpl = "i am {0}, age {1}, really {0}".format("seven", 18)
  
tpl = "i am {0}, age {1}, really {0}".format(*["seven", 18])
  
tpl = "i am {name}, age {age}, really {name}".format(name="seven", age=18)
  
tpl = "i am {name}, age {age}, really {name}".format(**{"name": "seven", "age": 18})
  
tpl = "i am {0[0]}, age {0[1]}, really {0[2]}".format([1, 2, 3], [11, 22, 33])
  
tpl = "i am {:s}, age {:d}, money {:f}".format("seven", 18, 88888.1)
  
tpl = "i am {:s}, age {:d}".format(*["seven", 18])
  
tpl = "i am {name:s}, age {age:d}".format(name="seven", age=18)
  
tpl = "i am {name:s}, age {age:d}".format(**{"name": "seven", "age": 18})
 
tpl = "numbers: {:b},{:o},{:d},{:x},{:X}, {:%}".format(15, 15, 15, 15, 15, 15.87623, 2)
 
tpl = "numbers: {:b},{:o},{:d},{:x},{:X}, {:%}".format(15, 15, 15, 15, 15, 15.87623, 2)
 
tpl = "numbers: {0:b},{0:o},{0:d},{0:x},{0:X}, {0:%}".format(15)
 
tpl = "numbers: {num:b},{num:o},{num:d},{num:x},{num:X}, {num:%}".format(num=15)

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值