码迷,mamicode.com
首页 > 编程语言 > 详细

Python笔记---DAY3:格式化输出、for循环、列表操作

时间:2018-02-26 11:35:58      阅读:604      评论:0      收藏:0      [点我收藏+]

标签:insert   choice   justify   name   append   login   ever   标志位   choices   

1、引号使用:

多行变量用三引号

python单双引号相同,当命令内容中有引号时注意最外层引号的使用

 

2、格式化输出:

name=input("name:")

age=int(input("age:"))

job=input("job:")

salary=input("salary:")

number=input(“number:”)

 

if salary.isdigit(): 判断输入是否为数字,其中.的作用是调出其属性

 salary=int(salary)

else:

 exit(“must input digit”) 输入错误便退出程序

 

msg=‘‘‘ 多行变量使用三引号

-----info of %s----- %s为占位符,sstring

name:%s

age: %s

job: %s

salary:%s

you will retire in %s years

number:%d 必须输入数字,ddigit整数;%f中,float为浮点数,即小数

-----end-----

‘‘‘%(name,name,age,job,salary,65-age) age转为int格式使得可以相减

print(msg)

 

3、其他指令:

ctrl+? 几行命令全部注释

字符串的拼接中尽量用,而非+,因为+运行速度慢

for为有限循环,while为无限循环

 

4、for循环:

for i in range(1,101,2):

    if i<50 or i>70: 确定输出i范围

     print(“loop:”,i)

range中,1为起始数,101-1为终结数,2为步长

 

5、用户登录:

username1="lilei"

password1="123"

passed_authentication=False 添加标志位

for i in range(3): 循环3

    username=input("name:")

    password=input("password:")

    if username==username1 and password==password1:

        print("welcome %s login..."%username)

        passed_authentication=True

        break 输入正确之后退出当次循环

    else:

        print("invalid username or password!") 提示不具体指明错误,否则信息会被猜中

 

if passed_authentication==False: 三次输出均失败时给出提示

print("臭流氓")

 

6、forelse版用户登录:

username1="lilei"

password1="123"

for i in range(3):

    username=input("name:")

    password=input("password:")

    if username==username1 and password==password1:

        print("welcome %s login..."%username)

        passed_authentication=True

        break breakfor之后不执行与for同列的else

    else:

        print("invalid username or password!")

else: for正常执行完再执行else

print("臭流氓")

 

7、用户登录while版:

username1="lilei"

password1="123"

counter=0

while counter<3:

    username=input("name:")

    password=input("password:")

    if username==username1 and password==password1:

        print("welcome %s login..."%username)

        break

    else:

        print("invalid username or password!")

counter+=1

    if counter==3:

        keep_going_choices=input("还想继续吗?:[Y/N]")

        if keep_going_choices=="Y":

            counter=0 如果想继续输入,将循环次数清零

else:

print("臭流氓")

 

8、continuebreak的使用:

exit_flag=False

for i in range(10):

    if i<5:

        continue 结束本次循环,回到当个循环的开始

    print(i)

    for j in range(10):

        print("layer 2",j)

        if j==6:

            exit_flag=True 定义了标志位

            break 结束本次循环,并跳出当个循环

    if exit_flag:

        break 利用标志位连跳两次循环

 

9、列表的操作:

a=["A","B","C","D","E","F"]

b=a[1:4] 从列表中第2个数据取到第4个数据

c=a[1:] 从列表中第2个数据取到最后一个数据

d=a[1:-1:2] 从列表中第2个数据取到倒数第2个数据,步长为2

e=a[-1:1:-2] 从列表中右数第2个数据取到左数第2个数据,步长为2

a.append("ZZZ") 在列表最后添加内容,追加

a.insert(1,”ZZZ”) 将添加内容作为位置1插入列表

a[1:3]=["LL","DD"] 将列表中第2个数据和第3个数据的内容修改

a.remove("A") 删除列表中的A”,只能删除一个元素

a.pop(1) 删除列表中第2个数据,未指定则默认删除最后1个元素

del a[0:4:2] 删除列表中的第1和第3个数据

b=a.count("A") 计算列表中某一元素的重复次数

a.extend(b) b中元素添加至a之后

c=a.index(“B”) 查询a中“B”的位置,若a中含有多个“B”则显示第1个“B”的位置

a.reverse() a中的元素倒置

a.sort() a中元素按ASCII顺序排序,大写在前,a.sort(recerse=True)可逆序排列

a.clear 清空列表

 

Python笔记---DAY3:格式化输出、for循环、列表操作

标签:insert   choice   justify   name   append   login   ever   标志位   choices   

原文地址:https://www.cnblogs.com/lilei0128/p/8470592.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!