First article on python data types

Keywords: PHP Python encoding less

IO file output

Question 1: How does Python implement print without line change?

By default, Python's print() function is newline, such as

print("Hello Dacheng")
print("!!!")

# Output
Hello Dacheng
!!!

Look at the source code of the print() function. By default, end=" n" (newline), n means newline character.

So, if you want to not change the line, you need to change the line property of end and replace the n of end with any value you want.

The effect of cross-line stitching can be achieved by using print line breaking.

print("Hello Dacheng", "end=")
print("!!!")

# Output
Hello Dacheng!!!


print("Hello Dacheng", "end="@@Let's go for a tucky meal in the evening \")
print("???!!!")

# Output
Hello Dacheng @@Let's go for a meal in the evening. !!!

Question 2: How does Python implement input interaction?

Implemented by input() function

In Python 3, raw_input() and input () are integrated, only input () is retained, and string type is returned.

a = input("Please enter your name:")
print(a)
print(type(a))

c=input("Please enter your age:")
print(c)
print(type(c))

# Output
Please enter your name: Big Orange
Big orange
<class 'str'>
Please enter your age: 30
30
<class 'str'>

So, when you need floating-point or integer types, you need to convert the input string.

a = input("Please enter your name:")
print(a)
print(type(a))


c=int(input("Please enter your age:"))
print(c)
print(type(c))

b = float(input("How much tax do you pay every month?\n"))
print(b)
print(type(b))


# output
Please enter your name: Big Orange
//Big orange
<class 'str'>
//Please enter your age:30
30
<class 'int'>
//How much tax do you pay every month?
644.54
644.54
<class 'float'>

Question 3: How does Python manipulate files?

1. Write documents:

(1) Open the file first: the open() function

(2) Write file: write() function

(3) Close the file: close() function

a = input("Please enter your name:")
print(a)
print(type(a))


file = open("test.txt",'w')  # Open a document, write something into it, and you need to mode Change the value to w
file.write(a)  # Write content to a file
file.close()   # Close the one just opened test.txt file

By default, encoding encoding is not specified when writing files. It can be mandatory in the code, or it can be set in the editor, depending on the needs of the project.

utf-8 is a universal encoding language in the world computer. Any language can be recognized by utf-8 encoding.

a = input("Please enter your name:")
print(a)
print(type(a))

file = open("test.txt",'w', encoding='utf-8')  # Open a document, write something into it, and you need to mode Change the value to w
file.write(a)  # Write content to a file
file.close()   # Close the one just opened test.txt file
Parameter values of open() function mode:

"r": Open Reading (default)

"w": Open for writing, first truncate the file, if there is already content in the file, the existing content will be overwritten.

"x": Create a new file and open it for writing

"a": Open for writing and attach to the end of the file if it exists

"b": Binary mode

"t": Text mode (default)

"+": Open disk file to update (read and write)

2. Reading Documents

file = open("hello.py" , "r")  # Open the file first
b = file.read()    # Reading File Content
file.close()      # Close the newly opened file
print(b)

Exercises

# According to the name of the file entered by the user, open the file and print out the contents of the file.

fileName = input("Please enter the file name:\n")
file = open(fileName,'r',encoding="utf-8")
fileText = file.read()
print(fileText)

Question 4: How does string formatting work?

Two commonly used string formatting methods:

1.% mode

print("Wumart is doing activities today, Man%s Yuan, minus%s element" %(200,60))
print("Wumart is doing activities today, Man%d Yuan, minus%s element" %('200',60))   # Error will be reported because the parameter value is a string and the previous received value is a string.

# Disadvantage: The parameter type must be the same as the received parameter type
print("Octal number system %o ,\n Decimal system %i, \n Hexadecimal %x ,\n Scientific Counting %e ,\n Floating Point %f"
      %(99, 99,99,999.9999999,99.999))
# Satisfies 6 digits, does not need to fill, retains four decimal places
print("Floating point formatting% 06.4f" (66666666.666666))

# 06 denotes width, less than 6 bits are complemented by 0, leaving four decimal places.
print("Floating point formatting% 06.4f" (6666.666666)) 

# Output
Floating-point formatting 66666666.6667
Floating-point formatting 6666.6667

 

2.format (new string formatting method in Python 3, recommended)

print("Wumart is doing activities today, Man{0}Yuan, minus{1}element".format(200,60))
print("Wumart is doing activities today, Man{0}Yuan, minus{1}element".format('200',60)) 
print("Wumart is doing activities today, Man{}Yuan, minus{}element".format('200',60))

# {}Numbers in it represent format()Location of internal parameters
# {}When there are no numbers in it, the default is to follow format()The position of the inner parameter goes to{}Infill value
# Advantages: Avoiding%Disadvantage of inconsistent types of parameters
print("Octal number system {:o} ,Decimal system {:d}, Hexadecimal {:x} ,Scientific Counting {:e} ,Floating Point {:.2f}".format(99, 99,99,999.9999999,666.6666))
print("Octal number system {:o} ,Decimal system {:04d}, Hexadecimal {:x} ,Scientific Counting {:e} ,Floating Point {:.2f}".format(99, 99,99,999.9999999,666.6666))
print("Octal number system {:o} ,Decimal system {:04d}, Hexadecimal {:x} ,Scientific Counting {:e} ,Floating Point {:08.2f}".format(99, 99,99,999.9999999,666.6666))
# : 04  The width is 4 bits, less than 4 bits are filled with 0.
# : 08.2f  Represents a width of 8 bits, including decimal points, less than 8 bits are filled with 0, leaving 2 decimal points.

Posted by matthewhaworth on Sat, 27 Jul 2019 04:48:39 -0700