当前位置: 首页 > 编程日记 > 正文

Python20-Day02

1、数据

数据为什么要分不同的类型

数据是用来表示状态的,不同的状态就应该用不同类型的数据表示;

数据类型

数字(整形,长整形,浮点型,复数),字符串,列表,元组,字典,集合

2、字符串

1、按索引取值,只能取

input_info = 'Hello World'
print(input_info[6])

2、切片(顾头不顾尾)

input_info = 'Hello World'
print(input_info[0:5])

3、长度len

input_info = 'Hello World'
print(len(input_info))

4、成员运算in和not in

input_info = 'Hello World'
# if 'Hello' in input_info:
#     print('OK')
if 'wang' not in input_info:print('wang is not in input_info!' )

5、移除空白strip (移除开头和结尾的空格)

input_info = '    Hello World'
input_info2 = '    Hello World    '
print(input_info)
print(input_info.strip())
print(input_info2.strip())

6、切分split

input_info = 'egon,alex,wupeiqi,oldboy' #默认按照空格进行切分
# print(input_info.split()) #切分完成后得到一个列表
print(input_info.split(',')) #指定分隔符进行切分

7、循环

input_info = 'hello world'
for item in input_info:print(item)

#字符串需要掌握的方法

1、lower,upper

input_info = 'Hello World'
print(input_info.upper())
print(input_info.lower())

2、startswith和endswith

input_info = 'hello world'
print(input_info.startswith('hello')) #返回布尔值
print(input_info.endswith('world')) #返回布尔值

3、format

res='{} {} {}'.format('egon',18,'male')
res='{1} {0} {1}'.format('egon',18,'male')
res='{name} {age} {sex}'.format(sex='male',name='egon',age=18)

4、join

tag=' '
print(tag.join(['egon','say','hello','world'])) #可迭代对象必须都是字符串

5、replace

name='hello,every one,are you ok,are you good'
print(name.replace('hello','Hello'))
print(name.replace('you','then',1))

6、isdigit

res_input = input('Input>>: ')
print(res_input.isdigit())

  #字符串类型总结:

    1、只能存一个值,2、有序3、不可变:值变,id就变。不可变==可hash

3、列表

定义:[]内可以有多个任意类型的值,逗号分隔

#按索引存取值(正向存取+反向存取):即可存也可以取 
my_girl_friends=['alex','wupeiqi','yuanhao',4,5] print(my_girl_friends[0]) my_girl_friends.insert(1,'zhaoliying') print(my_girl_friends)

  #切片(顾头不顾尾,步长)
my_girl_friends=['alex','wupeiqi','yuanhao',4,5,'zhaoliying','wangxiaomi']
print(my_girl_friends[1:6:2])

  #长度
my_girl_friends=['alex','wupeiqi','yuanhao',4,5]
print(len(my_girl_friends))


  #成员运算in和not in
my_girl_friends=['alex','wupeiqi','yuanhao',4,5]
if 'alex' in my_girl_friends:print('This is ok')
if 'zhaoliying' not in my_girl_friends:print('ni xiang sha ne')

  #追加
my_girl_friends=['alex','wupeiqi','yuanhao',4,5]
my_girl_friends.append('zhaoliying') #append,在列表的尾部添加
print(my_girl_friends)
my_girl_friends.insert(1,'zhaoliying') #insert,可以指定列表的下标添加元素
print(my_girl_friends)
  #删除
my_girl_friends=['alex','wupeiqi','yuanhao',4,5]
print(my_girl_friends.pop())  #pop,删除列表中的元素,从尾部开始删除,并且会返回删除的值
print(my_girl_friends.pop())
print(my_girl_friends.pop())
  #循环
my_girl_friends=['alex','wupeiqi','yuanhao',4,5]
for item in my_girl_friends:print(item)
  #步长
l=[1,2,3,4,5,6]
print(l[0:3:1]) #正向步长
print(l[2::-1]) #反向步长
print(l[::-1])  #列表翻转

#列表类型总结

1、可以存多个值,值都可以是字符串,列表,字典,元组

2、有序

2、可变:值变,id不变。可变==不可hash

4、元组

#按索引取值(正向取+反向取):只能取
age=(11,22,33,44,55)
print(age[1])
 #2、切片(顾头不顾尾,步长)
age=(11,22,33,44,55)
print(age[1:3])
  #3、长度
age=(11,22,33,44,55)
print(len(age))
  #4、成员运算in和not in
age=(11,22,33,44,55)
if 11 in age:print('This is ok!')
if 88 not in age:print('This is error')
  #5、循环
