Python之路,Day02-IDE、用户输入与列表数据

本节内容

1、IDLE的替代工具

2、数字、字符串、对象

3、注释

4、用户输入

5、python的内置数据结构

6、列表

7、习题

 

1、pycharm and jupyter notebook

思考:

  1、甚么是IDE?

1 '''
2 集成开发环境(IDE,Integrated Development Environment )是用于提供程序开发环境的应用程序,一般包括代码编辑器、编译器、调试器和图形用户界面等工具。集成了代码编写功能、分析功能、编译功能、调试功能等一体化的开发软件服务套。
3 '''
IDE

  2、甚么是IDLE?

1 #IDLE 是一个纯 Python 下自带的简洁的集成开发环境(IDE)
IDLE

 

 

1、pycharm

安装网址:https://www.jetbrains.com/pycharm/ 安装过程(略)

永久激活:https://www.liuzhishi.com/3184.html

 

 

 

优势:1、可以自动补全。

      2、方便于代码调试,可以随时终止进程。(vim、sublime等不能直接调试)

2、jupyter notebook

问题1:甚么是jupyter notebook?

问题2:如何在CMD中运行jupyter notebook?

https://jupyter.org/ 

尝试一下吧:

 

 

甚么是jupyter notebook?

1、jupyter是一个基于web的IDE(集成开发环境)。

2、兼具脚本操作和交互式操作的特性;

3、笔记式编辑代码和运行,便于调试,便于保存。

 

 

 notebook使用举例:

https://www.kaggle.com/  

主要为开发商和数据科学家提供举办机器学习竞赛、托管数据库、编写和分享代码的平台。

该平台已经吸引了80万名数据科学家的关注,这些用户资源或许正是吸引谷歌的主要因素。
 
Kaggle offers a no-setup, customizable, Jupyter Notebooks environment. Access free GPUs and a huge repository of community published data & code.

Inside Kaggle you’ll find all the code & data you need to do your data science work. Use over 19,000 public datasets and 200,000 public notebooks to conquer any analysis in no time.

eg:

 

2、 在cmd中运行jupyter:

①安装

 

 

②运行

 

 学会了么?是不是很简单。

总结:

cmd/IDLE 中的交互执行:偶尔执行一些简单的语句、测试

jupyter notebook:介于交互和脚本之间的(可做笔记,关心中间过程的输出)

IDLE 小型项目,学习初期的选择,功能完善

PyCharm 中大型项目中方便组织较多文件,功能更为丰富

pycharm 将成为学习python之路上的最重要的工具。

jupyter 将成为我们数据科学(包含大数据、数据分析)之路的重要工具。

 

 2、变量(数字、字符串、对象)

 请同学们一定要详细阅读:https://docs.python.org/3.7/tutorial/introduction.html#numbers 学有余力的同学将链接中的代码学习一遍。

1、数字(Numbers)

The integer numbers (e.g. 2420) have type int, the ones with a fractional part (e.g. 5.01.6) have type float. We will see more about numeric types later in the tutorial.

1 >>> 2 + 2
2 4
3 >>> 50 - 5*6
4 20
5 >>> (50 - 5*6) / 4
6 5.0
7 >>> 8 / 5  # division always returns a floating point number
8 1.6

Division (/) always returns a float. To do floor division and get an integer result (discarding any fractional result) you can use the // operator; to calculate the remainder you can use %:

1 >>> 17 / 3  # classic division returns a float
2 5.666666666666667
3 >>>
4 >>> 17 // 3  # floor division discards the fractional part
5 5
6 >>> 17 % 3  # the % operator returns the remainder of the division
7 2
8 >>> 5 * 3 + 2  # result * divisor + remainder
9 17

 

1 import random
2 wait_time = random.randint(1,60)
3 print(wait_time)

Python中的数据类型(整型、浮点型和 复数 ) 

eg:

 

2、字符串(Strings)

Besides numbers, Python can also manipulate strings, which can be expressed in several ways.

They can be enclosed in single quotes ('...') or double quotes ("...") with the same result 2\ can be used to escape quotes

 1 >>> 'spam eggs'  # single quotes
 2 'spam eggs'
 3 >>> 'doesn\'t'  # use \' to escape the single quote...
 4 "doesn't"
 5 >>> "doesn't"  # ...or use double quotes instead
 6 "doesn't"
 7 >>> '"Yes," they said.'
 8 '"Yes," they said.'
 9 >>> "\"Yes,\" they said."
10 '"Yes," they said.'
11 >>> '"Isn\'t," they said.'
12 '"Isn\'t," they said.'

 

3、对象

python中“一切皆是对象”。P48-49 了解

3、注释

单行注释:# 被注释内容

eg1:

