python初学day01
1.执行Python脚本时打印的字符有颜色
- 1. print "\033[32;1mhello\033[0m" #打印绿色
- 2. print "\033[31;1mhello\033[0m" #打印红色
2.字符串常用方法
- 【strip,脱掉空格】
- username = input("user:")
- if username.strip() == 'lance':
- print("welcome.")
- 【split,字符串拼接】
- name='xia yi feng'
- name2=name.split(" ")
- print(name2)
- name='xia,yi,feng'
- name2=name.split(",")
- print("|".join(name2))
- 【判断是否有空格:】
- name="Lance Tang"
- print(' ' in name)
- 【format,格式化输出】
- msg="hello:{name} the is age:{age}"
- msg2=msg.format(name='lance',age=23)
- print(msg2)
- msg="hello:{0} the is age:{1}"
- msg2=msg.format('lance',23)
- print(msg2)
- 【center,给字符串居中增加指定字符:】
- name='lance tang'
- print(name.center(30,'-'))
- 【find,查找指定字符所在的下标位置,find找不到时返回“-1”;index找不到时会“报错”】
- name='lance tang'
- print(name.find('e'))
- print(name.index(''))
- 【isdigit (判断是否为数字):】
- age=input("you num:")
- if age.isdigit():
- age = int(age)
- print(age)
- else:
- print("Iivalid data type.")
- 【判读是否只是字母】
- name = 'lance123'
- result = name.isalpha()
- print(result)
- 【 检测是否为空】
- name = 'lance'
- result = name.isspace()
- print(result)
- 【sialnum (判断是否为数字或字母,或数字加字母的类型):】
- name="123tang"
- print(name.isalnum())
- 【endswith && startswith (判断是否为1开始3结束)】
- name=""
- print(name.endswith(''))
- print(name.startswith(''))
- 【endswith ,在 0,4 这个范围内查找是不是以 y 结尾】
- 【查找范围规则:大于等于0 and 小于3】
- name = 'lance'
- result = name.endswith('y',0,4)
- print(result)
- 【upper && lower (字符,大小写转换)】
- name="tang123"
- name2="lance"
- print(name.upper())
- print(name.lower())
- 【capitalize,把首字母转换成大写】
- name = 'lance hahahahah'
- result = name.capitalize()
- print(result)
- 【casefold,把首字母的大写转换成小写】
- name= 'lance'
- result = name.casefold()
- print(result)
- 【count,查找d出现的次数】
- name = 'lance'
- result = name.count('d')
- print(result)
- 【count,在0~4范围内,查找d出现的次数】
- name = 'lance'
- result = name.count('d',0,4)
- print(result)
- 【encode,把“妖风”转换成gbk编码】
- name = '妖风'
- result = name.encode('gbk')
- print(result)
- 【partition,以“is”为标志位,把字符串分割】
- name = 'lanceissb'
- result = name.partition('is')
- print(result)
- 【把s替换成8; "":指定你想替换几个,并不是替换的范围】
- name = 'lanceissb'
- result = name.replace('s','')
- #result = name.replace('s','8',3)
- print(result)
- 【去掉换行符,以列表形式打印出来】
- name = '''
- aa
- bb
- cc
- '''
- result = name.splitlines()
- print(result)
- 【strip:指定去掉的字符,和splitlines功能相同;split:去掉分割符并进行拼接】
- name = '''
- aa
- bb
- cc
- '''
- result = name.split("\n")
- print(result)
- 【删除列表最后一个值】
- name = ['lance','tang',123]
- print ("list is :",name.pop())
- 【删除列表制定值】
- name = ['lance','tang',123,456]
- name.remove(123)
- print ("list :", name)
- 【左对齐并制定字符填充】
- str = "what do you want?"
- print(str.ljust(50,'*'))
- name.strip() 【去除两边的空格】
- name.lstrip() 【去除左边的空格】
- name.rstrip() 【去除右边的空格】
3.列表操作:
- 【列表赋值:】
- >>> name=["Lance",'tang','','']
- 【取tang字符,取的是下标,下标从0开始:】
- >>> name[1]
- 'tang'
- 【取88数字:】
- >>> name[-1]
- ''
- 【顾首不顾尾:】
- >>> name=["lance",'tang','','']
- >>> name[0:2]
- ['lance', 'tang']
- 【取后三个值,取几个就输入 - 几:】
- >>> name[-3:]
- ['tang', '', '']
- 【多次取列表里面的数据:】
- >>> name=["lance",'tang','','',234,234,324,32,4]
- >>> name[-6:]
- ['', 234, 234, 324, 32, 4]
- >>> name[-6:][2:4]
- [234, 324]
- >>> name[-6:][2:4][0]
- 234
- 【给列表中234,改成lancelot:】
- >>> name=["lance",'tang','','',234,234,324,32,4]
- >>> name[4]='lancelot'
- 结果:
- >>> name
- ['lance', 'tang', '', '', 'lancelot', 234, 324, 32, 4]
- 【insert,给列表插入值[在”tang“后面插入"xia"]】
- >>> name
- ['lance', 'tang', '', '', 'lancelot', 234, 324, 32, 4]
- >>> name.insert(2,'xia')
- >>> name
- ['lance', 'tang', 'xia', '', '', 'lancelot', 234, 324, 32, 4]
- 【append,在列表末尾追加一个值:】
- ['lance', 'tang', 'xia', '', '', 'lancelot', 234, 324, 32, 4]
- >>> name.append('tail')
- >>> name
- ['lance', 'tang', 'xia', '', '', 'lancelot', 234, 324, 32, 4, 'tail']
- 【remove,删除列表中指定的值:】
- >>> name
- ['lance', 'tang', 'xia', '', '', 'lancelot', 234, 324, 32, 4, 'tail']
- >>> name.remove("lance")
- >>> name
- ['tang', 'xia', '', '', 'lancelot', 234, 324, 32, 4, 'tail']
- 【删除下标5~9,也是顾首不顾尾】
- >>> name
- ['cisco', 'xia', 'lancelot', 'feng', '', 234, 54, 234, 123, 523]
- >>> del name[5:9]
- >>> name
- ['cisco', 'xia', 'lancelot', 'feng', '', 523]
- >>>
- 【pop,删除列表中的值,是根据下标来删除的:】
- name=['cisco', 'xia', 'lancelot', 'feng',23, 523,23,43,23,435]
- name.pop(0)
- print(name)
- 【以布长为2,打印列表:】
- >>> name
- ['cisco', 'xia', 'lancelot', 'feng', '', 523]
- >>> name[::2]
- ['cisco', 'lancelot', '']
- >>>
- 【count,统计列表中23元素的个数(注意:如果列表中,23被‘单引号’括起来,就不能被count所计算):】
- name=['cisco', 'xia', 'lancelot', 'feng', 23, 523,23,43,23,435]
- if 23 in name:
- list_count=name.count(23)
- print("---%s--- 23 is/are in name" %list_count)
- 【把name列表中的所有23元素,全部替换成99999999:】
- name=['cisco', 'xia', 'lancelot', 'feng', 23, 523,23,43,23,435]
- for i in range(name.count(23)):
- list_index=name.index(23)
- name[list_index]=9999999999
- print(name)
- 【extend,扩展列表:】
- name=['cisco', 'xia', 'lancelot', 'feng', 23, 523,23,43,23,435]
- name2=[1,2,3,4,5]
- name.extend(name2)
- print(name)
- 【reverse,反转显示列表内容:】
- name=['cisco', 'xia', 'lancelot', 'feng', 23, 523,23,43,23,435]
- name.reverse()
- print(name)
- 【sort,列表排序(注意:字符串和数字不能混排):】
- name=['cisco', 'xia', 'lancelot', 'feng']
- name.sort()
- print(name)
- name=[ 23, 523,23,43,23,435]
- name.sort()
- print(name)
- 【copy列表:】
- name=['cisco', 'xia', 'lancelot', 'feng',23, 523,23,43,23,435]
- name1=name.copy()
- name[0]='TANG'
- print(name)
- print(name1)
- 【浅copy,二级列表的内容不会被重新copy:】
- name=['tang', 'xia', 'lancelot', 'feng',[23, 523,23],43,23,435]
- name1=name.copy()
- name[0]='TANG'
- name[4][1]=11111111
- name1[4][2]=22222222
- print(name)
- print(name1)
- 【deepcopy,深浅copy】
- import copy
- name=['tang', 'xia', 'lancelot', 'feng',[23, 523,23],43,23,435]
- #浅copy,不会copy二级列表里面的元素;
- name1=name.copy()
- # name1=copy.copy(name)
- #深copy,不管有几级,都会copy过去;
- name2=copy.deepcopy(name)
- #和原文件不一样;
- name[0]='TANG'
- name[4][1]=11111111
- name1[4][2]=22222222
- name2[4][0]=33333333
- print(name)
- print(name1)
- print(name2)
- 结果:
- ['TANG', 'xia', 'lancelot', 'feng', [23, 11111111, 22222222], 43, 23, 435]
- ['tang', 'xia', 'lancelot', 'feng', [23, 11111111, 22222222], 43, 23, 435]
- ['tang', 'xia', 'lancelot', 'feng', [33333333, 523, 23], 43, 23, 435]
- 【【删除一个列表中所有lance】】
- >>> name = ['lance','aaa','ccc','lance','ddd']
- >>> for i in range(name.count('lance')):
- ... print(name.remove('lance'))
- ...
- None
- None
- >>> name
- ['aaa', 'ccc', 'ddd']
- >>>
- 【列表中匹配值的方法】
- >>> name
- ['', '', 'aa', 2, 8]
- >>> "aa" in name
- True
- >>> 2 in name
- True
- >>> if "aa" in name:
- ... print("aaa")
- ...
- aaa
- >>>
- 【判读列表中有没有haha这个值】
- name = ['lance','aaa','ccc','lance','ddd']
- if 'haha' not in name:
- print("name list not haha")
- 【判读“t”是不是一个列表】
- >>> type(t) is list
- True
4.隐式密码输入
- 在输入的密码是使用隐式输入,在pycharm中不好使:
- 【getpass.getpass,】
- import getpass
- username = input("username:")
- password = getpass.getpass("password:")
- print(username,password)
5.python格式化输出:
- %d 整数
- %f 浮点数
- %s 字符串
- %x 十六进制整数
- name = input("you input name:")
- age = int(input("you input age:"))
- job = input("you input job:")
- msg = """
- Infomation is %s
- ------------
- name = %s
- age = %d
- job = %s
- ----END-----
- """ %(name,name,age,job)
- print(msg)
6.模块
- 【OS模块】
#!/usr/bin/env python- # -*- coding: utf-8 -*-
- import os
- os.system("df -h") #调用系统命令
- 【sys模块】
#!/usr/bin/env python- # -*- coding: utf-8 -*-
- import sys
- print(sys.argv)
- #输出
- $ python test.py helo world
- ['test.py', 'helo', 'world'] #把执行脚本时传递的参数获取到了
- 【time 模块的使用】
%y 两位数的年份表示(00-99)- %Y 四位数的年份表示(000-9999)
- %m 月份(01-12)
- %d 月内中的一天(0-31)
- %H 24小时制小时数(0-23)
- %I 12小时制小时数(01-12)
- %M 分钟数(00=59)
- %S 秒(00-59)
- a = time.time() #打印当前时间的时间戳
- print a
- b = time.localtime() #打印元组形式的当前时间
- print b
- c = time.strftime("%Y-%m-%d %H:%M:%S",c) #把元组形式的时间转化为格式化的时间
- print c
- 【Tab模块】for Linux(居家旅行,常备神奇!!!)
- #!/usr/bin/env python
- # python startup file
- import sys
- import readline
- import rlcompleter
- import atexit
- import os
- # tab completion
- readline.parse_and_bind('tab: complete')
- # history file
- histfile = os.path.join(os.environ['HOME'], '.pythonhistory')
- try:
- readline.read_history_file(histfile)
- except IOError:
- pass
- atexit.register(readline.write_history_file, histfile)
- del os, histfile, readline, rlcompleter
python初学day01的更多相关文章
- 孤荷凌寒自学python第四十五天Python初学基础基本结束的下阶段预安装准备
孤荷凌寒自学python第四十五天Python初学基础基本结束的下阶段预安装准备 (完整学习过程屏幕记录视频地址在文末,手写笔记在文末) 今天本来应当继续学习Python的数据库操作,但根据过去我自 ...
- Python初学笔记之字符串
一.字符串的定义 字符串是就一堆字符,可以使用""(双引号).''(单引号)来创建. 1 one_str = "定义字符串" 字符串内容中包含引号时,可以使用转 ...
- Python初学的易犯错误
当初学 Python 时,想要弄懂 Python 的错误信息的含义可能有点复杂.这里列出了常见的的一些让你程序 crash 的运行时错误. 1)忘记在 if , elif , else , for , ...
- Python初学
经同学推荐,学习了下Python语言,看Python的介绍,它本身是一个面向对象的解释型脚本语言,我初看到这句话的时候就在想,一个脚本语言还搞成面向对象?有这个必要么?原谅我肤浅了一把. 它还被俗称为 ...
- python 初学笔记 (一)
初学python第一天,希望自己真正了解计算机语言,并且做出成效. 写下学习笔记,记录学习进度,娱乐学习,不断成长. python详细介绍: python是什么?运用到哪里?有哪些在使用它? pyth ...
- Python初学(1)
最近在学习python,以后想编写一些工作中用的到的脚本.python的入门我选择了<python从初学到入门>,这篇文章我会跟进我的学习进度.算是一个笔记吧. 我本身是熟悉C语言的,看p ...
- python初学心得之一
昨天开始接触并学习python,对python有了初步印象. 一.python主要应用方向 二.python语言类型 三.python2和3的主要区别 四.常见字符编码 五.Python语法初学 一 ...
- Python基础-day01
写在前面 先后接触过很多编程语言,最喜欢的就是C和Python,相比其他语言,C 是神器,优点太多了:个人而言,C 最重要的一点就是能够让你在敲代码的时候是以一个计算机科学家的角度去思考,而不是仅仅停 ...
- python初学杂记
python常用命令: 1.python 或者 python3 打开交互式python解释器 2.python hello.py 通过命令提示符运行python脚本 交互式python解释器常用 ...
随机推荐
- Modelsim-altera 仿真 顶层原理图设计的FPGA
我的原理图采用的是bdf的顶层原理图的设计,仿真工具用的是modelsim-altera,调用仿真后的错误提示: # ** Error: (vsim-3033) C:/Users/lenovo/Des ...
- 常用的.Net 知识点
1.Replace C#:(using System.Text.RegularExpressions;) string txt = Regex.Replace(txtLog.Text.ToString ...
- 网站压力测试工具webbench使用说明
一.webbench简介 Webbench是有名的网站压力测试工具,它是由Lionbridge公司(http://www.lionbridge.com)开发.它的帮助文件和文档请到:ww ...
- Android 自定义表格显示数据
Android 自定义TextView控件,用来组成表格方便数据的展示. 首先看一下效果 样式不是很好看,需要用的可以自己优化一下. 实现方式很简单. 1.自定义控件 MyTableTextView ...
- TENDA-F322路由器管理工具
https://yunpan.cn/cYsfNxJLfVnUY (提取码:d0ae)
- C#遍历集合与移除元素的方法
如果用foreach,会造成被遍历的集合更改后带来异常问题. 此时,用for循环可有效的解决这个问题. for(int i=0;i<List.Count;i++) { if(条件是真) { Li ...
- nodejs(1)
node.js 是一个让javascript运行在服务端的开发平台 node.js的环境部署 1.下载安装包 https://nodejs.org/en/ 安装后 打开cmd的dos窗口 运行node ...
- weedfs getsockopt: connection timed out
启动master weed master -ip 10.191.197.133 -mdir /namenode -ip.bind 10.191.197.133 I0809 16:53:51 7721 ...
- Spring-Batch CSV文件读取时的注意点
按照Spring Batch 之 Sample(CSV文件操作)(四) 的方式配置好csvItemReader, 发现读入的数据很是奇怪,通过修改配置文件发现, commit-interval=&qu ...
- VUE 入门基础(7)
八,事件处理器 监听事件 可以用v-on 指令监听DOM 事件来触发一些javaScript <div id="example-1"> <button v-on: ...