Chapter 3, Ternary Operations, File Handling, Functions

Chapter 3, Ternary Operations, File Handling, Functions

Ternary operation

Ternary operation, also known as ternary operation, is a shorthand for simple conditional statements, such as:

Simple conditional statement:

if 条件成立:
    val = 1
else:
    val = 2
# 可以写成如下:
val = 1 if 条件成立 else 2

file handling

read, write, modify

  • read

  • f = open(file='d:/*.txt', mode='r', encoding='utf-8')
    date = f.read()
    f.close()
  • r is read-only mode, rb is a binary read file

The third-party module chardet detects what encoding the binary content may be

Use pip to install third-party libraries, py3 use pip3 install chardet

mark

import chardet

chardet.detect(data)

{'encoding':'GB2312','confidence':0.823045,'language':'Chinese'}

  • loop file

  • f = open("*.txt", "r", encoding="gbk")
    for line in f:
        print(line)
    f.close()

print will automatically wrap after each execution

  • write file

  • Mode w write mode, wb binary write mode

  • f.write("路飞".encoding("gbk"))

    When writing in wb mode, you need to specify what encoding to write in.

  • w write mode will first clear the file, and then write, which is equivalent to directly overwriting the original content of the file

  • w is a create mode

  • addition

  • a append mode, ab binary append mode

  • Read and write mixed mode

  • r+ Read and write mode, read first and then write, and can append after reading.

    After f.read() finishes reading, it will point the pointer to the end of the file content, and execute f.read() again to read it is empty

  • w+write-read mode, first write and then read, first overwrite and then read what you wrote. (It is equivalent to creating an empty file to replace the original one, and then you can read the newly written content, almost using less than)

  • Other functional methods for file operations

  • def fileno(self, *args, **kwargs):  
            返回文件句柄在内核中的索引值,以后做IO多路复用时可以用到
    
    def flush(self, *args, **kwargs):  
            把文件从内存buffer里强制刷新到硬盘
    
    def readable(self, *args, **kwargs):  
            判断是否可读
    
    def readline(self, *args, **kwargs):  
            只读一行,遇到\r or \n为止,指针会指向下一行开头.
    
    def seek(self, *args, **kwargs):  
            把操作文件的光标移到指定位置
            *注意seek的长度是按字节算的, 字符编码存每个字符所占的字节长度不一样。
            如“路飞学城” 用gbk存是2个字节一个字,用utf-8就是3个字节,因此以gbk打开时,seek(4) 就把光标切换到了“飞”和“学”两个字中间。
            但如果是utf8,seek(4)会导致,拿到了飞这个字的一部分字节,打印的话会报错,因为处理剩下的文本时发现用utf8处理不了了,因为编码对不上了。少了一个字节
    
    def seekable(self, *args, **kwargs):  
            判断文件是否可进行seek操作
    
    def tell(self, *args, **kwargs):  
            返回当前文件操作光标位置,按字节单位
    
    def truncate(self, *args, **kwargs):  
            按指定长度截断文件
            *指定长度的话,就从文件开头开始截断指定长度,不指定长度的话,就从当前位置到文件尾部的内容全去掉。模式必须可写
    
    def writable(self, *args, **kwargs):  
            判断文件是否可写
  • f.tell() returns the current cursor position

  • f.seek(0) move the cursor to the beginning of the file

Both f.tell() and f.seek(0) are in bytes

f.read(1) is a character unit, a Chinese character in gbk is 2 bytes, and a Chinese character in utf-8 is 3 bytes

  • Modify the file

  • f = open("兼职白领学生空姐模特护士联系方式utf8.txt",'r+',encoding="utf-8")
    f.seek(6)
    f.write("[路飞学城]")
    f.close()
  • 王心[路飞学城]9    46    1381234424
    马纤羽     深圳    173    50    1374423423
    乔亦菲     广州    172    52    1582342525
    罗梦竹     北京    175    49    1862343421
    刘诺涵     北京    170    48    1862343765
    岳妮妮     深圳    177    54    1883524553
    贺婉萱     深圳    174    52    1893344452
    叶梓萱    上海    171    49    1804242324
    杜姗姗 北京  167 49 13324523342
    black girl  河北  167 50  13542342233
  • It was indeed changed from the third word, but [Luffy Academy] covered the following content

