Summary of common python statements

爱喝马黛茶的安东尼
Release: 2019-06-14 15:43:39
Original
7050 people have browsed it

In the process of learning python, we will encounter some statements that we often encounter. Below I will take stock of these statements to facilitate everyone's memory.

Related recommendations: "python Video"

Summary of common python statements

Commonly used python statements:

(1), assignment: Create variable reference value

a,b,c='aa','bb','cc'
Copy after login

(2), call: execute function

log.write('spam,name')
Copy after login

Print, output: call print object, print statement

print ('abc')
Copy after login

(3) if, elif, else: select conditional statements, if statements, else and elif statements

if 'iplaypython' in text:
    print (text)
Copy after login

(4) for, else: sequence iteration

for x in list:
    print x
Copy after login

(5), while , else: general loop statement

while a > b :
    print 'hello'
Copy after login

(6), pass: placeholder (empty)

while True:
    pass
Copy after login

(7), break: exit the loop

while True:
    if exit:
        break
Copy after login

(8) , continue: skip the current loop and continue the loop

while True:
    if skip:
        continue
Copy after login

The following statements are used in relatively large programs. Regarding functions, classes, exceptions and modules, they also only briefly introduce the purpose and format of the methods.

(9), def: define functions and methods

def info(a,b,c=3,*d):
    print (a+b+c+d[1])
Copy after login

(10), return: function return value

def info(a,b,c=3,*d):
    return a+b+c+d[1]
Copy after login

(11), python global: namespace, role Domain

x = 'a'
def function():
    global x ; x = 'b'
Copy after login

(12), import: access, import module

import sys
Copy after login

(13), from: attribute access

from sys import stdin
Copy after login

(14), class: create Object, class definition method

class Person:
    def setName(self,name):
        self.name = name
    def getName(self):
        return self.name
    def greet(self):
        print 'hello,%s' % self.nameclass Person:
    def setName(self,name):
        self.name = name
    def getName(self):
        return self.name
    def greet(self):
        print 'hello,%s' % self.names
Copy after login

(15), try, except, finally: python catch exception

try:
    action()
except:
    print ('action error')
Copy after login

(16), raise: trigger exception

raise Endsearch (location)
Copy after login

(17), assert: python assertion, inspection and debugging

assert a>b ,'a roo small'
Copy after login

The above is the detailed content of Summary of common python statements. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template