分享

1章

 小世界的野孩子 2022-12-14 发布于北京

单引号和双引号

每行代码不要超过80个

在Python中我们都知道单引号和双引号都可以用来表示一个字符串,比如

str1 = 'python'
str2 = "python" 

str1和str2是没有任何区别的。但是如果遇到需要转义字符的情况,来看单引号和双引号的版本。

单引号版本:

str3 = 'We all know that \'A\' and \'B\' are two capital letters.'

双引号版本:

str4 = "We all know that 'A' and 'B' are two capital letters."

  

单引号需要加 '' 来让编译器判断目前是转义字符,而双引号方便了很多。

反之,如果字符串中有双引号,为了避免使用转义符,可以使用单引号来定义这个字符串。

str5 = 'The teacher said: "Practice makes perfect" is a very famous proverb.'

  

1 变量

name = input("name:")
# raw_input 2.x     input 3.x
age = int(input("age:"))        # integer
print(type(age) , type( str(age) ))
job = input("job:")
salary = input("salary:")
​
info = """
------ info of %S ------
Name:%s
Age:%d
Job:%s
Salary:%s
""" % (name,name,age,job,salary)
​
info2 = """
------ info of {_name} ------
Name:{_name}
Age:{_age}
Job:{_job}
Salary:{_salary}
""".format(_name=name,_age=age,_job=job,_salary=salary)
​
info2 = """
------ info of {0} ------
Name:{0}
Age:{1}
Job:{2}
Salary:{3}
""".format(name,age,job,salary)
print(info3)

  

2 用户交互程序

getpass模块 输入不可见

import getpass
​
username = input("username:")
password = getpass.getpass("password:")
​
print(username,password)

3 if else流程判断

_username = 'arfu'
_password = '123456'
username = input("username: ")
#password = getpass.getpass("password:")
password = input("password: ")
​
if _username == username and _password == password:
    print("Welcome user {name} login...".format(name=username))
else:
    print("Invalid username or password!")
 

 

age_of_oldboy = 56
guess_age = int(input("guess age:"))
​
if guess_age == age_of_oldboy:
    print("yes, you got it. ")
elif guess_age > age_of_oldboy:
    print("think smaller...")
else:
    print("think bigger!")

  

4 while 循环

count = 0
while True:
    print("count:",count)
    count = count +1
    if count == 1000:
        break

 



 

age_of_oldboy = 56
​
count = 0
while True:
    if count ==3:
        break
    guess_age = int(input("guess age:") )
    if guess_age == age_of_oldboy:
        print("yes, you got it. ")
        break
    elif guess_age > age_of_oldboy:
        print("think smaller...")
    else:
        print("think bigger!") 
    count +=1

  

age_of_oldboy = 56
​
count = 0
while count <3:
    guess_age = int(input("guess age:") )
    if guess_age == age_of_oldboy:
        print("yes, you got it. ")
        break
    elif guess_age > age_of_oldboy:
        print("think smaller...")
    else:
        print("think bigger!") 
    count +=1
else:
    print("you have tried too many times..fuck off")

  

 

age_of_oldboy = 56
​
count = 0
while count <3:
    guess_age = int(input("guess age:") )
    if guess_age == age_of_oldboy:
        print("yes, you got it. ")
        break
    elif guess_age > age_of_oldboy:
        print("think smaller...")
    else:
        print("think bigger!") 
    count +=1
    if count == 3:
        countine_confirm = input("do you want to keep guessing..?")
        if countine_confirm != 'n':
            count = 0
#else:
#    print("you have tried too many times..fuck off")

 

5 for 循环

age_of_oldboy = 56
​
for i in range(0,10,2)
    guess_age = int(input("guess age:") )
    if guess_age == age_of_oldboy:
        print("yes, you got it. ")
        break
    elif guess_age > age_of_oldboy:
        print("think smaller...")
    else:
        print("think bigger!") 
else:
    print("you have tried too many times..fuck off")


 

for i in range(0,10,2):
    if i <3:
        print("loop",i)
    else:
        continue
    print("hehe...")

 

for i in range(10):
    print('----------',i)
    for j in range(10):
        print(j)
        if j >5:
            break

  

 

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多