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

Python learning day-4

編輯:Python

Learning Diary for Python - 4

One 、 Ternary operator

1、C Language

  • expression - ? value 1: value 2
  • Operation rules : If the expression is true , The result of the operation is the value 1, Otherwise it's worth 2

2、python

  • expression - value 1 if expression 1 else value 2

  • Operation rules : If the result of the expression is True, Result is value 1, Otherwise it's worth 2.

    # Example :
    #1
    a=100
    result=1 if a>100 else 0
    print(result)
    #2
    a=100
    a+=1 if a>10 else -1
    print(a)
    #3
    a=100
    a=a+1 if a>10 else a-1# The value can be an operator 
    print(a)
    

Two 、while loop

1、 grammar

# Format :
while Conditional statements :
The loop body
Other code
Noun function while keyword ; Fixed writing Conditional statements An expression with a result ( Except assignment statements ): Fixed writing The loop body And while One or more statements that hold an indent ; Code that will be executed repeatedly

2、 Operation rules

First, judge whether the conditional statement is True, If so, execute the loop body ; Judge and execute after execution , Until the result of the conditional statement is False

3、 How to use it

# loop 5 Time 
times=0
while times<5
print(' loop ')
n+=1
# Infinite loop 
while True:
print(' loop ')

4、for And while Usage choice

  • The number of cycles determines the use of for; Not sure to use while
  • for Problems that cannot be solved can be reused while
# Login system 
acount={
"acount1":"123","acount2":"234","acount3":"345"}
while True:
name =input (' Please enter a user name :')
if name in acount:
break
else:
print(' The username does not exist ')
while True:
password = input (' Input password :')
if acount[name] == password:
print(' Get into ')
break
else:
print(' Wrong password ')
continue

3、 ... and 、 Circular keyword

1、continue

  • usage : End a cycle ( End the current cycle )

2、break

  • usage : End the cycle

    # Guess number games 
    import random# Import random modular 
    my_num = random.randint(1, 100)# Produce a 1 To 100( Closed interval ) The random number .
    times = 1
    while True:
    print(f' Start the first {
    times} Guess the number ')
    you_num = input(" Please enter an integer (1-100):")
    if you_num=='':
    continue
    you_num = int(you_num)
    if not 0<you_num<=100:
    continue
    if my_num == you_num:
    print(f' Congratulations on your guesses , The number is {
    my_num}')
    break
    else:
    if my_num > you_num:
    print(' The number is small ')
    else:
    print(' The number is too big ')
    times += 1
    

3、else keyword

  • Complete cycle structure

    • complete for:

      for Variable in Sequence :
      The loop body
      else:
      Code segment
      
    • complete while:

      while Conditional statements :
      The loop body
      else:
      Code segment
      
    • About else:

      • else The existence of does not affect the execution of the original loop
      • Cyclic Division break Execute at the end of the else Code snippet after
      # Determine whether the string is a stored numeric string 
      # Method 1 
      str='123456789ab123'
      for x in str1:
      if not '0' <= x <= '9':
      print(str1, ' It's not a pure numeric string ')
      break
      else:
      print(str1, ' Is a pure numeric string ')
      # Method 2 
      str1 = '123456789ab123'
      flag = True
      for x in str1:
      if not '0' <= x <= '9':
      flag = False
      break
      if flag:
      print(' Pure numeric string ')
      

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