python快速入门(Python快速入门第三版 中文PDF)
off999 2024-10-04 18:51 28 浏览 0 评论
python 快速入门
python简介
Python 是一个高层次的结合了解释性、编译性、互动性和面向对象的脚本语言。
- Python 是一种解释型语言: 这意味着开发过程中没有了编译这个环节。类似于PHP和Perl语言。
- Python 是交互式语言: 这意味着,您可以在一个 Python 提示符 >>> 后直接执行代码。
- Python 是面向对象语言: 这意味着Python支持面向对象的风格或代码封装在对象的编程技术。
python版本
python目前支持两个大的版本Python, 2.7 and 3.5. 令人疑惑的是python的2和3的版本不兼容,因此使用2的版本的代码在3中可能会出现不兼容的现象,目前推荐使用python 3+的版本作为开放和学习。
基本的数据类型
像大多数语言一样,Python具有许多基本类型,包括整数,浮点数,布尔值和字符串。这些数据类型以其他编程语言所熟悉的方式运行。
数值:
整数和浮点数可以像其他语言一样使用:
x = 3
print(type(x)) # Prints "<class 'int'>"
print(x) # Prints "3"
print(x + 1) # Addition; prints "4"
print(x - 1) # Subtraction; prints "2"
print(x * 2) # Multiplication; prints "6"
print(x ** 2) # Exponentiation; prints "9"
x += 1
print(x) # Prints "4"
x *= 2
print(x) # Prints "8"
y = 2.5
print(type(y)) # Prints "<class 'float'>"
print(y, y + 1, y * 2, y ** 2) # Prints "2.5 3.5 5.0 6.25"
python不像其他编程语言,其不支持x++和x—这种操作。
布尔值:
不同于其他编程语言,python使用英文代替(||, &&等):
t = True
f = False
print(type(t)) # Prints "<class 'bool'>"
print(t and f) # Logical AND; prints "False" 与
print(t or f) # Logical OR; prints "True" 或
print(not t) # Logical NOT; prints "False" 非
print(t != f) # Logical XOR; prints "True"
字符串
python对字符串的支持是很强大的:
hello = 'hello' # 单引号
world = "world" # 双引号也可
print(hello) # Prints "hello"
print(len(hello)) # String length; prints "5"
hw = hello + ' ' + world # String concatenation
print(hw) # prints "hello world"
hw12 = '%s %s %d' % (hello, world, 12) # sprintf style string formatting
print(hw12) # prints "hello world 12"
容器
Python包含几种内置的容器类型: lists, dictionaries, sets和tuples.
Lists
列表与数组的Python等效,但可调整大小,并且可以包含不同类型的元素:
xs = [3, 1, 2] # Create a list
print(xs, xs[2]) # Prints "[3, 1, 2] 2"
print(xs[-1]) # Negative indices count from the end of the list; prints "2"
xs[2] = 'foo' # Lists can contain elements of different types
print(xs) # Prints "[3, 1, 'foo']"
xs.append('bar') # Add a new element to the end of the list
print(xs) # Prints "[3, 1, 'foo', 'bar']"
x = xs.pop() # Remove and return the last element of the list
print(x, xs) # Prints "bar [3, 1, 'foo']"
切片: 除了一次访问一个列表元素,Python还提供了简洁的语法来访问子列表。这称为切片
nums = list(range(5)) # range is a built-in function that creates a list of integers
print(nums) # Prints "[0, 1, 2, 3, 4]"
print(nums[2:4]) # Get a slice from index 2 to 4 (exclusive); prints "[2, 3]"
print(nums[2:]) # Get a slice from index 2 to the end; prints "[2, 3, 4]"
print(nums[:2]) # Get a slice from the start to index 2 (exclusive); prints "[0, 1]"
print(nums[:]) # Get a slice of the whole list; prints "[0, 1, 2, 3, 4]"
print(nums[:-1]) # Slice indices can be negative; prints "[0, 1, 2, 3]"
nums[2:4] = [8, 9] # Assign a new sublist to a slice
print(nums) # Prints "[0, 1, 8, 9, 4]"
循环:
animals = ['cat', 'dog', 'monkey']
for animal in animals:
print(animal)
如果要访问循环体内每个元素的索引,请使用内置的枚举函数enumerate:
animals = ['cat', 'dog', 'monkey']
for idx, animal in enumerate(animals):
print('#%d: %s' % (idx + 1, animal))
列表推导式(List comprehensions):
列表推导式可以将循环这种方式表达的更为简洁,如下所示:
# 构建1-10的列表
l1 = [x for x in range(1,11)]
# 可以对迭代的元素进行操作
l2= [x*x for x in range(1,11)]
# for循环后跟if语句
l3 = [i for i in range(1,11) if i % 2 == 0]
# 嵌套列表合成单个列表 [[1,2],[3,4]] -> [1,2,3,4]
l = [[1,2],[3,4]]
l4 = [j for i in l for j in i]
字典
字典是是无序的键值对(key:value)集合,等同于c++中的unordered_map哈希表。同一个字典内的键必须是互不相同的。
其形式为 :键:值
# 创建字典
d = {'cat': 'cute', 'dog': 'furry'}
# 获取key值
print(d['cat']) # Get an entry from a
# 查询key是不是在字典中
print('cat' in d)
# 新增key:value
d['fish'] = 'wet' # Set an entry in a dictionary
print(d['fish']) # Prints "wet"
# 如果没有key,则报错,所以一般不直接进行取值操作
# print(d['monkey']) # KeyError: 'monkey' not a key of d
# 使用get获取key值,如果key不存在,则赋予默认值
print(d.get('monkey', 'N/A'))
# 删除元素
del d['fish']
迭代
迭代字典很简单,和列表差不多:
d = {'person': 2, 'cat': 4, 'spider': 8}
# 注意这种遍历的是key的值
for animal in d: #key
legs = d[animal] #value
print 'A %s has %d legs' % (animal, legs)
# Prints "A person has 2 legs", "A spider has 8 legs", "A cat has 4 legs"
# 如果想直接获得键值对, 使用iteritems:
d = {'person': 2, 'cat': 4, 'spider': 8}
for animal, legs in d.iteritems():
print 'A %s has %d legs' % (animal, legs)
# Prints "A person has 2 legs", "A spider has 8 legs", "A cat has 4 legs"
字典的表达式
nums = [0, 1, 2, 3, 4]
even_num_to_square = {x: x ** 2 for x in nums if x % 2 == 0}
print even_num_to_square # Prints "{0: 0, 2: 4, 4: 16}"
集合
集合,也就是没有顺序的,同时所有的元素都不相同的集合。A set is an unordered collection of distinct elements,用英文更好的表达其含义。集合同样是用花括号创建:
animals = {'cat', 'dog'}
print('cat' in animals) # Check if an element is in a set; prints "True"
print('fish' in animals) # prints "False"
animals.add('fish') # Add an element to a set
print('fish' in animals) # Prints "True"
print(len(animals)) # Number of elements in a set; prints "3"
animals.add('cat') # Adding an element that is already in the set does nothing
print(len(animals)) # Prints "3"
animals.remove('cat') # Remove an element from a set
print(len(animals)) # Prints "2"
集合的循环很列表一致:
animals = {'cat', 'dog', 'fish'}
for idx, animal in enumerate(animals):
print '#%d: %s' % (idx + 1, animal)
# Prints "#1: fish", "#2: dog", "#3: cat"
元组
元组是(不可变的)有序值列表。元组在很多方面类似于列表。最重要的区别之一是,元组可以用作字典中的键和集合的元素,而列表则不能。这是一个简单的示例:
# 使用括号
t = (5, 6) # Create a tuple
print(type(t)) # Prints "<class 'tuple'>"
函数
python函数的定义使用def:
def sign(x):
if x > 0:
return 'positive'
elif x < 0:
return 'negative'
else:
return 'zero'
for x in [-1, 0, 1]:
print(sign(x))
# Prints "negative", "zero", "positive"
类
类的定义很简单,如下所示的基本结构:
class Greeter(object):
# 构造函数
def __init__(self, name):
self.name = name # Create an instance variable
# 方法
def greet(self, loud=False):
if loud:
print('HELLO, %s!' % self.name.upper())
else:
print('Hello, %s' % self.name)
g = Greeter('Fred') # Construct an instance of the Greeter class
g.greet() # Call an instance method; prints "Hello, Fred"
g.greet(loud=True) # Call an instance method; prints "HELLO, FRED!"
?
相关推荐
- android13正式版下载(安卓版本13)
-
出现该问题的原因是,用户在设置里开启了新下载的APP,仅添加到APP资源库选项。大家只要进入“设置-主屏幕”,把新下载的APP,改为“添加到主屏幕”即可解决问题。修改完成后,你再进入AppStore下...
- firefox浏览器安卓版(firefox浏览器安卓版 打开本地网页)
-
要进入火狐浏览器手机版的主页,你可以通过以下几种方式进行:首先,打开火狐浏览器App,然后点击右上角的三条横线菜单按钮,接着选择“主页”选项。另外,你也可以直接在浏览器地址栏中输入“about:hom...
- 电脑cpu性能排行榜天梯图(“电脑cpu性能天梯图”)
-
一、英特尔酷睿i7670。这款英特尔CPU采用的是超频新芯,最大程度的提升处理器的超频能力。二、英特尔酷睿i74790kCPU:这款CPU采用22纳米制程工艺的框架,它的默认频率是4.0到4.4Ghz...
- 电脑自由截屏的快捷键是什么
-
快捷键是ctrl+alt+a,我们可将聊天窗口缩小,放在旁边。然后找到想要截屏的位置,这时我们在截屏旁边,就更加的方便了。在键盘中按下PrintScreenSysRq(简写为PrtSc)键,此快捷...
- windows10精简版官网下载(win10官方精简版下载)
-
精简版的意思的它比原版的功能和软件少了,其实精简版的更适合大众,没有多余的其他必要功能,更快Win10版本主要为四个分别是专业版、家庭版、企业版、教育版,其实除了这四个之外,还有工作站版、LTSB/L...
- cad2008安装失败(Win11安装cad2008安装失败)
-
解决方法:1、右键点击“开始”按钮,选择“程序和功能”;2、然后点击“启用或关闭windows功能”;3、勾选“Microsoft.NETFramework3.5(包括.Net2.0)”后点击确定按钮...
- u盘在电脑上怎么找出来(u盘在电脑上怎么找到)
-
在电脑中找不到u盘,是因为系统没有自动识别出来,手动打开即可,具体的解决步骤如下:1、在桌面上点击我的电脑,右键,管理。2、打开管理界面,点击储存。3、进到储存页面。4、到这一步,也就可以看到了,有这...
- 联想一体机怎么进入bios(联想一体机怎么进入u盘启动)
-
所需工具:联想Lenovo品牌一体机、启动U盘。具体步骤如下:1、联想一体机从U盘启动设置步骤如下重启联想一体机,启动过程中按F1进入BIOS,部分机型则是开机按Enter键,进入之后再按F12选择进...
- 如何装ghost系统盘(ghost装机教程)
-
ghost是不能做系统c盘,它是一种对硬盘和分区制作成映像文件进行备份和恢复的工具软件,是不能进行操作系统安装。这个软件的使用目的是,当我们安装配置好操作系统以后,用ghost软件对c盘进行备份,或者...
- 加密u盘如何格式化(加密u盘如何格式化手机)
-
1,点击系统与安全进入电脑的控制面板界面,点击上方的系统与安全的选项,在系统界面找到最下方的管理工具功能组。2,选中u盘选择管理工具下面的创建并格式化硬盘分区,点击弹出磁盘管理的界面,在这个里面选中你...
- 万能显卡驱动离线版pc(万能显卡驱动离线版)
-
万用驱动是综合各电脑硬件的性能而制做的软件,对于大多数的电脑硬件驱动都好用,但对于少数品牌电脑驱动要求严格的,就不灵了。有的硬件用万能驱动后,使用效果不佳,就是因为没有完全驱动好。所以,知名品牌电脑硬...
- 笔记本windows8系统下载(笔记本电脑系统win8)
-
在电脑上面就可以下载,打开浏览器搜索windous8系统会出现一些下拉选择,选择第一条或者选择有官网字样的,就直接有下载按钮,然后点击下载就可以了win8可以支持现在可以见到的所有Photosho...
欢迎 你 发表评论:
- 一周热门
-
-
抖音上好看的小姐姐,Python给你都下载了
-
全网最简单易懂!495页Python漫画教程,高清PDF版免费下载
-
Python 3.14 的 UUIDv6/v7/v8 上新,别再用 uuid4 () 啦!
-
python入门到脱坑 输入与输出—str()函数
-
宝塔面板如何添加免费waf防火墙?(宝塔面板开启https)
-
Python三目运算基础与进阶_python三目运算符判断三个变量
-
飞牛NAS部署TVGate Docker项目,实现内网一键转发、代理、jx
-
(新版)Python 分布式爬虫与 JS 逆向进阶实战吾爱分享
-
慕ke 前端工程师2024「完整」
-
失业程序员复习python笔记——条件与循环
-
- 最近发表
- 标签列表
-
- python计时 (73)
- python安装路径 (56)
- python类型转换 (93)
- python进度条 (67)
- python吧 (67)
- python的for循环 (65)
- python格式化字符串 (61)
- python静态方法 (57)
- python列表切片 (59)
- python面向对象编程 (60)
- python 代码加密 (65)
- python串口编程 (77)
- python封装 (57)
- python写入txt (66)
- python读取文件夹下所有文件 (59)
- python操作mysql数据库 (66)
- python获取列表的长度 (64)
- python接口 (63)
- python调用函数 (57)
- python多态 (60)
- python匿名函数 (59)
- python打印九九乘法表 (65)
- python赋值 (62)
- python异常 (69)
- python元祖 (57)
