程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

[python learning 2] basic input and output

編輯:Python

(1) Basic output function print()

1, basic use

a = 100b = 50 #define a,b valueprint(a) # print the value of aprint(a+b) #Directly output the operation result of the arithmetic expressionprint("Hello",a,b) #Separated by commas, multiple values ​​can be output without newlines, and the output values ​​are separated by spaces

As can be seen from the above code snippet, print() directly outputs the result of the operation; commas can be used to output multiple values ​​without line breaks

2, the method of print() to output characters

print(chr(98)) #chr() function converts ASCII code to corresponding characterprint(ord("North")) #ord("character"), you can get the digital code corresponding to the character

chr (ASCII code), which can be converted to the corresponding character with ASCII code and output

ord("character"), you can get the digital code corresponding to the character and output it

3, print() directly outputs Chinese

From the picture, Chinese is encoded in hexadecimal, and the encoding range and encoding format of Chinese are given

print(ord("North")) #The encoding of the Chinese character North is 21271, and the corresponding hexadecimal is 5317print(ord("Jing")) #The encoding of the Chinese character Jing is 20140, and the corresponding hexadecimal is 4EACprint("\u5317\u4EAC") #Can output Chinese characters Beijing

Note: The Chinese character encoding needs to be converted to hexadecimal, and pay attention to the format written in print() when outputting

4, use print() to write content to text

fp = open("note.txt", "w") # Open the file and writeprint("Beijing welcomes you", file=fp) #Write to the file, and the file exists under the project folderfp.close

The written note.txt file and the python file exist in the same project directory.Enter the project directory through the resource manager to view the written content of note.txt

5, use print() for complex output

def print(self, *args, sep=' ', end='\n', file=None): # known special case of print"""print(value, ..., sep=' ', end='\n', file=sys.stdout, flush=False)Prints the values ​​to a stream, or to sys.stdout by default.Optional keyword arguments:file: a file-like object (stream); defaults to the current sys.stdout.sep: string inserted between values, default a space.end: string appended after the last value, default a newline.flush: whether to forcibly flush the stream."""

From the definition of the print() function:

①values ​​are separated by commas, corresponding to output multiple values ​​without a line break in front of them

②sep=" ", define the default output values ​​separated by spaces

③end="\n", after defining the default output, it will end with a newline, so outputting a value will be output in a newline

The above definitions can be modified

# can be modified as followsprint("Beijing", end="--->")print("Welcome")print(192,168,1,1,sep=".") #Use the separator. Connect the output result#The above output result is "Beijing--->Welcome" "192.168.1.1"

5.1 Use the concatenator to concatenate multiple strings

 

Note: "+" cannot connect numeric values ​​and other types, otherwise an error will be reportedp>

print("Beijing welcomes you"+2022) #This statement is considered to be a direct addition of strings and numbers, and the result cannot be obtained, so an error will be reported directly and cannot be executed. The solution is as followsprint("Beijing welcomes you"+"2022") #Use all of them as strings and then use "+" to connect, the output result is "Beijing welcomes you 2022"

(2) Basic input function input()

It is necessary to pay attention to the basic syntax structure; the input() function is uniformly output according to the string type

name = input("your name:") #When the program runs, it will stay here and wait for keyboard inputprint("Name:" + name) #Because they are both strings, you can directly use "+" to connect strings

num = (int)(input("Your number: "))#print("my number"+num) will report the wrong data type, because at this time num is a number and cannot be connected with a string with +print("My Number" , num)

Run .py files in python interactive mode:

1, cmd opens the command line terminal

2, enter python's full filename (including path)

Example: python E:\PycharmProjects\pythonProject\charpter1\exam1.py


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved