Python学习笔记

1.变量类型

    x=5    int
x="ss" string
x='a' string
x=True bool
#查看变量类型
type(x)

2.字符串常用操作

a.首字母大写

 x="add user"
print(x.title()
# Add User

b.全部大写/小写

 print(x.upper())
print(x.lower())
# ADD USER
# add user

c.字符串中使用变量

 full=f"Function: {x}"
# Function: add user

d.制表符,换行符

    print("python")
print("\tpython")
print("python\njava")

e.删除空白

 x=x.rstrip()

3.数据

a.基本运算符

 + - * /
a**b 表示乘方 ==pow(a,b)

b.浮点数

要注意,浮点数运算包含的小数位是不确定的

 0.1 + 0.2 = 0.30000000000000004 != 0.3

c.变量名称全大写表示常量

 MAX=200

4.列表

列表很奇特,更像是一个集合,可以包含所有类型

 shops=["egg","bike",20,0.22,True]
print(shops)
# ['egg', 'bike', 20, 0.22, True]

a.访问列表元素

 从前面开始访问 "bike"
print(shops[1])
从后面开始访问 "bike"
print(shops[-4])

b.修改列表元素

 shops[1]="tomato"

c.添加列表元素

 在列表末尾插入
shops.append("ice")
在列表中间插入
shops.insert(1,"baseketball")
# ['egg', 'bike', 20, 0.22, True]
# ['egg', 'baseketball', 'bike', 20, 0.22, True]

d.删除列表元素

 根据元素的“序列号”删除元素
del shops[0]
shops.pop(0) 删除列表末尾的元素,同时可将该值赋予其他变量
shops.pop()
end=shops.pop() 根据元素值删除元素,但只会删除第一个,后面不会删除
shops.remove("bike")

5.组织列表

a.排序

 永久排序
shops.sort() #正向
shops.sort(reverse=True) #反向 暂时输出排序
print(sorted(shops)) 反转列表,最后一个变为第一个
shops.reverse()

b.获取列表长度

 len(shops)

6.操作列表

a.遍历列表

 for shopping in shops:
print(shopping)

b.range()函数

 for value in range(1,6):
print(value) 使用range()生成列表
numbers=list(range(1,6)) range(x,y(,z)
x为起始数值
y为末尾数值
z为步长

c.数字列表

 max(num)
min(num)
sum(num)

d.列表解析

 super=[value **2 for value in range(1,8)]

e.切片:列表的部分元素

    print(super[0:3])    #输出 0->2
print(super[:3]) #输出 1->3
print(super[3:]) #输出 3->len(super)
print(super[-3:]) #输出 len(super)-3 ->len(super) #遍历切片
for su in super[:3]
print(su)
#复制切片
superCopy=super[:] #同时省略起始索引和终止索引

f:元组:不可变的列表,使用()定义

 #定义一个元组
enemy=("tai","mi")
print(enemy[0])
#遍历元组
for en in enemy:
print(en)
#修改元组元素,不能单个修改,只能重新定义
enemy=[100,200]
enemy=[400,600]
#相对于列表,元组是更简单的数据结构。

7.if语句

a.if的使用

 if 1==9:
print(True)
else:
print(False) python区分大小写 if 1 != 9
print(TRUE) #使用and,or进行多重判断
car=["ss","asd"]
print("ss" in car)
print("ss" in car)
print("ss" not in car) #if-elif-else
if age<4:
print("down 4")
elif age <18:
print("down 18")
else
print("up 18") #python不强制要求if-elif最后必须要else

b.列表操作

 #判断列表是否为空
car=["ss","sddd"]
if car:
print("not null")
else:
print("null")

8.字典

可以理解为QMap<QString,auto>

a.基础操作

 #创建字典
alien={
'color':'green',
'point':5
}
print(alien)
#使用字典元素
1.直接访问,存在错误风险
print(alien['color'])
print(alien['point'])
2.使用get()访问
print(alien.get('point','There is no clas point'))
#添加字典元素
alien['x']=4
alien['y']=44
#修改字典元素值
alien['x']=77
#删除字典元素
del alien['x']
#遍历字典所有键值对
for key,value in alien.items():
print(key+": "+value)
#遍历字典的键
for name in alien.keys():
print(name)

b.嵌套

 alien_1={'color':'green','point':1}
alien_2={'color':'blue','point':2}
alien_3={'color':'red','point':10}
aliens={alien_1,alien_2,alien_3}

c.字典存储列表

 pizza={
'price':5,
'food':['chicken','coca']
}
#访问元素
print(pizza['foods'][0])
for food in pizza['foods']:
print(food)

d.字典存储字典

 #可以用来存储用户名,以每个用户独有名称作为标识符,但不现实

9.用户输入和while循环

a.input()

 age=input("Tell me your age")
#都已字符串形式输入

b.while

    nums=1
while nums < 5:
print(nums)
nums += 1
# break 跳出当前循环
# continue 结束当前循环,进行下一轮循环 #读取列表
users=['a','s','d0','qw']
while users:
print(users.pop())
#删除特定元素
while 'cat' in zoos:
zoos.remove('cat')

10.函数

Python学习笔记.md的更多相关文章

  1. [Python学习笔记]文件的读取写入

    文件与文件路径 路径合成 os.path.join() 在Windows上,路径中以倒斜杠作为文件夹之间的分隔符,Linux或OS X中则是正斜杠.如果想要程序正确运行于所有操作系统上,就必须要处理这 ...

  2. python学习笔记整理——字典

    python学习笔记整理 数据结构--字典 无序的 {键:值} 对集合 用于查询的方法 len(d) Return the number of items in the dictionary d. 返 ...

  3. VS2013中Python学习笔记[Django Web的第一个网页]

    前言 前面我简单介绍了Python的Hello World.看到有人问我搞搞Python的Web,一时兴起,就来试试看. 第一篇 VS2013中Python学习笔记[环境搭建] 简单介绍Python环 ...

  4. python学习笔记之module && package

    个人总结: import module,module就是文件名,导入那个python文件 import package,package就是一个文件夹,导入的文件夹下有一个__init__.py的文件, ...

  5. python学习笔记(六)文件夹遍历,异常处理

    python学习笔记(六) 文件夹遍历 1.递归遍历 import os allfile = [] def dirList(path): filelist = os.listdir(path) for ...

  6. python学习笔记--Django入门四 管理站点--二

    接上一节  python学习笔记--Django入门四 管理站点 设置字段可选 编辑Book模块在email字段上加上blank=True,指定email字段为可选,代码如下: class Autho ...

  7. python学习笔记--Django入门0 安装dangjo

    经过这几天的折腾,经历了Django的各种报错,翻译的内容虽然不错,但是与实际的版本有差别,会出现各种奇葩的错误.现在终于找到了解决方法:查看英文原版内容:http://djangobook.com/ ...

  8. python学习笔记(一)元组,序列,字典

    python学习笔记(一)元组,序列,字典

  9. Pythoner | 你像从前一样的Python学习笔记

    Pythoner | 你像从前一样的Python学习笔记 Pythoner

随机推荐

  1. JDBC 使用详解

    1.JDBC 编程步骤: 加载驱动程序; Class.forName(driverClass) 加载Mysql驱动:Class.forName("com.mysql.jdbc.Driver& ...

  2. 『现学现忘』Docker基础 — 27、Docker镜像的commit操作

    目录 1.commit命令作用 2.commit命令说明 3.示例演示 1.commit命令作用 在运行的容器中,并在镜像的基础上做了一些修改,我们希望保存起来,封装成一个新的镜像,方便我们以后使用, ...

  3. RandomStringUtils 生成随机字符串

    代码: System.out.println(RandomStringUtils.randomAlphanumeric(32));System.out.println(RandomStringUtil ...

  4. 4月4日 python学习总结 os pickle logging

    1.序列化和反序列化 我们把对象(变量)从内存中变成可存储或传输的过程称之为序列化,在Python中叫pickling. 反过来,把变量内容从序列化的对象重新读到内存里称之为反序列化,即unpickl ...

  5. Mod_Security 安装配置

    Mod_Security 安装配置 OS平台:Ubuntu 16 服务器:Apache2 一.更新Ubuntu apt-get更新源 sudo mv /etc/apt/sources.list /et ...

  6. 面试官:Zookeeper是什么,它有什么特性与使用场景?

    哈喽!大家好,我是小奇,一位不靠谱的程序员 小奇打算以轻松幽默的对话方式来分享一些技术,如果你觉得通过小奇的文章学到了东西,那就给小奇一个赞吧 文章持续更新 一.前言 作为一名Java程序员,Zook ...

  7. Spring Cloud与Spring Boot版本匹之间的关系

    由于学习的起步较晚,创建项目的时候一直采用的都是较新的springboot,用的2.0.2.RELEASE版本.参照网上的示例进行实验的时候,有时候会才坑,特记录一二以备忘 首先就是SpringBoo ...

  8. Mapper 编写有哪几种方式?

    第一种:接口实现类继承 SqlSessionDaoSupport:使用此种方法需要编写 mapper 接口,mapper 接口实现类.mapper.xml 文件. 1.在 sqlMapConfig.x ...

  9. java-关于getResourceAsStream

    1111class.getClassLoader().getResourceAsStream InputStream ips = testResource.class.getClassLoader() ...

  10. Java 中,Serializable 与 Externalizable 的区别?

    Serializable 接口是一个序列化 Java 类的接口,以便于它们可以在网络上传输 或者可以将它们的状态保存在磁盘上,是 JVM 内嵌的默认序列化方式,成本高. 脆弱而且不安全.Externa ...