python(条件判断、input语句、循环、dict和set、不可变现象)

学习目标:

Python学习五、


学习内容:

1、条件判断
2、input语句使用
3、循环
4、使用dict和set
5、不可变现象


1、条件判断语句if

1、if语句判断是True,就把缩进的两行print语句执行,否则啥也不执行

age = 80
if age > 18:
    print('your age is',age)
    print('take care of youself')
输出:
your age is 80
take care of youself

2、if判断是False,不执行if的内容,而是执行else

age = 3
if age >= 18:
    print('your age is', age)
    print('adult')
else:
    print('your age is', age)
    print('teenager')
输出:
your age is 3
teenager   

3、用elif进行更精细的判断

if <条件判断1>:
<执行1> elif <条件判断2>:
<执行2> elif <条件判断3>:
<执行3> else:
<执行4>

age = 3
if age >= 18:
    print('adult')
elif age >= 6:
    print('teenager')
else:
    print('kid')
输出:
kid    

4、if也可以简写

只要x是非零数值、非空字符串、非空list等,就判断为True,否则为False

if x:
    print('True')

2、input语句使用

  • input返回的数据是str,当需要与整数进行比较时,可以用int()函数转换
s = input('birth: ')
birth = int(s)
if birth < 2000:
    print('00前')
else:
    print('00后')
输出:
birth: 1998
00

3、循环

  • python提供的循环有两种for和while
    1、一种是for…in循环,依次把list或tuple中的每个元素迭代出来
name = ['jack', 'tom', 'jonse', 'alice']
for i in name:
    print(i)
 输出:
 jack
tom
jonse
alice
  • 当我们想让列表里的每一个元素代入变量i,然后执行缩进块的语句
sum = 0
num = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
for i in num:
  if i < 10:
    sum = i + sum
else:
    print(sum)
输出:
45

2、range函数

range()函数,可以生成一个整数序列,再通过list()函数可以转换为list

sum = 0
for n in range(1001):
    sum = sum + n
print(sum)
输出:
500500
  • 实例:
    1、 实现乘法运算表
 for i in range(1, 10):
    strs = '\n'
    for j in range(1, i+1):
     strs+=(str(i)+'*'+str(j)+'='+str(i*j)+'   ')
    print(strs)

2、查找1000以内的水仙花数for i in range(1001):

    a = (i // 100)
    b = (i // 10 % 10)
    c = (i % 10)
    if i == a**3+b**3+c**3:
        print(i)

3、while语句

while循环,只要条件满足,就不断循环,条件不满足时退出循环

sum = 0
n = 1
while n < 1001:
    sum = n+sum
    n = n+1
print(sum)
500500

4、break语句

  • break可以结束当前循环
sum = 0
n = 1
while n < 1001:
    if n > 100:
        break
    sum = n+sum
    n = n+1
print(sum)
输出:
5050

5、continue语句

  • 用continue语句,跳过当前的这次循环,直接开始下一次循环
sum = 0
n = 0
while n < 11:
 if n < 11:
   sum = n+sum
   n = n+1
 else:
  continue
print(sum)
输出:
55
n = 0
for n in range(10):
    if n % 2 == 0:
       continue
    else:
     print(n)
     n = n + 1
输出:
    1
3
5
7
9

4、使用dict和set

1、dict字典

  • Python内置了字典:dict的支持,dict全称dictionary,其他语言中叫map,使用键-值(key-value)存储,具有极快的查找速度,dict内部存放的顺序和key放入的顺序没有关系,而list有关系

例如三名同学的成绩查询:其中学生名叫key,成绩是value

d = {'jack':99, 'alice':95, 'tom':90}
print(d['alice'])
输出:
95
  • dict不仅可以初始化添加value也可以通过key添加value

dict名[添加的字典元素名]=value

d = {'jack':99, 'alice':95, 'tom':90}
d['anmy']=97
print(d)
输出:
{'jack': 99, 'alice': 95, 'tom': 90, 'anmy': 97}
  • 一个key只能对应一个value,所以,多次对一个key放入value,后面的值会把前面的值冲掉
d = {'jack':99, 'alice':95, 'tom':90}
d['anmy']=97
d['anmy']=96
d['anmy']=100
print(d)
输出:
{'jack': 99, 'alice': 95, 'tom': 90, 'anmy': 100}
  • 当key不存在时,可以通过in或者get方法判断

‘元素名’ in dict名

d = {'jack':99, 'alice':95, 'tom':90}
print('thomas' in d)
输出:
False

dict名.get(‘元素名’, 指定值)

d = {'jack':99, 'alice':95, 'tom':90}
i = d.get('thomas')
j = d.get('tony', 2)
print(i)
print(j)
输出:
None
2
  • 用items函数对字典的key和value进行拿取

dict.items函数

 - dicts = {'hello':'world','hello1':'world1'}
dict1 = {'a':'b'}
for a,b in dicts.items():
    print(a,b)
for a in dicts.keys():
    print(a)
输出:
hello world
hello1 world1
hello
hello1
  • 删除key时用pop函数

pop(key)

d = {'jack':99, 'alice':95, 'tom':90}
d.pop('jack')
print(d)
输出:
{'alice': 95, 'tom': 90}

2、set
set和dict类似,也是一组key的集合,但不存储value

  • 创建一个set,需要提供一个list作为输入集合

set名 = set(list)

s = set(['jack', 'tomase', 'tom', 'tony'])
print(s)
输出:
{'tom', 'jack', 'tomase', 'tony'}
  • 通过add()函数添加key

add(key)

s = set(['jack', 'tomase', 'tom', 'tony'])
s.add('alice')
print(s)
输出:
{'tomase', 'tom', 'tony', 'jack', 'alice'}
  • 通过remove()删除key

remove(key)

s = set(['jack', 'tomase', 'tom', 'tony'])
s.remove('tom')
print(s)
输出:
{'tony', 'tomase', 'jack'}
  • set可以看成数学意义上的无序和无重复元素的集合,两个set可以做数学意义上的交集、并集等
s = set(['jack', 'tomase', 'tom', 'tony'])
s1 = set(['张三', 'tom', '王五'])
print(s & s1)
print(s | s1)
输出:
{'tom'}
{'tom', '张三', 'tony', 'tomase', 'jack', '王五'}

5、不可变现象

1、当对可变对象如list进行操作时,其内容可变

sort()函数

s = [5, 6, 2, 3, 7, 1, 4]
s.sort()
print(s)
输出:
[1, 2, 3, 4, 5, 6, 7]

2、当对不可变对象进行操作时如str

replace()函数

a = 'abc'
b = s.replace('a', 'A')
print(a)
print(b)
输出:
abc
Abc
  • 可以看出虽然用了replace函数,但是a并未改变,而是replace创造出了一个新的字符串

a的内容是’abc’,但其实是指,a本身是一个变量,它指向的对象的内容才是’abc’
在这里插入图片描述

replace方法创建了一个新字符串’Abc’并返回,用变量b指向该新字符串
变量a仍指向原有的字符串’abc’,但变量b却指向新字符串’Abc’
在这里插入图片描述

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值