current position:Home>Python interface automation test framework (basic) -- basic syntax

Python interface automation test framework (basic) -- basic syntax

2022-01-31 09:42:41 I am the white moonlight you can't get

「 This is my participation 11 The fourth of the yuegengwen challenge 10 God , Check out the activity details :2021 One last more challenge

Preface

The author's python The basics of programming has written about functions and built-in functions , Although I didn't say it python How much writing grammar , Write code, talk about concepts, and then demonstrate with code , The author acquiesces that readers should or self-study python Basics , But for most of Xiaobai's classmates, they may be at a loss , Therefore, a basic grammar is specially added to solve doubts .

Basic grammar

code

python The language used in most cases is recognized as a scripting language , Because it can be used with the smallest py Run as a module , No compilation required , It's very convenient to use it immediately , stay linux The environment is like a fish in water ; Therefore, its beginning needs to have the beginning of coding format , But it defaults to utf-8 Coded Unicode character string , So I often don't see this line ; Of course, you can specify other formats if necessary Unicode Coding format

# -*- coding: utf-8 -*-
 Copy code 

identifier

Identifiers are variables 、 Class name 、 A general term for method names , Not as an individual ; Then there are several special points to pay attention to :

  • Must be in letters or _ Start with an underline , It can't be a number 、$ The beginning of the dollar sign
  • Other parts after the beginning can have letters 、 Numbers 、 Underline composition
  • Identifiers are case sensitive

Identifier naming rules

Rules matter , Don't mess up , It's better to see the name and the meaning ; The second is the writing rules , Improve readability

  • Follow identifier naming rules : Must be letters or underscores _ start
  • Between variables and values = The assignment symbol has a space :a = 1
  • If the variable is long , Use underscores to connect :first_name = "Joe"
  • The class name specification suggests that the name of the big hump , Capitalize the first letter of a word :class MyFirstClass:
  • The method name is the same as the variable name , If there is inheritance , Try to avoid _ or __ Name at the beginning
  • Little hump nomenclature , Less in python See you in :firstName
  • All variable or method names , Try not to use built-in keywords to name , Lest success cover
  • python Provides a keyword modular , There are all keywords in it ( Reserved words )
import keyword

print(keyword.kwlist)
#  Output :['False', 'None', 'True', 'and', 'as', 'assert', 'async', 'await', 'break', 'class', 'continue', 'def', 'del', 'elif', 'else', 'except', 'finally', 'for', 'from', 'global', 'if', 'import', 'in', 'is', 'lambda', 'nonlocal', 'not', 'or', 'pass', 'raise', 'return', 'try', 'while', 'with', 'yield']
 Copy code 

notes

Develop good coding habits , Write notes for your convenience , Not the next one .

  • Single-line comments , Use # Well No

#  This is a line of comments , Will not be output 

a = 1

print(a) #  This is the second line of comment 

#  Output :1
 Copy code 
  • Multiline comment , Three single or double quotation marks in pairs
'''  The comments here will not be output during the running of the program  '''

"""  This also means multi line comments  """

print("Hello World!")
 Copy code 

Lines and indents

The author is learning python When , Almost crazy by its indentation , One is really not used to ( from java To python The transformation of ), Second, there is no good use of IDE Editing tools

  • That's ok , If there are no special circumstances , The code should be aligned with the top grid , If there is , Then you need to indent , It has no {} Such symbols represent blocks of code

a = 1

b = 2

for i range(10):
print(a) #  It's wrong here , It's a new line , stay : Subsequent blocks of code must be indented 
    print(a) #  For for Yes, there is 4 A space 
    
 Copy code 
  • Multiple lines , A string variable or other type , If you want to represent it as a line, you need to use \ Symbol

a1 = " Here is a long string , If it is too long, it will affect the appearance , You need to wrap to beautify "

print(a1) 

a2 = " Here is a long string ,\  If it is too long, it will affect the appearance ,\  You need to wrap to beautify "

print(a2) #  The output is the same 
 Copy code 

Input and output

  • input(), Input function ,python Use... When interacting with clients , The input is a string type
  • print(), Output function , You can format and output any data type ; Later, we will talk about string formatting output

One line shows multiple statements

  • stay python in ; Semicolons are separators
import sys;print(sys.__doc__)
#  Output sys All methods, attributes and usage instructions in the module 
 Copy code 

Guide pack

This is used in encapsulation or reference , The format is import、from...import; The main function is to introduce external modules or third-party libraries , That is not in this py Defined in module , External libraries need to be introduced

import sys

print(sys.platform) #  Output the system of the current environment :win32
 Copy code 

summary

python The grammatical basis of programming , It's over here , More and more interesting knowledge will be introduced later ; At the same time python Learning from , Don't limit yourself to reading 、 read 、 Go to the Forum , It should be practice more often 、 Constantly consolidate the basic knowledge , The foundation is not solid 、 The earth trembled and the mountains swayed , Remember !

copyright notice
author[I am the white moonlight you can't get],Please bring the original link to reprint, thank you.
https://en.pythonmana.com/2022/01/202201310942373987.html

Random recommended