practice questions

Exercise 1 - Global Replacement Program:

  • Write a script that allows users to globally replace the contents of a specified file when executed as follows

`python your_script.py old_str new_str filename`

  • After the replacement is completed, print how many places have been replaced
# -*- coding:utf-8 -*-
# !/usr/bin/env python
# Author:dc0012

import sys

old_str = sys.argv[1]  # 接受被替换文本参数
new_str = sys.argv[2]  # 接收新文本
file_name = sys.argv[3]  # 接收需要处理的文件名字

f = open(file_name, "r+", encoding="utf-8")

content = f.read().replace(old_str, new_str)  # 替换后的文件全部内容给conten
f.seek(0)  #将指针移到文件开头
f.write(content)  # 将替换后的文件内容写入文件
f.truncate(f.tell())  # 防止新内容比旧内容少,将多出来的截取出去

f.close()

Example: python your_script.py beijing shaanxi f_test

Replace Beijing with Shaanxi in the f_test file.

Exercise 2 - Mock Login:

  • User enters account password to log in
  • User information is stored in the file
  • After entering the wrong password for three times, the user will be locked, and the next time you log in, it is detected that the user cannot log in.
# -*- coding:utf-8 -*-
# !/usr/bin/env python
# Author:dc0012

import sys
with open("lock_account", "r+") as f_lock:
    with open("account", "r") as f_account:
        count = 0
        _username = input("username:").strip()
        for i in f_lock:
            if _username == i.strip():
                print("This account was locked....")
                sys.exit()
        for i in f_account:
            username, password = i.strip().split(",")
            if _username == username:
                while count < 3:
                    _password = input("password:")
                    if _password == password:
                            print("Welcome %s login..." % _username)
                            sys.exit()
                    else:
                        print("Wrong password...")
                        count += 1
                        continue
                else:
                    print("You have tried too many times.This username will be locked")
                    f_lock.write(_username + "\n")
                    sys.exit()
        print("No such this username....")
        sys.exit()

Account stores the username and password as one user, separate the username and password with ",", for example: dc,123

lock_account stores the locked username, one per line.

function

definition:

A function refers to encapsulating a set of statements with a name (function name). To execute this function, you only need to call its function name.

characteristic:

  1. reduce duplicate code
  2. Make the program extensible
  3. Make programs easier to maintain
  • Grammar definition

  • def sayhi():#函数名
        print("Hello, I'm nobody!")
    
    sayhi() #调用函数
  • with parameters

    #下面这段代码
    a,b = 5,8
    c = a**b
    print(c)
  • #改成用函数写
    def calc(x,y):
    res = x**y
    return res #返回函数执行结果
    
    c = calc(a,b) #结果赋值给c变量
    print(c)

function parameter

Parameters can make your function more flexible, I don’t know if it can do dead actions, and it can also determine the execution process inside the function according to the different parameters passed when calling

Formal parameters:

  • The memory unit is allocated only when it is called, and the allocated memory unit is released immediately at the end of the call. Therefore, the formal parameter is only valid inside the function. After the function call ends and returns to the main calling function, the formal parameter variable cannot be used again.

Arguments:

  • Can be constants, variables, expressions, functions, etc. No matter what type of quantity the actual parameter is, when the function is called, they must have a definite value in order to pass these values ​​to the formal parameters. Therefore, it should be pre-assigned, Input and other methods make the parameters obtain definite values.

mark

