Again the study is a Thu God, the website posted
http://nbviewer.jupyter.org/github/lijin-THU/notes-python/tree/master/
I'm just a little bit familiar with the basics of other programming languages.
Chat 2 Part2 Some function about number and string
import syssys.maxint# 求最大int
Python uses the J-open to represent complex numbers
a = 1 + 2jtype(a)a.reala.imaga.conjugate()
Python Rounding Method
int()# 向下取整round()# 四舍五入ceil()# 向上取整
Multi-input system
1e-60xFF067# 八进制0b101010
String Segmentation method
line = "1 2 3 4 5"numbers = line.split()print numbers# 有分割就有合并s = ‘ ‘s.join(numbers)
String substitution
s = "hello world"s.replace(‘world‘, ‘python‘)
Other string operations
The S.upper () method returns a new string that capitalizes all the letters in S
The S.lower () method returns a new string that capitalizes all the letters in S
S.strip () returns a new string that removes extra spaces at both ends of s
S.lstrip () returns a new string that removes the extra space beginning with s
S.rstrip () returns a new string that removes the extra space at the end of S
s = "123 2341"dir(s)# 查看s可以做的操作
When the code is too long or for the sake of aesthetics, there are two ways to convert a line of code to multiple lines of code:
As follows
a = ("hello, world. " "it‘s a nice day. " "my name is basasuya")a = "hello, world. " "it‘s a nice day. " "my name is basasuya"
Integers can be converted into different types of strings in different ways
hex(255)oct(255)# 八进制bin(255)int(‘23‘)float(‘23‘)int(‘FF‘, 16)# 指定原本是什么进制
Python formatted string
‘{} {} {}‘.format(‘a‘, ‘b‘, ‘c‘)‘{2} {1} {0}‘.format(‘a‘, ‘b‘, ‘c‘)# 数字代表在format当中的第几个‘{color} {n} {x}‘.format(n=10, x=1.5, color=‘blue‘)# 字符相当于map‘{0:10} {1:10d} {2:10.2f}‘.format(‘foo‘, 5, 2 * 3.14)‘{0:10} {1:10d} {n:10.2f}‘.format(‘foo‘, 5, 2 * 3.14, n=12)
s = "some numbers:"x = 1.34y = 2# 用百分号隔开,括号括起来t = "%s %f, %d" % (s, x, y)
The string in python has a very interesting phenomenon.
Negative numbers represent the characters of the following numbers
s = "hello world"s[-2]# s[11]# 大于长度的字符串却是不行的
s[:]->‘hello world‘s[::2]hlowrd# 每隔2个字符取一次
Python Learning Note 2