yum install python 安装python
#!coding=utf-8 编码申明
wo是一个变量,通过raw_input将变量输入进去。
print是一个输出,通过这个将变量输出出来,使用%s%wo。(s为字符串,d为数字,f为浮动小数)
#!coding=utf-8
wo=raw_input("请输入账号:")
print("您输入的账号是:%s"%wo)
ni=raw_input("请输入密码:")
print("您输入的是:%s"%ni)
age是一个变量,通过raw_input将变量输入进去,此时变量为字符串,要将其变为数字,age=int(age)。(str是将数字转换字符串)
if age>=18 如果变量age大于等于18,通过print输出(“老”)这个字符串,esle(否则)就通过print输出(“年轻”)。
age=raw_input("请输入数字")
age=int(age)
if age>=18:
print("老")
else:
print("年轻")
在三间房间随机分配8人
import random
houses = [[],[],[]]
person = ["a","b","c","d","e","f","g","h"]
for persons in person:
wo=random.randint(0,2)
houses[wo].append(persons)
i=1
for house in houses:
num=len(house)
print("第%d个房间有%d个人"%(i,num))
print(house)
i+=1
打印三角形
n = input("请输入要打印的行数:")
n = int(n)
i = 1
while i <= n:
j = 1
while j <= i:
print(" ☆ ", end="")
j += 1
print("\n")
i += 1
for循环
break是结束整个循环
name = "qwertyuiop"
for temp in name:
if temp == 'r':
break
print(temp)
continue是结束本次循环
name = "qwertyuiop"
for temp in name:
if temp == 'r':
continue
print(temp)
字符串方法
str="xiaoming nihao"
-
len(str) #返回字符串长度
print(len(str))
-
find(“ao”,start,end)
看一个字符串是否在这个字符串中出现第一个参数是在哪里开始查找第二个参数是到哪里查找结束str.find("ao",0,len(str))
-
index(“ao”,start,end) 同上 只不过 index查不到报错
str.index("ao",0,len(str))
-
将第一个字符串替换为第二个字符串 替换count次
replace(“one”,“two”,count)str.replace("o","a",count(o))
-
count(“ao”) 返回这个字符串在str中出现的次数
-
startswith(“come”) 查看str是否以"come"开头
-
endswith(“hao”) 查看str是否以hao结尾 是的话返回True 否则返回false
-
upper() 无参数 将str全部改为大写字母
-
lower() 无参数 将str全部变为小写
-
capitalize() 将字符串第一个字母改为大写
-
title() 将字符串中每个单词首字母改为大写
-
split() 代表将字符串以o分割 返回一个数组