Default parameters:

  • When defining a function, assign a default value to the parameter. If you do not assign a value to this parameter when calling, it will call the default parameter for processing.

Default arguments must be placed after positional arguments

One thing to keep in mind when defining default parameters: default parameters must point to immutable objects! For example: if the default parameter is an empty list, and once the value of this default empty list is changed, it will be called with the changed list as the parameter in the next call.

key parameter:

  • Under normal circumstances, you need to pass parameters to a function in order. You can use key parameters if you don't want to order them. You only need to specify the parameter name (the parameter with the parameter name specified is called the key parameter), * But remember one requirement is that the key Parameters must be placed after positional parameters (parameters that determine correspondence in positional order) *

When assigning, the parameters that specify the object assignment are called key parameters.

def stu_register(name, age, course='PY' ,country='CN'):
    print("----注册学生信息------")
    print("姓名:", name)
    print("age:", age)
    print("国籍:", country)
    print("课程:", course)

call can be like this

stu_register("王山炮",course='PY', age=22,country='JP' )

but never

stu_register("王山炮",course='PY',22,country='JP' )

Of course not

stu_register("王山炮",22,age=25,country='JP' )

This is equivalent to assigning age twice, and an error will be reported!

Non-fixed parameters:

If your function is defined when you are not sure how many parameters the user wants to pass in, you can use non-fixed parameters

def stu_register(name,age,*args): # *args 会把多传入的参数变成一个元组形式
    print(name,age,args)

stu_register("Alex",22)
#输出
#Alex 22 () #后面这个()就是args,只是因为没传值,所以为空

stu_register("Jack",32,"CN","Python")
#输出
# Jack 32 ('CN', 'Python')

There can also be a **kwargs

def stu_register(name,age,*args,**kwargs): # *kwargs 会把多传入的参数变成一个dict形式
    print(name,age,args,kwargs)

stu_register("Alex",22)
#输出
#Alex 22 () {}#后面这个{}就是kwargs,只是因为没传值,所以为空

stu_register("Jack",32,"CN","Python",sex="Male",province="ShanDong")
#输出
# Jack 32 ('CN', 'Python') {'province': 'ShanDong', 'sex': 'Male'}
  • **kwargs accepts keyword arguments, which are automatically converted to dictionaries.

If * appears before the parameters of the function definition, then the parameters passed by this function can no longer be a fixed number, and all the passed parameters are packaged into a tuple

>>> nums = [1, 2, 3]
>>> calc(*nums)
14

*numsIndicates that numsall elements of this list are passed in as variable parameters. This notation is quite useful and common.

function return value

If the code outside the function wants to obtain the execution result of the function, it can use the return statement in the function to return the result.

Notice

  • As long as the function encounters the return statement during the execution process, it will stop executing and return the result, so can also be understood as the return statement representing the end of the function
  • If return is not specified in the function, the return value of this function is None

A function can always return only one value, this value can be a tuple, a list, a dictionary

local variable

  • Variables defined in a function are called local variables, and variables defined at the beginning of a program are called global variables.
  • The global variable scope is the entire program, and the local variable scope is the function in which the variable is defined.
  • When a global variable has the same name as a local variable, within the function in which the local variable is defined, the local variable takes effect; elsewhere the global variable takes effect.

Modify global variables in functions

  • Use the global variable name inside the function to declare this variable as a global variable and modify it

Changing global variables inside a function is generally not recommended

  • Dictionaries, lists, sets, objects, and objects in classes can be modified directly inside functions
  • If the global variable is a string or an integer, it cannot be changed directly inside the function.

Function summary:

  • When defining a function, you need to determine the function name and the number of parameters;
  • If necessary, you can check the data type of the parameter first;
  • The function body can be used returnto return the function result at any time;
  • Automatically when there is no returnstatement after the function is executed return None.
  • A function can return multiple values ​​at the same time, but is actually a tuple.

nested functions

  1. The function can be defined again inside the function
  2. execution needs to be called
