python learning | first day

1. Output function print

#输出数字,直接输出
print(2023)
print(2023.1)

#输出字符串,要加单引号或者双引号(实际效果都是一样的),输出后自动换行
print('hello 2023')
print("hello 2023")
print('hello','2023','不换行')#逗号分隔字符串,可以使它们输出在同一行

#输出表达式,会自动计算结果输出
print(1+2)

#输出到文件中。有些知识后面才会介绍,这里先观察输出效果
fp=open('D:/text.txt','a+')#a+ 表示文件不存在就创建,文件存在就在文件内容后面进行内容追加
print('hello 2023',file=fp)#必须使用file,若无则不会将字符串输出到文件中
fp.close()#关闭文件

Output effect:

2023
2023.1
hello 2023
hello 2023
hello 2023 不换行
3

2. Floating point output

n1=1.1
n2=2.2
n3=2.1
print(n1,type(n1))
#n1+n2 print的结果是3.3000000000...3
#n1+n3 print的结果却是3.2
#要使n1+n2 print结果为3.3,可以使用如下语句

from decimal import Decimal
print(Decimal('1.1')+Decimal('2.2'))

3. Data type conversion

'hello' is a string, '2023' is a string, and 2023 is an int number. When using '+' in print to connect the two parts before and after to form a whole output, the front and back must be of the same type, otherwise an error will occur (The blue arrow implements int to str, and the yellow arrow reports an error without type conversion.)
insert image description here
So why do you need data type conversion? ——It is to splice data of different data types together

Function name effect Precautions
str() Convert other data types to strings Can also be converted with quotes
int() Convert other data types to integers 1. Text and decimal strings cannot be converted; 2. Floating point numbers are converted into integers, and the decimal part will be discarded
float() Convert other data types to floating point numbers 1. Text strings cannot be converted; 2. Integers are converted to floating point, and the decimal part is filled with 0

4. Operator precedence

Priority descends from top to bottom

operator type symbol
arithmetic operator ** > *./. //. % > +.-
bitwise operator <<.>> > & > l
comparison operator <.>.>=.<=.==.!=
boolean operator and or
assignment operator =

5. list object list

list elements can be of different types

#列表对象创建一: []
lst1=['hello','2023','1.3']
#列表对象创建二:list()函数
lst2=list(['hello','2023','1.3'])

6. Common operations on strings

String content comparison can use ==, != direct comparison,
string address comparison can be compared with is, not is (whether it is the same string)

string lookup

index(): the first occurrence of the substring, if it does not exist, an error will be reported
rindex(): the last occurrence of the substring, if it does not exist, an error will be reported
find(): the first occurrence of the substring, if it does not exist, -1 will be returned
rfind (): The last occurrence of the substring, if it does not exist, return -1

string case conversion operation

Conversion is to apply for a new address space to store the result, even if the result is the same as the original string, a new address will be applied for
upper(): all uppercase
lower(): all lowercase
swapcase(): original string uppercase becomes lowercase, lowercase becomes uppercase
capitalize (): The first letter is capitalized, the remaining lowercase
title(): distinguish words, the first letter of the word is capitalized, and the rest of the word is lowercased (a string can have multiple words)

string alignment

If the set width is smaller than the actual width, the original string will be returned.
center (width, default space for filler): center
ljust (width, default space for filler): left alignment
rjust (width, default space for filler): right alignment
zfill (width) : right-aligned, padding with 0 on the left

string splitting operation

The return value of the split is a list
split() or split(sep='', maxsplit=x): split from the left, and the default is to divide by spaces; the split string is specified by the parameter sep; the split string is the split character by the parameter maxsplit specifies the maximum number of splits when splitting a string. After the maximum number of splits, the remaining string will be taken as a separate part.
rsplit(): split from the right, and the rest is the same as the previous split().

String judgment

Quote: str1.is...()
return result: Boolean value
isidentifier(): Is the identifier valid?
isspace(): consists entirely of blank characters? Carriage return, line feed, horizontal tab \t
isalpha(): all letters? The judgment result of abc Zhang San is true
isdecimal(): full decimal number? 123 is true, 4 and Ⅴ are false
isnumeric(): all numbers? 123 Four and five judgment results are all true
isalnum(): all letters + numbers?

string other operations

replace (the original string to be replaced, the string to replace the original string, the number of replacements): string replacement, return to get the string join
(): string merge, merge the strings in the list or tuple into one string

s='hello,python,python,python'
print(s.replace(python,java,2))#java替换两次python
#输出结果
hello,java,java,python

Guess you like

Origin blog.csdn.net/qq_45788060/article/details/128521448