Python Learning Diary 3/10

Source: Internet
Author: User
Tags arithmetic operators

Part 1 if Judgment statement

Grammar rules

if Judge Condition:    Execute statement else:    EXECUTE statement

eg

Age_of_shen = 22Guess_age= Int (Input (">>:") )#The pseudo code is as follows" "if guess_age = = Age_of_shen then print ("yes") else print ("no")" "#code that conforms to the Python syntax rules is as followsifGuess_age = =Age_of_shen:Print("Yes,you got it.")Else:    Print("no,you is wrong.")

Part 2 Indent

Indentation Error:

Not indented:

Indentationerror:expected an indented block

tab! = Four spaces:

Indentationerror:unident dose not match no outer indentation level

Indents are equivalent to curly braces in other programming languages

Indentation level must be consistent, official rules knock four spaces

Syntax error: Syntaxerror:invalid Syntax

Native tab is not recommended, because win and Linux tab is not the same, cross-platform is all messed up

Therefore, in order to once and for all, you can replace the TAB function with four spaces, in notepad++, settings, preferences, language, check replace with a space

You can also display symbols in view, tick "show Spaces and Tabs," and the effect is as follows

  

Part 3 operator

Arithmetic operators

Subtraction +-*/

Divisible//

Residual%

Exponential arithmetic * *

Exponential operator has precedence over subtraction

Comparison operators

Greater than > greater than or equal to >=

Less than > less than or equal to <=

equals = = Not equal to! =

"Note" links in Python that support comparison operators

eg

A = 2= 3= 1if c<a<B:    print ("True" )

Output Result:

"Practice" The user enters three numbers and outputs the maximum value

num_1 = Int (input ("NUM1:")) Num_2= Int (Input ("Num2:")) Num_3= Int (Input ("Num3:")) Max_num=0ifNum_1 >Num_2:max_num=num_1ifMax_num <Num_3:max_num=Num_3Else: Max_num=num_2ifNum_2 <Num_3:max_num=Num_3Print("Max Unmber is:"+str (Max_num))

Operation Result:

Assignment operator =

Plus + + + minus etc-= multiply etc *= except/=//% * *

Conditional operators

And or not to repeat

Expression: A code or statement consisting of operands and operators that can be placed on the right of a variable to assign a value to a variable

Short Circuit principle

Condition 1 and Condition 2 if condition 1 is false, condition 2 is no longer evaluated, direct output false

Condition 1 or Condition 2 condition 1 is true, condition 2 is no longer evaluated, direct output true

Part4 while loop

Grammar rules

 while condition:    Execute statement     #breakcontinue with C language

Basically the same as the C language

"Exercise" outputs 1-100 of all even numbers

Count = 1;  while count<=100:    if count%2 = = 0        :print  ( Count)    + = 1

"Exercise 2" Guess age-Enhanced Edition

Age_of_shen = 22Tag=False while  notTag:guess_age= Int (Input (">>:"))    ifGuess_age = =Age_of_shen:tag=TruePrint("Yes,you got it.")    elifGuess_age >Age_of_shen:Print("should try smaller")    Else:        Print("try bigger")

  "Note" In Python the inverse operator is not, do not write C language write silly habitual use!

Operation Result:

"Exercise" Output 99 multiplication table

# 99 multiplication Table  = 1 while J <= 9:    = 1 while     i<= J:        print (str (i) +"*"+str (j) +"="+str (i*j), end=" \ t " )        + = 1    print(    )+ = 1

Output Result:

"Exercise 2" outputs a rectangle made up of "#", with a long width specified by the user

#output rectangle consisting of "#", long and wide specified by userHeight= Int (Input ("Height:")) Width= Int (Input ("Width:")) Num_height=0num_width=0 whilenum_height<Height:num_width=0 whilenum_width<Width:Print("#", end="") Num_width+ = 1Print() Num_height+ = 1

Output Result:

In Summary

  1. Familiar with the IF statement in Python and the syntax rules of the while statement

While condition :

      EXECUTE statement

If condition :

      EXECUTE statement

elif conditions :

      EXECUTE statement

    Else Condition :

      EXECUTE statement

The red part differs from the C language, so be sure to remember

2. Understand the while....else .... How to use

While condition:

Code

Else

Code

Code after the while loop performs the else after the normal end, and does not execute if there is a break

3. Learn the difference between tab and space, and learn the effect of switching tab in notepad++

4. Familiar with the operators in Python

    With and

OR OR

Non-not

These three are different from C language, please remember

Python Learning Diary 3/10

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.