Python学习(2)

Python运算符

1、python的算数运算符

a = 10
b = 3
c = 0

c = a + b
print("1 - c 的值为:", c)

c = a - b
print("2 - c 的值为:", c)

c = a * b
print("3 - c 的值为:", c)

c = a / b
print("4 - c 的值为:", c)

c = a % b
print("5 - c 的值为:", c)

运行结果:

2、Python成员运算符

a = 10
b = 20
list = [1, 2, 3, 4, 5];

if (a in list):
    print("1 - 变量 a 在给定的列表中 list 中")
else:
    print("1 - 变量 a 不在给定的列表中 list 中")

if (b not in list):
    print("2 - 变量 b 不在给定的列表中 list 中")
else:
    print("2 - 变量 b 在给定的列表中 list 中")

# 修改变量 a 的值
a = 2
if (a in list):
    print("3 - 变量 a 在给定的列表中 list 中")
else:
    print("3 - 变量 a 不在给定的列表中 list 中")

运行结果:

3、Python条件语句

flag = False
name = "luren"
if name == 'python':
    flag = True
    print("welcome boss")
else:
    print(name)

运行结果:

luren

4、Python while循环

a = 1
while a < 10:
    print(a)
    a += 2

运行结果:

1、3、5、7、9

复杂点的例子:

numbers = [12,37,5,42,8,3]
even = []
odd = []
while len(numbers) > 0 :
    number = numbers.pop()
    #pop()方法默认移除列表中最后一个元素
    if(number % 2 == 0):
        even.append(number)
    else:
        odd.append(number)
print(even)
print(odd)

运行结果:

5、while循环

当前字母 : P
当前字母 : y
当前字母 : t
当前字母 : h
当前字母 : o
当前字母 : n
当前水果 : banana
当前水果 : apple
当前水果 : mango
Good bye!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值