age = 19
def func1():
    # global age 定义全局变量后就不会出错
    def func2():
        print(age)
    func2()
    age = 73 
func1()
# 这种调用会导致出错

scope

  • A function in python is a scope
  • local variables are placed in their scope
  • After the definition is complete, the scope has been generated, and the scope chain is looked up

anonymous function

declare an anonymous function

lamdba x, y: x*y   # 声明一个匿名函数
  • lambda only supports simple logic and supports up to ternary operation syntax

  • def func1(x, y):
        if x < y:
            return x * y
        else:
            return x / y
    # 上面的函数换成lambda就是下面的格式
    func2 = lambda x, y: x * y if x < y else x / y
  • Use it in combination with other methods and throw it away after use

effect:

  1. save code
  2. looking senior

Higher order functions

A variable can point to a function, and the parameters of a function can receive variables, so a function can receive another function as a parameter. This kind of function is called a higher-order function.

Only one of the following conditions needs to be met, that is, a higher-order function:

  1. receives one or more functions as input
  2. return returns another function

recursive function

Recursive Features:

  1. There must be an explicit end condition
  2. Each time you enter a deeper recursion, the problem size should be reduced compared to the last recursion
  3. The recursion efficiency is not high, and too many recursive levels will lead to stack overflow (in the computer, function calls are implemented through the data structure of the stack (stack), whenever a function call is entered, a stack frame will be added to the stack. When the function returns, the stack will decrease by one stack frame. Since the size of the stack is not infinite, too many recursive calls will lead to stack overflow)

Let's take a simple example first: calculate the sum of additions between 1 and 100; implement it in two ways: loop and recursion

#!/usr/bin/env python3
# 1-100 sum

import sys
def sum_cycle(n):
    '''
    1 to n,The sum function
    '''
    sum = 0
    for i in range(1,n + 1):
        sum += i
    return sum

def sum_recu(n):
    '''
    1 to n,The sum function
    '''
    if n > 0:
        return n + sum_recu(n - 1)    #调用函数自身
    else:
        return 0
print("循环求和:",sum_cycle(100))
print("递归求和:",sum_recu(100))
执行结果:
[root@localhost tmp]# ./sum.py
循环求和: 5050
递归求和: 5050

The advantage of recursive functions is that the definition is simple and the logic is clear. In theory, all recursive functions can be written as loops, but the logic of loops is not as clear as recursion.

***Use of recursive functions requires attention to prevent stack overflow. In the computer, function calls are implemented through the data structure of the stack. Whenever a function call is entered, a stack frame will be added to the stack, and whenever the function returns, the stack will be decremented by one layer. Since the size of the stack is not infinite, too many recursive calls will cause stack overflow.

Changing the parameter of the above recursive sum function to 10000 will cause stack overflow!

[root@localhost tmp]# ./sum.py
循环求和: 5050
Traceback (most recent call last):
  File "./sum.py", line 23, in <module>
    print("递归求和:",sum_recu(1000))
  File "./sum.py", line 19, in sum_recu
    return n + sum_recu(n - 1)
  File "./sum.py", line 19, in sum_recu
    return n + sum_recu(n - 1)
  File "./sum.py", line 19, in sum_recu
    return n + sum_recu(n - 1)
  [Previous line repeated 994 more times]
  File "./sum.py", line 18, in sum_recu
    if n > 0:
RecursionError: maximum recursion depth exceeded in comparison

***The way to solve the recursive call stack overflow is through tail recursion optimization. In fact, the effect of tail recursion and loop is the same. Therefore, it is also possible to regard loop as a special tail recursion function.

Tail recursion optimization: http://www.open-open.com/lib/view/open1480494663229.html

You should have heard of the binary search method; it is a fast search method with low time complexity and simple and easy-to-understand logic. Generally speaking, it is to continuously find the middle value, and use the middle value to compare the actual value you need to find; If the middle value is large, continue to find the left; if the middle value is small, continue to find the right; it can be seen that the dichotomy is to repeat the above process, so the binary search can be implemented recursively!

