7.17python learning content summary

7.17python learning content summary

python branch structure

The basic syntax

if conditional expression:

Statement

​ ......

The conditional expression must evaluate to a Boolean expression can not be the end of the colon Province

If you belong to the same if, the same statement block indentation level

Two-way branch structure

if conditional expression:

Statement

​ ......

else:

Statement

​ ......

Multiple branches

if conditional expression:

Statement

​ ......

elif conditional expression:

Statement

​ ......

else:

Statement

​ ......

There can be multiple elif

else optional

It will eventually execute a branch

python flow control

if sentence structure

if sentence structure:

if <Condition 1>:

<Statement 1>

elif <Condition 2>:

<Statement 2>

​ else:

<Statement 3>

At this time, when the statement execution sentence 2 2 1 ,; condition is established when the condition 1 is established; executing the statement 3:00 Conditions 1 and 2 are not established.


if statement may also be used in a single sentence, to achieve ternary operator, basic forms: <. 1 Expression> if <condition> the else <Expression 2>


Nested if statements

if <condition>:

if <condition>:

<Statement 1>

elif <Condition>:

<Statement 2>:

​ else:

<Statement 3>

for loop

Python language for the statement and other high-level programming languages ​​are very different, other high-level language statements to use for loop control variable to control the loop. Python for statement by the loop through a sequence of objects (strings, lists, tuples, etc.) to construct the cycle, the end of the loop condition is traversed object is completed.

  Form for statement is as follows:

for <loop variable> in <cyclic object>:

<Statement 1>

​   else:

<Statement 2>

  else statement 2 statement executes only the normal cycle exit (finish traverse the value of all the traverse object).

while loop

while judging condition:
execute a statement ......

Exception Handling

What is unusual

An exception is the error signal generation program is running (when the program error occurs, an exception, if the program does not handle it, it will throw the exception, run the program also will be terminated)

Unusual species

Common abnormalities
  • AttributeError no attempt to access an object tree, such as foo.x, but no property x foo
  • IOError input / output is abnormal; substantially not open file
  • ImportError package or module can not be introduced; substantially path problem or error name
  • IndentationError syntax error (subclass); the code is not properly aligned
  • IndexError subscript index out of sequence boundaries, such as when only three elements of x, x is trying to access [. 5]
  • KeyError trying to access key does not exist in the dictionary
  • KeyboardInterrupt Ctrl + C is pressed
  • NameError use a variable has not been given an object
  • SyntaxError Python code is illegal, the code does not compile (personally think it's a syntax error, wrong)
  • TypeError incoming object type does not conform with the requirements of
  • UnboundLocalError trying to access a local variable has not been set, basically due to a global variable otherwise the same name leads you to think that it is being accessed
  • ValueError incoming caller does not expect a value, even if the value of the type is correct
After the error exception handling

try:
Code block is detected
except exception types:
the try in the event of an anomaly is detected, the logic executed at this location

Exercise today

1. The while loop output 1234568910

a = 0
while a<10:
    a=a+1
    print(a)

2. All numbers 1-100 and seek

a=0
sum1=0
for i in range(100):
   a+=1
   sum1=a+sum1
print(sum1)

3. The outputs of all the odd-numbered 1-100

a =0;
for i in range(100):
    a+=1
    if a%2==1:
        print(a)

4. Output all even within 1-100

a =0;
for i in range(100):
    a+=1
    if a%2==0:
        print(a)

The request and all numbers 1-2 + 3-4 + 5 ... 99

a=0
b=0
sum=0
for i in range(50):
    a+=1
    b+=2
    sum=a-b
print(sum)

6. User Login (three chances to retry)

user = "guowei"
password = "guowei"
cs = 0;
for i in range(3):
    username = input("username:")
    password = input("password:")
    if username == user and password == password:
        print("Welcome Login")
        cs = 3
        break
    else:
        print("Wrong username or password")
        cs += 1
        print("you can try", 2 - i, "times")

7. guess the age of the game

age = 18
cs = 0;
for i in range(3):
    guess = input("请输入猜的年龄")
    if guess == age:
        print("你猜对了")
        cs = 3
        break
    else:
        print("猜错了")
        cs += 1
        print("你还可以猜", 2 - i, "次")

9.for print circulation 99 multiplication table

for i in range(1,10):
    for j in range(1,10):
        print('%s×%s=%s'%(i,j,i*j))

10.for circulation print pyramid: as follows

for i in range(1,5):
        print(' '*(5-(i-1))+'*'*(2*i-1))

Guess you like

Origin www.cnblogs.com/gw1027/p/11202962.html