python基础教程第三版下载,python零基础入门书籍pdf

本文详细介绍了Python编程的基础知识,包括变量和数据类型、列表、字典、条件语句、函数、类、文件操作、异常处理以及JSON数据存储。涵盖了变量定义、数字和文本结合、列表操作、元组、if-else结构、字典用法、类和继承等内容。
摘要由CSDN通过智能技术生成

大家好,小编为大家解答python基础教程第三版下载的问题。很多人还不知道python零基础入门书籍pdf,现在让我们一起来看看吧!

结构:

第一部分 基础知识
第1章 起步
第2章 变量和简单数据关型
第3章 列表简介
第4章 操作列表
笃5章 if语句
笃6章 字典
第7章 用户输入和While看环
第8章 函数
第9章 类
第10章 文件和异常
第11章 测试代码
第二部分项目
项目1外星人入侵
顶目2数据可视化
项目3Web应用程序

第一部分:基础知识

1.变量。每个变量都存储了一个值——与变量相关联的信息用python画雪人
在这里,存储的值为文本“Hello Python world!”。
message = "Hello Python world!" 
print(message) 
解读:添加变量导致Python解释器需要做更多工作。处理第1行代码时,它将文本“Hello Python 
world!”与变量message关联起来;而处理第2行代码时,它将与变量message关联的值打印到屏幕。
ps:慎用小写字母l和大写字母O,因为它们可能被人错看成数字1和0

2.数字和文本合用。需要用str
age = 23 
message = "Happy " + str(age) + "rd Birthday!" 
print(message) 

3.注释
在Python中,注释用井号(#)标识。井号后面的内容都会被Python解释器忽略。如果存在中文,要在程序最前面增加
#coding=gbk
#字符串案例

4.列表-索引
Python为访问最后一个列表元素提供了一种特殊语法。通过将索引指定为-1,可让Python返
回最后一个列表元素:
bicycles = ['trek', 'cannondale', 'redline', 'specialized'] 
print(bicycles[-1]) 

5.列表-增删改查
 1)改
 motorcycles = ['honda', 'yamaha', 'suzuki'] 
print(motorcycles) 
 motorcycles[0] ='lidns'
print(motorcycles)
2)增
#在列表最后面
otorcycles.append('ducati') 
print(motorcycles) 
#列表任意位置
motorcycles.insert(0, 'ducati') 
print(motorcycles) 
3)删
#删掉任意的
del motorcycles[0] 
print(motorcycles) 
#删掉最后一个
popped_motorcycle = motorcycles.pop() 
print(motorcycles) 
print(popped_motorcycle) 
#删掉任意的 和第一个相比,多了返回值
first_owned = motorcycles.pop(0) 
print('The first motorcycle I owned was a ' + first_owned.title() + '.') 
#remote只删除第一个指定的值
motorcycles = ['honda', 'yamaha', 'suzuki', 'ducati'] 
print(motorcycles) 
motorcycles.remove('ducati') 
print(motorcycles) 
4)遍历
 magicians = ['alice', 'david', 'carolina'] 
 for magician in magicians: 
 print(magician) 
解读:这行代码让Python从列表magicians中取出一个名字,并将其存储在变量magician中。最后,我们
让Python打印前面存储到变量magician中的名字(见)。这样,对于列表中的每个名字,Python
都将重复执行处和处的代码行。你可以这样解读这些代码:对于列表magicians中的每位魔术
师,都将其名字打印出来。输出很简单,就是列表中所有的姓名

6.列表-排序
#按照首字母排序
cars = ['bmw', 'audi', 'toyota', 'subaru'] 
print(cars) 
cars.sort()
#按照首字母排序,排序之后,原列表不变
sorted(cars,reverse=True)
#倒着排序
cars.reverse() 
print(cars) 
#列表长度
len(cars)

7.for循环
for value in ge(2,1,20)
    print(value)

8.元组
列表非常适合用于存储在程序运行期间可能变化的数据集。列表是可以修改的,这对处理网
站的用户列表或游戏中的角色列表至关重要。然而,有时候你需要创建一系列不可修改的元素,
元组可以满足这种需求。Python将不能修改的值称为不可变的,而不可变的列表被称为元组
 dimensions = (200, 50) 
 print(dimensions[0]) 
print(dimensions[1]) 

9.if语句
age = 17 
 if age >= 18: 
 print("You are old enough to vote!") 
 print("Have you registered to vote yet?")
 else: #if-elif-else结构
 print("Sorry, you are too young to vote.") 
 print("Please register to vote as soon as you turn 18!") 

