学习笔记
1、 生成一个1到50的大字符串,每个数字之间有空格
result = ‘‘ 存放结果
for i in range(1,50,1): #遍历1-到50个数据
if i == 50: #为了结尾出现空格这样处理
result = result + str(i) #把结果存放到变量中去
result = result + str(i) + " " #拼接成功
print result
============
2、使用while统计的句子中有几个数字,动态输入
content = raw_input("please input a sentenece:\n") #输入
result = 0 #定义结果存放
index = 0 #定义索引
while index <= len(content) - 1: #判断长度
if content[index] in "0123456789": #判断是否在数字里面
result += 1 #如果在就加一存放结果
index += 1 #下标往移动
print result #打印结果
==================
第三题,将一个列表元素倒序存在一个新列表中
a = [1,3,4,5,6]
b = [] #保存结果
for i in a: #循环输出a
b.insert(0,i) #始终从第一个插入数据
print b
========================
输入四数倒叙排序:并且排序
number = 0
b = []
c = []
for i in range(4):
number = int(raw_input("please enter hour\n"))
b.insert(0,number) #往第一个里面增加倒叙
c.append(number) #往数组里面增加,
c.sort() #调用函数进行排序
print b
print c
==========
输出26小写字母
result = []
for i in range(97,97 + 26):
result += chr(i)
print result
输出大写字母:
result = []
for i in range(65,65 + 26):
result += chr(i)
print result
大小写字母一起输出
result = ‘‘ #保存结果
for i in range(65,65 + 26): #从65到26为大写
result += chr(i) + chr(i + 32) #从97倒128小写
print result
==============
判断一个句子中包含多少字母
content = raw_input(‘please input a sentence\n‘)
Letter_count = 0
for s in content:
if s not in ‘0123456789‘:
Letter_count += 1
print Letter_count
=====================
import string
sentence = raw_input("input youtsentence\n") #存储位置
letter_nbr=0 #计数器
for i in sentence:
if i instring.letters #内置函数
letter_nbr+=1 #计数
print letter_nbr
==========================
加密:
letters = raw_input("please input some letter to encode:")
encode_letters = ""
for s in letters:
if (s >= "a" and s < "w") or (s >= "A" and s <= "W"):
encode_letters += chr(ord(s) + 4)
elif s >= "W" and s <= "z":
encode_letters += chr(ord(s) - ord("W") + 97)
elif s >= "W" and s <= "Z":
encode_letters += chr(ord(s) - ord("W") + 65)
else:
print "some content is not letter! please try agan!"
continue
print encode_letters
=========
判断字符类型
>>> a = ‘dd‘
>>> isinstance(a,int)
False
>>> isinstance(a,list)
False
>>> isinstance(a,str)
True
>>> b = [‘1w‘,3,‘de‘,233]
>>> isinstance(b,list)
True
>>> isinstance(b,tuple)
False
>>> isinstance(b,dict)
False
>>> a = 1
>>> b = 0
>>> bool(1)
True
>>> bool(b)
False
>>>