Python学习代码记录
#!/usr/bin/python
# -*- coding: UTF-8 -*-
num1 = input("Pls input a number: ")
num2 = input("Pls input a number: ")
print "%s + %s = %s" % (num1, num2, num1+num2)
print "%s - %s = %s" % (num1, num2, num1-num2)
print "%s * %s = %s" % (num1, num2, num1*num2)
print "%s / %s = %s" % (num1, num2, num1/num2)
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import redis
#r = redis.Redis(host='127.0.0.1', port=6379,db=0)
#r.set('name', 'zhangsan')
#print (r.get('name'))
pool = redis.ConnectionPool(host='127.0.0.1', port=6379)
r = redis.Redis(connection_pool=pool)
r.set('name', 'lisi') #添加
print (r.get('name')) #获取
#!/usr/bin/python
# -*- coding: UTF-8 -*-
#coding:utf-8
# 字典创建 while开关 字典添加 字典寻找
dictionary = {}
flag = 'a'
pape = 'a'
off = 'a'
while flag == 'a' or 'c' :
flag = raw_input("添加或查找单词 ?(a/c)")
if flag == "a" : # 开启
word = raw_input("输入单词(key):")
defintion = raw_input("输入定义值(value):")
dictionary[str(word)] = str(defintion) # 添加字典
print "添加成功!"
pape = raw_input("您是否要查找字典?(a/0)") #read
if pape == 'a':
print dictionary
else :
continue
elif flag == 'c':
check_word = raw_input("要查找的单词:") # 检索
for key in sorted(dictionary.keys()): # yes
if str(check_word) == key:
print "该单词存在! " ,key, dictionary[key]
break
else: # no
off = 'b'
if off == 'b':
print "抱歉,该值不存在!"
else: # 停止
print "error type"
break
#!/usr/bin/python
# -*- coding: UTF-8 -*-
#num1 = input("Pls input a number: ")
#num2 = input("Pls input a number: ")
#print "%s + %s = %s" % (num1, num2, num1+num2)
#print "%s - %s = %s" % (num1, num2, num1-num2)
#print "%s * %s = %s" % (num1, num2, num1*num2)
#print "%s / %s = %s" % (num1, num2, num1/num2)
#var = 101
#if ( var == 100 ) : print "变量 var 的值为100"
#print "Good bye!"
#x = int(raw_input("pls input a number: "))
#y = int(raw_input("pls input a number: "))
#z = int(raw_input("pls input a number: "))
#aa = [x,y,z]
#list(aa)
#aa.sort()
#print aa
#for i in xrange(10):
# if i == 5:
# print "id:%s" %i
# continue ##当i==3的时候就会跳出此次循环,也就是下面的程序不会再次执行。
# if i == 4: ##当i ==5的时候就直接跳出整个循环。
# break
# print i
#else:
# print "main end"
#import time #插入一个time对象(一切皆对象,)
#import sys #插入一个sys对象
#for i in xrange(10):
# if i == 3:
# continue
# elif i == 5:
# continue
# elif i == 6:
# sys.exit() ## 当i==6的时候主动退出整个脚本。
# print i
#else:
# print "main end"
#import random
#import sys
#bb = random.randint(1,20)
#print bb
#for i in xrange(1,7):
# aa = int(raw_input("pls input a number: "))
# if aa == bb:
# print "you're so smart!!"
# sys.exit()
# elif aa > bb:
# print "pls input a small number."
# else:
# print "pls input a big number."
#else:
# print "time is end!"
#for i in xrange(1,10):
# for j in xrange(1,i+1):
# print "%sx%s=%s" % (j, i, j*i), ## ,的原因是不让程序自动换行
# print
#count=0
#for i in '1234':
# for j in '1234':
# for m in '1234':
# if (i != j) and (j != m) and (i != m):
# print "%s %s %s" % (i, j, m)
# count+=1
#print
#print count
#for i in xrange(100,1000):
# unit = i % 10
# ten = i // 10 % 10
# hundred = i // 100
# if unit ** 3 + ten ** 3 + hundred ** 3 == i:
# print(i)
#for a in 'xyz':
# for b in 'xyz':
# for c in 'xyz':
# if a != b and b != c and c != a:
# if a != 'x' and c != 'x' and c != 'z':
# print "a vs %s b vs %s c vs %s" % (a, b, c)
#list1 = ['x','y','z']
#for a in list1: # a 从 xyz中挑一个对手
# for b in list1: # b 挑一个对手
# if a != b: # a的对手和b的对手不相同
# for c in list1: # c挑一个对手
# if a != c and b != c: # a,b,c,对手互不相同
# if a != 'x' and c != 'x' and c != 'z': # a的对手不是x,c的对手不是x,c的对手不是z
# print "a-{0} b-{1} c-{2}".format(a, b, c) # 打印出结果
#aa = open('/root/study/python/vm.ini')
#for line in aa:
# print line,
#aa = open('/root/study/python/vm.ini')
#while True:
# line=aa.readline()
# if not line:
# break
# print line,
#with open('/proc/meminfo') as aa:
# for line in aa:
# if line.startswith('MemTotal'):
# MemTotal = line.split()[1]
# continue
# if line.startswith('MemFree'):
# MemFree = line.split()[1]
# break
# print "The Memory Total: {:.2f}".format(int(MemTotal)/1024.0) + 'M'
# print "The Memory left: {:.2f}".format(int(MemFree)/1024.0) + 'M'
# print "The Memory left percent: {:.2f}".format((int(MemFree)/1024.0)/(int(MemTotal)/1024.0)*100) + '%'
#mac = '00:0c:29:9c:dc:41' ##本地电脑的Mac地址
#mac_prefix = mac[:-3] ##取出Mac地址的前缀
#last_two = mac[-2:] ##取出MAC地址的后三位
#plus_one = int(last_two, 16) + 1 ##把16进制的mac转换成10进制并+1
#new_last_two = hex(plus_one)[-2:] ##再次把10进制的mac地址转换成原来的16进制,如果不加[-2:],则会出现 0x的前缀
#new_mac = mac_prefix + ':' + new_last_two ##把刚刚整理好的前缀和新的后两位拼接起来。
#print new_mac
#import codecs
'''
/etc/passwd 查找用户,对用户优先级进行排序
'''
#3with codecs.open("passwd", "r") as f:
# result = sorted(f.readlines(), key=lambda x: int(x.split(':')[2]))
# print result
#with codecs.open("sortPasswd", "w") as f:
# f.writelines(result)
#def Jianjia(x):
# y =0.5
# x = x * y
# return x
#print(Jianjia(100))
#print(Jianjia(50))