python - string, numeric type

Knowledge point one: String type

          1.Definition

             1) Data type enclosed in quotes

             2) Used to describe properties and characteristics. a sentence, a string of characters

             3) When the program is running, the statements inside are not executed and are only considered to be a string of meaningless characters.

             4) Quotes: 'Single quote' "Double quote" '''Triple quote: multi-line string'''

          2. Commonly used methods

              1) increase

                   a) Add two strings

a='hello '
b='world'
c=a+b
print(c)

                   b) Multiplication: number * string → addition of multiple identical strings

name='lihua'
names=10*name
print(names)

                   c) Formatted output

name='lihua'
age=29
print(f'你好,{name}')
print('你好,%s'%name)
print('大家好,我叫%s,今年%d岁了'%(name,age))

              2) Delete

                   a) Strings are immutable types and cannot be changed but the entire string can be deleted

name='lihua'
del name
print(name)

              3) Check

                    a) Index: The characters in the string are sorted starting from 0, and the position of each character is the index.

words='welcome to python world!'
print(words[0]) #打印第一个字符

                    b) Slicing: intercept a string (regardless of the beginning and end)

words='welcome to python world!'
print(words[3:8]) #截取索引3到7的字符

                    c) Find

find方法:
words='welcome to python world!'
print(words.find('python'))
#找到就返回第一个字符的索引
#找不到就返回-1
index方法:
print(words.index('python'))
#找到就返回第一个字符的索引
#找不到就会报错

                    d) Get the string length:

ss='dafasfgwaFRwadf'
print(len(ss))

                     e) Member function: in not in (determine whether the character is in the target string)

words='welcome to python world!'
print('python' in words)

               4) Change

                     a) String is an immutable type and cannot be changed

name='simon'
name[2]='r'
print(name) #会报错

                     b) Method of converting upper and lower case: upper lower

name='lihua'
print(name.upper())
name='SIMON'
print(name.lower())

                     c) Convert the first letter to capitalize capitalize

letter = 'abcd'
print(letter.capitalize())

                      d) Capitalize the first letter of each word title

letter_msg = 'hello world'
print(letter_msg.title())

                      e) Split the string: split

msg='welcome to python world'
print(msg.split())
#返回列表(默认按照空格分割)
ss='dasfwaerfwerieyghjikaxdniuAHDFLIUEQWhrfjkdgfhlads'
print(ss.split('a'))
#按照字符‘a’分割

                      f). Remove the characters on the left and right sides of the string: stri (remove the left side: lstrip, remove the right side rstrip)

name=' simon '
name1=name.strip()
print(name)
print(name1)
#默认清除两边空格
ss='***afewirfu********'
print(ss.strip('*'))

                     g) Filling characters on both sides: center (left filling: ljust, right filling: rjust)

print('Simon'.center(11,'*'))
print('Simon'.ljust(11,'*'))
print('Simon'.rjust(11,'*'))

                     h). Replace characters in a string: replace

a='fatnrakglnsahuiafebwafwalfnsdmf'
b=a.replace('a','*')
print(b)

                 5) Input function input()

name=input('你叫什么名字?')
print(f'你好!{name}')

                 6) Escape character \

# 在字符串中将某些字符进行意义的转换
'\n' # 换行符
'\t' # 制表符
'输出\'引号\'' # 引号嵌套

Knowledge point 2: Number types

          1. Integer data ( the concept of integer in mathematics )

# 记录年龄
age=29

         2. Floating point data ( the concept of decimals in mathematics: float )

# 记录身高
height=175.6

         3. Complex number type data ( the concept of complex numbers in mathematics (real part + imaginary part) - the extended concept of numbers (not usually used) complex )

a=1+5j

         4. Operation symbols ( addition, subtraction, multiplication and division: + - * / correspond to mathematical operators )

print(2+3)
print(2-3)
print(2*3)
print(2/3)

            1) Power operation: **

print(2**3) #2的3次方

            2) Integer division operation: // (floor division/rounding)

print(13//3) # 整除:求商、相当于除法结果取整数部分

            3) Modulo operation % (remainder)

print(13%3) #求模:求余数

        5. Operation priority ( comply with the operation priority of mathematics: power > multiplication = division = integer division = modulus > addition = subtraction, calculated from left to right, if there are parentheses, calculate the ones inside the parentheses first )

print(3*(4+8**2)-26//5%2)

           1) Comparison operators:

                        Greater than > Less than < Equal to ==

                        Greater than or equal to >= Less than or equal to <= Not equal to !=

print(2<4)
print('a'>'b')

            2) Logical operators

                  a) AND or NOT: used to connect Boolean operations

                  b) and and or or not not

                  c) If you score 100 in the Chinese language test and 100 in the math test, you will be rewarded - you must score 100 in both subjects

                         If you score 100 in the Chinese language test or 100 in the math test, you will get a reward - just score 100 in either test.

print(2<4 and 3>4)
print(3<8 or 7>9)
print(not 7<8)

Logical operator precedence: not>and>or

            3) Key point: non-empty means true

                  a) In python, all data types have boolean values

                  b) Empty data is False: None, 0, empty (empty string, empty list, empty dictionary)

                  c) Non-empty data are all True

print(bool(None))
print(bool(0))
print(bool(''))
print(bool([]))
print(bool({}))

 

           6.Abbreviation of assignment symbol

a=22
b=4
a=a+b # 将a+b的值赋值给a ==> 把b加到a上面
a+=b # 与上式等效
# 类比:
a-=b
a*=b
a/=b
a**=b
a%=b
a//=b

Knowledge point three: Boolean type

          1. Two values: True False 

             Function: Determine whether the condition is established, right or wrong, true or false

             The condition is true—True; the condition is not true—False

print(type(True))
# <class 'bool'>
print(2<6)
# True
print(2>6)
# False

 

 

Guess you like

Origin blog.csdn.net/m0_73463638/article/details/127513436