学习内容1
- . 搭建Python环境
按照下面的的教程在anaconda中搭建Python环境,并安装PyCharm:
windows下Anaconda与PyCharm的安装与使用 - . 检验Python是否安装成功
在anaconda Prompt中输入Python,得到下图,表示已安装成功。
- . 解释什么是变量,变量的命名规则
在Python中,等号=是赋值语句,可以把任意数据类型赋值给变量,同一个变量可以反复赋值,而且可以是不同类型的变量,如
message="hello world"
print(message)
结果:
hello world
变量的命名规则:与C++类似
1)变量名只能包含字母、 数字和下划线。 变量名可以字母或下划线打头, 但不能以数字打头, 例如, 可将变量命名为a_1, 但不能将其命名为1_a。
2)变量名不能包含空格, 但可使用下划线来分隔其中的单词。 例如, 变量名greeting_message可行, 但变量名greeting message会引发错误。
3)不要将Python关键字和函数名用作变量名, 即不要使用Python保留用于特殊用途的单词, 如print 。
4)变量名应既简短又具有描述性。 例如, name比n好, student_name比s_n好。
5)慎用小写字母l和大写字母O, 因为它们可能被人错看成数字1和0。
注意:变量名尽量使用小写
- .了解字符串这一数据类型,通过书和搜索整理字符串的方法,包括大小写的转换,合并字符串,删除空白等
message="hello world"
#字符串用引号或双引号括起来
print(message)
#首字母大写
print("----------------------")
print(message.title())
#全部大写
print("----------------------")
print(message.upper())
name=message.upper()
print("----------------------")
#全部小写
print(name)
print(name.lower())
#拼接
print("----------------------")
first_name="Fang"
last_name="Yujie"
full_name=first_name+" "+last_name
print(full_name)
#删除空白
print("----------------------")
str=" python "
str=str.strip()
print(str)
结果:
hello world
----------------------
Hello World
----------------------
HELLO WORLD
----------------------
HELLO WORLD
hello world
----------------------
Fang Yujie
----------------------
python
- 了解基本的转义字符和格式化字符,并整理成文字或者代码。
转义字符 描述
------------------------------------
\(在行尾时) 续行符
\\ 反斜杠符号
\' 单引号
\" 双引号
\a 响铃
\b 退格(Backspace)
\e 转义
\000 空
\n 换行
\v 纵向制表符
\t 横向制表符
\r 回车
\f 换页
\oyy 八进制数yy代表的字符,例如:\o12代表换行
\xyy 十进制数yy代表的字符,例如:\x0a代表换行
格式化字符 含义
============================
%c 格式化字符及其ASCII码
%s 格式化字符串
%d 格式化整数
%u 格式化无符号整型
%o 格式化无符号八进制数
%x 格式化无符号十六进制数
%X 格式化无符号十六进制数(大写)
%f 格式化浮点数字,可指定小数点后的精度
%e 用科学计数法格式化浮点数
%E 作用同%e,用科学计数法格式化浮点数
%g %f和%e的简写
%G %f 和 %E 的简写
%p 用十六进制数格式化变量的地址
- 了解数字类型,整数和浮点数。
#整数运算
print(3*2-4/2+2**2)
#浮点数
print(0.1*3)
print(0.2+0.3)
结果
8.0
0.30000000000000004
0.5
- 如何添加注释?
使用#注释一行代码
使用三个单引号或三个双引号进行多段注释
学习内容2
- 了解列表数据类型。
列表: 由一系列按特定顺序排列的元素组成。用方括号([] ) 来表示列表, 并用逗号来分隔其中的元素。
friend=['tom','jack','Lulu','lucy']
print(friend)
结果:
['tom', 'jack', 'Lulu', 'lucy']
- 完成列表的增删查改(增加,删除,查找,修改)。
friend = ['tom','jack','Lulu','lucy']
print(friend)
print("1-------------------------")
#查询(访问)列表元素,注意索引从0开始
print(friend[0].title())
print("My best friend is "+ friend[2].title()+".")
#以下两种方式等价,负数表示倒数第几个元素
print(friend[3])
print(friend[-1])
print("2-------------------------")
#修改列表元素
motor = ['honda','yamaha','suzuki']
print(motor)
motor[1] = 'ducati'
print(motor)
print("3-------------------------")
#添加列表元素
# 1 末尾添加append(),可用于空列表的创建
print(motor)
motor.append('yamaha')
print(motor)
#2 列表中插入元素insert(),与指定索引与值
friend = ['tom','jack','lucy']
print(friend)
friend.insert(2,'lulu')
print(friend)
print("4-------------------------")
#删除列表元素
#已知元素位置,del语句,且删除后无法访问
friend = ['tom','jack','lucy']
print(friend)
del friend[1]
print(friend)
#pop()删除末尾元素,但能接着使用;#pop()也可以删除任意位置,在括号内加索引值即可
friend = ['tom','jack','Lulu','lucy']
print(friend)
poped_friend = friend.pop()
print(friend)
print("My best friend is "+poped_friend+'.')
#根据值删除元素,remove()
friend = ['tom','jack','Lulu','lucy']
print(friend)
friend.remove('tom')
print(friend)
结果:
['tom', 'jack', 'Lulu', 'lucy']
1-------------------------
Tom
My best friend is Lulu.
lucy
lucy
2-------------------------
['honda', 'yamaha', 'suzuki']
['honda', 'ducati', 'suzuki']
3-------------------------
['honda', 'ducati', 'suzuki']
['honda', 'ducati', 'suzuki', 'yamaha']
['tom', 'jack', 'lucy']
['tom', 'jack', 'lulu', 'lucy']
4-------------------------
['tom', 'jack', 'lucy']
['tom', 'lucy']
['tom', 'jack', 'Lulu', 'lucy']
['tom', 'jack', 'Lulu']
My best friend is lucy.
['tom', 'jack', 'Lulu', 'lucy']
['jack', 'Lulu', 'lucy']
- 了解列表的排序,比较sorted和sort两个函数。
friend = ['tom','jack','lulu','lucy']
print(friend)
#sort()永久性排序,reverse=True逆序
friend.sort()
print(friend)
friend.sort(reverse=True)
print(friend)
print("-------------------------")
#sorted临时排序,reverse=True逆序
friend = ['tom','jack','lulu','lucy']
print("Here is the original list:")
print(friend)
print("\nHere is the sorted list:")
print(sorted(friend))
print("\nHere is the original list again:")
print(friend)
结果:
['tom', 'jack', 'lulu', 'lucy']
['jack', 'lucy', 'lulu', 'tom']
['tom', 'lulu', 'lucy', 'jack']
-------------------------
Here is the original list:
['tom', 'jack', 'lulu', 'lucy']
Here is the sorted list:
['jack', 'lucy', 'lulu', 'tom']
Here is the original list again:
['tom', 'jack', 'lulu', 'lucy']
- 了解列表的方法,sort(),reverse()等。(附加题:了解sort()函数里的key参数,并实现一个列表的排序[[“a”,1],[“b”,0],[“c”,3]],要求变成[[“b”,0],[“a”,1],[“c”,3]])。
list_abc = [["a",1],["b",0],["c",3]]
print(list_abc)
#reverse()逆序输出
list_abc.reverse()
print(list_abc)
list_abc.sort(key=lambda x:x[1])
print(list_abc)
结果:
[['a', 1], ['b', 0], ['c', 3]]
[['c', 3], ['b', 0], ['a', 1]]
[['b', 0], ['a', 1], ['c', 3]]
- 了解循环,实现简单的遍历列表。
abc = ["a","b","c"]
#遍历列表
for a_b_c in abc:
print(a_b_c)
friends = ['tom','jack','lulu','lucy']
for friend in friends:
print(friend.title() + ", that is my best friend!\n")
print("We are all friends.")
结果:
a
b
c
Tom, that is my best friend!
Jack, that is my best friend!
Lulu, that is my best friend!
Lucy, that is my best friend!
We are all friends.
- 了解range函数,并实现创建列表,解析列表。
#range函数,生成一系列数字,前闭后开
for value in range(1,5):
print(value)
#创建数字列表list函数,range可指定步长
numbers = list(range(1,11,2))
print(numbers)
#三种创建平方列表方法
print("通过循环创建")
squares = []
for value in range(1,11):
square = value**2
#列表末尾插入
squares.append(square)
print(squares)
print("直接插入")
squares = []
for value in range(1,11):
squares.append(value**2)
print(squares)
print("通过列表解析")
#指定一个描述性的列表名,如squares;然后,指定一个左方括号,并定义一个表达式,用于生成你要存储到列表中的值。
squares = [value**2 for value in range(1,11)]
print(squares)
结果:
1
2
3
4
[1, 3, 5, 7, 9]
通过循环创建
[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
直接插入
[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
通过列表解析
[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
- 了解切片并使用切片。
#切片,指定要使用的第一个元素与最后一个元素,与range范围一样前闭后开
friends = ['tom','jack','lulu','lucy']
print(friends[0:2])
print(friends[1:4])
print(friends[:3])
print(friends[1:])
#倒数3个
print(friends[-3:])
print("遍历前三个")
for friend in friends[:3]:
print(friend.title())
#复制列表[:]
my = ['a','b','c','d']
your = my[:];
print("my is ")
print(my)
print("your is ")
print(your)
结果:
['tom', 'jack']
['jack', 'lulu', 'lucy']
['tom', 'jack', 'lulu']
['jack', 'lulu', 'lucy']
['jack', 'lulu', 'lucy']
遍历前三个
Tom
Jack
Lulu
my is
['a', 'b', 'c', 'd']
your is
['a', 'b', 'c', 'd']