1 # -*- coding:utf-8 -*-
2 # Author:Zhichao

 

多行注释:""" 被注释内容 """  

eg2:

1 '''
2 import random
3 wait_time = random.randint(1,60)
4 print(wait_time)
5 
6 word = "bottles"
7 print(word)
8 '''

 

除此之外,""" 被注释内容 """  还可以打印变量

eg:3

1 test1 ='''
2 import random
3 wait_time = random.randint(1,60)
4 print(wait_time)
5 
6 word = "bottles"
7 print(word)
8 '''
9 print(test1)

 

4、用户输入

eg:

1 # -*- coding:utf-8 -*-
2 # Author:Zhichao
3 
4 username = input("username:")
5 password = input("password:")
6 
7 print(username,password)

 

 

 字符串拼接+打印:

eg:

 1 name = input("name:")
 2 age = input("age:")
 3 job = input("job:")
 4 salary = input("salary:")
 5 
 6 info = '''----- INFO OF ''' + name +'''------''' + '''
 7 age:''' + age+'''
 8 job:''' + job +'''
 9 salary:'''+salary
10 
11 print(info)

 

是不是很麻烦?有没有更简单的方式将打印内容?接下来我们来学习 %s 占位符。

 1 name = input("name:")
 2 age = input("age:")
 3 job = input("job:")
 4 salary = input("salary:")
 5 
 6 info = '''-------INFO OF %s -------
 7 Name:%s
 8 Age:%s
 9 Job:%s
10 Salary:%s
11 '''% (name,name,age,job,salary)
12 
13 print(info)

%s代表 string

%d代表 number

%f代表 float

更进一步:设置数据类型,将age、salary设置为number。

 1 name = input("name:")
 2 age = int(input("age:"))  #注意! python中默认所有的输入均为string
 3 job = input("job:")
 4 salary = int(input("salary:"))
 5 
 6 info = '''-------INFO OF %s -------
 7 Name:%s
 8 Age:%d
 9 Job:%s
10 Salary:%d
11 '''% (name,name,age,job,salary)
12 
13 print(info)
View Code

.format():

 1 info2 =  '''-------INFO OF {_name} -------
 2 Name:{_name}
 3 Age:{_age}
 4 Job:{_job}
 5 Salary:{_salary}
 6 '''.format(_name=name,
 7            _age=age,
 8            _job=job,
 9            _salary=salary)
10 print(info2)
View Code
1 info3 =  '''-------INFO OF {0} -------
2 Name:{0}
3 Age:{1}
4 Job:{2}
5 Salary:{3}
6 '''.format(name,age,job,salary)
7 print(info3)
View Code

 

项目1:

 1 # Author:Zhichao
 2 
 3 today = input("weekday:")
 4 if today == "Saturday":
 5     print("Party!")
 6 elif today == "Sunday":
 7     condition = input("mood:")
 8     if condition == "Headache":
 9         print("Recover, then rest.")
10     else:
11         print("Rest.")
12 else:
13     print("Work, work, work.")
View Code

5、Python内置数据结构

6、列表

思考:如果把我们班同学的名字都存起来,你会怎么做?用变量name1、name2、name3....来存么?

还是这样:names=“zhangyang  liuming  lihua”?。。那么如果想让你取出其中某一个值该怎么办?

Python knows a number of compound data types, used to group together other values. The most versatile is the list, which can be written as a list of comma-separated values (items) between square brackets. Lists might contain items of different types, but usually the items all have the same type.

请同学们详细阅读官方文档:https://docs.python.org/3/tutorial/introduction.html#lists

来看一些例子

eg1:

1 names = ["Mike","Mary","Jan","Jack"]
2 
3 print(names)

 eg2:

1 >>> squares = [1, 4, 9, 16, 25]
2 >>> squares
3 [1, 4, 9, 16, 25]

回顾:for循环

1 for ch in "Hi!":
2      print(ch)

for加上列表来让我们的列表开始工作(大脑P60):

1 vowels = ['a','e','i','o','u']
2 word = "Milliways"
3 for letter in word:
4     if letter in vowels:
5         print(letter)
vowels
1 vowels = ['a','e','i','o','u']
2 word = "Milliways"
3 found = []
4 for letter in word:
5     if letter in vowels:
6         if letter not in found:
7             found.append(letter)
8 for vowel in found:
9     print(vowel)
vowels2
1 vowels = ['a','e','i','o','u']
2 word = input("Provide a word to search for vowels:")
3 found = []
4 for letter in word:
5     if letter in vowels:
6         if letter not in found:
7             found.append(letter)
8 for vowel in found:
9     print(vowel)
vowels3

 

 列表认识开始、结束和步长值:

letters[start:stop:step]

如果没有指定开始值,则默认为0;

