Python基础

In [1]: #Bool

In [2]: True
Out[2]: True

In [3]: False
Out[3]: False

In [4]: 3>2
Out[4]: True

In [5]: 3>5
Out[5]: False

In [6]: # and or not ,&& || !

In [7]: True and True
Out[7]: True

In [8]: True and False
Out[8]: False

In [9]: 5>3 and 3>1
Out[9]: True

In [10]: # or

In [11]: True or True
Out[11]: True

In [12]: True or False
Out[12]: True

In [13]: 5>3 or 3>1
Out[13]: True

In [14]: 5>3 or 3<1
Out[14]: True

In [15]: # not

In [16]: not True
Out[16]: False

In [17]: not False
Out[17]: True

In [18]: 1>2
Out[18]: False

In [19]: not 1>2
Out[19]: True

In [20]: None

In [21]: a = None

In [22]: type(a)
Out[22]: NoneType

In [23]: a = ‘abc’

In [24]: a = 123

In [25]: x = 10

In [26]: x = x + 2

In [27]: x
Out[27]: 12

In [28]: # 常量

In [29]: PI = 3.1415926

In [42]: 10 / 3
Out[42]: 3.3333333333333335

In [43]: 9 / 3
Out[43]: 3.0

In [44]: 10 // 3
Out[44]: 3

In [45]: 10 % 3
Out[45]: 1

In [46]: 2**2
Out[46]: 4

In [47]: 2**3
Out[47]: 8

In [67]: # 字符串

In [68]: str1 =‘hello python’

In [69]: str1 =‘hello I’m python’
File “”, line 1
str1 =‘hello I’m python’
^
SyntaxError: invalid syntax

In [70]: str1 =‘hello I’m python’

In [71]: str1
Out[71]: “hello I’m python”

In [72]: str1 =r’hello I’m python’

In [73]: str1
Out[73]: “hello I\'m python”

In [74]: # 3种字符串拼接方式

In [75]: # +

In [76]: str2 = ‘北京’ + ‘你好’

In [77]: str2
Out[77]: ‘北京你好’

In [78]: # list拼接

In [79]: list_str= [‘ni’,‘hao’,‘bei’,‘jing’]

In [80]: str2 = ‘’.join(list_str)

In [81]: str2
Out[81]: ‘nihaobeijing’

In [82]: str2 = ’ '.join(list_str)

In [83]: str2
Out[83]: ‘ni hao bei jing’

In [84]: # format 方式拼接字符串

In [85]: str3 = ‘我喜欢{},因为我需要{}’.format(‘python’,‘赚钱’)

In [86]: str3
Out[86]: ‘我喜欢python,因为我需要赚钱’

In [87]: s = ‘abcdef’

In [88]: s[0]
Out[88]: ‘a’

In [89]: s[1]
Out[89]: ‘b’

In [97]: # 切片

In [98]: s =‘abcdefgh’

In [99]: s[0:3]
Out[99]: ‘abc’

In [100]: s[:3]
Out[100]: ‘abc’

In [101]: s[2:]
Out[101]: ‘cdefgh’

In [102]: s[:-1]
Out[102]: ‘abcdefg’

In [103]: s[::2]
Out[103]: ‘aceg’

In [119]: # 面试题 a= ‘abcdef’ b=‘fedcba’

In [120]: a= ‘abcdef’

In [121]: a[::-1]
Out[121]: ‘fedcba’

In [122]: str2 = a.replace(‘cd’,‘你好’)

In [123]: str2
Out[123]: ‘ab你好ef’

In [124]: str2.find(‘你’)
Out[124]: 2

In [125]: str2.index(‘你’)
Out[125]: 2

In [126]: str3 = ‘abbbccc’

In [127]: str3.count(‘b’)
Out[127]: 3

In [128]: str4 = ‘小红,小白,小花’

In [129]: str4.split(’,’)
Out[129]: [‘小红’, ‘小白’, ‘小花’]

In [133]: # list有序集合

In [134]: classmates = [‘yh’,‘xiaobai’,‘xiaoming’]

In [135]: type(classmates)
Out[135]: list

In [136]: len(classmates)
Out[136]: 3

In [137]: classmates[1]
Out[137]: ‘xiaobai’

In [138]: classmates[-1]
Out[138]: ‘xiaoming’