age=(11,22,33,44,55)
for item in age:print('your age is %s' %item)

5、字典

作用:存多个值,key-value存取,取值速度快

定义:key必须是不可变类型,值可以是任意类型

info={'name':'egon','age':18,'sex':'male'}

  #1、按key存取值:可存可取
info={'name':'egon','age':18,'sex':'male'}
print(info['name'])
  #2、长度len
info={'name':'egon','age':18,'sex':'male'}
print(len(info))

  #3、成员运算in和not in
info={'name':'egon','age':18,'sex':'male'}
if 'name' in info:print(info['name'])
  #4、删除
info={'name':'egon','age':18,'sex':'male'}
print(info.pop('age'))
print(info)
  #5、键keys(),值values(),键值对items()
info={'name':'egon','age':18,'sex':'male'}
for item in info:print(item,info[item])

6、集合

作用:去重,关系运算

定义集合:

集合可以包含多个元素,用逗号分隔

集合的元素遵循三个原则:

1. 每个元素必须是不可变类型

2. 没有重复的元素

3. 无序

注意:集合的目的是将不同的值存放到一起,不同的集合用来做关系运算

#1、长度len
# pythons={'alex','egon','yuanhao','wupeiqi','gangdan','biubiu'}
# print(len(pythons))
pythons={'alex','egon','yuanhao','wupeiqi','gangdan','biubiu'}
linuxs={'wupeiqi','oldboy','gangdan'}
#3、|合集
# print(pythons|linuxs)
#4、&交集
# print(pythons & linuxs)
#5、-差集
# print(pythons - linuxs)
#6、^对称差集
# print(pythons ^ linuxs)

7、 文件编码

字符编码的发展3个阶段:

1、 现代计算机起源于美国,最早诞生也是基于英文考虑的ASCII

2、为了满足中文和日文,中国定制了GBK

3、每一个国家都有自己的标准,就不可避免的会有冲突,在多语言混合的文本中,显示出来就会乱码,如何解决:

1. 能够兼容万国字符

2. 与全世界所有的字符编码都有映射关系,这样就可以转换成任意国家的的字符编码。

总结:内存中统一采用Unicode,浪费空间来换取可以转换成任意编码(不乱码),硬盘可以采用各种编码,如utf-8,保证存放于硬盘或者基于网络传输的数据量很小,提高传输效率与稳定性。

总结:保证不乱码的的核心法则:

字符按照什么标准而编码的,就按照什么标准解码。

在内存中写的所有字符,都是Unicode编码。

8、 文件操作

1、文件操作流程

打开文件,得到文件句柄并赋值给一个变量

通过句柄对文件进行操作

关闭文件

打开文件的模式有:

r,只读模式(默认)。
    w,只写模式。【不可读;不存在则创建;存在则删除内容;】
    a,追加模式。【可读; 不存在则创建;存在则只追加内容;】
    "+" 表示可以同时读写某个文件

r+,可读写文件。【可读;可写;可追加】
    w+,写读
    a+,同a
    "U"表示在读取时,可以将 \r \n \r\n自动转换成 \n (与 r 或 r+ 模式同使用)

rU
    r+U
    "b"表示处理二进制文件(如:FTP发送上传ISO镜像文件,linux可忽略,windows处理二进制文件时需标注)

rb
    wb
    ab

f = open('文件操作.file','r',encoding='utf-8')
# print(f.read())
print(f.readline(3).strip())
#readline每次读一行,可以设置limit(字符)

2、with语句

为了避免打开文件后忘记关闭,可以用with:

with open('文件操作.file','r',encoding='utf-8') as f:pass

python2.7后,with支持对多个文件的上下文进行管理

with open('log1') as obj1, open('log2') as obj2:pass

转载于:https://www.cnblogs.com/mrwang1101/p/8023521.html

相关文章:

Android网络框架-OkHttp3.0总结

一、概述 OkHttp是Square公司开发的一款服务于android的一个网络框架,主要包含: 一般的get请求一般的post请求基于Http的文件上传文件下载加载图片支持请求回调,直接返回对象、对象集合支持session的保持github地址:https://githu…

第一天写,希望能坚持下去。

该想的都想完了,不该想的似乎也已经尘埃落定了。有些事情,终究不是靠努力或者不努力获得的。顺其自然才是正理。 以前很多次想过要努力,学习一些东西,总是不能成,原因很多: 1.心中烦恼,不想学…

mac gource_如何使用Gource显示项目的时间表

