Python based
theme: juejin
Highlight: Python
Python is an interpreted, object-oriented, dynamic data typed high-level programming language.
Python was invented by Guido van Rossum in late 1989, with the first public release in 1991. Like Perl, Python source code is governed by the GNU General Public License (GPL). It is officially announced that Python 2 updates will be stopped on January 1, 2020. Python 2.7 was identified as the last Python 2.x release.
fromwww.runoob.com/python/pyth…
Variable type
In Python, you don’t have to define a basic variable like you do in C and Java, so you just define the name of the variable and you just assign it to it, just like the default var in javascript is just a declaration, this is a variable.
# byte string Integer floating point bool
# pass line
# true false indicates that Boolean is used for if judgment
var0 = 'a'
print(var0)
pass
var1 = "hr"
print(var1)
pass
var2 = 10
print(var2)
pass
var3 = 100000.00
print(var3)
pass
# Note: True and False are capitalized in Python
varTrue = True
varFalse = False
if varTrue:
print("bool值True is True")
else:
print("Bool value false is True")
pass
if not varFalse:
print("bool值True is False")
else:
print("Bool value false is false")
pass
# String usage
str = "Who are you, old bean?"
# Just the first two
print(str[:2])
# need the second to last digit
print(str[2:len(str) - 1])
pass
# List usage
list1 = ['python'.'java'.1996.2020]
list2 = [1.2.3.4.5]
list3 = ["a"."b"."c"."d"]
The data items in the list do not need to have the same type; the type is Object
print(list1)
# Can be freely spliced
print(list1 + list2)
You are free to concatenate the array using the method append with any type
# Method 1:
list3.append(1)
list3.append(2)
list3.append(3)
print(list3)
# method 2: (Note that the format of the array is: [..., [],...]. )
list3.insert(2.3)
list3.insert(2, list1)
print(list3)
Delete data with specified subscript
# Method 1:
del list3[1]
print(list3)
# Method 2:
list3.remove("c")
print(list3)
pass
# meta components (similar to arraylist tuples using () and lists using [])
a = (1.2.3.4.5."1"."2")
print(a)
pass
# dictionary (which is a hash of a key value)
a = {"key1": "value1"."key2": "value2"}
print(a)
Access the values in the dictionary
# Method 1:
print(a["key1"])
# Method 2:
print(a.get("key1"))
Modify the data in the dictionary
a["key2"] = "my love"
print(a["key2"])
Delete data from the dictionary
del a['key2'] # delete entries whose key is 'Name'
# a.clear() # clear all dictionary entries
# delete a dictionary
print(a)
pass
Copy the code
Arithmetic operators
The operator | describe |
---|---|
+ | A plus b |
– | A value minus b value |
* | A times b |
/ | A value over B value |
% | A value % b value |
支那 | A value ** B value |
// | A value // B value |
# Python -- operator
int1 = 2
int2 = 6
int3 = 8
int4 = 10
int5 = 3
# addition
print("Int addition:", int1 + int2)
# subtraction
print("Int subtraction:", int4 - int2)
# multiplication
print("Int multiply:", int3 * int2)
# division
print("Int division:", int4 / int1)
# modulus
print("Int:", int4 % int5)
# power etc.
print("Int idempotent:", int1 ** int2)
# take divisible
print("Int divisible:", int4 // int5)
pass
double1 = 10.00
double2 = 20.10
double3 = 30.20
double4 = 40.30
# addition
print("Double addition:", double1 + double2)
# subtraction
print("Double subtraction:", double4 - double3)
# multiplication
print("Double multiplication:", double4 * int2)
# division
print("Double division:", double4 / int1)
# modulus
print("Double:", double4 / double1)
# power etc.
print("Double idempotent:", double4 ** int2)
# take divisible
print("Double divisible:", double4 // int5)
Copy the code
Conditional statements and loop statements
Common statements:
1. if… else…
2. while
3. for
4. The break, continue
# if... Else conditional statement
a = 0
if a == 0:
print("I'm equal to zero!")
else:
print("I'm not equal to zero!")
pass
# while loop statement
# 1. While loop indefinitely (note: End with Stop or Ctrl+C)
a = 0
b = 0
while a == 0:
print("I came to number one.", b, "Time!")
b = b + 1
# 2. While conditional loops
a = 0
while a < 5:
print("I am", a, "No!")
a = a + 1
# 3. While loop statement + condition statement
a = 0
exact = []
not_exact = []
while a < 10:
# 0-10 Whether there is a number that is divisible
if a % 2= =0:
exact.append(a)
# Number that is not divisible
else:
not_exact.append(a)
a = a + 1
else:
print("I'm done with the cycle!")
print("A number that can be divided exactly:", exact)
print("A number that is not divisible:", not_exact)
pass
# 4. While statement + condition statement + break loop
a = 0
exact = []
while a < 10:
# 0-10 Whether there is a number that is divisible
if a % 2= =0:
exact.append(a)
# Number that is not divisible
else:
a = a + 1
continue
a = a + 1
print("The number that is divisible continues:", exact)
pass
# 5. While statement + condition statement + break out of loop
a = 0
exact = []
while a < 10:
# 0-10 Whether there is a number that is divisible
if a % 2= =0:
exact.append(a)
# Number that is not divisible
else:
a = a + 1
break
a = a + 1
print("The number that can be divisible break:", exact)
pass
# for loop statement
# 1. For loop
a = [1.2.3."4".5.True]
for b in a:
print(b)
pass
# 2. For loop + conditional statement
a = [1.2.3."4".5.True.9.'10']
b = False
d = []
for c in a:
if c == True:
b = True
continue
d.append(c)
print("Does it have a bool? ", b)
print("Array without bool:", d)
pass
Copy the code
Date and time
# Date and time
# Introduce the time module
import time
pass
Get the millisecond value
ticks = time.time()
print("The current timestamp is :", ticks)
pass
Get local time
localtime = time.localtime(time.time())
print("Local time is :", localtime)
pass
Get time-structured data
localtime = time.asctime(time.localtime(time.time()))
print("Local time is :", localtime)
pass
# 1. Formatting time:
Format to 2020-12-04 15:44:05
print(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()))
Format to Fri Dec 04 15:44:05 2020
print(time.strftime("%a %b %d %H:%M:%S %Y", time.localtime()))
# convert format string to timestamp
a = "Sat Mar 28 22:24:24 2016"
print(time.mktime(time.strptime(a, "%a %b %d %H:%M:%S %Y")))
pass
# 2. Get the calendar of the month
There are a wide range of ways to work with calendars and monthly calendars, such as printing a Calendar for a particular month
import calendar
cal = calendar.month(2020.1)
print("Output calendar for January 2020 :")
print(cal)
pass
Copy the code
5. Functions and Modules
1. A function is an organized, reusable piece of code that implements a single, or related, function.
2. Functions can improve application modularity and code reuse.
3. Modules can define functions, classes, and variables. Modules can also contain executable code.
# define a function
def test_function(str) :
if str % 2= =0:
return str
else:
return False
str = 3
print(test_function(str))
Copy the code
# import module
import dbtest
Use functions in modules
print(dbtest.test_function(2))
pass
Copy the code
Vi. Method of using File
The # input([prompt]) function can take a Python expression as input and return the result
str = input("Please enter:")
print("Final value is:".str)
pass
# open function
# file object = open(file_name [, access_mode][, buffering])
# 1.file_NAME: The file_name variable is a string value containing the name of the file you want to access.
# 2. Access_mode: Access_mode determines the mode of opening a file: read only, write, append, etc. All available values are shown in the complete list below. This parameter is optional, and the default file access mode is read-only (r).
# 3. Buffering: If buffering is set to 0, there will be no storage. If buffering is 1, rows will be stored when the file is accessed. If buffering is set to an integer greater than 1, it indicates that this is the size of the host buffer. If the value is negative, the buffer size of the host area is the system default.
# 4.encoding: UtF8 is used
# 5. Errors: Error level
# 6. Newline: Distinguishes newlines
# 7. Closefd: The type of file parameter passed in
# 8.opener: Sets custom opener. The return value of opener must be an open file descriptor
fo = open("D: \ \ desktop \ \ test TXT"."w+", -1)
print("File name:", fo.name)
print("Closed or not:", fo.closed)
print("Access mode:", fo.mode)
# print(" Force space at the end: ", fo. Softspace)
pass
Write ([string])
fo.write("When a reference to a file object is reassigned to another file, Python closes the previous file. It's a good habit to close files with the close () method.)
print("Write successful!")
pass
Read ([number])
var = fo.read(10)
print("Document Content:", var)
pass
# file location tell()
print("File Location:", fo.tell())
pass
# close close
When a reference to a file object is reassigned to another file, Python closes the previous file. Closing files with the close () method is a good habit.
fo.close()
print("Closed")
pass
Rename ([string -- old],[string -- new])
import os
var1 = "D: \ \ desktop \ \ test TXT"
var2 = "D: \ \ desktop \ \ test2. TXT"
os.rename(var1, var2)
print(var1, "Renamed", var2)
pass
Os. remove([string -- filename])
os.remove(var2)
print(var2, ", the file has been deleted!)
pass
# change current directory chdir([string]) method
var3 = "D: \ \ desktop \ \"
os.chdir(var3)
print("Directory jumped to:", var3)
pass
Os.mkdir ([string])
var4 = "123456789"
# os.mkdir(var4)
print("Created successfully!")
pass
# os.rmdir([string]) Rmdir () deletes a directory whose name is passed as an argument. Delete folders
var5 = var3+var4
# os.rmdir(var5)
print(var3, var4, ", the file has been deleted!)
pass
# File Write multi - line File writelines([string])
var6 = "D: \ \ desktop \ \ test123. TXT"
# create file os.mknod([string -- file_path])
# os.mknod(var6)
fo2 = open(var6, "w+")
The # readline() method is used to read the entire line from the file, including the "\n" character. If a non-negative parameter is specified, the number of bytes of the specified size, including the "\n" character, is returned
line_read = fo2.readline(10)
print("Read string: %s" % line_read)
The # writelines() method is used to write a sequence of strings to a file
a = 0
while 10 > a:
fo2.writelines("Here I go!)
a += 1
print("Entry successful!")
pass
The # isatty() method checks if the file is connected to a terminal device, returning True if it is, False otherwise
ret = fo2.isatty()
print(ret)
# flush buffer
fo2.flush()
# close file
fo2.close()
pass
Copy the code