马士兵python_马士兵:python学习(一)

python学习

一. 输出函数print(P6)

1. 输出数字和字符串

print(520)

print(52.01)

print("hello world")

print(hello world) #报错

2. 输出表达式:结果

print(3+2)

3. 输出到到文件夹的文件(pythonStudy文件夹里的text.txt)

注意:

① a+: 读写的方式创建文件。没同名文件创建,有的话在文件内容后面追加

② file = 必加

fp = open('c:/pythonStudy/text.txt', 'a+')

print("hello wold", file = fp)

fp.close()

4. 不换行输出

print("hello", "world", "!")

hello world !

二. 转义字符(P7)

1. 定义

\ +转义字符的首字母 转义字符

2. 常见用法

名称用法反斜杠\\

单引和双引\’ 和 \"

换行\n

return\r

水平制表符\t

退格\b

print('hello\n world') #换行

hello

_world

#水平制表符

print('hello\tworld') #\t三个空格,补齐八个

print('helloooo\tworld')#四个空格,补齐12个

hello___world

helloooo____world

#\r

print('hello\rworld') # return 回到开头 然后

print('hello\rwo')

world

wo

#反斜杠

print('http:\\\\www.baidu.com') #要四个反斜杠,两个输出一个

http:\\www.baidu.com

3. 原字符(不希望转意字符起作用,在字符串前加r或R)

print(r"hello\nworld")

hello\nworld

三.二进制与字符编码(P8)

八位(8bit)= 1 字节(byte 或者 B)

汉字在计算机里也归于字符,每个汉字对应了一个数字,数字可以是十进制 十六进制 二进制等等…但最终在计算机里都会变为二进制被计算机认识

二进制0b 八进制0o

四. python中的标识符与保留字

name = ('杨不鸽')

print("标识", id(name)) #存放地址

print('value', name)

print('类型', type(name))

标识 2255127658736

value 杨不鸽

类型

2.多次赋值会指向新的空间(旧空间为空间垃圾)

name = ('杨不鸽')

name = ('杨鸽子了!')

print(name)

杨鸽子了!

五. python中的变量和数据类型

1.常用类型

int

float

bool

str

2. 浮点数存储不精确

n1= 3.2

n2= 3.1

print(n1 + n2)

6.300000000000001

解决:通过模块decimal

from decimal import Decimal

print(Decimal('3.2') + Decimal('3.1'))

6.3

*浮点数不是所有相加都不准备,有的是准确的

3.单引号和双引号只能在一行使用,分行会报错;三引号字符串可以分布在连续的多行

print('''杨不鸽

还是

杨哥''')

杨不鸽

还是

杨哥

4.数字类型转换(拼接的时候用):str( )和int( )

name = '杨鸽'

age = 18

print(name + age) #报错

print(name + str(age))

杨鸽18

5. 转int( )

s1 = '128'

f1 = 98.7

s2 = '98.7'

ff = True

s3 = 'hello'

print (type(s1), type(f1), type(s2), type(ff),type(s3))

print(int(s1))

print(int(f1)) #float转int 小数部分抹去

#print(int(s2))

print(int(ff))

#print(int(s3)) #str转int 必须是数字

128

98

1

6. 转浮点型float()

(1) 数字符串中的非数字串没办法转成float

(2) 整数转的时候后面+ .0

六. python中的注释

1. 单行注释

:#开头 直到换行

2. 多行注释

:并没有单独的多行注释表寄,将一对三引号之间的代码成为多行注释

3. 中文编码声明注释

:在文件开头加上中文声明的注释,用以指定源码文件的编码格式(python3不需要写了)

笔记总结于马士兵python基础学习视频:https://www.bilibili.com/video/BV1wD4y1o7AS

原文链接:https://blog.csdn.net/wistonty11/article/details/108650340

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值