ps:Python将在列表
至少包含一个元素时返回True,并在列表为空时返回False。
 requested_toppings = [] 
 if requested_toppings: 
 else: 


10.字典
alien_0 = {'color': 'green', 'points': 5}
favorite_languages = { 
 'jen': 'python', 
 'sarah': 'c', 
 'edward': 'ruby', 
 'phil': 'python', 
 }  
print(alien_0['color']) 
print(alien_0['points'])

del alien_0['color']

11.遍历字典
user_0 = { 
 'username': 'efermi', 
 'first': 'enrico', 
 'last': 'fermi', 
 } 
 for key,value in user_0.items():
#.keys()返回list
 for name in favorite_languages.keys(): 
 print(name.title()) 
#set集合,去掉重复项
for language in set(favorite_languages.values()): 
 print(language.title()) 


12.字典-嵌套,字典嵌套列表,列表嵌套字典, 字典嵌套字典
people = {'bob':[1,2,3,4,5],
    'Lily':{'frist_name':'Bob','age':10},
    'Alice':[3],
    'Mary':[4]}

13.文本-》整数转换
 age = int(age) 

14.while循环
prompt = "\nTell me something, and I will repeat it back to you:" 
prompt += "\nEnter 'quit' to end the program. "
 active = True 
 while active: 
 message = input(prompt) #和终端交互
 
 if message == 'quit': 
         active = False 
        break #退出循环  continue 用法和其他语言一样

 else: 
 print(message) 

14.函数
def describe_pet(animal_type, pet_name): 
 """显示宠物的信息""" 
 print("\nI have a " + animal_type + ".") 
 print("My " + animal_type + "'s name is " + pet_name.title() + ".") 
 #位置实参
describe_pet('harry', 'hamster')
#关键对实参
describe_pet(animal_type='hamster', pet_name='harry') 
#可给默认值
#def describe_pet(pet_name, animal_type='dog')
#传递列表副本。不改变列表
要将列表的副本传递给函数,可以像下面这样做:
function_name(list_name[:])

15.函数-不定实参
def make_pizza(*toppings): 
 """打印顾客点的所有配料""" 
 print(toppings) 
 #打包成元组
make_pizza('pepperoni') 
make_pizza('mushrooms', 'green peppers', 'extra cheese') 
形参**user_info中的两个星号让Python创建一个名为user_info的空字典,
user_profile = build_profile('albert', 'einstein', 
 location='princeton', 
 field='physics') 
print(user_profile) 

16.函数-导入程序
import module_name
module_name.function_name()
#直接导入函数
from module_name import function_name
from pizza import * 
#设置别名
from pizza import make_pizza as mp 

17.类
class Ia():
    def _init_(self,param1,param2..):
    def ...
ia = Ia(param1,param2...)

18.继承-》可重写
 class ElectricCar(Car): 
 """电动汽车的独特之处""" 
 def __init__(self, make, model, year): 
 """初始化父类的属性""" 
 super().__init__(make, model, year) 
 
19.类-》导入
from car import Car 
from electric_car import ElectricCar 

20.文件的读写
file_path = 'C:\\Users\\ehmatthes\\other_files\\text_files\\filename.txt' 
with open(file_path) as file_object: 
#写
with open(file_path,'a'/'w') as file_object:

21.异常处理
 """计算一个文件大致包含多少个单词""" 
 try: 
 --snip-- 
 except FileNotFoundError: 
 pass 
 else: 
 --snip-- 
filenames = ['alice.txt', 'siddhartha.txt', 'moby_dick.txt', 'little_women.txt'] 
for filename in filenames: 
 count_words(filename) 


22.数据存储结构-JSON
#读取
with open(file_name) as file_object:
    number = json.load(file_object)
#写入
with open(file_name,'w') as file_object:
    json.dump(number,file_object)

23.测试代码

PS:
1 python不支持自增运算符和自减运算符。例如i++/i-是错误的,但i+=1是可以的。
2 1/2在python2.5之前会等于0.5,在python2.5之后会等于0。
3 不等于为!=或<>
4 等于用==表示
5 逻辑表达式中and表示逻辑与,or表示逻辑或,not表示逻辑非
6 通过切片截取字符串:
word=”world”
print word[0:3]
7 简单处理文件:
context=”hello,world”
f
f=file(“hello.txt”,’w’)
f
f.write(context);
f
f.close()

8 读取文件可以使用readline()函数、readlines()函数和read函数。
9 写入文件可以使用write()、writelines()函数
 

  • 24
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值