赞
踩
一、变量名不能与python的关键字相同
二、变量使用字母、数字、下划线组成,首字母必须使用字母或者下划线
三、变量的命名尽量与所代表的内容意义相同
ip 变量名
var = input('请输入变量名')
if var[0] != '_' and not var[0].isalpha():
print('变量名不合理')
exit()
for i in var:
if i != '_' and not i.isalnum():
print('变量名不合理')
exit()
else:
print('变量名合理')
sum = 0
for i in range(100):
if i % 2 == 1:
sum += i
else:
sum -= i
print(sum)
i = 1
while i <= 12:
if i ==6 or i==10:
print(' ', end='')
elif i==12:
print(i)
else:
print(i,end=',')
i += 1
1). 注册新用户
2). 用户登录
3). 注销用户
4). 显示用户信息
5). 退出系统(exit(0))
dic = {'帐号':'密码'}
while True:
print("""
1). 注册新用户
2). 用户登录
3). 注销用户
4). 显示用户信息
5). 退出系统(exit(0))
""")
spt = int(input('请输入你对系统的管理'))
if spt == 1:
user=input('请输入注册的用户名')
if user in dic:
print('用户已经存在')
continue
else:
passwd=input('请输入用户的密码')
dic[user]=passwd
elif spt == 2:
user=input('请输入登录的用户名')
if not user in dic:
print('用户名不存在')
else:
passwd=input('请输入密码')
if dic[user]==passwd:
print('登录成功')
else:
print('密码错误')
elif spt == 3:
user=input('请输入需要注销的用户')
if not user in dic:
print('用户不存在')
else:
dic.pop(user)
elif spt == 4:
for i,j in dic.items():
print(i,j)
elif spt == 5:
print('用户退出')
exit()
else:
print('请输入正确的操作')
li = ['frdgrfgdsHHJJ', 'cdsfregHHHJDGF']
li = ['frdgrfgdsHHJJ', 'cdsfregHHHJDGF']
for i in li:
li[li.index(i)]=i.lower()
print(li)
n1 = 123456
n2 = n1
n1的值为123456,然后将n1的值赋给n2.完成后,n1和n2是两个独立的变量,可以修改。但是他们两个在内存中的地址相同。
True False
>>>seq = [1, 2, 3, 4]
>>>seq[:2]
[1, 2]________________________
>>>seq[-2:]
[3, 4]________________________
>>>seq[10:]
[]________________________
>>>seq[::-1]
[4, 3, 2, 1]________________________
>>>seq[:]
[1, 2, 3, 4]________________________
>>> id(seq[:]) == id(seq)
False________________________
li = ['happy', 'lucky', 'linux']
print(len(li))
li.append('seven')
print(li)
li.insert(0,'Tony')
print(li)
li[1]='Kelly'
print(li)
print(li.pop(1))
print(li)
li.remove(li[2])
print(li)
for i in li[1:4] :
li.remove(i)
print(li)
li=li[::-1]
print(li)
dic = {'k1': "v1", "k2": "v2", "k3": [11,22,33]}
for key in dic:
print(key)
dic = {'k1': "v1", "k2": "v2", "k3": [11,22,33]}
for key,value in dic:
print(value)
dic = {'k1': "v1", "k2": "v2", "k3": [11,22,33]}
for key,value in dic:
print(key,value)
dic = {'k1': "v1", "k2": "v2", "k3": [11, 22, 33]}
dic['k1'] = 'harry'
print(dic)
dic = {'k1': "v1", "k2": "v2", "k3": [11, 22, 33]}
dic['k3'].append(44)
print(dic)
dic = {'k1': "v1", "k2": "v2", "k3": [11, 22, 33]}
dic['k3'].insert(0,18)
print(dic)
# 1. 用户输入一句英文句子;
# 2. 打印出每个单词及其重复的次数;
sentence = input('请输入句子')
sentence = sentence.split()
dic = {}
for i in sentence:
if i in dic:
dic[i] += 1
else:
dic[i] = 1
for i,j in dic.items():
print(i,j)
set1 = {11, 22, 33, 44, 55, 66, 77, 88, 99, 90}
dic = {'k1': [], 'k2': []}
for i in set1:
if i > 66:
dic['k1'].append(i)
else:
dic['k2'].append(i)
print(dic)
单行注释:#
多行注释:
"""
"""
for i in range(1,10):
for j in range(1,i+1):
print('%d * %d = %d\t' %(i,j,i*j),end='')
print()
num1 = int(input('请输入第一个数'))
num2 = int(input('请输入第二个数'))
cyc = min(num1, num2)
for i in range(cyc, 0, -1):
if num1 % i == 0 and num2 % i ==0:
print('%d和%d的最大公约数为%d,最小公倍数为%d' %(num1,num2,i,num1*num2/i))
break
a = ‘1’
b = 2
print a + b 的结果是什么,为什么会出现这个结果,如果希望结果是3,要怎么操作?
结果会报错:因为a为字符串类型,b为整形,无法相加
如果希望结果为3,需要强制转换类型
print(int(a) + b)
a='abcd'
#方法一
print(a[3])
#方法二
for i in a:
if i =='d':
print(i)
方法1:
print('[',end='')
for i in range (1,8):
print('%d,'%(i),end=' ')
print('8]')
方法二:
b = [1,2,3,4,5]
for i in range (6,9):
b.append(i)
print(b)
方法1
b = [1,2,3,4,5]
print(b[::-1][:2])
方法2
b = [1,2,3,4,5]
print(b[4:2:-1])
b = [1,2,3,4,5]
print(2 in b)
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。