一、数据运算
1、简介
你写的许多语句(逻辑行)会包含表达式。表达式的最简单的例子是 2 + 3 。表达式可以被分解成操作符和操作数。运算符的功能是完成某件事,它们由如 + 这样的符号或者其他特定的关键字表示。运算符需要数据来进行运算,这样的数据被称为操作数。在这个例子中,2 和 3 是操作数。
2、操作符
a = 10, b = 20
算数运算:
比较运算:
赋值运算:
逻辑运算:
成员运算:
身份运算:
位运算:
a = 60 # 60 = 0011 1100
b = 13 # 13 = 0000 1101
c = 0
c = a & b # 12 = 0000 1100
print("Line 1 - Value of c is ", c)
c = a | b # 61 = 0011 1101
print("Line 2 - Value of c is ", c)
c = a ^ b # 49 = 0011 0001 #相同为0,不同为1
print("Line 3 - Value of c is ", c)
c = ~a # -61 = 1100 0011
print("Line 4 - Value of c is ", c)
c = a << 2 # 240 = 1111 0000
print("Line 5 - Value of c is ", c)
c = a >> 2 # 15 = 0000 1111
print("Line 6 - Value of c is ", c)
tips:~按位取反运算规则(按位取反再加1)
运算符优先级:
以下表格列出了从最高到最低优先级的所有运算符:
二、控制流
1、if语句
age = 10
guess_age = int(input("guess_age:"))
if age == guess_age:
print("good")
elif age > guess_age:
print("age 大于 guess_age")
else:
print("age 小于 guess_age")
2、for loop
for i in range(10):
print("loop:", i ) #循环打印十次
continue 与 break的区别:
for i in range(10):
if i<5:
continue #不往下走了,直接进入下一次loop
print("loop:", i )
for i in range(10):
if i>5:
break #不往下走了,直接跳出整个loop
print("loop:", i )
3、while loop
有一种循环叫死循环,一经触发,就运行个天荒地老、海枯石烂。
#海枯石烂代码
count = 0
while True:
print("你是风儿我是沙,缠缠绵绵到天涯...",count)
count +=1
其实除了时间,没有什么是永恒的,死loop还是少写为好,上面的代码循环100次就退出吧
count = 0
while True:
print("你是风儿我是沙,缠缠绵绵到天涯...",count)
count +=1
if count == 100:
print("退出整个循环")
break
三、文件操作
对文件操作的基本流程:
1、打开文件,得到文件句柄并赋值给一个变量
2、通过句柄对文件进行操作
3、关闭文件
打开文件的模式有:
- r, 只读模式(默认)。
- w,只写模式。【不可读;不存在则创建;存在则删除内容;】
- a, 追加模式。【可读; 不存在则创建;存在则只追加内容;】
"+" 表示可以同时读写某个文件:
- r+, 可读写文件。【可读;可写;可追加】
- w+,写读
- a+, 同a
"U"表示在读取时,可以将 \\r \\n \\r\\n自动转换成 \\n (与 r 或 r+ 模式同使用):
- rU
- r+U
"b"表示处理二进制文件(如:FTP发送上传ISO镜像文件,linux可忽略,windows处理二进制文件时需标注):
- rb -----》以二进制模式打开文件,不能声明encoding
- wb-----》以二进制模式写入文件,必须写入bytes格式
- ab
修改文件方法一:
#修改文件:方法1----->先全部读到内存,然后修改,最后写入-------->缺点是太吃内存
f = open("lyrics", mode="r", encoding="utf-8")
data = f.read()
data = data.replace("默认", "RRRRRRRR")
f.close()
f = open("lyrics", mode="w", encoding="utf-8")
f.write(data)
f.close()
修改文件方法二:
#方法二 将旧文件一行一行读出来,修改好后,写入另外一个新建的文件,完成之后,
#将旧文件删除,把新文件重命名为旧文件的名字
#缺点是------>费两倍硬盘空间
import os
f = open("lyric", mode="r", encoding="utf-8")
f_new = open("lyric_new", mode="w", encoding="utf-8")
for line in f:
if "模式" in line:
line = line.replace("模式", "mode")
f_new.write(line)
f.close()
f_new.close()
os.remove("lyric")
os.rename("lyric_new", "lyric")
其他语法:
def close(self): # real signature unknown; restored from __doc__
"""
Close the file.
A closed file cannot be used for further I/O operations. close() may be
called more than once without error.
"""
pass
def fileno(self, *args, **kwargs): # real signature unknown
""" Return the underlying file descriptor (an integer). """
pass
def isatty(self, *args, **kwargs): # real signature unknown
""" True if the file is connected to a TTY device. """
pass
def read(self, size=-1): # known case of _io.FileIO.read
"""
注意,不一定能全读回来
Read at most size bytes, returned as bytes.
Only makes one system call, so less data may be returned than requested.
In non-blocking mode, returns None if no data is available.
Return an empty bytes object at EOF.
"""
return ""
def readable(self, *args, **kwargs): # real signature unknown
""" True if file was opened in a read mode. """
pass
def readall(self, *args, **kwargs): # real signature unknown
"""
Read all data from the file, returned as bytes.
In non-blocking mode, returns as much as is immediately available,
or None if no data is available. Return an empty bytes object at EOF.
"""
pass
def readinto(self): # real signature unknown; restored from __doc__
""" Same as RawIOBase.readinto(). """
pass #不要用,没人知道它是干嘛用的
def seek(self, *args, **kwargs): # real signature unknown
"""
Move to new file position and return the file position.
Argument offset is a byte count. Optional argument whence defaults to
SEEK_SET or 0 (offset from start of file, offset should be >= 0); other values
are SEEK_CUR or 1 (move relative to current position, positive or negative),
and SEEK_END or 2 (move relative to end of file, usually negative, although
many platforms allow seeking beyond the end of a file).
Note that not all file objects are seekable.
"""
pass
def seekable(self, *args, **kwargs): # real signature unknown
""" True if file supports random-access. """
pass
def tell(self, *args, **kwargs): # real signature unknown
"""
Current file position.
Can raise OSError for non seekable files.
"""
pass
def truncate(self, *args, **kwargs): # real signature unknown
"""
Truncate the file to at most size bytes and return the truncated size.
Size defaults to the current file position, as returned by tell().
The current file position is changed to the value of size.
"""
pass
def writable(self, *args, **kwargs): # real signature unknown
""" True if file was opened in a write mode. """
pass
def write(self, *args, **kwargs): # real signature unknown
"""
Write bytes b to file, return number written.
Only makes one system call, so not all of the data may be written.
The number of bytes actually written is returned. In non-blocking mode,
returns None if the write would block.
"""
pass
f.seek(10) 代表移动10个字节
f.read(6) 代表读取6个字符