Python学习(一)
- 多行注释也可用于多行打印
- Python中单双引号意义相同
- 格式化输出
- 数据类型
- for循环
- 列表(一)
-
1. 电影推荐
- 被解救的姜戈
- 华尔街之狼
- 阿甘正传
- 辛德勒的名单
- 上帝之城
- 焦土之城
- 绝美之城
2. 格式化输出
- 占位符
- %s string 字符串
- %d digit 整数
- %ffloat浮点数
-
e.g.:
name = Han msg = Name : %s %(name)
3. 数据类型
- 整型
- 长整型(Python不区分整型和长整型)
- 浮点型
- 布尔型
- 字符串
4. for循环
-
e.g:
for i in range(3): print("i")
5. 列表★
- e.g:
```python
a = ['1','2','3','4','5']
增删改查
查询-切片
print(a[1:]) #取到最后
print(a[1:-1]) #取到倒数第二个(顾头不顾尾)
print(a[1👎2]) #步长默认为一
print(a[3::-2]) #步长的正负代表方向
count-计算元素出现次数
['to','be','or','not','to','be'].count('to')
index-查找某一内容的索引
print(a.index('1')) #结果:0
增加 append insert
a.append('6') #默认插入到最后一个位置
a.insert(1,'x') #插入到任意位置
extend-将一个列表的内容添加到另一个列表中
a=[1,2,3]
b=[4,5,6]
a.extend(b) #结果: a = [1,2,3,4,5,6] , b = [4,5,6]
修改-取值->赋值
a[1] = 'y'
a[1:3] = ['a','b']
删除 remove pop del
a.remove('1') #删除固定内容
a.pop(1) #根据索引删除内容并返回删除的值
del a[0]
排序 reserve sort
a.reserve()
print(a) #结果为a的倒序
a.sort()
print(a) #按照编码从小到大排序
```
6. 作业
- 购物车程序-题干
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @File : shopping.py
# @Author: Ryoma
# @Date : 2017/9/9
# @Desc :
# @license : Copyright(C), blanc.site
# @Contact : ryomahan1996@gmail.com
# @Software : PyCharm Community Edition
# 制定商品价格
name = ['iphone6','mac book','coffee','books','bicyle']
price = [5800,9000,32,80,1500]
# 购物车
shopping_cart = []
print("Welcome to JD")
# 输入工资
salary = int(input("Please tell me your salary:"))
# 打印商品列表
print("Now you can buy :")
for i in range(len(name)):
print("%s . %s---%s" %(i+1,name[i],price[i]))
print("Input 0 to move")
# 购买
while salary:
id = int(input("What did you want to buy:(1-5)"))
if id == 0:
break
elif 0<id<6 :
if salary - price[id-1] >= 0:
salary -= price[id-1]
print("You buy a %s | balance is %s" %(name[id-1],salary))
shopping_cart.append(id)
continue
else:
print("Sorry , you didn't have enough money!")
break
else:
print("You can input 1-5 to buy!")
# 退出
if len(shopping_cart)>0:
print("You have purchased these goods:")
for i in range(len(shopping_cart)):
print("%s---%s" %(name[shopping_cart[i]-1],price[shopping_cart[i]-1]))
print("Your balance is %s" %salary)
print("Looking forward to your next visit!")
else:
print("You have purchased nothing!")
多行注释也可用于多行打印
Python中单双引号意义相同
格式化输出
数据类型
for循环
列表(一)
1. 电影推荐
被解救的姜戈
华尔街之狼
阿甘正传
辛德勒的名单
上帝之城
焦土之城
绝美之城
2. 格式化输出
占位符
%s string 字符串
%d digit 整数
%ffloat浮点数
e.g.:
name = Han
msg = Name : %s %(name)
3. 数据类型
整型
长整型(Python不区分整型和长整型)
浮点型
布尔型
字符串
4. for循环
e.g:
for i in range(3):
print("i")
5. 列表★
e.g:
```python
a = ['1','2','3','4','5']
增删改查
查询-切片
print(a[1:]) #取到最后
print(a[1:-1]) #取到倒数第二个(顾头不顾尾)
print(a[1👎2]) #步长默认为一
print(a[3::-2]) #步长的正负代表方向
count-计算元素出现次数
['to','be','or','not','to','be'].count('to')
index-查找某一内容的索引
print(a.index('1')) #结果:0
增加 append insert
a.append('6') #默认插入到最后一个位置
a.insert(1,'x') #插入到任意位置
extend-将一个列表的内容添加到另一个列表中
a=[1,2,3]
b=[4,5,6]
a.extend(b) #结果: a = [1,2,3,4,5,6] , b = [4,5,6]
修改-取值->赋值
a[1] = 'y'
a[1:3] = ['a','b']
删除 remove pop del
a.remove('1') #删除固定内容
a.pop(1) #根据索引删除内容并返回删除的值
del a[0]
排序 reserve sort
a.reserve()
print(a) #结果为a的倒序
a.sort()
print(a) #按照编码从小到大排序
```
6. 作业
购物车程序-题干
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @File : shopping.py
# @Author: Ryoma
# @Date : 2017/9/9
# @Desc :
# @license : Copyright(C), blanc.site
# @Contact : ryomahan1996@gmail.com
# @Software : PyCharm Community Edition
# 制定商品价格
name = ['iphone6','mac book','coffee','books','bicyle']
price = [5800,9000,32,80,1500]
# 购物车
shopping_cart = []
print("Welcome to JD")
# 输入工资
salary = int(input("Please tell me your salary:"))
# 打印商品列表
print("Now you can buy :")
for i in range(len(name)):
print("%s . %s---%s" %(i+1,name[i],price[i]))
print("Input 0 to move")
# 购买
while salary:
id = int(input("What did you want to buy:(1-5)"))
if id == 0:
break
elif 0<id<6 :
if salary - price[id-1] >= 0:
salary -= price[id-1]
print("You buy a %s | balance is %s" %(name[id-1],salary))
shopping_cart.append(id)
continue
else:
print("Sorry , you didn't have enough money!")
break
else:
print("You can input 1-5 to buy!")
# 退出
if len(shopping_cart)>0:
print("You have purchased these goods:")
for i in range(len(shopping_cart)):
print("%s---%s" %(name[shopping_cart[i]-1],price[shopping_cart[i]-1]))
print("Your balance is %s" %salary)
print("Looking forward to your next visit!")
else:
print("You have purchased nothing!")
本文来自博客园,作者:小珍珠在河里敲代码,转载请注明原文链接:https://www.cnblogs.com/Jansens520/p/7510460.html