summary

  • The advantage of using recursive functions is that the logic is simple and clear, and the disadvantage is that too deep calls can lead to stack overflow.
  • Languages ​​optimized for tail recursion can prevent stack overflows by tail recursion. Tail recursion is actually equivalent to looping. Programming languages ​​without looping statements can only implement looping through tail recursion.
  • The standard Python interpreter is not optimized for tail recursion, and any recursive function has stack overflow problems.
#!/usr/bin/env python3

#The binary search function

def  Binary_Search(data_source,find_n):
    if len(data_source) >= 1:                                           #判断列表长度是否大于1,小于1就是一个值
        mid = int(len(data_source)/2)                                   #获取列表中间索引;奇数长度列表长度除以2会得到小数,通过int将转换整型
        if find_n > data_source[-1]:                                    #判断查找值是否超出最大值
            print('{}查找值不存在!'.format(find_n))
            exit()
        elif find_n < data_source[0]:                                   #判断查找值是否超出最小值
            print('{}查找值不存在!'.format(find_n))
            exit()
        if data_source[mid]  > find_n:                                  #判断列表中间值是否大于查找值
            #print('查找值在 {} 左边'.format(data_source[mid]))
            Binary_Search(data_source[:mid],find_n)                     #调用自己,并将中间值左边所有元素做参数
        elif data_source[mid] < find_n:                                 #判断列表中间值是否小于查找值
            #print('查找值在 {} 右边'.format(data_source[mid]))           #调用自己,并将中间值右边所有元素做参数
            Binary_Search(data_source[mid:],find_n)
        else:
            print('找到查找值',data_source[mid])                          #找到查找值
    else:
        print('{}查找值不存在!'.format(find_n))                          #特殊情况,返回查找不到

Data = [22,12,41,99,101,323,1009,232,887,97]
Data.sort()                                                             #列表从小到大排序
Binary_Search(Data,323)                                                 #查找323
执行结果:
[root@localhost tmp]# ./binary_search.py 
找到查找值 323

built-in method

--------------------Function name------------------ effect
abs() take the absolute value of a number
dict() Convert a data to a dictionary type
help() help
min () Take the smallest of a set of integer data
max() take the maximum number
seattr()
all() Check if all elements are True, otherwise return False, use all to judge the empty list will return True
any() Check all elements as long as one of them is True and return True, judging the empty list will return False
to you() print all variables that exist in the current program
hex() convert hexadecimal
next() execute iterator...
slice() 切片,class slice(start, stop[, step])
divmod () divmod(10,3) returns the quotient and remainder
sorted() sorted(d.items(), key = lambda x:x[1], reverse=True) can define sorting rules
ascii() Convert to unicode encoding
oct() convert octal
staticmethod()
bin() convert binary
evel() Convert characters into code to run according to the rules of the interpreter, only a single line of code can be processed, and the running result can be obtained
exec() Convert characters to code to run according to the rules of the interpreter, you can execute multiple lines of code , but you cannot get the return value of the function or the result of the program.
isinstance() isinstance("a", str) ---> True
words() convert string to ascii code
sum() sum
bytearray() Converting the byte type to bytearray type can modify the memory address to achieve the purpose of modifying the string, which is used to change the large string.
filter() Used to filter a sequence, filter out elements that do not meet the condition, and return a new list of elements that meet the condition. This receives two parameters, the first is a function, the second is a sequence, each element of the sequence is passed as a parameter to the function for judgment, then returns True or False, and finally puts the element that returns True into a new list.
map() map(lambda x: x*x, [1,2,3,4,5,6]) Multiply each element of the list by itself
reduce() import functools,functools.reduce()
bytes()
pow() how many powers to return
print()

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325313077&siteId=291194637