函数
1. Print()函数的使用
-
1.1 print()的输出内容
-
输出数字
-
输出字符串
-
含有运输符的表达式:输出运算结果
-
将数据输出到文件中
(Notes: 1.指定地址存在 2.print输出注意格式)
#输出数字
print(520)
print(95.5)
#输出字符串
print('hello world')
print("hello world\n")
#含有运输符的表达式:输出运算结果
print(3+1)
#将数据输出到文件中Notes:1.指定地址存在2.print输出注意
fp = open('D:/test.txt','a+')#读写方式打开,文件存在追加,不存在创建
print('hello world',file=fp)
fp.close()
-
1.2 print的返回值
与C中Print()函数返回字符个数不同,Python中print()不存在返回值。
2. input函数
2.1 input函数的使用
2.2 input函数的高级使用
转义字符
#输出字符串
print('hello world')
print("换行:hello\nworld")#换行
print('水平制表符: 123456world')#水平制表符\t最大为4个字符长度
print('水平制表符: \thello\tworld')
print('水平制表符:helloooo\tworld')
print('回车:hello\rworld')#回车
print('退格:hello\bworld')#退格
print('http:\\\\www.baidu.com')
print('老师说:\'大家好\'')
#原字符‘r',字符串中转义字符不起作用,Notes(最后一个字符不能是’\')
print(r"换行:hello\nworld")
Python中的标识符和保留字
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']
变量的定义和使用
-
变量的组成
- 标识:内存地址,使用id()函数
- 类型:变量类型,使用type()函数
- 值
-
多次赋值
多次赋值后会指向新的空间
name = '杨斌'
print(name)
print('标识',id(name))
print('type',type(name))
print('值',name)
name = 'YangBin'
print('标识',id(name))
name = '杨斌'
print('标识',id(name))
数据类型
1. 常用的数据类型
-
整数类型
-
二进制 前面加0b
-
八进制 前面加0o
-
十六进制 前面加0x
-
-
浮点数类型
-
布尔类型
-
字符串类型
2. 类型转换
注释
运算符
-
算术运算符
-
赋值运算符
-
比较运算符
-
布尔运算符
-
位运算
-
运算符的优先级