如果没有指定结束指,则取列表允许的最大值;

如果没有指定步长值,则默认步长为1.

 

首先,列表也是一个变量,构建列表需要用”[]“,里面的数据之间用”,“进行分隔。

①list的查询(切片):

思考eg1中如果我们想取出某一个值应该怎么办?取出一些值呢?

eg3:

 1 #Zhichao
 2 
 3 names = ["Mike","Mary","Jan","Jack"]
 4 
 5 print(names)
 6 print(names[0],names[2]) #取出某一个值
 7 print(names[1:3]) #切片 取出中间一些连续的值(还是列表)
 8 #思考:如果列表有几千个值,几万个值,那么想取出最后一个值怎么办?
 9 print(names[-1]) #切片 取出最后一个值
10 print(names[-3:-1]) #切片 从后往前取出一些值(还是列表),思考为什么不是[-1:-3]?
11 print(names[-2:]) #切片 取到最后一个值
12 print(names[:2]) #切片 同上,即“0”可以省略

 

②list 增、删、改:

eg:

 1 #Zhichao
 2 
 3 names = ["Mike","Mary","Jan","Jack"]
 4 
 5 print(names)
 6 names.append("Alex") #list增:append增加数据,观察增加的位置。
 7 print(names)
 8 names.insert(1,"Dan") #list增:insert指定位置增加数据
 9 print(names)
10 names[3] = "Zhichao" #list改:直接找到列表中某个值,然后赋新值
11 print(names)
12 names.remove("Zhichao") #list删:remove指定某个数据进行删除
13 print(names)
14 del names[1] #list删: del指定[x]的某个数据位置进行删除
15 print(names)
16 names.pop() #list删:Remove and return item at index (default last) 不指定默认删除最后一个值
17 print(names)
18 names.pop(1) #lsit删:如果加上指定位置,names.pop(1)<==> del names[1]
19 print(names)

参考结果:

 ③列表的复制 .copy()

 

 

1 first = [1,2,3,4,5]
2 second = first
3 print(second)
4 second.append(6)
5 print(first)

正确的复制列表的方法1:

1 #接上面的代码
2 third = second.copy()
3 third.append(7)
4 print(second,third)

方法2:

利用切片:

1 a = [1,2,3,4]
2 b = a[:]
3 print(a,b)
4 b.append(5)
5 print(a,b)

 

预:尝试填大脑P67处理列表代码

项目2(将"Don‘t panic!" 转换为 "on tap"):大脑P68(上课详解)

 1 # Author:Zhichao
 2 
 3 phrase = "Don't panic!"
 4 plist = list(phrase)
 5 print(phrase)
 6 print(plist)
 7 
 8 for i in range(4):
 9     plist.pop()
10 plist.pop(0)
11 plist.remove("'")
12 plist.extend([plist.pop(),plist.pop()])
13 plist.insert(2,plist.pop(3))
14 new_phrase = ''.join(plist)
15 print(plist)
16 print(new_phrase)
panic.py

 

 1 # -*- coding:utf-8 -*-
 2 # Author:Zhichao
 3 
 4 phrase = "Don't panic!"
 5 plist = list(phrase)
 6 print(phrase)
 7 print(plist)
 8 
 9 new_pharse = ''.join(plist[1:3])
10 new_pharse = new_pharse + ''.join([plist[5],plist[4],plist[7],plist[6]])
11 print(plist)
12 print(new_pharse)
panic2.py

 

思考:panic和painc2 那个更好?

阅读大脑P82-P85

for 循环了解列表:

课本案例:

1 # -*- coding:utf-8 -*-
2 # Author:Zhichao
3 
4 paranoid_android = "Marvin"
5 letters = list(paranoid_android)
6 for char in letters:
7     print('\t',char)
marvin.py
 1 # -*- coding:utf-8 -*-
 2 # Author:Zhichao
 3 
 4 paranoid_android = "Marvin, the Paranoid Android"
 5 letters = list(paranoid_android)
 6 for char in letters[:6]:
 7     print('\t',char)
 8 print()
 9 for char in letters[-7:]:
10     print('\t'*2,char)
11 print()
12 for char in letters[12:20]:
13     print('\t'*3,char)
14 print()
marvin2.py

 

 

 

7 习题

购物车程序项目:

要求:1、运行程序后,让用户输入支付宝余额,然后打印我们的商品列表给用户。

     2、让用户输入商品编号进行商品的购买。

     3、用户选择商品后,检查用户的余额是否够,若够则直接扣款,不够则提醒用户。

     4、用户可以随时退出购买,推出时打印用户已购买的商品和支付宝余额。

 

posted @ 2019-08-30 23:20  智超(Zhichao)  阅读(4931)  评论(1编辑  收藏  举报