一、数据类型-数值、布尔、字符串学习

1.int(整型)

python3中已经不再区分整数和长整数;

2.float(浮点型)

浮点数用来处理实数,即带有小数的数字。类似于C语言中的double类型,占8个字节(64位),其中52位表示底,11位表示指数,剩下的一位表示符号,浮点数不全是小数;

二、for循环

1.基本语法:

for i in range(5):

range(5)代表[0,1,2,3,4]

如果我们要输出1~101之间的奇数,代码如下:

1 for i in range(1,101):
2     if i % 2 == 1:
3         print('loop:',i)

也用下面代码实现,更加简练:

1 for i in range(1,101,2):
2     print('loop:',i)

range(1,101,2)第三个参数代表步长,从1开始,每次+2

简单的for循环使用例子,判断用户名、密码输入是否正确,允许尝试三次,具体代码如下:

 1 _user = 'swc'
 2 _password = 'abc123'
 3 
 4 for i in range(3):
 5     username = input('Username:')
 6     password = input('Password:')
 7 
 8     if username == _user and password == _password:
 9         print('Welcome to here....')
10         break
11     else:
12         print('Please try username or password again!')
13 print('还登录,打死你,你个蠢蛋!')

但此程序现在有个问题,不管你登录失败还是成功,都会最后输出“还登录,打死你,你个蠢蛋!”

为了解决这个问题,我们引入一个变量,用来判断是不是要输出这句话,具体代码如下:

 1 _user = 'swc'
 2 _password = 'abc123'
 3 
 4 passed_authentication = False #此为定义的变量,默认是false
 5 for i in range(3):
 6     username = input('Username:')
 7     password = input('Password:')
 8 
 9     if username == _user and password == _password:
10         print('Welcome to here....')
11         passed_authentication = True #登录成功后将此变量赋值为真
12         break
13     else:
14         print('Please try username or password again!')
15 #如果登录成功现在变量为True,但if后面是‘真’才执行下面print,故我们用not取反
16 if not passed_authentication:
17     print('还登录,打死你,你个蠢蛋!')

以上代码还有一个更简便的实现方法,具体代码如下:

 1 _user = 'swc'
 2 _password = 'abc123'
 3 
 4 for i in range(3):
 5     username = input('Username:')
 6     password = input('Password:')
 7 
 8     if username == _user and password == _password:
 9         print('Welcome to here....')
10         break
11     else:
12         print('Please try username or password again!')
13 else:
14     print('还登录,打死你,你个蠢蛋!')

此程序中如果for循环循环完毕而未被打断,便执行else:语句,如果通过break后,就不再执行else:后面语句,while循环同for循环一个用法

 三、列表

1 s = ['a','g','f','e','y','m']
2 print(s[1:]) 

输出结果为:['g', 'f', 'e', 'y', 'm'],:后面什么都不加,取值取到最后,包含最后一个值

1 s = ['a','g','f','e','y','m']
2 print(s[1:4])

输出结果为:['g', 'f', 'e'],输出列表中1号位置到4-1号位置的元素,输出的是列表中1号位置到n-1号位置的元素

1 s = ['a','g','f','e','y','m']
2 print(s[1:-1])

输出结果为:['g', 'f', 'e', 'y'],输出到倒数第二个值;

1 s = ['a','g','f','e','y','m']
2 print(s[1::2])

输出结果为:['g', 'e', 'm'],第三个参数为步长,位号+2

四、list操作

1.添加append、insert

append是添加到list的末尾,而insert是插入到list的指定位置

1 s = ['a','g','f','e','y','m']
2 s.append('n')
3 print(s)

输出结果:['a', 'g', 'f', 'e', 'y', 'm', 'n']

1 s = ['a','g','f','e','y','m']
2 s.insert(3,'n')
3 print(s)

输出结果:['a', 'g', 'f', 'n', 'e', 'y', 'm'],将‘n’插入到3号位置

2.更改list中的元素

1 s = ['a','g','f','e','y','m']
2 s[1:3]=['swc','zxl']
3 print(s)

输出结果:['a', 'swc', 'zxl', 'e', 'y', 'm']

3.删除list中的元素:remove、pop、del

