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

Python Basics (statement structure)

編輯:Python

python Basics ( Sentence structure )

One 、 The organizational structure of the program

Any simple or complex algorithm can be constructed by sequential structure 、 The three basic structures of selection structure and cycle structure are combined

Two 、 Sequential structure

The program executes code sequentially from top to bottom , There is no judgment or jump in the middle , Until the end of the program

3、 ... and 、 Boolean value of the object

  • Python Everything is the object , All objects have a Boolean value

    • Gets the Boolean value of the object
      • Use built-in functions bool()
  • The Boolean values of the following objects are False

    • 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('------------ Boolean values for other objects are True-----------------')
      print(bool(18))
      print(bool("0"))
      

      Output :

      False
      False
      False
      False
      False
      False
      False
      False
      False
      False
      False
      False
      ------------ Boolean values for other objects are True-----------------
      True
      True
      

Four 、 Branching structure

Selection structure

The program selectively executes part of the code according to the Boolean value of the judgment condition

Clearly let the computer until under what conditions , What to do

Single branch structure

  • Chinese semantics : If … Just …

    If it rains , Take an umbrella

  • Grammatical structure :

    if Conditional expression :

    ​ Conditional executors

# Judge whether the balance is sufficient 
if money >=s:
money=money-s
print(' Successful withdrawals , The balance is :',money)

Input 100, Output :

 Please enter the withdrawal amount :100
Successful withdrawals , The balance is : 900

Input 1200, Don't go ahead

Two branch structure

  • Chinese semantics :

    If … dissatisfaction … Just …

    If you win the prize , If you don't win, you won't receive

  • Grammatical structure :

    if Conditional expression :

    ​ Conditional executors 1

    else:

    ​ Conditional executors 2

# Two branch structure if...else... Choose one of the two 
num=int(input(' Please enter an integer :'))
# conditional 
if num%2==0:
print(num,' It's even ')
else:
print(num,' Is odd ')

Input 9, Output :

 Please enter an integer :9
9 Is odd

Input 10, Output :

 Please enter an integer :10
10 It's even

Multi branch structure

  • Chinese semantics

    The result is 90 Above ? No

    The result is 80 To 90 Between points ? No

    The result is 70 To 80 Between points ? No

    The result is 60 To 70 Between points ? No

    The result is 60 Is it below ? yes

  • Grammatical structure :

    if Conditional expression 1:

    ​ Conditional executors 1

    elif Conditional expression 2:

    ​ Conditional executors 2

    elif Conditional expression N:

    ​ Conditional executors N

    [else:]

    ​ Conditional executors N+1

Square brackets indicate whether to write or not

Code example :

''' Multi branch structure , Select one more to execute 90-100 A 80-89 B 70-79 C 60-69 D 0 -59 E Less than 0 Or greater than 100 Is an illegal value '''
score= int(input(' Please enter a score :'))
# Judge 
if score >=90 and score <=100:
print('A level ')
elif score >=80 and score <= 89:
print('B level ')
elif score >= 70 and score <= 79:
print('C level ')
elif score >=60 and score <= 69:
print('D level ')
elif score >=0 and score <=59:
print('E level ')
else:
print(' I'm sorry , Wrong grades , Not within the valid range of the grade ')
 Please enter a score :99
A level
 Please enter a score :66
D level
 Please enter a score :122
I'm sorry , Wrong grades , Not within the valid range of the grade
  • Writing transformation

  • ''' Multi branch structure , Select one more to execute 90-100 A 80-89 B 70-79 C 60-69 D 0 -59 E Less than 0 Or greater than 100 Is an illegal value '''
    score= int(input(' Please enter a score :'))
    # Judge 
    if 90 <=score <=100:
    print('A level ')
    elif 80<=score <= 89:
    print('B level ')
    elif 70<=score <= 79:
    print('C level ')
    elif 60<=score <= 69:
    print('D level ')
    elif 0<=score <=59:
    print('E level ')
    else:
    print(' I'm sorry , Wrong grades , Not within the valid range of the grade ')
    

Output :

 Please enter a score :100
A level

nesting if

  • Grammatical structure :

    if Conditional expression 1:

    ​ if Inner conditional expression :

    ​ Inner conditional actuator 1

    ​ else:

    ​ Inner conditional actuator 2

    else:

    ​ Conditional executors

  • Code example

''' members >= 200 8 fold >= 100 9 fold Non members >= 9.5 fold No discount '''
answer= input(' Are you a member ?y/n')
money=float(input(' Please enter your purchase amount :'))
# If you are a member 
if answer=='y':
if money>=200:
print(' hit 8 fold , The amount of payment is :',money*0.8)
elif money>=100:
print(' hit 9 fold , The amount of payment is :',money*0.9)
else:
print(' No discount , The amount of payment is :',money) # Although a member , But the amount is too small to discount 
else: # Non members 
if money >=200:
print(' hit 9.5 fold , The amount of payment is :',money*0.95)
else:
print(' No discount , The amount of payment is :',money)

Output :

 Are you a member ?y/ny
Please enter your purchase amount :2000
hit 8 fold , The amount of payment is : 1600.0
 Are you a member ?y/ny
Please enter your purchase amount :100
hit 9 fold , The amount of payment is : 90.0

5、 ... and 、pass Empty statement

pass sentence

#pass sentence , Don't do anything? , Just a placeholder , Use where you need to write a statement 
answer = input(' Are you a member ?y/n')
# Judge whether you are a member 
if answer=='y':
pass
else:
pass

Preemption , Post completion

Use the Boolean value of the object

age = int(input(' Please enter your age :'))
if age: # No need to interpret , Use the Boolean value of the object directly 
print(age)
else:
print(' Age is :',age)

Output :

 Please enter your age :18
18

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