Python学习之旅 —— 基础篇(二)数据类型、运算、while循环
>>> MYSQL_CONNECTION = '10.x.x.x.x’ # 所有字母大写
>>> a = 100 >>> b = 100.1 >>> type(a) <class 'int'> >>> type(b) <class 'float'> >>>
>>> 0 == False True >>> 1 == True True >>>
# 定义字符串 msg = 'Pesen is a good man' print(msg)# 查看字符串长度 len_of_str = len(msg) print(len_of_str)# 字符类型 print(type(msg))# strip() 移除空白 msg = msg.strip() print(msg)# 分割 split() & 合并 join() str1 = 'id,Name,ps' list1 = str1.split(',') # 字符串,逗号分隔,输出结果是一个列表 print(list1) str2 = '|'.join(list1) # 用'|' 重新合并字符串 print(str2)# 判断字符串有没有空格 print('' in msg)# 另外一种形式的变量赋值 _msg = 'My name is {name},{age} years old.' msg = _msg.format(name = 'Pesen', age = 18) print(msg)_msg = 'My name is {0},{1} years old.' msg = _msg.format('Pesen', 19) print(msg)# 字符串切片 msg = 'Pesen is a good man' print(msg[4:10:2]) # 输出第4/6/8个字符,最后面的2表示步长,就是隔多少取一个字符# 输出的格式填充 print(msg.center(40, '+'))# 判断字符串中有没有某些内容 if msg.find('man'):print(msg)# isdigit表示判断age的值是不是数字类型: age = '18' if age.isdigit():print('True') else:print('False') # 规定用户必须输入数字类型时,可使用isdigit age = input('年龄: ') if age.isdigit():age = int(age) else:print('输入有误,必须是数字,请重新输入.')# 判断是否有特殊字符 name = 'Pesen@Xie' print(name.isalnum()) # 有特殊字符返回False.# startswith endswith 判断是以什么字符或者字符组开头 msg = 'Pesen is a good man.' print(msg.startswith('Pe')) # 匹配到返回True print(msg.endswith('man')) # 匹配不到返回False# 字符转成全大写或者全小写 msg = 'Pesen is a good man.' print(msg.upper()) # 转全大写 print(msg.lower()) # 转全小写
# 创建一个列表,把逗号分隔的不同的数据项使用方括号括起来即可 list1 = ['Pesen', 18, [1,2,3,4,5]] # 可以存储任意类型的数据 print(list1)# 获取列表中的值 list1 = ['Pesen', 18, [1,2,3,4,5]] print(list1[0]) print(list1[2][2:5:2]) # 与字符串一样,列表也支持切片和步长# 更新列表中的元素 list1 = ['Pesen', 18, [1,2,3,4,5]] list1[1] = 20 print(list1)# 删除列表元素,使用del list1 = ['Pesen', 18, [1,2,3,4,5]] del list1[2][3] # 删除小列表中的4 print(list1)# 列表操作的方法 # list.append(obj):在列表末尾添加新的对象 list1 = ['Pesen', 18, [1,2,3,4,5]] dict1 = {'name': 'Pesen', 'age': 20, 'job': 'engineer'} list1.append(dict1) # 把字典dict1作为元素追加到list1中 print(list1)# list.count(obj):统计某个元素在列表中出现的次数 list1 = [123, 54, 456, 23, 7, 35, 45, 34, 9, 'lalalal',9, 234, 74356, 234, 67, 124, 5, 23, 9, 34, 9, 344, 223, 34, 9] num_of_ele = list1.count(9) # 统计9在列表中的个数 print(num_of_ele)# list.extend(seq):在列表末尾一次性追加另一个序列中的多个值(用新列表扩展原来的列表) list1 = [1,2,3] list2 = [4,5,6] list1.extend(list2) # 把列表2中的元素都追加到list1的末尾 print(list1)# list.index(obj):从列表中找出某个值第一个匹配项的索引位置 list1 = [123, 54, 456, 23, 7, 35, 45, 34, 9, 'lalalal',9, 234, 74356, 234, 67, 124, 5, 23, 9, 34, 9, 344, 223, 34, 9] position = list1.index(9) # 第一个9元素的下标 print(position)# 修改所有9元素为999 for i in range(list1.count(9)):position_of_ele = list1.index(9)list1[position_of_ele] = 999 print(list1)# 删除所有34元素 for x in range(list1.count(34)):del list1[list1.index(34)] # 等同于list1.remove(34)print(list1)# list.insert(index, obj) 给列表插入对象 list1 = [1,2,3,4,5,6] list1.insert(2,'Pesen') # 把字符串Pesen插入到第三个位置 print(list1)# list.pop(obj) 移除列表中的一个元素(默认最后一个元素) list1 = [1,2,3,4,5,6] list1.pop() # 删除元素6 list1.pop(2) # 删除第二个位置的元素,3 print(list1)# list.reverse():反转列表中元素 list1 = [1,2,3,4,5,6] list1.reverse() print(list1)# list.sort(obj):对原列表进行排序 list1 = [7,2,3,14,6,6,2,76,88,12,543,76,85] list1.sort() # python3无法对多种类型的数据排序 print(list1)# list.copy copy一个列表 浅copy list1 = ['Pesen', 18, [1,2,3,4,5]] #list2 = list1.copy() import copy list2 = copy.deepcopy(list1) # 用copy模块的deepcopy() 实现深层次的copy list1[1] = 20 # 更新list1中的元素18为20,list2中的元素18未改变 list1[2][0] = 10 # 更新list1中的小列表里的元素1为10,两个列表中都变化了, list.copy() 是浅copy print(list1,list2)# 操作列表的函数 list1 = [1,2,3,4,5,6] print(len(list1)) # 打印列表的长度 print(max(list1)) # 打印列表中最大的值 print(min(list1)) # 打印列表中最小的值# 元组转换成列表 tuple1 = (0,1,2,3) # 定义个一个元组 list1 = list(tuple1) print(list1)
# 创建字典 dict = {0: {'name': 'Pesen','age': 18},1: {'name': 'Jack','age': 19} }# 查询 print(dict[0])# 修改 dict[0]['age'] = 20 print(dict)# 新增 dict[0]['job'] = 'IT' print(dict)# 删除 dict[0].pop('job') print(dict)# 获取key对应的value print(dict[0].get('name'))# update,key不重复添加,key重复把新的value赋给原key dict2 = {1: 'BALABALA',2: 'lalala',3: 'hehe' }dict.update(dict2) print(dict)# items 转换成列表 数据量大时不要这么做,太消耗内存资源 print(dict.items())# keys 获取所有的key print(dict.keys())# value 获取key对应的value print(dict.values())# 判断某个数据是否在字典的key中 print(3 in dict)# setdefault 取一个key的value,如果不存在,就添加 print(dict.setdefault(3,'haha'))# fromkeys dict3 = {} print(dict3.fromkeys([5,6,7,8], 'gagaga')) # 列表中的元素作为key,gagaga作为value# 循环取值 for key in dict2: #高效率的循环方式print(key,dict2[key])for key,value in dict2.items(): # 先把字典转换成列表,再取值,效率低,耗资源,不推荐print(key,value)
count = 0 while True: # 格式 while + 循环条件,成立时才循环 ,本例子中是死循环,少写为妙.count += 1if count >= 50 and count <= 60: # 满足这个条件是跳出本次循环 continuecontinueif count == 100: # 满足这个条件,用break跳出整个循环print("Byebye")breakprint("Loop: ", count)
转载于:https://www.cnblogs.com/pesen/p/5496267.html
Python学习之旅 —— 基础篇(二)数据类型、运算、while循环相关推荐
- Python学习笔记_1_基础_2:数据运算、bytes数据类型、.pyc文件(什么鬼)
Python学习笔记_1_基础_2:数据运算.bytes数据类型..pyc文件(什么鬼) 一.数据运算 Python数据运算感觉和C++,Java没有太大的差异,百度一大堆,这里就不想写了.比较有意思 ...
- Python学习日记-day1基础篇 字符 输出 注释
Python学习日记-day1基础篇 字符 输出 注释 by北栀一刺 # -*- coding: utf-8 -*- """ Spyder EditorThis is a ...
- python进阶记录之基础篇二十六_Python进阶记录之基础篇(十六)
回顾 在Python进阶记录之基础篇(十五)中,我们介绍了面向对象的基本概念以及Python中类和对象的基础知识,需要重点掌握类的创建和对象的使用.今天我们继续讲一下Python中面向对象的相关知识点 ...
- python学习之路基础篇(第四篇)
一.课程内容回顾 1.python基础 2.基本数据类型 (str|list|dict|tuple) 3.将字符串"老男人"转换成utf-8 s = "老男人" ...
- python学习历程之----基础篇(五)
去年今日此门中,人面桃花相映红. 人面不知何处去,桃花依旧笑春风. 老铁们,又到见面时间了,不知不觉,Python编程的基础篇已经四篇了,今天是第五篇,根据章节安排,最多再有三次基础文章,我们就会进入 ...
- Python学习笔记(基础知识点二)开更了~
下午有事忙活了几个小时现在开始开更啦. 首先想说那个一直困惑我的问题得以解决了.是什么呢??? 八进制是以数字0开头,也就是12=015(后面是八进制):十六进制是以数字0加上字母x(大写小写都可以, ...
- python学习之路基础篇(第八篇)
一.作业(对象的封装) 要点分析 1.封装,对象中嵌套对象 2.pickle,load,切记,一定要先导入相关的类二.上节内容回顾和补充 面向对象基本知识: 1.类和对象的关系 2.三大特性: 封装 ...
- Python学习之路基础篇--10Python基础,函数进阶
1 命名空间 对于Python 来说命名空间一共有三种 1 内置命名空间 -- Python 解释器 就是Python 解释器一启动就可以使用的名字,储存在内置命名空间中.内置的名字在启动解释器的时候 ...
- 提权学习之旅——基础篇
首发于先知社区 https://xz.aliyun.com/t/8054 前言: 无论是CTF赛题还是渗透测试,有很多时候拿到WebShell的权限并不高,没有办法继续深入,所以需要进行提权操作,方便 ...
最新文章
- Matlab与随机变量和样本的数字特征
- 图算法在斗鱼反作弊中的实践
- redis持久化(2)
- Access数据库访问助手类
- Entity Framework Core 实现MySQL 的TimeStamp/RowVersion 并发控制
- 30万条数据,搜索文本字段的各种方式对比
- android 6.0获取蓝牙权限
- 给我一个小碗,还你一个奇迹——结构工程师教你吃垮必胜客(有图,附论文)【ZT】...
- 【Elasticsearch】 es 排查问题 explain 使用 内容解释
- Protable 自定义添加搜索栏和toolbar
- ubuntu 14.04 编译yocto源码--环境配置篇
- 西柚SWPU新生赛(场外同步赛)miscAK 别的在混(
- Unity 自定义扩展Hierachy右键菜单
- Linux安装NVIDIA显卡驱动的正确姿势
- React Native - 使用图片选择器react-native-image-picker拍照、选照片
- 0基础学3dmax建模难吗?
- POJ 3264 线段树
- python中文词典构建_python-构建英语学习词典
- 王浩:哥德尔思想概说
- Java sql 计算时间差
热门文章
- 网站SEO中内页标签该如何进行优化?
- 网站建设前的准备工作你了解多少?
- java sessionstate_在Java Web开发中自定义Session
- vnr光学识别怎么打开_水晶光电:光学元件需求向好,光学创新打开成长新空间,未来可期...
- 清华大学计算机李雪,李雪 | 北京外国语大学国际商学院|International Business School,BFSU|本硕博,北外留学,来华留学Solbridge,EDP...
- python3字典升序排序_Python字典和元组总结
- ssl charles 参数看不到_SSL证书=安全?小心,别错漏了TA……
- 不同的二叉搜索树-战胜100%的Java用户
- leetcode 412. Fizz Buzz
- docker run Influxdb