Topic 3:An Informal Introduction to Python

1. hash character # starts the comment

2. Division always returns a float.

3. floor division '//' discards the fractional part

'%'

4. power **

5. '_' acts as the 'ans' in matlab

6. single quotes and double quotes is the same 

'' ""

\ can be used to escape quotes as is shown bellow

"\"Yes.\" he said."

'"Yes." he said.'

7. You can use raw string by adding an r before the first quote to prevent characters from being interpreted as special characters.

>>> print('C:\some\name')  # here \n means newline!
C:\some
ame
>>> print(r'C:\some\name')  # note the r before the quote
C:\some\name
8. String literals can span multiple lines using '''...''' or """...""". 

End of lines are automatically included in the string but it’s possible to prevent this by adding a \ at the end of the line.

print("""\
Usage: thingy [OPTIONS]
     -h                        Display this usage message
     -H hostname               Hostname to connect to
""")
9.String can be concatenated and repeated

3*"hello"+"world"

10."hel" "llo"="hello"

Note: this only works with two literals though, not with variables or expressions. 

+ and * can do some help

11. This feature is particularly useful when you want to break long strings:

>>> text = ('Put several strings within parentheses '
            'to have them joined together.')
>>> text
'Put several strings within parentheses to have them joined together.'

12. String can be indexed (subscripted), with the first character have index 0

 +---+---+---+---+---+---+
 | P | y | t | h | o | n |
 +---+---+---+---+---+---+
 0   1   2   3   4   5   6
-6  -5  -4  -3  -2  -1
13. Python string is immutable. 

If you need to change you can just create a new one .

14. List: compound data types. Can be indexed and changed. 

a[2:5]=[] this help to delete.

Nest lists is possible.

a=[1,2,3]

b=['a','b','c']

c=[a,b]

c[0][1]=2



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值