python基本语法

打印字符串: 

print ("I love python")

"I love"

>>> "I love python"+"and you"

'I love pythonand you'

>>> print("I love python" *2)       // 会打印两遍
I love pythonI love python

>>> "I love python" *2+" "
'I love pythonI love python '


//  \n  为换行符   然而却出现不同的结果

>>> "I love python\n" *3
'I love python\nI love python\nI love python\n'
>>> print ("I love python\n" *3)
I love python
I love python
I love python


不支持  printf ("I love python");

不支持  print  "I love python"

不支持

>>> print (s)
Traceback (most recent call last):
  File "<pyshell#28>", line 1, in <module>
    print (s)
NameError: name 's' is not defined

不支持

>>> I love python
SyntaxError: invalid syntax

不支持 "I love python" + 2

>>> "I love python" + 2

Traceback (most recent call last):

  File "<pyshell#14>", line 1, in <module>

    "I love python" + 2

TypeError: Can't convert 'int' object to str implicitly             // 不能将 int进行隐式的str转换

不支持  

>>> print("I love python" - 0)
Traceback (most recent call last):
  File "<pyshell#18>", line 1, in <module>
    print("I love python" - 0)
TypeError: unsupported operand type(s) for -: 'str' and 'int'

不支持

>>> print("I love python" + 3)
Traceback (most recent call last):
  File "<pyshell#19>", line 1, in <module>
    print("I love python" + 3)
TypeError: Can't convert 'int' object to str implicitly

不支持

>>> print("I love python" / 1)
Traceback (most recent call last):
  File "<pyshell#20>", line 1, in <module>
    print("I love python" / 1)
TypeError: unsupported operand type(s) for /: 'str' and 'int'

不支持

>>> 3*2+""
Traceback (most recent call last):
  File "<pyshell#24>", line 1, in <module>
    3*2+""
TypeError: unsupported operand type(s) for +: 'int' and 'str'


python 进行计算:      + - * /   //   **   %     e(E)位移

+

-

*

/除    除法 会得到float类型的值   例如:3/2 = 1.5    1/1 = 1.0

//整除整除取下整   例如: 3//2 = 1   1.9//1 = 1.0

**密运算(次方) 例如: 2**2 = 4  2**3 = 8

%求余例如:3%2 = 1

e(E)小数点位移 (科学计数方式)  结果为folat   例如:  10e2  = 1000.0    10e-2 = 0.110.0e1 = 100.0

print (3*2)

3+5

print (3+2);

(3+3);

(3+2)

3+2;


不支持   print 3*2

不支持   printf (3+3);

不支持   printf (3+3)

判断符:if     elif    else    >  >=  <  <=   ==  !=   and   or    not

>>> not True 0为 False   其他为True  字符串必须加引号   not为取相反值
False
>>> not 3
False
>>> not 2
False
>>> not 1
False
>>> not 0
True
>>> not 's'
False
>>> not a
Traceback (most recent call last):
  File "<pyshell#53>", line 1, in <module>
    not a
NameError: name 'a' is not defined

while     True     False

判断支持:  3<a<4
三元运算:
>>> x = 4
>>> y = 7
>>> x if x>4 else y
7

断言:assert  4==4   
如果断言错误 会报错
>>> assert 4==5
Traceback (most recent call last):
  File "<pyshell#36>", line 1, in <module>
    assert 4==5
AssertionError


for循环       会自动调用迭代器的 movetonext

>>> a = [1,3,5,7,9]
>>> for i in a:
print(i)

1
3
5
7
9

>>> a = [1,'a',2,'b',3,'c']
>>> for i in a:
print(i)

1
a
2
b
3
c

break   和    continue  和java中作用相同

列表:打了激素的数组   可同时存  int  str  flot  对象  列表 等...

>>> member = []
>>> member
[]
>>> len(member)
0
>>> member.append(2)
>>> member
[2]
>>> member.extend('a')
>>> member
[2, 'a']
>>> b = [1,'b']
>>> member.append(b)
>>> member
[2, 'a', [1, 'b']]
>>> member.extend(b)
>>> member
[2, 'a', [1, 'b'], 1, 'b']
>>> member.insert(0,'插入到第一')
>>> member
['插入到第一', 2, 'a', [1, 'b'], 1, 'b']
>>> len(member)
6

调换顺序:

>>> temp = member[0]
>>> member[0]= member[2]
>>> member[2]= temp
>>> member
['a', 2, '插入到第一', [1, 'b'], 1, 'b']
>>> 

删除:

>>> member.remove("a")
>>> member
[2, '插入到第一', [1, 'b'], 1, 'b']
>>> member.remove(0)
Traceback (most recent call last):
  File "<pyshell#99>", line 1, in <module>
    member.remove(0)
ValueError: list.remove(x): x not in list
>>> del member[0]
>>> member
['插入到第一', [1, 'b'], 1, 'b']
>>> del member                  // 删除全部

pop方法   是出栈    列表是以栈形式存储的

>>> member
['插入到第一', [1, 'b'], 1, 'b']
>>> member.pop()
'b'
>>> member
['插入到第一', [1, 'b'], 1]
>>> 

//  pop方法参数 只能为  索引

