Python basic syntax review, python syntax Review

Source: Internet
Author: User

Python basic syntax review, python syntax Review

Because I took the AI Pattern Recognition Course and asked to use phthon to implement algorithms, I did not take any classes on Wednesday evening. I 'd like to review the main Syntax of python.

Environment: Anaconda

Python3.6

1. variables:

  • In python, variables do not need to be declared in advance.
1 #data type2 str_test = "China"3 int_test = 1234 float_test = 122.55 6 print(str_test)7 print(int_test)8 print(float_test)  
  • The type can be converted. Use type to view the variable type.
1 # Convert 2 str_eight = str (8) 3 eight = 8 4 str_eight_two = str (eight) 5 6 str_eight = "8" 7 int_eight = int (str_eight) 8 9 print (int_eight) 10 print (type (int_eight ))

2. list type

  • The element types in the list type can be different. When an element is added, list. apppend () can be used. When an element is added, index of the subscript is automatically set. You can use the subscript to access the element in the list.
 1 countries = [] 2 temperatures = [] 3  4 countries.append("China") 5 countries.append("India") 6 countries.append("United States") 7  8 temperatures.append(30.5) 9 temperatures.append(25.0)10 temperatures.append(15.1)11 12 print(countries)13 print(temperatures)14 15 china = countries[0]16 china_temperature = temperatures[0]17 print(china)18 print(china_temperature)
  • Calculate the list length and slice. It is worth noting that list [-1] = list [length-1], that is, subscript is cyclic.
1 int_months = [1, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12] 2 length = len (int_months) 3 print (length) 4 index = len (int_months) -1 5 last_value = int_months [index] 6 print (last_value) 7 print (int_months [-1]) # countdown can also be 8 # Slice 9 two_four = int_months [] # Get the header without taking the end 10 print (two_four) 11 tree_last = int_months [3:] 12 print (tree_last)

 

3. Program Structure

  • Loop> for, range () usage is worth noting
1 #loop2 cities = ["Austin","Dallas","Houston"]3 for city in cities:4     print(city)5 for i in range(10):6     print(i)
  • Loop> while
1 i = 02 while i < 3:3     i += 14     print(i)
  • Dual Loop
1 cities = [["Austin","Dallas","Houton"],["Haerbin","Shanghai","Beijing"]]2 print(cities)3 #for city in cities:4     #print(city)5     6 for i in cities:7     for j in i:8         print(j)
  • Judgment statement
1 # if statements2 sample_rate = 7003 greater = (sample_rate> 5) 4 if greater: # It can also be expression 5 print (sample_rate) 6 else: # You can also print ('less thance') 7 without writing else ')
  • Abbreviation of search in list
1 #find a value 2 animals = ["cat","dog","rabbit"]3 for animal in animals:4     if animal == "cat":5         print("Cat found")6 if "cat" in animals:7     print("2 is also right")

4. dictionary type

  • Create, initialize, and assign values to dictionaries
 1 students = {} 2 students["Tom"] = 60 3 students["Jim"] = 70 4 print(students) 5  6 students = {} 7 students = { 8     "Tom": 60, 9     "Jim": 7010 }11 print(students)

 

  • Dictionary application-count
1 # statistics 2 pantry = ["apple", "orange", "grape", "apple", "orange", "apple", "tomato", "potato ", "grape"] 3 pantry_counts ={} 4 5 for item in pantry: 6 if item in pantry_counts: 7 pantry_counts [item] = pantry_counts [item] + 1 8 else: 9 pantry_counts [item] = 110 print (pantry_counts)

5. File Processing

  • File Reading and Writing
1 f = open ("test_write.txt", "w") # automatically create a file that does not exist. write ("123456") 3 f. write ("\ n") 4 f. write ("234567") 5 6 f. close ()
1 # File 2 # open 3 f = open ("test.txt", "r") 4 5 # process 6g = f. read () 7 print (g, type (g) 8 9 # disable 10 f. close () # Do not forget to close

 

  • File csv operation examples and use of split ()
 1 weather_data = [] 2 f = open("weather.csv",'r') 3 data = f.read() 4 #print(data) 5 rows = data.split("\n") 6 #print(rows) 7 for row in rows: 8     split_row = row.split(",") 9     print(split_row)10     weather_data.append(split_row[0])11 print(weather_data)12 f.close()

During this operation, I first created an xlsx file, and then changed the name to a csv file. I couldn't open the file, and used the "rb" operation to display garbled characters. Then I found that saving the file as a csv can solve this problem well, and I also had a deep understanding of the composition of the csv file.

6. function operations

 1 def printHello(): 2     print("hello python") 3      4 def printNum(): 5     for i in range(0,10): 6         print(i) 7     return 8 def add(a,b): 9     return a+b10 printHello()11 printNum()12 add(1,2)

Different from C/C ++, input parameters do not need to declare the type.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.