1 s = ['a','g','f','e','y','m','m']
2 s.remove('m')
3 s.remove('m')
4 print(s)

remove后面直接跟list中你要删除的元素,需要两次remove('m')才能全部删除,而不是一次,默认是从左往右删除搜索到的第一个元素

1 s = ['a','g','f','e','y','j']
2 receive = s.pop(3)
3 print(s)
4 print(receive)

输出结果为:

['a', 'g', 'f', 'y', 'j']
e

pop是运用索引将list中的元素删除后,并返回删除的值

1 s = ['a','g','f','e','y','j']
2 del s[4]
3 print(s)

输出结果为:['a', 'g', 'f', 'e', 'j']

4.统计list中某元素的数量:count

1 s = ['a','e','f','e','e','j']
2 n = s.count('e')
3 print(n)

输出结果为:3

5.extend方法

1 a = [1,2,3,4]
2 b = [7,8,9]
3 a.extend(b)
4 print(a)
5 print(b)

输出结果为:

1 [1, 2, 3, 4, 7, 8, 9]
2 [7, 8, 9]

6.index方法

用于查找某个元素在list中的位号

1 a = ['a','p','f','e','m','j']
2 b = a.index('m')
3 print(b)

输出结果为:4

7.reverse方法,倒序排列

1 a = [1,2,3,4,5,6,7]
2 a.reverse()
3 print(a)

输出结果:[7, 6, 5, 4, 3, 2, 1]

8.sort方法,升序排列

1 a = [5,4,7,8,2,4,0]
2 a.sort()
3 print(a)

输出结果:[0, 2, 4, 4, 5, 7, 8]

五、作业——购物车程序

1.自己完成的代码:

 1 #__author__:Administrator
 2 #DATE:2018/2/6
 3 
 4 salary = float(input('Please input your salary:'))
 5 msg = """
 6 ------info of shoping------
 7 1.  iphone6s    5800
 8 2.  mac book    9000
 9 3.  coffee      32
10 4.  python book 80
11 5.  bicyle      1500
12 """
13 print(msg)
14 shopping_list = []
15 user_choose = input('>>>:')
16 while user_choose != 'q':
17     if user_choose == '1':
18         balance = salary - 5800
19         if balance >=0:
20             print('已加入iphone6s到你购物车,当前余额:',balance)
21             salary = balance
22             shopping_list.append('iphone6s')
23         else:
24             print('您的余额不足,还差:%f'% balance)
25 
26     elif user_choose == '2':
27         balance = salary - 9000
28         if balance >=0:
29             print('已加入mac book到你购物车,当前余额:',balance)
30             salary = balance
31             shopping_list.append('mac book')
32         else:
33             print('您的余额不足,还差:%f'% balance)
34 
35     elif user_choose == '3':
36         balance = salary - 32
37         if balance >=0:
38             print('已加入coffee到你购物车,当前余额:',balance)
39             salary = balance
40             shopping_list.append('coffee')
41         else:
42             print('您的余额不足,还差:%f'% balance)
43 
44     elif user_choose == '4':
45         balance = salary - 80
46         if balance >=0:
47             print('已加入python book到你购物车,当前余额:',balance)
48             salary = balance
49             shopping_list.append('python book')
50         else:
51             print('您的余额不足,还差:%f'% balance)
52 
53     elif user_choose == '5':
54         balance = salary - 1500
55         if balance >=0:
56             print('已加入bicyle到你购物车,当前余额:',balance)
57             salary = balance
58             shopping_list.append('bicyle')
59         else:
60             print('您的余额不足,还差:%f'% balance)
61     user_choose = input('>>>:')
62 if user_choose == 'q':
63     print('-----您已购买以下商品-----')
64     for i in shopping_list:
65         print(shopping_list.index(i)+1,i)
66     print('您的余额为:%f'%balance+'欢迎下次光临!')
67     print('-------------------------')
View Code

相关文章:

  • 2021-12-19
  • 2019-11-17
  • 2022-12-23
  • 2022-12-23
  • 2021-11-27
  • 2021-11-15
  • 2021-11-13
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-12-11
  • 2021-05-24
相关资源
相似解决方案