程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Summary of basic Python knowledge part 2

編輯:Python

List of articles

  • Preface
  • The organizational structure of the program
    • Sequential structure
    • Selection structure
      • Boolean value
      • Single branch selection structure
      • Two branch selection structure
      • Multi branch selection structure
      • nesting if Use
  • Conditional expression
  • pass sentence
  • range function
  • loop
    • while Loop statement
    • for-in loop
    • Flow control statement
      • break
      • continue
      • else

Preface

The article is synchronized with my personal blog https://quan9i.github.io/python2/ in , Welcome to

The organizational structure of the program

Divided into three , Sequential structure , Loop structure and selection structure

Sequential structure

The program structure executed from top to bottom in sequence is called sequential structure
Sample code :

print('------- Program starts -----')
print('1、 Open the refrigerator door ')
print('2、 Put the elephant in ')
print('3、 Close the refrigerator door ')
print('------- Program end -----')

Execution results

Selection structure

Before learning this structure , First you need to learn bool value

Boolean value

Code :

print(bool(False))
print(bool(0))
print(bool(0.0))
print(bool(None))
print(bool(''))
print(bool(""))
print(bool([])) # An empty list 
print(bool(list())) # An empty list 
print(bool(())) # An empty tuple 
print(bool({
})) # An empty dictionary 
print(bool(dict())) # An empty dictionary 
print(bool(set())) # Empty set 
print(' The above is bool Values are false')

Execution results

Single branch selection structure

Sample code :

money=2000
s=int(input(' Please enter the withdrawal amount :'))
if money >=s:
money = money-s
print(' Successful withdrawals , Your balance is :',money)

Execution results

When the withdrawal amount is greater than 2000 when , There is no output result

That is, not implemented if Next sentence , Just skip , This is a single branch selection structure .

Two branch selection structure

Code :

s=int(input(' Please enter a number , Let me decide whether it is odd or even :'))
print(type(s))
s=s%2
print(s,type(s))
if s==0:
print(' This is an even number ')
else:
print(' This is an odd number ')

Execution results

Multi branch selection structure


Code :

score=int(input(' Please enter your score :'))
if score >=90 and score <=100:
print('A level ')
elif score >=80 and score <90:
print('B level ')
elif score >=70 and score <80:
print('C level ')
elif score >=60 and score <70:
print('D level ')
elif score >=0 and score<60:
print(' fail, , Stop learning ')
else:
print(' The result output is incorrect ')

Execution results

nesting if Use

Sample code :

money=float(input(' Please enter your purchase amount :'))
vip =input(' Are you a member ?/r/n')
if vip =='r':
if money>=200:
print(' 20% off , Your purchase amount is :',money*0.8)
elif money>=100:
print(' 10% off , Your purchase amount is :',money*0.9)
else:
print(' No discount , Your purchase amount is :',money)
else:
if money>=200:
print(' Give a 95% discount , Your purchase amount is :',money*0.95)
else:
print(' No discount , Your purchase amount is :',money)

Execution results

Conditional expression

The condition code is relatively simple , A lot of code will be saved compared with the conventional method , As shown in the figure below
Sample code :

num1=input(' Please enter the first integer :')
num2=input(' Please enter the second integer :')
# General code 
""" if num1>=num2: print('num1>=num2') else: print('num1<num2') """
# Next, use the conditional expression 
print(num1+' Greater than or equal to '+num2 if num1>=num2 else num1+' Less than '+num2)
#if The following statement is a judgment statement , If the result is true, Just execute the sentence on the left , Otherwise, execute the sentence on the right 
# You can add... Before variables str(), Easy to read , Although I can't feel 

Execution results

pass sentence

When constructing a statement , But I didn't think about how to write the content , You can use it first pass To replace , This avoids statement errors
Sample code :

money=float(input(' Please enter your purchase amount :'))
vip =input(' Are you a member ?/r/n')
if vip =='r':
pass
else:
pass

range function

A built-in function means that it can be called directly in the code , You don't have to enter the code of the reference class at the top
The return value is the iterator object
An iterator can be simply understood as “ Built in for Iteratable object of a loop ”, Every time you use next () Function to access the iterator object once , It returns the current element at the same time , The internal pointer will point to the next element .

Three ways to use range, As shown below
Code :

q=range(7)#7 It means the end bit , If the start digit is not filled in , The default is 0, If the step size is not filled in , The default is 1
print(q)# Output results 
print(list(q))# Output each number 
w=range(3,7)# It means from 3 Start , To 7 end , Step size not written , The default is 1
print(w)# Output results 
print(list(w))# Output each number 
e=range(2,6,2)# It means from 2 Start , To 6 end , In steps of 2
print(e)# Output results 
print(list(e))# Output each number 

Execution results

loop

while Loop statement

Sample code :

a=0 # First step , Initialize variable 
sum=0 # For storage and storage 
while a<5: # The second step , conditional 
sum+=a # The third step , Conditional executors , Sum up 
a+=1 # Step four , Change variables 
print(' Current sum is :',sum)

The specific process can be understood as follows :

So please 1-100 The code for the even sum of is similar , As shown in the figure below

i=1 # First step , Initialize variable 
sum=0 # Storage and 
while i<101: # The second step , conditional 
if i%2==0: # To judge whether it is an even number 
sum+=i # Sum up , These two are also the third step , Conditional executors 
i+=1 # Step four , Change variables 
print('1~101 The even sum of is :',sum)

The execution result is

To find the odd sum, you just need to change the line to judge whether it is even , Here's the picture

print(bool(1%2)) # In order to make i%2 Easy to understand 
i=1
sum=0
while i<101:
if i%2:# i%2!=0, It can be understood as if i Can not be 2 to be divisible by , Just execute the following statement 
sum+=i
i+=1
print('1~101 The odd sum of is :',sum)

Execution results

If you can't understand , You can refer to This article Think again

for-in loop


Specific examples are as follows

for a in 'helloworld': // take helloworld The letters in are assigned to a
print(a)
for i in range(10): //[1,10) Assign values to i
print(i)
for _ in range(5): // loop 5 Time
print('helloworld')

The results are as follows
Referred to the for in, Let's review it here for loop , Examples are as follows ( Multiplication tables )

for i in range(1,10): //[1,10) Assign values to i
for j in range(1,i+1): //[1,i+1) Assign values to j
print(i,'*',j,'=',i*j,end='\t') // The output format is i * j = x i * j = x
print() // Each time the internal loop is completed, a new line is generated

The results are as follows

Flow control statement

break

Examples are as follows

b=0
for a in range(10):
print(a)
b+=1
if b==8:
break

Execution results

continue

b=0
for a in range(10):
print(a)
b+=1
if b>3:
continue
print(222)

Execution results

else

for b in range(10):
if b>3:
print(222)
else:
print('b>=4')

The results are as follows



  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved