python-02

简介: python-02

while 循环
flag = True # 标志位
while flag:

print("123")
print("123")
print("777")
flag = False
print("结束了?")
print("5675")

代码全部执行完,再回到头部判断条件
count = 1
while count <= 3:

username = input('请输入用户名: ')
password = input('请输入密码: ')
code = 'qwer'
your_code = input('验证码:')
if your_code == code:
    if username == 'alex' and password == '123':
        print('登陆成功')
    else:
        print('用户名或密码错误')
else:
    print('验证码错误')
count +=1

格式化输出

制作一个公共的模板

让一个字符串的某些位置变成动态可传入的。

格式化输出

name = input('请输入你的姓名:')
age = input('请输入你的年龄:')
job = input('请输入你的工作:')
hobby = input('请输入你的爱好:')

% 占位符 s --> str d i r

msg = '''------------ info of %s -----------
Name : %s
Age : %d
job : %s
Hobbie: %s
------------- end -----------------''' % (name, name, int(age), job, hobby)
print(msg)

坑:在格式化输出中,% 只想表示一个百分号,而不是作为占位符使用

bjk = '我叫%s,今年%s,学习进度1%%' % ('太白金星', 18)
print(bjk)

break :循环中遇到break 直接退出循环
练习

输出1~100的所有数字

i = 0
while i < 100:

print(i+1)
i += 1

1 + 2 + 3 + ~~~ +100 的最终结果

j = 1
sum = 0
while j <= 100:

sum += j
if j == 100:
    print("sum:%d" % (+sum))
j += 1

2

k = 1
sum1 = 0
while k <= 100:

sum1 += k
k += 1

print(sum1)

打印所有1~100的偶数:

b = 1
while b <= 100:

if b % 2 == 0:
    print(b)
b += 1

count = 2
while True:

count += 2
print(count)
if count == 102:
    break

continue 相当于到了while循环的底部 continue以下的代码不会执行
进出本次循环,继续下一次循环
while True:

print(222)
print(111)
continue
print(333)

while else: while 循环如果被break打断,则不执行else语句。

while else: while 循环如果被break打断,则不执行else语句。

count =1
while count < 5:

print(count)
count += 1

else:

print(666)

运算符:
1.算数运算符 + -
2.比较运算符 > ==
3.赋值运算符 =
4.逻辑运算符 and or
5.成员运算符
逻辑运算符
and 两真则真
or 一 真则真
not 非 取反
1.在没有()的情况下,优先级 not > and > or ,
1.两边都是比较运算 同一优先级从左至右依次计算
2.两边都是整数,
x or y ,x 为真, 值就是x ,x为假,值是y
x and y, x为真,值是y,x为假,值是x

相关文章
|
27天前
|
机器学习/深度学习 数据挖掘 开发工具
Python100天:01.初识python
【4月更文挑战第7天】Python100天:01.初识python
37 1
Python100天:01.初识python
|
11月前
|
设计模式 自然语言处理 JavaScript
【21天python打卡】第1天 python预备知识(1)
大家好,今天是21天python打卡的第一天,我们要想学好python,我们先了解一些关于python的基础知识。
103 0
|
11月前
|
自然语言处理 算法 Java
【21天python打卡】第2天 python预备知识(2)
​​大家好,今天是21天python打卡的第2天,我们要想学好python,要先了解一些关于python的基础知识。上一篇我们介绍了python的基本概念,python编程思想,python安装以及python的运行方式,本文我们继续讲python的预备知识。
87 0
|
存储 Java 程序员
初识Python——“Python”
初识Python——“Python”
初识Python——“Python”
|
机器学习/深度学习 XML 存储
认识 Python
人生苦短,我用 Python —— Life is short, you need Python
|
Python
Python:使用2to3将Python2转Python3
Python:使用2to3将Python2转Python3
85 0
|
Python
每天一个Python小技巧(2)
很多人都用过if/else,但却不知道while和for后面也可以接else
每天一个Python小技巧(2)
|
索引 Python
第9天:Python tupple
第9天:Python tupple
82 0
|
Python
Python—Python 问题
离线安装库
111 0
|
JavaScript Python
【Python】Python3之i18n
最近在完成阿里云MVP共创任务定pgAdmin4定制任务的时候,接触到了Python的本地化与国际化,了解了Python多语言化的基本知识,记录一下分享。其中涉及Python基础类库gettext,大家可访问link。
1388 0