python基础(自用)
1 列表
1 列表连接与列表复制:
>>> [1, 2, 3] + ['A', 'B', 'C']
[1, 2, 3, 'A', 'B', 'C']
>>> ['X', 'Y', 'Z'] * 3
['X', 'Y', 'Z', 'X', 'Y', 'Z', 'X', 'Y', 'Z']
>>> spam = [1, 2, 3]
>>> spam = spam + ['A', 'B', 'C']
>>> spam
[1, 2, 3, 'A', 'B', 'C'
2 多重赋值技巧:
>>> cat = ['fat', 'black', 'loud']
>>> size, color, disposition = cat
3 index方法:
>>> spam = ['hello', 'hi', 'howdy', 'heyas']
>>> spam.index('hello')
0
>>> spam.index('heyas')
3
4 sys.exit方法:
import sys
while True:
print('Type exit to exit.')
response = input()
if response == 'exit':
sys.exit()
print('You typed ' + response + '.')
5 名字相同的局部变量与全局变量:
def spam():eggs = 'spam local'
print(eggs) # prints 'spam local'
def bacon():eggs = 'bacon local'
print(eggs) # prints 'bacon local'
spam()
print(eggs) # prints 'bacon local'eggs = 'global'
bacon()
print(eggs) # prints 'global'#输出
bacon local
spam local
bacon local
global
6 del语句:
>>> spam = ['cat', 'bat', 'rat', 'elephant']
>>> del spam[2]
>>> spam
['cat', 'bat', 'elephant']
>>> del spam[2]
>>> spam
['cat', 'bat']
7 append方法:
>>> spam = ['cat', 'dog', 'bat']
>>> spam.append('moose')
>>> spam
['cat', 'dog', 'bat', 'moose']
8 insert方法:
>>> spam = ['cat', 'dog', 'bat']
>>> spam.insert(1, 'chicken')
>>> spam
['cat', 'chicken', 'dog', 'bat']
9 remove方法:
>>> spam = ['cat', 'bat', 'rat', 'cat', 'hat', 'cat']
>>> spam.remove('cat')
>>> spam
['bat', 'rat', 'cat', 'hat', 'cat']
10 sort方法:
>>> spam.sort(reverse=True)
>>> spam
['elephants', 'dogs', 'cats', 'badgers', 'ants']>>> spam = ['a', 'z', 'A', 'Z']
>>> spam.sort(key=str.lower)
>>> spam
['a', 'A', 'z', 'Z']
11 list、tuple方法:
>>> tuple(['cat', 'dog', 5])
('cat', 'dog', 5)
>>> list(('cat', 'dog', 5))
['cat', 'dog', 5]
>>> list('hello')
['h', 'e', 'l', 'l', 'o']
12 copy 模块的 copy()和 deepcopy()函数:
>>> import copy
>>> spam = ['A', 'B', 'C', 'D']
>>> cheese = copy.copy(spam)
>>> cheese[1] = 42
>>> spam
['A', 'B', 'C', 'D']
>>> cheese
['A', 42, 'C', 'D']
2 字典和结构化数据
1 keys、values、items方法
2 get方法:
>>> picnicItems = {'apples': 5, 'cups': 2}
>>> 'I am bringing ' + str(picnicItems.get('cups', 0)) + ' cups.'
'I am bringing 2 cups.'
>>> 'I am bringing ' + str(picnicItems.get('eggs', 0)) + ' eggs.'
'I am bringing 0 eggs.'
3 setdefault方法:
>>> spam = {'name': 'Pooka', 'age': 5}
>>> spam.setdefault('color', 'black')
'black'
>>> spam
{'color': 'black', 'age': 5, 'name': 'Pooka'}
>>> spam.setdefault('color', 'white')
'black'
>>> spam
{'color': 'black', 'age': 5, 'name': 'Pooka'
4 pprint模块:
pprint.pprint(someDictionaryValue)
print(pprint.pformat(someDictionaryValue))
3 字符串操作
1 转义字符:
2 原始字符串:
>>> print(r'That is Carol\'s cat.')
That is Carol\'s cat.
3 用三重引号的多行字符串:
print('''Dear Alice,
Eve's cat has been arrested for catnapping, cat burglary, and extortion.
Sincerely,
Bob''')#输出
Dear Alice,
Eve's cat has been arrested for catnapping, cat burglary, and extortion.
Sincerely,
Bob
4 多行注释:
"""This is a test Python program.
Written by Al Sweigart al@inventwithpython.com
This program was designed for Python 3, not Python 2.
"""
def spam():
"""This is a multiline comment to help
explain what the spam() function does."""
print('Hello!')
5 字符串方法 upper()、lower()、isupper()和 islower():
>>> spam = 'Hello world!'
>>> spam = spam.upper()
>>> spam
'HELLO WORLD!'
>>> spam = spam.lower()
>>> spam
'hello world!'>>> spam = 'Hello world!'
>>> spam.islower()
False
>>> spam.isupper()
False
>>> 'HELLO'.isupper()
True
>>> 'abc12345'.islower()
True
>>> '12345'.islower()
False
>>> '12345'.isupper()
False
6 isX 字符串方法:
>>> 'hello'.isalpha()
True
>>> 'hello123'.isalpha()
False
>>> 'hello123'.isalnum()
True
第 6 章 字符串操作
>>> 'hello'.isalnum()
True
>>> '123'.isdecimal()
True
>>> ' '.isspace()
True
>>> 'This Is Title Case'.istitle()
True
>>> 'This Is Title Case 123'.istitle()
True
>>> 'This Is not Title Case'.istitle()
False
>>> 'This Is NOT Title Case Either'.istitle()
False
7 字符串方法 startswith()和 endswith():
>>> 'Hello world!'.startswith('Hello')
True
>>> 'Hello world!'.endswith('world!')
True
>>> 'abc123'.startswith('abcdef')
False
>>> 'abc123'.endswith('12')
False
>>> 'Hello world!'.startswith('Hello world!')
True
>>> 'Hello world!'.endswith('Hello world!')
True
8 字符串方法 join()和 split():
>>> ', '.join(['cats', 'rats', 'bats'])
'cats, rats, bats'
>>> ' '.join(['My', 'name', 'is', 'Simon'])
'My name is Simon'
>>> 'ABC'.join(['My', 'name', 'is', 'Simon'])
'MyABCnameABCisABCSimon'>>> 'MyABCnameABCisABCSimon'.split('ABC')
['My', 'name', 'is', 'Simon']
>>> 'My name is Simon'.split('m')
['My na', 'e is Si', 'on']
9 用 rjust()、ljust()和 center()方法对齐文本:
>>> 'Hello'.rjust(20, '*')
'***************Hello'
>>> 'Hello'.ljust(20, '-')
'Hello---------------'
>>> 'Hello'.center(20, '=')
'=======Hello========'
10 用 strip()、rstrip()和 lstrip()删除空白字符:
>>> spam = ' Hello World '
>>> spam.strip()
'Hello World'
>>> spam.lstrip()
'Hello World '
>>> spam.rstrip()
' Hello World'>>> spam = 'SpamSpamBaconSpamEggsSpamSpam'
>>> spam.strip('ampS')
'BaconSpamEggs'
11 用 pyperclip 模块拷贝粘贴字符串:
>>> import pyperclip
>>> pyperclip.copy('Hello world!')
>>> pyperclip.paste()
'Hello world!'
python基础(自用)相关推荐
- 自用python基础
python基础 print("hello "+ " liuchenxu") print('let\'s go') print("the fisrt ...
- python基础(一):python简介
前言 本系列python基础教程均采用python3.8并向前兼容,所谓吃水不忘挖井人,希望我的教程能够帮助到大家,当然此教程也是参阅了许多博文结合自己的见解而来,仅供参考. python是一款非常友 ...
- 数据分析与爬虫实战视频——学习笔记(一)(python基础、urllib、超时设置、自动模拟HTTP请求、异常处理、浏览器伪装、代理服务器、新闻爬虫、淘宝登陆和图片爬取)
未经允许,请勿转载. 连载未完成状态 网址: [数据挖掘]2019年最新python3 数据分析与爬虫实战_哔哩哔哩 (゜-゜)つロ 干杯~-bilibili https://www.bilibili ...
- f是一个python内部变量类型,Python基础变量类型——List浅析
Python使用list 一.list Python内置的一种数据类型是列表:list.list是一种有序的集合,可以随时添加和删除其中的元素. 比如,列出班里所有同学的名字,就可以用一个list表示 ...
- python 等号报错_Python学习----Python基础
Python基础 一.数据类型和变量 1.在Python中,等号=是赋值语句,可以把任意数据类型赋值给变量,同一个变量可以反复赋值,而且可以是不同类型的变量. 例如: a =520# a是整数prin ...
- python基础——继承与派生、组合
python基础--继承与派生 1 什么是继承: 继承是一种创建新的类的方式,在python中,新建的类可以继承自一个或者多个父类,原始类成为基类或超累,新建的类成为派生类或子类 1.1 继承分为:单 ...
- Python学习笔记1 Python基础
第1章 Python基础 1.1 Python概述及版本介绍 1.Python是一种面向对象的解释型计算机程序设计语言(解释型语言源代码->解释器逐行解释每一句源代码) 2.优点:高级语言.可移 ...
- python基础---元组、字典、函数、文件、异常
文章目录 python基础---元组.字典.函数.文件.异常 Tuple(元组) 常用操作 dict(字典) 函数 文件 异常 python基础-元组.字典.函数.文件.异常 Tuple(元组) tu ...
- 快速掌握Python的捷径-Python基础前传(1)
文: jacky(朱元禄) 开文序 最近看新闻,发现高考都考Python了,随着人工智能的火热,学数据科学的人越来越多了!但对于数据行业本身来说,现象级的火热,这并不是什么好事. 方丈高楼平地起,无 ...
- Python培训教程之Python基础知识点梳理
Python语言是入门IT行业比较快速且简单的一门编程语言,学习Python语言不仅有着非常大的发展空间,还可以有一个非常好的工作,下面小编就来给大家分享一篇Python培训教程之Python基础知识 ...
最新文章
- 电子学会青少年编程等级考试Python案例10
- console.log(text)打印不出来的值,用console.log(text.length)却打印出来长度为1?
- ggplot2设置坐标轴范围_R可视化03|ggplot2图层-几何对象图层(geom layer)
- Discuz!NT 中的数据库链接类(重构到抽象类工厂模式)
- 2019 年 React 学习路线图
- POJ-3067 Japan(树状数组、线段树)
- 玩转oracle 11g(9):crud操作(亲测)
- 图解,C语言希尔排序
- 执行计划级别mysql 2ef,Mysql 层级、执行顺序、执行计划分析
- P1273 有线电视网 树形dp
- win10专业版开机画面模糊_Win10系统登录背景变模糊的两种恢复方法
- linux-SSH远程服务的管理操作,查看虚拟机信息
- dw编写手机版html,手机移动网页制作:用
- Flutter问题集锦——Android Studio 4.0.1 在AVD Manger中启动模拟器报“unable to locate abd”
- Flutter高仿微信-第26篇-新的朋友
- Python生成汉字字库文字,以及转换为文字图片
- POJ - 3067
- 数据驱动运营,为门店开拓第二增长曲线。
- TO_DATE使用詳解
- 百年老店焕发青春 中华医学会杂志社漫步云端
热门文章
- 内网渗透之 arp欺骗-dns劫持-网站钓鱼
- 从简历被拒,到拿下头条面试,我花了一年的时间(经验分享+面试题)
- 大胆,都是哪些程序员在反对996?!
- IP地址及数制的转换
- 终端或cmd配置git
- ElasticsearchStatusException[Elasticsearch exception [type=circuit_breaking_exception, reason=[paren
- 平衡二叉树-AVL树(LL、RR、LR、RL旋转)
- “大服务器”拥有大智慧
- PLC控制柜的布局与结构设计
- 王通:站内搜索引擎的SEO策略