mac gourceThe first time I heard about Gource was in 2013. At the time I watched this cool video showing Ruby on Rails source code evolution:我第一次听说Gource是在2013年 。 当时,我观看了这段很酷的视频,展示了Ruby on Rails源代码的演变&a…

insert语句让我学会的两个MySQL函数

我们要保存数据到数据库,插入数据是必须的,但是在业务中可能会出于某种业务要求,要在数据库中设计唯一索引;这时如果不小心插入一条业务上已经存在同样key的数据时,就会出现异常。 大部分的需求要求我们出现唯一键冲突…

对PInvoke函数函数调用导致堆栈不对称。原因可能是托管的 PInvoke 签名与非托管的目标签名不匹配。...

C#引入外部非托管类库时,有时候会出现“对PInvoke函数调用导致堆栈不对称。原因可能是托管的 PInvoke 签名与非托管的目标签名不匹配”的报错。 通常在DllImport标签内加入属性CallingConventionCallingConvention.Cdecl即可解决该问题。 如: [Dll…

Python字符串方法用示例解释

字符串查找方法 (String Find Method) There are two options for finding a substring within a string in Python, find() and rfind().在Python中的字符串中有两个选项可以找到子字符串: find()和rfind() 。 Each will return the position that the substring …

关于命名空间namespace

虽然任意合法的PHP代码都可以包含在命名空间中,但只有以下类型的代码受命名空间的影响,它们是:类(包括抽象类和traits)、接口、函数和常量。在声明命名空间之前唯一合法的代码是用于定义源文件编码方式的 declare 语句…

一 梳理 从 HDFS 到 MR。

MapReduce 不仅仅是一个工具,更是一个框架。我们必须拿问题解决方案去适配框架的 map 和 reduce 过程很多情况下,需要关注 MapReduce 作业所需要的系统资源,尤其是集群内部网络资源的使用情况。这是MapReduce 框架在设计上的取舍,…

huffman树和huffman编码

不知道为什么&#xff0c;我写的代码都是又臭又长。 直接上代码&#xff1a; #include <iostream> #include <cstdarg> using namespace std; class Node{ public:int weight;int parent, lChildren, rChildren;Node(int weight, int parent, int lChildren, int …

react 监听组合键_投资组合中需要的5个React项目

react 监听组合键Youve put in the work and now you have a solid understanding of the React library.您已经完成工作&#xff0c;现在对React库有了扎实的了解。 On top of that, you have a good grasp of JavaScript and are putting its most helpful features to use …

Unity 单元测试(PLUnitTest工具)

代码测试的由来 上几个星期上面分配给我一个装备系统,我经过了几个星期的战斗写完90%的代码. 后来策划告诉我需求有一定的改动,我就随着策划的意思修改了代码. 但是测试(Xu)告诉我装备系统很多功能都用不上了. Xu: 我有300多项测试用例,现在有很多项都无法运行了. 你修改了部分…

Best Time to Buy and Sell Stock II

题目&#xff1a; Say you have an array for which the i th element is the price of a given stock on day i. Design an algorithm to find the maximum profit. You may complete as many transactions as you like (ie, buy one and sell one share of the stock multipl…

求给定集合的幂集

数据结构中说这个问题可以用类似8皇后的状态树解法。 把求解过程看成是一棵二叉树&#xff0c;空集作为root&#xff0c;然后遍历给定集合中的元素&#xff0c;左子树表示取该元素&#xff0c;右子树表示舍该元素。 然后&#xff0c;root的左右元素分别重复上述过程。 就形成…

angular 命令行项目_Angular命令行界面介绍

angular 命令行项目Angular is closely associated with its command-line interface (CLI). The CLI streamlines generation of the Angular file system. It deals with most of the configuration behind the scenes so developers can start coding. The CLI also has a l…

oracle-imp导入小错filesize设置

***********************************************声明*********************************************************************** 原创作品&#xff0c;出自 “深蓝的blog” 博客。欢迎转载&#xff0c;转载时请务必注明出处。否则追究版权法律责任。表述有错误之处&#xf…

CentOS 7 下用 firewall-cmd / iptables 实现 NAT 转发供内网服务器联网

自从用 HAProxy 对服务器做了负载均衡以后&#xff0c;感觉后端服务器真的没必要再配置并占用公网IP资源。 而且由于托管服务器的公网 IP 资源是固定的&#xff0c;想上 Keepalived 的话&#xff0c;需要挤出来 3 个公网 IP 使用&#xff0c;所以更加坚定了让负载均衡后端服务器…

八皇后的一个回溯递归解法

