day-python入门3
本节内容
鸡汤、电影
IDE介绍
知识回顾
数据类型
For循环
while循环
列表及常用操作
IDE介绍 IDE即集成开发环境
常见IDE
Visualstudio : windows C C++开发
Eclipse: Java开发
Vim:大神&小白
python常用: pycharm、sublime、IDLE、spider等
主推:pycharm
- Pycharm工程建立方式
File ==》 New Project
- Pycharm 字体设置
File ==》 Setting ==》Editor ==》Color& Fonts ===》Fonts
- Pycharm 解释器选择
File ==》 Setting ==》Project ===》 Project Interpreter
- Pycharm 代码模板设置
File ==》 Setting ==》Editor ==》 File and Code Templates ===》 Python Script
输入以下内容:
# -*- coding: utf-8 -*- """ Created on ${TIME} ${DATE} @author: Song @E-mail: songkunhuang@163.com """
字符串格式化输出
# -*- coding: utf-8 -*- """ Created on 17:19 2017/8/24 @author: Song @E-mail: songkunhuang@163.com """ name = input("Name:") age = input("Age:") job = input("Job:") salary = input("Salary:") # print(name, age, job, salary) # msg = """------info of Alex------ # name:Alex # age:22 # job:IT # salary:3000 # ---------end----------- # """ # print(msg) # msg = """------info of %s------ # name:%s # age:%s # job:%s # salary:%s # ---------end----------- # """ % (name, name, age, job, salary) # print(msg) msg = """------info of %s------ name:%s age:%s job:%s salary:%s ---------end----------- """ % (name, name, age, job, salary) # 改为%d 报错 改为%f输出浮点数 print(msg)
执行结果:
C:\Python3\python3.exe D:/Oldboy/pythonfullstack/day5/format_output.py Name:alex Age:22 Job:IT Salary:5000 ------info of alex------ name:alex age:22 job:IT salary:5000 ---------end-----------
% () 元组 %d %s %f
%s也可以直接用于输出数字,不报错(内部调用str())
数据类型
数值
整数 int python2中存在long型
python2中:
Python 2.7.13 (v2.7.13:a06454b1afa1, Dec 17 2016, 20:53:40) [MSC v.1500 64 bit (AMD64)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>> type(100) <type 'int'> >>> type(2*31) <type 'int'> >>> type(2**31) <type 'long'>
>>> 2**31
2147483648L # L表示long型数据
python3中:
Python 3.6.1 (v3.6.1:69c0db5, Mar 21 2017, 18:41:36) [MSC v.1900 64 bit (AMD64)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>> type(100) <class 'int'> >>> type(2*34) <class 'int'> >>> type(2*100) <class 'int'>
浮点数 float
Python 3.6.1 (v3.6.1:69c0db5, Mar 21 2017, 18:41:36) [MSC v.1900 64 bit (AMD64)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>> type(3/2) <class 'float'> >>> 3/2 1.5
除法区别
在python3中:/ 表示数学除法 , // 表示地板除
>>> 3/2 1.5 >>> 3//2 1
在python2中:都是地板除
Python 2.7.13 (v2.7.13:a06454b1afa1, Dec 17 2016, 20:53:40) [MSC v.1500 64 bit (AMD64)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>> 3/2 1 >>> 3//2 1
在python2中要实现数学除法,需要导入特殊方法:
>>> from __future__ import division >>> 3/2 1.5 >>> 3//2 1
布尔值
表示真假,即 True False
非零表示真,零表示假
>>> bool(1) True >>> bool(3) True >>> bool(0) False
字符串
python中没有字符的概念,python认为单个字符就是长度为1的字符串
Str()方法可以实现类型转换
>>> type(str(11)) <type 'str'>
For 循环
for循环是有限循环 ,一般与 in 连用 for in
range是python中的常用序列生成方法,用于生成整数序列列表
>>> range(10)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
range更一般的用法: range(start, end , step) 其中步长只能为整数
>>> range(3,8,2)
[3, 5, 7]
# a = range(3) # print(a) # in py3.x range is iterable & iterator # in py2.x xrange is iterable & iterator object range is list
打印0-100
# for i in range(101): # print(i)
打印0-100的奇数
方法一: If 取余运算
for i in range(101): if i % 2 == 1: print(i)
方法二:使用range方法,设置步长为2:
for i in range(1,101,2): print(i)
Break
用户输入 用For循环输多次尝试,正确的时候exit(),或者break
用户名密码验证程序 在尝试三次后退出程序 range(3)
用户名密码验证程序 在尝试三次后退出程序,并打印“你尝试次数太多了,滚”
方法一:使用标志位 passed_authentication=False
方法二:for…else…连用 else只在循环正常结束时执行
# 实现登陆接口功能 登陆正确退出 登陆三次错误 不允许再登陆 _username = "alex" _password = "abc123" # passed_authentication = False for i in range(3): username = input("Username:") password = input("password:") if username == _username and password == _password: print("Welcome, %s " % username) passed_authentication = True # exit() break else: print("用户名或密码错误") else: print("你试了太多次了,滚") # if not passed_authentication: # print("你试了太多次了,滚")
Continue 打印1-100,跳过50-70之间的数 1.用continue 2. 用or语句
for i in range(0,101): if i > 50 and i < 70: continue print(i) for i in range(0,101): if i <= 50 or i >= 70: print(i)
循环嵌套
Break只能跳出一层循环,如果想跳出多层循环,并依赖内层循环跳出
即实现you jump I jump 效果可以使用标志位
# for i in range(10): # print("----", i, "------") # for j in range(10): # print("Layer2:", j) # if j > 5: # break # 只能跳出一层循环 break_from_layer_two = False for i in range(10): print("----", i, "------") for j in range(10): print("Layer2:", j) if j > 5: break_from_layer_two = True # you jump break # 只能跳出一层循环 if break_from_layer_two: break # I jump
While循环
无限循环 while True当山峰没有棱角的时候
测试循环执行有多快 time统计时间
While循环实现用户登陆接口 直接copy for程序 使用count计数器计数
While else 实现与for循环登陆接口程序一样的效果
基本数据结构:列表、字典、元组
列表
列表的引入:多个变量存储一列数据困难,数据之间没有联系,无法知道上下文
使用列表存储人名
列表的基本方法
查:
1.索引、切片。根据位置a[0] a[:3] a[-1] a[0::2] a[3::-1]
2. count方法 统计个数
3.index 根据内容查位置
增:
追加 append
插入 insert(index,“content”)
扩展 extend 改变原列表
加法 a + b 不改变原列表 直接组合起来
删:
Remove(“content”) 根据内容删除,只删除一个
Pop(index) 有返回值 默认弹出最后一个值
Del a del a[0] 从内存中删除
改:
a[0] = ‘new’
a[a.index(“old content”)]=”new content”
a.sort(reverse=False) 排序(升序)
a.reverse() 列表反向
身份运算: in 查找列表中是否存在该元素
关于如何找到第二个重复值的索引
先找到第一个值的索引 然后对列表切片,找到余下的小列表的值索引,然后将两个索引进行叠加,即找到第二个重复值的位置
name = ['wuchao', 'jixin', 'ligang', 'xiaopang', 'sanpao', 'ligang'] print(name) # 查 print(name[0]) print(name[0:2]) print(name[2:]) print(name[:-2]) print(name[0:4:2]) print(name[4::-1]) print(name.count('ligang')) print(name.index('ligang')) # 根据内容找位置 # 增 name.append("append classmate") print(name) name.insert(2, "insert classmate") print(name) a = [1, 2, 3] b = [2, 7, 9] a.extend(b) # 扩展列表 print(a) print(a + b) # 删 name.remove("append classmate") print(name) name.remove("ligang") print(name) # 只能删除一个值 name.pop() # 默认弹出最后一个值 print(name) delete_name = name.pop(3) # 按索引弹出元素 可以获得弹出的值 print(name) print(delete_name) del name[3] print(name) del name # 从内存中删除 # 改 name[0] = 'linchao' name[name.index('xiaopang')] = 'xiaopeng' print(name) name.reverse() print(name) name.sort(reverse=True) # 默认从小到大 改为True为从大到小 ASCII码排序 print(name) # 身份运算符 print('ligan' in name) # 找到第二个'ligang' first_ligang_index = name.index('ligang') print(first_ligang_index) little_list = name[first_ligang_index+1:] little_list_ligang_index = little_list.index("ligang") print(little_list_ligang_index) second_ligang_index = first_ligang_index + little_list_ligang_index + 1 print(second_ligang_index)
Type()
作业 购物车