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

002-小白学python-运算符/判断/循环

时间:2018-12-07 23:28:16      阅读:380      评论:0      收藏:0      [点我收藏+]

标签:\n   一个   print   字符串类型   1.5   get   复合   tick   字符串   

目录

运算符/判断/循环

算术运算符

  1. +
  2. -
  3. *
  4. /
  5. // 整除(注意负数向小取整)
  6. % 余数
print(1+2)
print(10-5)
print(3*5)
print(6/3)
print(6.0/3)
print(6/4)
print(-6/4)
print(-6//4) # 负数向小取整
print(10%3)

赋值运算符

遇到赋值运算符,先看右边,在看左边

  1. =
    单变量赋值
    多变量赋值
  2. 复合赋值运算符
    • +=
    • -=
    • *=
    • /=
    • //=
    • %==
name = ‘mike‘
age, length = 24, 18
print(name)
print(age, length)
num = 10
print(num)
num += 1
print(num)
num -= 2
print(num)
num *= 2
print(num)
num /= 2
print(num)

数据类型转换

字符串转换为数字类型

int() 字符串必须严格符合数字类型格式
~~~python
num1 = ‘100‘
num2 = ‘200‘
print(int(num1) + int(num2))

~~~

字符串类型转换为浮点类型

float() 字符串必须严格符合数字浮点类型类型格式
~python
f = ‘1.23‘
print(float(f))
~

转换为字符串

str()
~python
b = True
a = 100
print(str(a))
print(str(b))
~

if 判断

单个if

条件判断执行代码
if condition:
pass
1. condition
* 比较运算
* 逻辑运算
* 值
* 或者组合的运算
~python
age = 18
if age > 16:
print(‘RNG牛逼‘)
~

if-else

条件分支
if condition:
? pass
else:
? pass

num = 15
if num > 20:
    print(‘大于20‘)
else:
    print(‘小于20‘)

elif

多条件分支, 只需要满足一个,其他的不会执行
~python
score = 80
if 90 <= score <= 100 : # 推荐语法
print(‘A‘)
elif score >= 80 and score < 90:
print(‘B‘)
elif score >= 70 and score < 80:
print(‘C‘)
elif score >= 60 and score < 70:
print(‘D‘)
else:
print(‘不及格‘)
~

if嵌套

多层条件
~python
danger = 0 # 没有危险
ticket = 1
if not danger and ticket:
print(‘可以进站‘)
if not danger:
print(‘可以进站‘)
if ticket:
print(‘可以上车‘)
else:
print(‘不可以上车‘)
else:
print(‘不可以进站‘)
~

比较运算符

得出的结果就是布尔值

  1. ==
  2. !=
  3. >
  4. =

  5. <
  6. <=
print(1 == 1)
print(‘name‘ == ‘name‘)
print(5 > 4)
print(3 != 2)
print(3 >= 3)
print(5 <= 7)
print(4 > 3)
print(6 != 6)

逻辑运算符

操作对象表达式

1. and     同真为真
2. or      有一个为真就可以
3. not      取反

优先级 () > not > and > or

操作对象至少一个是值(会调用bool)

  1. and 遇到假就停,返回该原值
  2. or 遇到真就停,返回该原值
  3. not 取反
home = 2
car = 3

if car >= 3 and home > 1:
    print(‘牛逼‘)
else:
    print(‘还得努力‘)

num = 10
if num > 5 and num < 20:
    print(‘大小合适‘)

if not (2 > 3):
    print(‘666666‘)
    
love = 1
if car > 2 and home >= 1 or love == 1:
    print(‘陈独秀‘)
else:
    print(‘李时珍皮‘)
    
num = int(input(‘>>>输入数字‘))
if num and  num > 10:
    print(‘ok‘)

while循环

条件满足,执行代码,然后去判断条件,看是否满足,满足则执行,否则跳出循环

while condition:
? pass

while True

死循环,通常看情况使用

一般情况下都需要终止条件

count = 0
while count < 10:
    print(f‘牛逼{count}‘)
    count += 1

计算1-100的和

num = 1
sum_ = 0
while num < 101:
    sum_ += num
    num += 1
print(sum_)

s = 0
i = 1
while i < 101:
    if i % 2 == 0:
        s += i
    i += 1
print(s)

# 计算 1-2+3-4+5-6,。。。+99-100
sum = 0
for i in range(1, 101):
    if i % 2 == 0:
        sum -= i
    else:
        sum += i
print(sum)

break continue

循环中使用
1. break 立即终止循环执行
2. continue 立即跳出当前循环,然后判断条件进入下一次循环
3. 只能作用于最近的循环
4. 一般情况下与判断结合使用

i = 1
while i < 10:
    print(f‘i:{i}‘)
    if i == 5:
        break
    i += 1

c = 1
while c <= 10:
    if c == 5:
        c += 1
        continue
    print(f‘c:{c}‘)
    c += 1

while 嵌套

while condition1:
? pass
? while condition2:
? pass

# 打星星
i = 1
while i < 6:
    j = 1
    while j <= i:
        print(‘*‘, end=‘ ‘)
        j += 1
    print(‘‘)
    i += 1
# 99
m = 1
while m < 10:
    n = 1
    while n <= m:
        print(f‘{n} * {m} = {m * n}‘, end=‘(-_-)‘)
        n += 1
    print(‘‘)
    m += 1
## 99
j = 9
while j > 0:
    k = 9
    while j <= k:
        print(f‘{k} * {j} = {j * k}‘, end=‘(-_-)‘)
        k -= 1
    print(‘‘)
    j -= 1

## 99
for i in range(9, 0, -1):
    for j in range(9, 0, -1):
        if j <= i:
            print(‘%d * %d = %d‘ % (j, i, i * j), end=‘ ‘)
    print(‘\n‘)

猜拳

import random
# 得到用户数据
# 分析用户数据
# 操作用户数据
# 进行判断,循环
# 得出结果
dic = {
    0: ‘剪刀‘,
    1: ‘石头‘,
    2: ‘布‘,
}
count = 5
while count <= 5:
    player = int(input(‘剪刀(0),石头(1),布(2)>>>:‘))
    if player not in (0, 1, 2):
        count -= 1
        if count == 0:
            flag = input(‘还想玩吗?Y/N‘)
            if flag.upper().strip() == ‘Y‘:
                count = 5
            else:
                break
        print(f‘请输入按照要求输入012,还有{count}次机会‘)
        continue
    value = random.randint(0, 2)
    print(f‘player--{dic[player]}‘)
    print(f‘value---{dic[value]}‘)
    if player == 0 and value == 2 or player == 1 and value == 0 or player == 2 and value == 1:
        print(‘you win‘)
        break
    elif player == value:
        print(‘peace‘)
    else:
        print(‘down‘)
    count -= 1
    print(f‘还有{count}次机会‘)
    if count == 0:
        flag = input(‘还想玩吗?Y/N‘)
        if flag.upper().strip() == ‘Y‘:
            count = 5
        else:
            break

测BIM

# 判断BIM指数:
# 1、输入一个人的身高(m)和体重(kg)(分两次输入),根据BMI公式(体重除以身高的平方)计算他的BMI指数,
# 判断这个指数输出对应的肥胖程度(过轻、正常、过重、肥胖、严重肥胖)
# 例如:一個52公斤的人,身高是155cm,则BMI为 :
# 52(kg)/1.552**2(cm)= 21.6
# BMI指数:
# 低于18.5:过轻
# 18.5-25:正常
# 25-28:过重
# 28-32:肥胖
# 高于32:严重肥胖
while True:
    high = input(‘输入身高(m)>>>:‘)
    weight = input(‘输入体重(kg)>>>:‘)
    BIM = int(weight) / (float(high) ** 2)
    if high and weight:
        if BIM < 18.5:
            print(‘过轻‘)
        elif 18.5 <= BIM < 25:
            print(‘正常‘)
        elif 25 <= BIM < 28:
            print(‘过重‘)
        elif 28 <= BIM < 32:
            print(‘肥胖‘)
        elif BIM >= 32:
            print(‘严重肥胖‘)
    flag = input(‘是否继续测量(y/n)>>>‘)
    if flag.strip().upper() == ‘N‘:
        break

输入反馈

# 编写代码,1-7七个数字,分别代表周一到周日,如果输入的数字是6或7,输出“周末”,
# 如果输入的数字是1-5,输出“工作日”,如输入其他数字,输出“错误”。

lst = [1, 2, 3, 4, 5, 6, 7]
while True:
    info = int(input(‘>>>>>>‘))
    if info in lst:
        if info == 1:
            print(‘周一‘)
        elif info == 2:
            print(‘周二‘)
        elif info == 3:
            print(‘周三‘)
        elif info == 4:
            print(‘周四‘)
        elif info == 5:
            print(‘周五‘)
        elif info == 6 or info == 7:
            print(‘周末‘)
        flag = input(‘还要来吗?(y/n)>>>‘)
        if flag.strip().upper() == ‘N‘:
            break
    else:
        print(‘输入的数字不正确请重新输入‘)

# 字典版本
dic = {
    1: ‘周一‘,
    2: ‘周二‘,
    3: ‘周三‘,
    4: ‘周四‘,
    5: ‘周五‘,
    6: ‘周末‘,
    7: ‘周末‘,
}
info = input(‘>>>>>>‘)
print(dic.get(int(info)))

过七游戏

#设计“过7游戏”的程序, 打印出1-100之间除了含7和7的倍数之外的所有数字。
# 方法一:
lst = (i for i in range(1, 101))
f_o = filter(lambda item: item % 7 != 0 and ‘7‘ not in str(item), lst)
for i in f_o:
    print(i)
# 方法二
# for i in range(1, 101):
#     if i % 7 != 0 and ‘7‘ not in str(i):
#         print(i)

002-小白学python-运算符/判断/循环

标签:\n   一个   print   字符串类型   1.5   get   复合   tick   字符串   

原文地址:https://www.cnblogs.com/py-coder/p/10085634.html

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