目录
一、环境
Python 3.9.2:https://www.python.org/downloads/release/python-392/
IDE:PyCharm Community Edition 2020.3.3 x64
https://www.jetbrains.com/pycharm/download/download-thanks.html?platform=windows&code=PCC
或 IDLE Shell 3.9.2
二:变量、字符串、操作符
-
变量
注意点:
^o^ 程序可以在任何时间对变量重新赋值
^o^ 变量名区分大小写
>>> a = 12
>>> a
12
>>> print(a)
12
>>> file = open('C:/Users/pql/Desktop/filename.txt', 'w')
>>> file.write('hello world!')
12
file = open('C:\Users\pql\Desktop\filename.txt', 'w')
File "<stdin>", line 1
file = open('C:\Users\pql\Desktop\filename.txt', 'w')SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \UXXXXXXXX escape
ANS:
将“\”换为“/”(https://blog.csdn.net/caibaoH/article/details/78335094)
-
字符串
字符串可以用单引号/双引号
what_he_does = 'plays'
his_instrument = 'guitar'
his_name = 'Robert Johnson'
artist_intro = his_name + what_he_does + his_instrument
print(artist_intro)
num = 1
string = '1'
print(num + string)
提示:
E:\PycharmProjects\pythonProject\venv\Scripts\python.exe E:/PycharmProjects/pythonProject/main.py
Traceback (most recent call last):
File "E:\PycharmProjects\pythonProject\main.py", line 27, in <module>
Hi, PyCharm
Robert Johnsonplaysguitar
Robert Johnsonplaysguitar
print(num + string)
TypeError: unsupported operand type(s) for +: 'int' and 'str'
int和str类型不同不能一起
🙃type() :来查看类型
//查看类型
num = 1
string = '1'
print(type(num))
输出:<class 'int'>
//数据转换
num = 1
string = '1'
print(type(num))
num2 = int(string)
print(num + num2)
输出:<class 'int'>
2
🙃字符串拼接 & 长字符串
words = 'words' * 3
print(words)
word = 'a loooooooooong word'
num = 12
string = 'bang!'
total = string *(len(word) - num)
print(total)
输出:
wordswordswords
bang!bang!bang!bang!bang!bang!bang!bang!
>>> xx = '5'
>>> yy = '3'
>>> xx +yy
'53'
如果希望得到一个跨多行的字符串,需要使用一种特殊的字符串——“三重引号字符串” ,所用引号可以是双引号/单引号
>>> long_string = '''ddddddddddddddddddddddddddddddddddddddddddd,
cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc,
qwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwdassssssssssd,dddddd
ddddddaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa3wccccccccccccccccccc'''
>>> print(long_string)
ddddddddddddddddddddddddddddddddddddddddddd,
cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc,
qwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwdassssssssssd,dddddd
ddddddaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa3wccccccccccccccccccc
🙃字符串的分片与索引
name = 'My name is Mike'
print(name[0])
print(name[-4])
print(name[11:14])
print(name[11:15])
print(name[5:])
print(name[:5])
输出:
M
M
Mik
Mike
me is Mike
My na
wor = 'friends'
find_the_evil_in_your_friends = wor[0] + wor[2:4] + wor[-3:-1]
print(find_the_evil_in_your_friends)
输出:
fiend
🙃字符串遮挡——replace()
phone_number = '1386-666-0006'
hiding_numebr = phone_number.replace(phone_number[:9], '*' * 9)
print(hiding_numebr)
输出:
*********0006
假设模拟手机通讯簿中的电话号码联想功能
search = '168'
num_a = '1386-168-0006'
num_b = '1681-222-0006'
print(search + 'is at' + str(num_a.find(search)) + ' to ' + str(num_a.find(search) + len(search)) + ' of num_a ')
print(search + 'is at' + str(num_b.find(search)) + ' to ' + str(num_b.find(search) + len(search)) + ' of num_b ')
输出:
168is at5 to 8 of num_a
168is at0 to 3 of num_b
🙃字符串填空——format()
city = input("write down the name of city:")
url1 = "http://aoistore.baidu.com/microservice/weather? citypinyin = {}".format(city)
-
操作符
补充python中需要转移的操作符。
T_T 整除
T_T 求幂 与E记法
3的5次幂
>>> print(3 ** 5)
243
E记法
>>> c = 2.6e33
>>> d = 9.4e12
>>> print(c + d)
2.6e+33
T_T 取余
>>> print(4%3)
1
T_T 自增自减 += 、 -=
三、数据类型
3.1 改变类型
- float():从一个字符串或整数创建一个新的浮点数(小数)
- int(): 从一个字符串或浮点数创建一个新的整数
- str():从一个数(可以是任何其他类型)创建一个新的字符串
>>> a = 24
>>> b = float(a)
>>> a
24
>>> b
24.0
>>> c = 38.0
>>> d =int(c)
>>> c
38.0
>>> d
38
>>> e = 54.99
>>> f = int(e)
>>> f
54
>>> e
54.99
>>> a = '76.3'
>>> b = float(a)
>>> b
76.3
>>> a
'76.3'
>>>
3.2 type() —— 明确告诉变量的类型
>>> a = '44.2'
>>> b = 44.2
>>> type(a)
<class 'str'>
>>> type(b)
<class 'float'>
四、输入与输出
4.1输入------ input()
- 一般输入是字符串类型
>>> answer = input()
ss
>>> type(answer)
<class 'str'>
-
将输入的类型转变为整型
guess = int(input("what is yer guess!"))
- 来自互联网的输入
import urllib.request
file = urllib.request.urlopen('http://helloworldbook2.com/data/message.txt')
message = file.read()
print(message)
4.2输出------print()
五、注释
5.1 单行注释
添加 “#”符号就可以把它变成一个注释
>>> #这是一个注释
>>> print("this is not a comment")
this is not a comment
5.2行末注释
>>> area = leng * width #计算矩形面积
5.3 多行注释
- 每行前面添加 # 字符
>>> # ***************
# 这个程序用来说明 Python 中如何使用注释
# 星号所在的行只为将注释
# 与其余代码清楚地区分开
# ***************
- 三重引号字符
参考:编程小白的第一本Python入门书
父与子的编程之旅