In [139]: classmates.append(‘xiaohong’)

In [140]: classmates
Out[140]: [‘yh’, ‘xiaobai’, ‘xiaoming’, ‘xiaohong’]

In [141]: classmates.insert(1,‘ww’)

In [142]: classmates
Out[142]: [‘yh’, ‘ww’, ‘xiaobai’, ‘xiaoming’, ‘xiaohong’]

In [143]: classmates.pop()
Out[143]: ‘xiaohong’

In [144]: classmates
Out[144]: [‘yh’, ‘ww’, ‘xiaobai’, ‘xiaoming’]

In [145]: classmates.pop(1)
Out[145]: ‘ww’

In [146]: classmates
Out[146]: [‘yh’, ‘xiaobai’, ‘xiaoming’]

In [147]: classmates[2]=‘xiaogou’

In [148]: classmates
Out[148]: [‘yh’, ‘xiaobai’, ‘xiaogou’]

In [149]: s = [‘python’,‘java’,[‘asp’,‘php’],‘c++’]

In [165]: # 元组

In [166]: t = (‘a’,‘b’,‘c’)

In [167]: t[1]=‘b’

TypeError Traceback (most recent call last)
in ()
----> 1 t[1]=‘b’

TypeError: ‘tuple’ object does not support item assignment

In [168]: t=(1)

In [169]: type(t)
Out[169]: int

In [170]: t=(1,)

In [171]: type(t)
Out[171]: tuple

In [172]: # 面试题

In [173]: l =[‘A’,‘B’]

In [174]: t =(‘a’,‘b’,l)

In [175]: t[2][0]=‘C’

In [176]: t
Out[176]: (‘a’, ‘b’, [‘C’, ‘B’])

In [177]: l1 = [‘A’,‘B’]

In [178]: t[2]=l1

TypeError Traceback (most recent call last)
in ()
----> 1 t[2]=l1

TypeError: ‘tuple’ object does not support item assignment

In [179]: # 字典

In [180]: names = [‘yh’,‘xiaohong’,‘xiaoming’]

In [181]: scores = [90,79,87]

In [182]: d = {‘yh’:90,‘xiaohong’:79,‘xiaoming’:87}

In [183]: d[‘xiaohong’]
Out[183]: 79

In [184]: d[‘xiaogou’]=34

In [185]: d
Out[185]: {‘yh’: 90, ‘xiaohong’: 79, ‘xiaoming’: 87, ‘xiaogou’: 34}

In [186]: d[‘xiaogou’]
Out[186]: 34

In [187]: d[‘xiaogou’]=54

In [188]: d
Out[188]: {‘yh’: 90, ‘xiaohong’: 79, ‘xiaoming’: 87, ‘xiaogou’: 54}

In [189]: ‘yh’ in d
Out[189]: True

In [190]: ‘Thomas’ in d
Out[190]: False

In [191]: d[‘Thomas’]

KeyError Traceback (most recent call last)
in ()
----> 1 d[‘Thomas’]

KeyError: ‘Thomas’

In [192]: d.get(‘Thomas’)

In [193]: d.get(‘Thomas’,-1)
Out[193]: -1

In [194]: d.pop(‘xiaogou’)
Out[194]: 54

In [195]: d
Out[195]: {‘yh’: 90, ‘xiaohong’: 79, ‘xiaoming’: 87}

In [196]: l =[]

In [197]: d[l]=‘fdfd’

TypeError Traceback (most recent call last)
in ()
----> 1 d[l]=‘fdfd’

TypeError: unhashable type: ‘list’

In [201]: # set集合

In [202]: s = set([1,2,3])

In [203]: s
Out[203]: {1, 2, 3}

In [204]: s = set([1,1,2,2,2,3,3])

In [205]: s
Out[205]: {1, 2, 3}

In [206]: s.add(4)

In [207]: s
Out[207]: {1, 2, 3, 4}

In [208]: s.add(3)

In [209]: s
Out[209]: {1, 2, 3, 4}

In [210]: s.remove(4)

In [211]: s
Out[211]: {1, 2, 3}

In [212]: s1 = set([1,2,3])

In [213]: s2 = set([2,3,4])

In [214]: s1 & s2
Out[214]: {2, 3}

In [215]: s1 | s2
Out[215]: {1, 2, 3, 4}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值