Python basic notes for beginners
Enter multiple parameters in one line of code
Method 1.) a, b, c = map ( type , input ( "Enter") .split ())
# default separated by spaces , to other types of transfer, into the desired type
such as rotation ----- Integer : map(int,input("Please input").split())
a,b,c=map(int,input("please enter").split()) print(a+b+c,type(a),type(b),type(c)) #Extension: When is When it is a character string, the character string is added, then the'+' sign is the splicing effect # When it is an integer, the integer is added, then the'+' sign is the addition operation in mathematics >>>Please enter 1 2 3 > >>> 6 <class'int'> <class'int'> <class'int'>
Such as-----to string : map(str,input("Please input").split())
a,b,c=map(str,input("please enter").split()) print(a+b+c,type(a),type(b),type(c)) #Extension: When is When it is a character string, the character string is added, then the'+' sign is the splicing effect # When it is an integer, the integer is added, then the'+' sign is the addition operation in mathematics >>>Please enter 1 2 3 > >> 123 <class'str'> <class'str'> <class'str'>
Method 2.) a,b,c=eval(input("Please enter"))
#default comma separation, what type you enter is what type, (Note: the input string needs to be enclosed in quotation marks with English letters)
Advantages: Convenient and concise, disadvantages: the security is not high , it is not recommended to use when it comes to projects, it is better to use input
a,b,c=eval(input("Please enter")) print(a,b,c,type(a),type(b),type(c)) print(a+b,c,type(a ),type(b),type(c)) #Extension: When it is a string, the string is added, then the'+' sign is splicing effect #When it is an integer, the integer is added, then'+' The number is the function of addition in mathematics >>>Please enter 1,2,'你好' #The string needs to be enclosed in quotation marks of English letters >>> 1 2 Hello<class'int'> <class'int' > <class'str'> >>> 3 Hello <class'int'> <class'int'> <class'str'> 12345678910
Convert to string str
a,b,c=eval(input("Please enter")) a=str(a) b=str(b) print(a+b,c,type(a),type(b),type(c) ) #Extension: When it is a string, the string is added, then the'+' sign is splicing function #When it is an integer, the integer is added, then the'+' sign is the addition operation in mathematics >>> Please enter 1, 2,'Hello' >>> 12 Hello <class'str'> <class'str'> <class'str'> 123456789
Method 3.) a,b,c=input("Please input").split('','')
#split('','') stands for comma separation, it can also be changed to other, the output result defaults to characters String , if you want to convert to other types, you need to add the type to be converted in front
a,b=input('Please enter').split(',') print(a,b,type(a),type(b)) print(a+b,type(a),type(b)) #Extension: When it is a string, the string is added, and the'+' sign is splicing. #When it is an integer, the integer is added, and the'+' sign is the addition operation in mathematics >>>Please Enter 1,2 >>> 1 2 <class'str'> <class'str'> >>> 12 <class'str'> <class'str'> 1234567891011
Convert to integer int
a,b=input('Please enter').split(',') a=int(a) b=int(b) print(a+b,type(a),type(b)) #Expansion: When When it is a string, the string is added, and the'+' sign is splicing. #When it is an integer, the integer is added, and the'+' is the addition operation in mathematics >>>Please enter 1,2 >>> 3 <class'int'> <class'int'>
Click here to get the complete project code!