Python学习第一天
print函数的用法
- 输出字符串或数字
print([string])
或者print([number])
- 输出到文件中
fp = open("out.txt","a+") # 以追加模式打开
print([something],file=fp)
fp.close()
转义字符:
\n
\t
\r
\b
\\
\'
\"
等
\t
的输出规则:
print('hello\tworld')
print('hellooo\tworld')
print('helloooo\tworld')
####
hello world
hellooo world
helloooo world
把\t
前的字符串称为A,后的字符串称为B,
四个字符为一个tab,A的长度不是\t
的整数倍时,那么下一个\t
开始输出B;如果是,那么就先输出一个\t
,然后输出B。
标识符和保留字
标识符的话与其他语言一样
保留字也没必要记
想要查看保留的话,
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']
变量
查看变量的地址:id([variable])
查看变量的类型:type([variable])
变量的值:[variable]
x = 20
print("变量地址", id(x))
print("变量类型", type(x))
print("变量值", x)
####
变量地址 4300716944
变量类型 <class 'int'>
变量值 20
数据类型
- 整数 int
不同进制的表现形式,默认十进制
- 二进制 0b____
- 八进制 0o____
- 十六进制 0x____ - 浮点数 float
解决浮点数运算不精确的方案之一
from decimal import Decimal
print(Decimal('1.1')+Decimal('2.2'))
####
3.3
- 字符串 str
- 布尔 bool
可参与运算
- True 1
- False 0
数据类型转换
int(float, bool)->str:
age = 20
sage = str(age)
str->int,float->int,bool->int:
x = int('123')
x = int(4.13)
str->float,int->float,bool->float:
f = float('3.14')
f = float(3)