-
print 2.7 和 3.0 的区别是 2.0 ===> print "hello world" 3.0 ===> print("hello world")
1 print("hello world")
-
变量赋值 临时存储数据
name = ‘20‘ age = 43 print(name,age)
-
输入值用变量来存储
# 3.0 输入是input("please input your text") # 2.X版本里有是raw_input("please input your text") # 2.7里 input() 括号内输入的是什么就会给输出什么类型,然并卵,3.0去掉 # 3.0里如果要用2.7里的input() eq:eval(input("please input yous text")) name = input("please your Nick name") print(name)
-
if ... else ... 缩进 eq: int(var):把var强转成int
luckNum = int(input("please input your luck number")) if luckNum == 1: print("i want say no") elif luckNum > 10: print("no come on baby") else: print("finally say yes")
-
while循环
inputNum = 0 bingoNum = 13 count = 0 while count < 3: inputNum = int(input("please input yours num:")) count = count+1 if(count ==3): print("try too many ") break if(inputNum > bingoNum): print("input number is bigger!") elif(inputNum<bingoNum): print("input number is small!") elif(inputNum == 13): print("congratulation") break
-
字符串拼接 %s表占位 %s。。。赋值 %(val...)company = input("conpany:")
company = input("conpany:") companyNum = input("number:") print("info:\n company"+company+"\nnumber"+companyNum) # “+”拼接字符串每加一次需要单独的开辟一个内存 print("info{} \n company: %s \n number: %s" % (company, companyNum)) #不会单独的开辟一块空间 节省内存
-
字符串方法
testString = "testsss1" print(testString.lower()) #变小写 print(testString.upper()) #变大写 print(testString.split("g"))#用 “g” 去分割 print(testString.count("s"))#统计“s”的次数 print(testString.replace("1","2"))
-
列表 List的使用
nameList = [‘jerry‘,‘tom‘,‘jeck‘,‘Alice‘] print(nameList) print(nameList[1]) #取值 nameList.append("jerry") print(nameList) #尾部添加数据 print(nameList.index("jerry")) #找到jerry的索引值 print(nameList.count("jerry")) #统计list包含字符串的count nameList.insert(0,"lulu") #根据索引插入数据 print(nameList) nameList.sort() print(nameList) #按照ASKII码去排序 nameList.reverse() #反转倒叙 print(nameList) print(len(nameList)) # len(list)获取list长度 print(dir(list)) #查看list有哪些方法可用
-
list进阶
nameList = [1,2,4,‘ok‘,‘yes‘] print(nameList[0:2]) # 结果是 【1,2】 0 2代表索引 print(nameList[0:2:2]) # 表示可以从0~2的隔2个切一下 print(nameList[-3]) #取后面的倒着取 print(nameList[-2:]) #比如取出 ok 和 yes print(range(10)) #range(0, 10) print(type(range(10))) #结果 <class ‘range‘> if 4 in nameList: print("is true") #判断列表是是否包含某个值
-
元组
#yuanzu = (1,3,4,5,5,6,) 两个方法 count和index list和元组可以互相转换 yuanzu = (1,3,4,5,5,6,) list(yuanzu) type(yuanzu) #结果是 list tuple(yuanzu) type(yuanzu) #结果是tuple
-
运算符
# 1. ** 幂 2**3 2的三次方 # 2. 整除 1//2 =0 2//2=0 2.X里 # 3. 2.X里 <>不等于 3.X去掉 # 4. and java里并且的意思 or 或者 not 非真 # 5.Is 和Isnot type(list) is list 结果 true
-
continue跳出本次循环,继续下一次循环
for i in range(10): if i > 3: continue print(i) 结果 0 1 2 3
本文仅供学习参考,如有不对敬请指正,我还是个小学生,需要学的东西有很多,若转载请标明出处
时间: 2024-10-12 21:16:33