解法来自严蔚敏的数据结构与算法。 代码如下&#xff1a; #include <iostream> using namespace std; const int N 8;//皇后数 int count 0;//解法统计 int a[N][N];//储存值的数组const char *YES "■"; const char *NO "□"; //const char *Y…

即时编译和提前编译_即时编译说明

即时编译和提前编译Just-in-time compilation is a method for improving the performance of interpreted programs. During execution the program may be compiled into native code to improve its performance. It is also known as dynamic compilation.即时编译是一种提…

bzoj 2588 Spoj 10628. Count on a tree (可持久化线段树)

Spoj 10628. Count on a tree Time Limit: 12 Sec Memory Limit: 128 MBSubmit: 7669 Solved: 1894[Submit][Status][Discuss]Description 给定一棵N个节点的树&#xff0c;每个点有一个权值&#xff0c;对于M个询问(u,v,k)&#xff0c;你需要回答u xor lastans和v这两个节点…

.Net SqlDbHelper

using System.Configuration; using System.Data.SqlClient; using System.Data;namespace ExamDAL {class SqHelper{#region 属性区// 连接字符串private static string strConn;public static string StrConn{get{return ConfigurationManager.ConnectionStrings["Exam&…

C语言的一个之前没有见过的特性

代码&#xff1a; #include <stdio.h>int test(){int a ({int aa 0;int bb 1;int cc 2;if(aa 0 && bb 1)printf("aa %d, bb %d\n", aa, bb);//return -2;cc;});return a; }int main(){typeof(4) a test();printf("a %d\n", a); } …

如何构建顶部导航条_如何构建导航栏

如何构建顶部导航条导航栏 (Navigation Bars) Navigation bars are a very important element to any website. They provide the main method of navigation by providing a main list of links to a user. There are many methods to creating a navigation bar. The easiest…

SPSS聚类分析:K均值聚类分析

SPSS聚类分析&#xff1a;K均值聚类分析 一、概念&#xff1a;&#xff08;分析-分类-K均值聚类&#xff09; 1、此过程使用可以处理大量个案的算法&#xff0c;根据选定的特征尝试对相对均一的个案组进行标识。不过&#xff0c;该算法要求您指定聚类的个数。如果知道&#xff…

[ 总结 ] nginx 负载均衡 及 缓存

操作系统&#xff1a;centos6.4 x64 前端使用nginx做反向代理&#xff0c;后端服务器为&#xff1a;apache php mysql 1. nginx负载均衡。 nginx编译安装&#xff08;编译安装前面的文章已经写过&#xff09;、apache php mysql 直接使用yum安装。 nginx端口&#xff1a;80…

中国剩余定理(孙子定理)的证明和c++求解

《孙子算经》里面的"物不知数"说的是这样的一个题目&#xff1a;一堆东西不知道具体数目&#xff0c;3个一数剩2个&#xff0c;5个一数剩3个&#xff0c;7个一数剩2个&#xff0c;问一共有多少个。 书里面给了计算过程及答案&#xff1a;70*2 21*3 15*2 -105*2 2…

osi七层网络层_OSI层速成课程

osi七层网络层介绍 (Introduction) Have you ever wondered how data is sent through the network from one machine to another? If yes, then the Open System Interconnected model is what you are looking for.您是否曾经想过如何通过网络将数据从一台机器发送到另一台机…

KMP算法求回溯数组的步骤

KMP算法到底是什么原理就不说了&#xff0c;各种资料上讲的明明白白&#xff0c;下面我就如何用代码来实现做一下说明和记录. KMP的核心思想就是&#xff0c;主串不回溯&#xff0c;只模式串回溯。而模式串匹配到第几位时失配&#xff0c;要回溯多少&#xff0c;由模式串本身来…

【.Net】vs2017 自带发布工具 ClickOnce发布包遇到的问题

一、遇到的问题 在安装了vs2017 社区版&#xff08;Community&#xff09;之后 想打包安装程序&#xff08;winform&#xff09; 还是想用之前的 installshield来打包 发现居然打不了&#xff0c;在官网查了 installshield不支持社区版&#xff08;Community&#xff09;&…

windows平台,开发环境变量配置

1.打开我的电脑--属性--高级--环境变量 JAVA配置环境变量变量名&#xff1a;JAVA_HOME 变量值&#xff1a;C:\Program Files\Java\jdk1.7.0 变量名&#xff1a;CLASSPATH 变量值&#xff1a;.;%JAVA_HOME%\lib\dt.jar;%JAVA_HOME%\lib\tools.jar; 变量名&#xff1a;Path 变…