Python基础--变量和字符串/猜数字/range()/Isinstance/list[]/tupple()

Posted 吴英强

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Python基础--变量和字符串/猜数字/range()/Isinstance/list[]/tupple()相关的知识,希望对你有一定的参考价值。

变量和字符串

>>> name = 'wuyq'
>>> name
'wuyq'
>>> print(name)
wuyq
>>> print('name = %s', % name)
SyntaxError: invalid syntax
>>> print('name = %s' % name)
name = wuyq
>>> print('name = %s',  name)
name = %s wuyq
>>> 


>>> first = 3
>>> second = 5
>>> third = first + second
>>> third
8
>>> 3+4
7
>>> '2'+'4'
'24'
>>> first
3
>>> str1 = 'Let\\'s go!'
>>> str1
"Let's go!"
>>> str2 = "c:\\\\day01"
>>> str2
'c:\\\\day01'
>>> print(str2)
c:\\day01
>>> str2 = 'c:\\now'
>>> str2
'c:\\now'
>>> print(str2)
c:
ow
>>> str2 = r'c:\\now'
>>> str2
'c:\\\\now'
>>> print(str2)
c:\\now
>>> 

猜数字小游戏

import random

secret = random.randint(1, 10)
temp = input("inut guess num[1-10]:")
guess = int(temp)

while guess != secret:
    if guess == secret:
        print(" wa, cai zhong le ")
        break
    else:
        if guess > secret:
            print("da le da le ")
        else:
            print("xiao le")
    temp = input("inut guess num[1-10]:")
    guess = int(temp)
print("game over ")

Range()

range(a, b)返回的是所有大于等于a,小于b的整数构成的数组

-比如range(2, 3)返回数组[2]range(2, 4)返回数组[2, 3]

 

 -另外,range()也可以接收1个和3个参数

 

  1个参数代表返回0到这个参数(不包括该参数)之间的值,如range(3) = [0, 1, 2]

 

   3个参数中的第三个代表步长,即间隔, 如range(1, 8, 2) = [1, 3, 5, 7]

Isinstance

>>> a = 'wuyq'
>>> isinstance(str, a)
Traceback (most recent call last):
  File "<pyshell#170>", line 1, in <module>
    isinstance(str, a)
TypeError: isinstance() arg 2 must be a type or tuple of types
>>> isinstance(a, str)
True
>>> 

列表list[]

>>> member = ['wuyq', 'zhang', 'wang', 'cheng']
>>> member
['wuyq', 'zhang', 'wang', 'cheng']
>>> number = [1, 2, 3, 4]
>>> number
[1, 2, 3, 4]
>>> mix = [12, 3.12, 'wuyq', [1, 3, 4], 'haha']
>>> mix
[12, 3.12, 'wuyq', [1, 3, 4], 'haha']
>>> mix[3]
[1, 3, 4]
>>> empty = []
>>> empty
[]
>>> member.append('nihao')
>>> member
['wuyq', 'zhang', 'wang', 'cheng', 'nihao']
>>> len(member)
5
>>> member.extend(['one', 'two'])
>>> len(member)
7
>>> member.insert(1, 'second')
>>> member
['wuyq', 'second', 'zhang', 'wang', 'cheng', 'nihao', 'one', 'two']
>>> len(member)
8
>>> 

>>> member = ['wuyq', 'zhang', 'wang']
>>> member
['wuyq', 'zhang', 'wang']
>>> member.remove('zhang')
>>> member
['wuyq', 'wang']
>>> del member[1]
>>> member
['wuyq']
>>> 

>>> member = ['wuyq', 'zhang', 'wang', 'liu']
>>> member.pop()
'liu'
>>> member
['wuyq', 'zhang', 'wang']
>>> name = member.pop()
>>> name
'wang'
>>> member
['wuyq', 'zhang']
>>> member.pop(1)
'zhang'
>>> member
['wuyq']
>>> 

>>> member = ['wuyq', 'zhang', 'wang', 'liu', 'haha']
>>> member[1:3]
['zhang', 'wang']
>>> member[1:-2]
['zhang', 'wang']
>>> member
['wuyq', 'zhang', 'wang', 'liu', 'haha']
>>> member2 = member[:]
>>> member2
['wuyq', 'zhang', 'wang', 'liu', 'haha']
>>> 

>>> number = [1, 3, 4, 5, 12 , 1, 3, 4]
>>> number.count(3)
2
>>> number.index(4)
2
>>> number.index(1, 2, 7)
5
>>> number.reverse()
>>> number
[4, 3, 1, 12, 5, 4, 3, 1]
>>> number.sort()
>>> number
[1, 1, 3, 3, 4, 4, 5, 12]
>>> number2 = number[:]
>>> number2
[1, 1, 3, 3, 4, 4, 5, 12]
>>> number3 = number
>>> number2
[1, 1, 3, 3, 4, 4, 5, 12]
>>> number3
[1, 1, 3, 3, 4, 4, 5, 12]
>>> number.reverse()
>>> number
[12, 5, 4, 4, 3, 3, 1, 1]
>>> number2
[1, 1, 3, 3, 4, 4, 5, 12]
>>> number3
[12, 5, 4, 4, 3, 3, 1, 1]
>>> 

元组tuple()

元组是另一个数据类型,类似于List(列表)。

元组用"()"标识。内部元素用逗号隔开。但是元组不能二次赋值,相当于只读列表。

>>> tuple1 = (1, 2, 3, 4, 5, 6, 7, 8)
>>> tuple1
(1, 2, 3, 4, 5, 6, 7, 8)
>>> tuple1[2]
3
>>> tuple1[2:5]
(3, 4, 5)
>>> tuple2 = tuple1
>>> tuple2
(1, 2, 3, 4, 5, 6, 7, 8)
>>> tuple2[3] = 12
Traceback (most recent call last):
  File "<pyshell#138>", line 1, in <module>
    tuple2[3] = 12
TypeError: 'tuple' object does not support item assignment
>>> tmp = (1)
>>> tmp
1
>>> type(tmp)
<class 'int'>
>>> tmp2 = 2, 3, 4
>>> type(tmp2)
<class 'tuple'>
>>> list1 = []
>>> type(list1)
<class 'list'>
>>> tmp3 = ()
>>> type(tmp3)
<class 'tuple'>
>>> tmp4 = (2, )
>>> type(tmp4)
<class 'tuple'>
>>> 

>>> tmp = ('wuyq', 'zhang', 'wuang', )
>>> tmp = ('wuyq', 'zhang', 'wuang', 'xiao', 'python', 'cpp')
>>> tmp
('wuyq', 'zhang', 'wuang', 'xiao', 'python', 'cpp')
>>> tmp = tmp[:2] + 'java' + tmp[2:]
Traceback (most recent call last):
  File "<pyshell#153>", line 1, in <module>
    tmp = tmp[:2] + 'java' + tmp[2:]
TypeError: can only concatenate tuple (not "str") to tuple
>>> tmp = tmp[:2] + ('java',) + tmp[2:]
>>> tmp
('wuyq', 'zhang', 'java', 'wuang', 'xiao', 'python', 'cpp')
>>> 














以上是关于Python基础--变量和字符串/猜数字/range()/Isinstance/list[]/tupple()的主要内容,如果未能解决你的问题,请参考以下文章

python任务2

python游戏练手--猜数字

Python3基础学习-while循环实例- 猜数字游戏

python基础之猜数字游戏

Python基础练习-001-猜数字小游戏

Python基础入门-实现猜数字小游戏