>>> member
['插入到第一', [1, 'b'], 1]
>>> member.pop(1)
[1, 'b']
>>> member
['插入到第一', 1]
>>> member.pop('插入到第一')
Traceback (most recent call last):
  File "<pyshell#106>", line 1, in <module>
    member.pop('插入到第一')
TypeError: 'str' object cannot be interpreted as an integer


// 分片取值

>>> member
['插入到第一', 1, 'a', 'b']
>>> member[1:3]
[1, 'a']
>>> member
['插入到第一', 1, 'a', 'b']
>>> member[1,2]
Traceback (most recent call last):
  File "<pyshell#117>", line 1, in <module>
    member[1,2]
TypeError: list indices must be integers or slices, not tuple


>>> member
['插入到第一', 1, 'a', 'b']
>>> member[:2]
['插入到第一', 1]
>>> member[2:]
['a', 'b']
>>> member[:]
['插入到第一', 1, 'a', 'b']


比较:   只比较第一个元素     字符串的话  按ASSIC编码表比较 只比较第一个字符

>>> list1 = [123,456]
>>> list2 = [234,111]
>>> list3 = [123,456]
>>> list1>list2
False
>>> list1<list2 and list1 == list3
True
>>> list1 = ['a','b']
>>> list2 = ['b','c']
>>> list1>list2
False
>>> list1 = ['ab','c']
>>> list2 = ['ba','c']
>>> list1>list2
False
>>> list1<list2
True

加减:

>>> list4 = list1 + list2
>>> list4
['ab', 'c', 'ba', 'c']
>>> list4 = list1 * list2
Traceback (most recent call last):
  File "<pyshell#138>", line 1, in <module>
    list4 = list1 * list2
TypeError: can't multiply sequence by non-int of type 'list'
>>> list4 = list1 - list2
Traceback (most recent call last):
  File "<pyshell#139>", line 1, in <module>
    list4 = list1 - list2
TypeError: unsupported operand type(s) for -: 'list' and 'list'

乘:

>>> list1*3
['ab', 'c', 'ab', 'c', 'ab', 'c']
>>> list1 *= 2
>>> list1
['ab', 'c', 'ab', 'c']
>>> 

成员操作符:

>>> list1
['ab', 'c', 'ab', 'c']
>>> 'a' in list1
False
>>> 'c' in list1
True
>>> 'a' not in list1
True
>>> 

>>> list1
[1, 2, 4, ['a', 'b']]
>>> 'a' in list1
False
>>> 'a' in list1[3]
True
>>> list1[3][1]
'b'

count带参  返回的是  参数在列表中出现的次数

>>> list1.count(4)
1
>>> list1.count('a')
0

index参数在列表中的位置  返回索引值    第二个参数 和第三个参数  是查找的索引范围

>>> list1.index(4)
2
>>> list1.index(0,3)
Traceback (most recent call last):
  File "<pyshell#160>", line 1, in <module>
    list1.index(0,3)
ValueError: 0 is not in list
>>> list1.index(4,0,3)
2

reverse() 反转

>>> list1.reverse()
>>> list1
[['a', 'b'], 4, 2, 1]


sort排序   不带参是默认从小到大   参数的reverse默认为False  改为True时就是倒叙      默认使用的是归并排序(貌似是可以三个参数的)

>>> list1
[['a', 'b'], 4, 2, 1]
>>> list1.sort()
Traceback (most recent call last):
  File "<pyshell#164>", line 1, in <module>
    list1.sort()
TypeError: unorderable types: int() < list()
>>> list1 = [1,4,2,9,0,6,3]
>>> list1.sort()
>>> list1
[0, 1, 2, 3, 4, 6, 9]
>>> list1.sort(reverse=True)
>>> list1
[9, 6, 4, 3, 2, 1, 0]

不支持  str 与 int的比较

>>> list1 = ['a','c','b']
>>> list1.sort()
>>> list1
['a', 'b', 'c']
>>> list1.append(4)
>>> list1
['a', 'b', 'c', 4]
>>> list1.sort()
Traceback (most recent call last):
  File "<pyshell#175>", line 1, in <module>
    list1.sort()
TypeError: unorderable types: int() < str()

sort  排序支持  int  与  float

>>> list1 = [1,4,2,3,9,5,3.2]
>>> list1.sort()
>>> list1
[1, 2, 3, 3.2, 4, 5, 9]

拷贝  与  赋值的区别:

>>> list1
[1, 2, 3, 3.2, 4, 5, 9]
>>> list2 = list1[:]
>>> list3 = list1
>>> list1
[1, 2, 3, 3.2, 4, 5, 9]
>>> list2
[1, 2, 3, 3.2, 4, 5, 9]
>>> list3
[1, 2, 3, 3.2, 4, 5, 9]
>>> list1.sort()
>>> list1.reverse()
>>> list1
[9, 5, 4, 3.2, 3, 2, 1]
>>> list2
[1, 2, 3, 3.2, 4, 5, 9]
>>> list3
[9, 5, 4, 3.2, 3, 2, 1]

>>> list4 = list1.copy()
>>> list4
[9, 5, 4, 3.2, 3, 2, 1]
>>> list1.sort()
>>> list4
[9, 5, 4, 3.2, 3, 2, 1]
>>> list1
[1, 2, 3, 3.2, 4, 5, 9]

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值