搭建python开发环境
安装python解释器
#python官网
https://www.python.org
#python解释器下载地址
https://www.python.org/downloads/release/python-381


第一个python程序

Pycharm的下载安装
#安装教程
https://blog.csdn.net/lifulian318/article/details/120096284
使用Pycharm开发
创建一个项目

创建一个python文件


运行报错,原因是 没有解释器,编译环境不行,手动添加一下即可。


再次运行,成功!
输出字符串print
print(2)
print(21.3)
print("123")
print('1231231')
print(1 + 1)
fp = open('D:/demo.txt', 'a+')
print('hello', file=fp)
fp.close()
print('hello','world','python')
转义字符
print('hello\nworld')
print('hello\tworld')
print('helloooo\tworld')
print('hello\rworld')
print('hello\bworld')
print('http:\\\www.baidu.com')
print('说:\'hello\'')
print(r'hello\nworld')
print('hello\\')

二进制与字符编码

print(chr(0b100111001011000))
print(ord('乘'))
标识符和保留字
import keyword
print(keyword.kwlist)
#保留字:
['False', 'None', 'True', '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']
变量、函数、类、模块和其他对象的命名就叫标识符
命名规则:
- 字母、数字、下划线
- 不能以数字开头
- 不能是保留字
- 严格区分大小与
变量是内存中一个带标签的盒子
如:name = ‘lizhenwei’
name = 'lizhenwei'
print(name)
print('标识', id(name))
print('类型', type(name))
print('值', name)
name = 'merry'
print(name)
数据类型
常用的数据类型:
- 整数类型 (int : 98)
- 浮点数类型 (float : 3.1415)
- 布尔类型 (bool : True, False)
- 字符串类型 (str : ‘倾向于python’)
整数类型
n1 = 20
n2 = -20
n3 = 0
print(n1, type(n1))
print(n2, type(n2))
print(n3, type(n3))
print('十进制', 110)
print('二进制', 0b11011001000)
print('八进制', 0o17654)
print('十六进制', 0x1EFA09)

浮点类型
a = 1.345287
print(a, type(a))
print(1.1 + 2.2)
print(1.1 + 2.1)
from decimal import Decimal
print(Decimal('1.1') + Decimal('2.2'))
布尔类型
b1 = True
b2 = False
print(b1, type(b1))
print(b2, type(b2))
print(True + 1)
print(False + 1)
字符串类型
str1 = '我是中国人'
str2 = "我是中国人"
str3 = """我是中国人,
我骄傲!"""
str4 = '''我是中国人,
我骄傲'''
print(str1, type(str1))
print(str2, type(str2))
print(str3, type(str3))
print(str4, type(str4))
我是中国人 <class 'str'>
我是中国人,
我骄傲! <class 'str'>
我是中国人,
我骄傲 <class 'str'>
数据类型转换
name = '张三'
age = 20
print(type(name), type(age))
print('我叫' + name + ',今年' + str(age) + '岁')
print(str(123), int('123'), int(9.8), float('9.9'), float(9))

注释
'''多行注释
并没有单独的多行注释标记,将一对三引号之间的代码称为多行注释
'''

|