Article 1: Foundation

%

#与

|

#OR

not

#非

#Rounding
7 // 4

#求余
10% 4

#power
2 ** 3

#A place to pay attention to floating-point operations
a = 4.2
b = 2.1
c = a + b  # c = 6.300000000000001

from decimal import Decimal
a = Decimal(‘4.2’)
b = Decimal(‘2.1 ‘)

print(a + b)

(a + b) == Decimal(‘6.3’)

< /p>

#The string str is enclosed in single quotes (”) or double quotes (” “)

#Use a backslash (\) to escape special characters.
s =’Yes,he doesn\’t’
print(s, type(s), len(s))

#If you don’t want to escape backslashes,
#You can add an r in front of the string to represent the original string
print(‘C:\some\name’)

print(r’C:\some\name’)

#Backslash can be used as a line continuation character, indicating that the next line is a continuation of the previous line.
#You can also use “””…””” or”’…”’ to span multiple lines
s = “abcd\
efg”
print(s);

s = “””
Hello I am fine!
Thinks.
“””
print(s);

#String can Use the + operator string to connect together, or use the * operator to repeat:
print(‘str’+’ing’,’my’*3)

#There are two strings in Python A kind of indexing method
#The first kind is from left to right, increasing from 0 in order
#The second kind is from right to left, decreasing in order from -1
#Note that there is no separate character Type, a character is a string of length 1
word =’Python’
print(word[0], word[5])
print(word[-1], word[-6] )

#You can also slice a string to obtain a substring
#Separate two indexes with a colon, in the form of a variable [head subscript: end subscript]
# intercepted The range is closed before opening, and both indexes can be omitted
word =’ilovepython’
word[1:5]
#’love’
word[:]
# ‘ilovepython’
word[5:]
#’python’
word[-10:-6]
#’love’
#PythonString cannot be changed
# Assigning a value to an index position, such as word[0] =’m’ will cause an error.
word[0] =’m’

#Detect the beginning and end
filename =’spam.txt’
filename.endswith(‘.txt’)

filename.startswith(‘file:’)

url =’http://www.python.org’
url.startswith(‘http:’)

choices = (‘http:’,’https’)
url =’https://www.python.org’
url.startswith(choices)

choices = [ ‘http:’,’https’]
url =’https://www.python.org’
url.startswith(choices)

#Find a string
string = “I am KEN”
string.find(“am”)
string.find(“boy”)
#Ignore case search

import re
text =’UPPER PYTHON, lower python, Mixed Python’
re.findall(‘python’, text, flags=re.IGNORECASE)

#Search and Replace
text = ‘yeah, but no, but yeah, but no, but yeah’
text.replace(‘yeah’,’yep’)

#Ignore case replacement
text =’UPPER PYTHON, lower python, Mixed Python’
re.sub(‘python’,’snake’, text, flags=re.IGNORECASE)

#Merge and splice string parts = [‘Is’, ‘Chicago’,’Not’,’Chicago?’]”.join(parts)’Is Chicago Not Chicago?”,’.join(parts)’Is,Chicago,Not,Chicago?”’.join( parts)’IsChicagoNotChic ago?‘

Leave a Comment

Your email address will not be published.