# TextView some use techniques

2022-12-26   ES

1. IF conditions judgment
s = input (‘Name:’)
Naming = int (s)
if birth < 2000:
PRINT (‘00’))
else:
Print (‘post -after’))

Example
if <execute 1>
Elif <Terms of Judgment 2>:
<execute 2>
Elif <Judgment 3>:
<execute 3>
else:
<execute 4>
2. WHILE loop
BREAK statement (must be used with if statement)
n = 1
while n <= 100:
if n> 10: # When n = 10, the conditions are met, and the Break statement is executed
BREAK # BREAK statement will end the current cycle Break cannot be aligned with Print, otherwise it will not be output
print(n)
n = n + 1
Print (‘END’)#

CONTINUE statement (must be used with if statement)
n = 0
while n < 10:
n = n + 1
if n % 2 == 0: # If n is an even number, execute the Continue statement
Continue # Continue statement will continue the next round of cycle directly, the subsequent print () statement will not be executed
print(n)
i = 0
while i < 7:
i += 1
IF I == 3:# If i is equal to 3, the Continue statement will be executed.
continue
print(i)
3. For loop
hover = [1,5,7,25,95,68]
for y in hover:
if y ==95:
BREAK# When y is 95, quit the loop, no longer circulate the next value
print(y)
hover = [1,5,7,25,95,68]
for y in hover:
if y ==95:
print(y)
BREAK# When Y is 95, it will interrupt the cycle
4. Function parameters
def enroll(name, gender, age=6, city=‘Beijing’):
print(‘name:’, name)
print(‘gender:’, gender)
print(‘age:’, age)
print(‘city:’, city)
enroll(‘hh’, ‘m’,city=‘anhui’)
list function call
L.append(‘END’)
return L
5. List
Method description
Append () add an element at the end of the list
clear () Delete all elements in the list
Count () Returns the number of elements with a specified value.
Extend () add the list element (or any iterative element) to the end of the current list
Index () Returns the index of the first element with a specified value
Insert () Add elements at the specified location
pop () Delete the element of the specified position
Remove () Delete items with specified values
Reverse () Order of the inverted list
sort () Sort the list
6. Range function
for x in range(6):
Print (x)# from 0 to 5 does not include 6

for x in range(2, 30, 3):
Print (x)# Started from 2, add 3 when running each number, but it does not include 30 when it ends
Seven, nested loop
hov = [ 1,2,3,4,5]
hove = [28,35,36,39,52]
for w in hov:
for o in hove:
print(w,o)
8. Function
Pay parameter with list
def ccc(ooo):
for x in ooo:
print(x)
aaa = [“apple”, “banana”, “cherry”]
ccc(fruits)
default parameter
def che(zzz = “China”):
print(“I am from ” + zzz)
che(“Sweden”)
che(“India”)
che()
che(“Brazil”)
recursive function
def ff(s):
print(s)
if s <1:# Condition
return
else:
ff(s-1)
ff(10)
lambda anonymous function
def mmmm(s):
return lambda x:x + s
om=mmmm(5)
MO = mmmm (7)# Print two values respectively, or you can print a value
print(om(22))
print(mo(23))
nine, class \ object
built -in __init __ () function
class p:
def init(s, name, age):
s.name = name
s.age = age
p1 = p (“b”, 63)# Very important
print(p1.name)
print(p1.age)
object
class p:
def init(SELF, NAME, Gege, HE): Set the PP to the future:
SELF.NAME = name pp.he = Future
self.gege = gege
self.he=he
def my(self):
Print (‘Hello World My name’+Self.he)
pp = p (‘Hello Future’, ‘Hello World’, ‘Hello’)
pp.my()
ten, inheritance
Creation subclass
class Person:
def init(self, fname, lname):
self.firstname = fname
self.lastname = lname

def printname(self):
print(self.firstname, self.lastname)

class Student(Person):
def init(self, fname, lname, year):
super().init(fname, lname)

def welcome(self):
print(“Welcome”, self.firstname, self.lastname, “to the class of”, self.graduationyear)

x = Student(“Elon”, “Musk”, 2019)
x.welcome()
11, iterator
After 20 iterations stop:
class MyNumbers:
def iter(self):
self.a = 1
return self

def next(self):
if self.a <= 20:
x = self.a
self.a += 1
return x
else:
raise StopIteration

myclass = MyNumbers()
myiter = iter(myclass)

for x in myiter:
print(x)

source