1. Variable definition
The type of the Python variable does not need to be defined, only the name of the variable is defined, i.e. A = 10
Variable gets the value of the type, then the data type of the variable is the type
2. Input of data
Python input () can be added with input ("Please enter:").
It is important to note that the default input type of Python is a string string, so a number is required to force the conversion of the data
such as int (...), float (), str (), etc., conversions involving other advanced data types are not mentioned for the time being.
1 int = int (input (" integer:"))2 float = float (input (" Decimal:"))3 string = input (" string:")
View Code
Note: When you cast a data type that cannot be successfully converted, an error occurs and a try throw exception is required
1 Try:2 3int = Int (input ("integer:"))4float = Float (input ("decimal:"))5string = input ("string:")6 7 exceptValueError:8 Print("Invalid parameter passed in! ")9 Else:Ten Print("Success!")View Code
Here is a simple application of the exception handling content
3. Functions
Python's function style: def function name (< variable name >,<...>):
Definition requires only one keyword Def, and no return type is required relative to other languages
1 #Sum function2 3 4 defsum (A, b):5 """sum"""6 7 returnA +b8 9 #Default ParametersTen One A deffunction (A, B = 50): - """function Default Parameters""" - the Print(A +"-"+b) - return
View Code
This paper introduces two commonly used methods of parameters, the default is that when the parameter is not passed the value of B, then B defaults to 50.
In the first line of the function, the function is generally described in the form of multiple lines of annotations, so that the function name is ctrl+q at the time of invocation, which shows the annotations to the functions, as shown in:
Finally, the function either has a return type, plus return, indicating the end
4.list List
It can also be called an array, partially functionally combining the idea of a stack in a data structure
1 #Create lists and tuples2List1 = ["AA","BB","cc","DD"]3Seq = ("ee","FF")4 5 #length6 Len (list1)7 8 #tuples converted to lists9List2 =list (seq)Ten One #adding elements AList1.append (["FF"]) - list1.extend (List2) - Print(List1) the - #Query element Index location - Print(List1.index ("FF")) - + #Insert -List1.insert (0, ["GG"]) + Print(List1) A at #removed from -List1.pop (5) - Print(List1) -List1.remove ("ee") - Print(List1) -List1.clear ()View Code
Create lists and tuples, this way only the list, tuple is not change the value of the list, relatively simple
Get the length of the list, using Len (), this function can be used in many ways
Add elements, the main two append and extend, both can only be added at the end of the list, append can retain the original format, such as adding a list, then in the display is nested inside a list
and extend is directly into the content of the object is split into a separate to add
Index is simple, query the index of the existence of values, if the value does not exist will be an error, the use of this method requires attention
Insert insertion, passing in the position of two variables inserted and the incoming object
Move out there are three kinds, pop (int), delete the value at the specified position, if not fill the default to the end
Remove ("value"), which is deleted by getting the value, both of which can be an error if the value or position does not exist
Finally clear, clear the list
5. Dictionaries
The dictionary obtains the corresponding value by the key key value
1 #Dictionary2Dictionary = {"name":"HJHJ"}3 4 #Get Value5 Print(Dictionary.get ("name"))6 7 #Update8Dictionary.update ({" Age": 20,"name":"aaaaaa"})9 Print(dictionary)View Code
Dictionaries are unordered and only accessed through key values
By using a combination of lists and dictionaries, data can be formed in JSON format, which can be parsed in this way for data captured online.
{"Heweather":
[{"Basic": {
"CID": "CN101190401",
"Location": "Suzhou",
"Parent_city": "Suzhou",
"Admin_area": "Jiangsu",
"Cnty": "China",
"Lat": "31.29937935",
"Lon": "120.61958313",
"TZ": "+8.00",
"City": "Suzhou",
"id": "CN101190401",
"Update": {
"Loc": "2018-08-07 10:48",
"UTC": "2018-08-07 02:48"},
"Update": {
"Loc": "2018-08-07 10:48",
"UTC": "2018-08-07 02:48"},
"Status": "OK",
"Now": {
"Cloud": "6",
"Cond_code": "104",
"Cond_txt": "Yin",
"FL": "37",
"Hum": "70",
"PCPN": "0.0",
"Pres": "1006",
"TMP": "33",
"Vis": "20",
"Wind_deg": "150",
"Wind_dir": "Southeast Wind",
"WIND_SC": "2",
"WIND_SPD": "9",
"Cond": {
"Code": "104", "TXT": "Yin"}
},
"Daily_forecast": [
{"Date": "2018-08-07", "cond": {"Txt_d": "Clear"}, "tmp": {"Max": "$", "min": "28"}},
{"Date": "2018-08-08", "cond": {"Txt_d": "Clear"}, "tmp": {"Max": "$", "min": "27"}},
{"Date": "2018-08-09", "cond": {"txt_d": "Cloudy"}, "tmp": {"Max": "$", "min": "27"}}],
"AQI": {
"City": {"AQI": "", "" PM25 ":" "," "Qlty": "Excellent"}},
"Suggestion": {"Comf": {"type": "Comf", "BRF": "Very uncomfortable", "TXT": "The weather is fine during the day, but the scorching sun will make you feel very hot and uncomfortable." "}," sport ": {" type ":" Sport "," BRF ":" Less suitable "," TXT ":" The weather is good, but the temperature is very high, the wind is larger, please reduce the exercise time and reduce the intensity of exercise, outdoor sports should pay attention to the shelter sun. " "}," CW ": {" type ":" CW "," BRF ":" More appropriate "," TXT ":" More suitable car wash, no rain in the coming day, less wind, a new car scrub can at least maintain a day. " "}}}]}
6. String
String involves a lot of methods, here is a simple introduction to a few common
1 #string2String ="ASD fghjkla SDF"3 4 #Count5num = String.count ("a")6 Print(num)7 8 #Find Index9num2 = String.index ("a")Ten Print(num2) One A #gets the specified position character - Print(string[0]) - the #Split -List = String.Split (" ") - Print(list) - + #merging into Strings -List.insert (1,"ssssss") +string2 =" ". Join (list) A Print(string2)View Code
--------------------------------------------------------------------------------------------------------------- ----------
It's here for the time being
Python Basic Learning