Python Basics: Notes 2

How to run a python program:
  $python3 xxxx.py


Number type:
int int 
float float
boolean bool
complex type complex [easy to use in mathematics]


Nonetype null None is an object, it really means nothing


expression number and operator +-*/ // ** %
comparison operator: >< >= <= == !=
boolean operation: not and or 


variable: bind value or bind data object
assignment statement: variable = expression
      no variable Just create a variable, and change the variable with this variable
Compound assignment operator: += -= *= /=
                          In order to change the variable, later use
is/is not to
    judge whether two objects are the same (id)
 id(obj) Return the object The Id address of the
  two objects is the same, that is, the same object


type(obj) returns the type of the object


help() see the help document


del statement deletes a variable


built -in function:
abs() round float int bool complex


line break \ 


pow function
  pow(x, y, z=None)
         is equivalent to x**y when there are two parameters, and when there are three parameters. Equivalent to x**y%z


line break 
1. Display line break: line break \
2. Hidden line break:
   all line breaks in parentheses are called hidden line breaks () [] {}
Example:
  i=1+2+3\
               +4+5 (display newline)
  i=(1+2+3
               +4+5)
   If the brackets appear in pairs, they will hide the newline until the corresponding bracket is found.




Basic input and output The
  basic input function input reads from the standard input device Take a string
  input('prompt string') Return the input string (only python3) 'prompt string' can be empty
  Basic output function: print
               outputs a series of values ​​in the form of strings to the standard output device , the default is the terminal
   print(value,...,sep='',end='\n')
             The keyword parameter of the option is:
      sep The separator between the two values, the default is a space
      end automatically after the output is completed Append a character that defaults to a newline '\n'


Exercise:
  1. Today is Xiao Ming's 20th birthday, assuming there are 365 days in a year. Count how many weeks he has passed. How many days are left?
  2. Enter the current hour, minute, and second three times. How many seconds has passed since the early morning when printed on the terminal?






If statement
  Function : Let the program execute certain statements selectively according to the conditions
  Description: The if statement is also called a conditional statement, also called a branch statement
Syntax  :
   if truth expression 1:
     statement block 1
   elif truth expression 2:
                        statement block 2
                 .......
             else:
                          statement block n
[multi-line compound statement, multiple statement packaging]
syntax description:
  elif clause can have 0, one or more
  else clauses can have 0 or 1 and can only be placed in The final


requirement : the statements inside the if statement are usually indented with 4 spaces to indicate the inclusion relationship, and the same indentation format represents the same level of belonging.
Exercise:
 1. Input a number arbitrarily, judge whether it is greater than 100, judge whether it is greater than 0, and judge whether it is between 20 and 50.
 2. Enter the month to find the quarter, and enter the quarter to find the month.






The truth expression of the if statement
  if 100: is equivalent to if bool(100):
bool(x) evaluates to false:
  0 '' [] {}} () set() empty set None
if statement nested
 if statement itself is a compound statement composed of multiple clauses
  if statement can be used as a statement nested inside another statement


statement is an execution unit
assignment Statement del statement if statement expression statement


expression : data + operator


conditional expression:
 syntax:
   expression 1 if truth expression else expression 2
 function:
   if the boolean environment value of the truth expression is True, execute the expression Form 1 and return a reference to the result, otherwise just form expression 2 and return a reference to the result.
  


Pass statement
 Function : usually used to fill in the syntax blank pass statement has a named empty statement




Boolean
 operation operator not and or non-and or
Boolean non-operation not
 syntax: not x
 function: take a boolean value on x, and return if bool(x) is True False, otherwise return True


 
Boolean AND operation and
syntax:
   x and y
        Note: x, y represents expression
Function : return false value object first
  When the value of bool(x) is False, return x, otherwise return y






Boolean or operation
 Syntax :
   Function of x or y
  : return the truth object first.
                     When x is True, return x, otherwise return y
    True or True #True
  True or False #True
   False or False #False
False or True #True
100 or 200 #100
100 or 0.0 #100
0 or 200 #200
0 or 0.0 #0.0Sign






operator
  + -
Syntax: + expression
    - expression
Exercise :
1. Beijing Taxi Charges The
   charging standard is 13 yuan within 3 kilometers, and the basic unit price is 2.3 yuan/km after 3 kilometers
        .  
         3.45 yuan/km)
   requirements: Enter the number of kilometers, and print out the amount of the cost. Round off
      in yuan
. 2. Lose a student's grades in three subjects
   Print the highest score and the lowest average score
3. Given a year, determine whether it is a run-in year and print the result; 
   The rule of running the year: every four years, one running year, every hundred years, no running year, four hundred years and another running year
4. BMI index body mass index
 BMI formula: BMI = weight (kg) / height in meters square
  Standard table: BMI < 18.5 underweight
  18.5 <= BMI <= 24 normal range
  BMI > 24 overweight

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325455695&siteId=291194637