Python编程-从入门到实践阅读笔记(基础)(记录防丢失)

阅读Python入门

1.变量和简单数据类型

1.1变量

1.1.1变量的命名和使用

  • 变量名只能包含字母、数字和下划线。变量名可以字母或下划线大头,但不能以数字大头

  • 变量名不能包含空格,但可以使用下划线来分割其中的单词

  • 不要将Python关键字和函数名用作变量名,即不要使用Python保留用于特殊用途的单词

    import keyword
    print(keyword.kwlist)
    
    ['False', 'None', 'True', '__peg_parser__', 'and', 'as', 'assert', 'async', 'await', 'break', 'class', 'continue', 'def', 'del', 'elif', 'else', 'except', 'finally', 'for', 'from', 'global', 'if', 'import', 'in', 'is', 'lambda', 'nonlocal', 'not', 'or', 'pass', 'raise', 'return', 'try', 'while', 'with', 'yield']
    
  • 变量名应既简短又具有描述性

  • 慎用小写字母“l”和大写字母“O”,因为他们可能被人错看成数字1和0

1.1.2使用变量时避免命名错误

Traceback (most recent call last):
  File "<pyshell#8>", line 1, in <module>
    print(mesage)
NameError: name 'mesage' is not defined

变量未定义:Python无法识别提供的变量名。名称错误通常意味着两种情况:

  • 使用变量前忘记了给变量赋值

  • 输入变量名时拼写不正确

1.2字符串

  • 字符串:在Python中用引号括起的都是字符串,其中的引号可以是单引号,也可以是双引号

    name = "ada lovelace"
    # 以首字母大写的方式显示每个单词
    print(name.title())
    # 将字符串改为全部大写或全部小写
    name = "Ada Lovelace"
    print(name.upper())
    print(name.lower())
    

1.2.1 字符串拼接

first_name = "ada"
last_name = "lovelace"
name = first_name + ' ' + last_name
message = "Hello,Welcome " + name.title() + "!"
print(message)

1.2.2使用制表符或换行符添加空白

print("Languages:\n\tPython\n\tC\n\tJavaScript")
Languages:
	Python
	C
	JavaScript

1.2.3删除空白

favorite_language = "  python  "
# 删除开头的空白
print(favorite_language.rstrip())
# 删除末尾的空白
print(favorite_language.lstrip())
# 删除两端的空白
print(favorite_language.strip())

1.3数字

1.3.1整数

  • 在Python中,可对整数执行加(+)减(-)乘(*)除(/)运算

  • 在Python中,使用两个乘号表示乘方运算

1.3.2浮点数

  • Python将带小数点的数字都称为浮点数
  • 需要注意的是,计算结果包含的小数位数可能是不确定的

1.3.3使用函数str()避免类型错误

age = 23
message = "Happy " + age + "rd Birthday"
print(message)

Traceback (most recent call last):
  File "day_01.py", line ××, in <module>
    message = "Happy " + age + "rd Birthday"
TypeError: can only concatenate str (not "int") to str
    
#可调用函数str() , 它让Python将非字符串值表示为字符串
message = "Happy " + str(age) + "rd Birthday"

1.4注释

在Python中,注释用井号(# )标识。井号后面的内容都会被Python解释器忽略。

1.5Python之道

>>>import this

The Zen of Python, by Tim Peters

Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let's do more of those!

2.列表简介

2.1列表是什么

在Python中,用方括号( [] )来表示列表,并用逗号来分隔其中的元素。下面是一个简单的列表示例,这个列表包含几种自行车:

bicycles = ['trek', 'cannondale', 'redline', 'specialized']
print(bicycles)

# ['trek', 'cannondale', 'redline', 'specialized']

2.1.1访问列表元素

# 在Python中,第一个列表元素的索引为0,而不是1
print(bicycles[0])
print(bicycles[3])
print(bicycles[0].title())
# 通过将索引指定为-1,可访问最后一个列表元素
print(bicycles[-1])
# 同理,索引-2则返回倒数第二个列表元素,索引-3则返回倒数第三个列表元素

2.1.2 使用列表中的各个值

message = 'my first bicycle was a ' + bicycles[0].title() + '.'
print(message)

2.2修改、添加和删除元素

motorcycles = ['honda', 'yamaha', 'suzuki']
# 修改列表元素
motorcycles[0] = 'ducat'

# 在列表中添加元素
# 1.在列表末尾添加元素
motorcycles.append('ducat')
'''
可以先创建一个空列表,在使用一系列的append()添加元素
motorcycles = []
motorcycles.append('honda')
motorcycles.append('yamaha')
motorcycles.append('suzuki')
print(motorcycles)
'''
# 2.在列表中插入元素
motorcycles.insert(0, 'ducati')

# 在列表中删除元素
# 1.使用del语句删除元素-条件是知道其索引
# 使用del语句将值从列表中删除后,无法在访问了
del motorcycles[0]
# 2.使用方法pop()删除元素
# 使用pop()可删除列表末尾的元素,并继续使用
# pop(index)可以删除列表中任何位置的元素
poped_motorcycle = motorcycles.pop(1)
print(poped_motorcycle)
"""
如果从列表中删除一个元素,且不在以任何方式使用它,则使用del语句
如果在删除元素后还能继续使用它,就使用方法pop()
"""
# 3.根据值删除元素,也可接着使用它的值
# remove()只删除第一个指定的值
motorcycles.remove('suzuki')

2.3组织列表

cars = ['bmw', 'audi', 'toyots', 'subaru']
# 使用方法sort()对列表进行永久性排序
# 按字母顺序排列
cars.sort()
print(cars)
# 传递参数reverse=True,按字母顺序反序排列
cars.sort(reverse=True)
print(cars)

# 使用方法sorted()对列表进行临时排序
print('没有排序前的列表顺序为:')
print(cars)
print('顺序排序后的列表顺序为:')
print(sorted(cars))
print('逆序排序后的列表顺序为:')
print(sorted(cars,reverse=True))
print('验证是否永久性的改变了列表的顺序:')
print(cars)

# 使用reverse()永久性反转列表元素的排列顺序,再次调用即可恢复
cars.reverse()

# 确定列表的长度
print('cars列表的长度为:' + str(len(cars)))

3.操作列表

3.1遍历整个列表

# 遍历整个列表
cars = ['bmw', 'audi', 'toyots', 'subaru']
# 使用for循环遍历打印整个列表
# for item in list_of_items:
for car in cars:
    # 在for循环中执行更多的操作
    print('我买了一辆' + car.title() + '车')
    print('但是我不是很喜欢,我又把这辆' + car.title() + '车卖了\n')
# 在for循环结束后执行一些操作
print('这些车我都不太喜欢,还是买别的吧')
'''
避免缩进错误
Python根据缩进来判断代码行与前一个代码行的关系,Python通过使用缩进让代码更易读
简单地说,他要求编写者使用缩进让代码整洁而结构清晰
'''

3.2创建数值列表

# 使用函数range()
# range()让Python从指定的第一个值开始数,并在到达指定的第二个值后停止、
# 因此输出不包含第二个值
for value in range(1, 10, 2):
    print(value)
# 使用rangge()创建数字列表
numbers 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值