Python Learning Note 2

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.