Py基础—变量名,条件循环,空执行,编码,运算符,字符比较,简化写法

变量名

只能是字母,数字,下划线。数字不能开头,不要和python内置的东西重复。
赋予变量名内容:
name1 = "shit"

输出变量名内容

print(name1)

条件语句

注意python里面和c不同,需要注意缩进(缩进四个空格)
,if块里面的必须需要缩进,而且分号变成冒号
if 1 == 1:
    print("sssss")
else:
    print("sssa")
 

可以进行嵌套

if 1 == 1:
    if x ==1:
        print("asd")
    else:
        print("ssad")
        print("sssss")
else:
    print("sssa")

可以弄同级if

inp = input("请输入数字")
if inp == "a":
    print("a")
elif inp =="b":
    print("b")
elif inp =="c":
    print("c")
else:
    print("shit")
print("end")

 

空执行,什么都不执行用pass

for循环语句

nums = [2,7,11,15,1,8,7]
for i1 in nums:
for i2 in nums:
print(i1,i2)
 

while语句,while的内容如果成立就一直执行里面的内容

count = 0
while count < 10:
    print("aa")
    count = count +1
else:
    print("bb")
print("end")

找出偶数,采用取余的方法,取余为%

n = 1
while n<101
    temp = n%2
    if temp ==0:
        print(n)
    else:
        pass
    n = n+1
print("end")

countinue和break的使用

当循环中出现continue,当前循环立即终止,执行下一次循环
以下可以输出1-10且其中单独不输出7,到7终止当前循环,执行下一次循环
break是直接把整个循环终止,不再执行这整个循环。
count = 0
while count < 10:
    if count == 7:
        count = count +1
        continue
    print(count)
    count = count  + 1
 

有关编码:unicode为全球通用的编码

utf8的中文为2字符 
gbk的中文为3字符
 

字符串与数字的转换

input输入的是字符串,"10"*3=101010,10*3=30
,需要强制转换成数字需要转换类型
inp = input(">>>")
>>>10
inp="10"
转换语句
newinp = int(inp)
 

用户登录(三次机会重试)

count = 0
while count<3:
    user = input(">>>")
    pwd= input(">>>")
    if user =="shit" and pwd == "asshole":
        print("welcome")
        break
    else:
        print("user or pwd error")
    count = count + 1
print("you are not allow to login")
 

运算符

%求余数    //求整数    **幂   >=大于等于   <=小于等于  !=不等于   not非,取反
and与   or或者  
 

字符串与字符的比较

使用in或者not in,比如判断 if 某个字符 in 某个字符串
name = "love"
spiddd = input ("input the world you want to check")
if spiddd in name :
    print("in name")
else:
    print("not in name")
 

布尔值:

True=1, False=0,True和False都能直接用
 ""为假   " "为真
0为假    其他的数字为真

简化写法

count= count +1     count+=1
count= count -1     count-=1
count= count %1     count%=1

判断是否是可迭代对象(只要可以for循环的都是可迭代对象)

for i in 值
    print(i)

range的用法,创建连续的数字

以下可以制作出一个0-100的开区间

v=range(100)

以下可以输出0-99的数字

v = range(100)
for item in v:
    print(item)

也可以创建不连续的,第一个起始数字,第二个终止数字,第三个步长

v = range(0,100,5)
for item in v:
    print(item)

给字符串创造对应区间数组

name = "alex"
name[1:2]
print(name[0])

将文字对应的索引打印出来

test=input(">>>")
print(test)
l = len(test)
print(l)
r= range(0,l)
for item in r:
    print(item,test[item])
posted @ 2020-09-09 21:29  克莱比-Kirby  阅读(227)  评论(0)    收藏  举报