python3:基础与数据类型

print

>>> print('hello','lg', 123, sep='*')
hello*lg*123
>>> print('hello','lg', 123, sep='')
hellolg123
>>> print('hello','lg', 123, sep='*',end='********')
hello*lg*123********>>>
>>> print('hello','lg', 123, sep='*',end='\n\n')
hello*lg*123

>>>

input()

a = input(‘please input number:’)
print(“int(a) + 5”)
print(int(a) + 5)
print(a+str(5))
输入5
输出:
int(a)+5
10
55

%s

username = input(‘please input username:’)
print(‘hello’,username,’, welcome to centos-7.5’)
print(‘hello %s,’ %username,‘welcome to centos-7.5’)
输入root
输出:
please input username:root
hello root , welcome to centos-7.5
hello root, welcome to centos-7.5
%s相当于占位符,会被后面的%替换

标准算术运算符

a+=1

a=10
a=a+1 对的 a=11
a+=a 对的 a=11
a++ 错误
++a 不会自增长,也不会报错,它是正正为正,还是等于10

5 / 3

>>>5 / 3
1.66666667
>>>type(5/3)
float类型
>>>5 // 3 只得到商
1
>>>5 % 3 求模,留余数
2
>>>a,b=divmod(5,3)  5除以3的商和余数,分别赋值给a和b
>>>a
1
>>>b
2
>>>2 ** 3 23次方
>>>int(8 / 3 *10 % 10 ) 求小数点后一位
2

比较运算符

>>> 3 == 3
True
>>>3 != 3
False
>>> 10 < 20 < 30 #python支持连续比较
True
>>> 10 < 20 > 15 
True

逻辑运算符

>>> 10 > 5 and 5 > 8 # and两边都要是true才能是真
False
>>> 10 > 5 or 5 > 8  # or两边有一边为真,则结果为真
True
>>> not 10 > 5 #not取反,将真变假,假变真
False
>>> not 10 > 50
True

数据类型

数字

  • int:有符号整数
  • bool:布尔值
    – True:1
    – False:0
  • float:浮点数
  • complex:复数
    0o或0O开头,代表8进制
    0x或0X开头,代表16进制
    0b或0B开头,表示2进制
oct() 108进制
hex() 1016进制
bin() 102进制

字符串

  • python中,字符串必须使用引号引起来,单双引号无区别
  • python支持三引号,用于保留字符串格式
>>> words = """hello
... ni hao
... abc"""
>>> print(words)
hello
ni hao
abc
>>> danci = 'aaa\nbbb\nccc\nddd'
>>> print(danci)
aaa
bbb
ccc
ddd
  • 字符串切片
>>> s1 = 'python'
>>> len(s1)   # 取长度
6
>>> s1[0]  # 取第1个字符
'p'
>>> s1[6]  # 报错,下标最大为5
>>> s1[5]  # 取最后一个字符
'n'
>>> s1[-1]  # 下标为负,表示从右向左取
'n'
>>> s1[-6]
'p'
>>> s1[2:4]  # 取切片,起始下标包含,结束下标不包含
'th'
>>> s1[2:6]  # 取切片时,下标超过范围也不会报错
'thon'
>>> s1[2:6000]
'thon'
>>> s1[2:]  # 结束下标不写,表示取到结尾
'thon'
>>> s1[:2]  # 开始下标不写,表示从开头取
'py'
>>> s1[:]  # 从开头取到结尾
'python'
>>> s1[::2]  # 2表示步长值
'pto'
>>> s1[1::2]  # 起始下标为1的字符开始取,步长为2
'yhn'
>>> s1[::-1]  # 步长值为负,表示自右向左取
'nohtyp'
  • 字符串的拼接和重复
>>> 'abc' + '123'   # 字符串拼接
'abc123'
>>> '*' * 30   # '*'号重复30遍
'******************************'
>>> 'ab' * 5   # 'ab'重复5遍
'ababababab'
>>> '#' * 20
'####################'
  • 成员关系判断
>>> s1
'python'
>>> 't' in s1   # t在s1中吗?
True
>>> 'th' in s1  # th在s1中吗?
True
>>> 'to' in s1  # to在s1中吗?
False
>>> 'to' not in s1   # to不在s1中吗?
True

列表

  • 类似于shell中的数组
  • 列表的大部分操作与字符串一样,可以取下标、切片等
>>> l1 = ['tom', 'jerry', 10, 20, [1, 2, 3]]
>>> len(l1)
5
>>> l1[-1]  # 取下标
[1, 2, 3]>>> l1[2] + 2 # 是数值,不是字符
12
>>> l1[-1] = 30  # 将最后一项重新赋值
>>> l1
['tom', 'jerry', 10, 20, 30]
>>> l1[:2]
['tom', 'jerry']
>>> 10 in l1
True
>>> l1.append(10)  # 在结尾追加10
>>> l1
['tom', 'jerry', 10, 20, 30, 10]
>>> l2 =l1 # l1和l2都指向内存相同的地址
>>> l2.append(40)
>>> l2
['tom', 'jerry', 10, 20, 30, 10,40]
>>> l1 #改变了,如果是字符串就不会变
['tom', 'jerry', 10, 20, 30, 10,40]

# 使列表l1改变,l2不改变,相互独立,需要使用copy.copy,如下:
import copy
a=[1,2,3]
b=copy.copy(a)

元组

  • 跟列表类似,也支持像字符串一样的切片、下标等操作
  • 元组可以认为是不可变的列表
>>> t1 = ('tom', 'jerry', 10, 20)
>>> len(t1)
4
>>> t1[2:]
(10, 20)
>>> t1[0] = 'bob'  # 报错,元组不可变

字典

  • 字典没有顺序
>>> d1 = {'name': 'tom', 'age': 20}
>>> len(d1)
2
>>> 'tom' in d1  # 'tom'是字典的键(key)吗?
False
>>> 'name' in d1
True
>>> d1['name']  # 取出字典中'name'对应的值(value)
'tom'

数据类型分类

按存储模型分类

  • 标量:数字、字符串
  • 容器:列表、元组、字典

按访问模型分类

  • 直接:数字
  • 序列:字符串、列表、元组
  • 映射:字典

按更新模型分类

  • 不可变:数字、字符串、元组
  • 可变:列表、字典
>>> s1 = 'python'
>>> s1[0] = 'P'    # 报错,因为字符串不可变
>>> s1 = 'Python'  # 可以重新赋值
>>> s2 = s1
>>> s2='qwe'
>>> s1 # 得到Python
>>> s2  # 得到qwe
>>> l1 = [10, 20]
>>> l2 = l1        # l1和l2都指向内存相同地址,并且内存是可变的
>>> l2.append(30)  # 修改l2,也会影响到l1
>>> l2
[10, 20, 30]
>>> l1
[10, 20, 30]
>>> id(l1)
>>> id(l2) #内存地址都是一样的
>>> l3=l2.copy()
>>> id(l3) #内存地址不一样,修改也只会改变自己
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值