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

Python learning day-2

編輯:Python

python Study Day-2

One 、 Variable

1.1 Defining variables ( Definition - establish )

  • Variable action —— Container for saving data , Using variables is using containers in variables
  • grammar —— Variable name = data
    • Variable name —— Named by the programmer himself
      • Is an identifier.
      • Cannot be keyword
      • Know what you know ( The variable name is highly related to the data content )
      • All the letters are lower case , Multiple words are separated by underscores
      • Do not use the system function name 、 Class name 、 Module name
    • = —— For fixed writing ( Assignment symbol )
    • data —— Any expression with a result
      • Any type of data
      • Operation expression

1.2 Using variables

  • What the saved data can do , Variables can be done

    print(12) And a=12;print(a)
    print(13+12) And a=13;b=12;print(a+b)
    

1.3 Variable reassignment

  • Different types of data can be assigned when reassigning
  • After reassigning , The variable is the data of the last assignment

1.4 Define multiple variables at the same time

  • Multiple variables define the same value

    Variable 1= Variable 2= Variable 3= data

  • Multiple variables define different values

    Variable 1, Variable 2= data 1, data 2 ( The number of variables must be consistent with the number of data )

1.5 The principle of defining variables and reassignment

  • python Define variables and apply for memory first , Apply for memory according to data size
  • python Re assign the value and apply for memory according to the new data

Two 、 Operator

2.1 Add, subtract, multiply and divide

  • python Addition, subtraction, multiplication and division are consistent with the operation rules in mathematics
  • The operator is (+、-、*、/)
  • Whether the operation result of addition, subtraction and multiplication is a floating-point number is determined by the operation object
  • The result of division is a floating point number

2.2 Remainder ( modulus )

  • Remainder is the remainder in mathematics
  • The rules :x%y - seek x Divide y The remainder of
  • Application scenarios : Judge whether there is an integer division relationship between two numbers

2.3 to be divisible by

  • The rules :X//y - seek x Divide y The business of , Rounding down
  • Application scenarios :
    • Get the quotient of an integer if the integer can be divided
    • Remove the lower digits of an integer

2.4 Power operation

  • The rules :x**y - seek x Of y Power
  • Application scenarios :
    • Perform power operation
    • Find the reciprocal or root

2.5 Comparison operator

  • Greater than (>) 、 Less than (<)、 be equal to (==)、 Greater than or equal to (>=)、 Less than or equal to (<=)、 It's not equal to (!=).
  • The result of all comparison operators is Boolean .

2.6 Logical operators

2.6.1 Logic and - and
  • Application scenarios : Equivalent to... In life ’ also ’, Used when two conditions are required to be true at the same time .
  • Operator rule : All two are True by True, Otherwise False.
2.6.2 Logic or - or
  • Application scenarios : Equivalent to... In life ’ or ’, Used when at least one of multiple conditions is satisfied .
  • Operator rule : Either condition is True by True, Otherwise False.
2.6.3 Logic is not - not
  • Application scenarios : Condition forward writing is complicated , The reverse is simpler .
  • Operator rule : Negate the specified condition .

3、 ... and 、 Input problem and floating point problem

3.1 Input

adopt input The data type of operation input is string str

3.2 The storage principle of floating point number

Some floating-point numbers cannot be represented as multiple 2 The form of the sum of powers , As a result, it cannot be accurately stored

Four 、 Assignment operator

  • Operator form :=、+=、-=、*=、/=、%=、//=、**=
  • Operator rule : After the variable is calculated , Then assign the result to the variable ( except = Outside , The variable name must be defined )
  • Assignment statement has no result , Data cannot be provided directly to the program
  • Operator precedence :
    • Mathematical operators > Comparison operator > Logical operators > Assignment operator ( The minimum )
    • Power operation > Multiplication and division > Addition and subtraction
    • If there are brackets, count them first

5、 ... and 、 Data types and Operator jobs

choice question

  1. print(100 - 25 * 3 % 4) What should be output ? (B)

    A. 1

    B. 97

    C. 25

    D. 0

  2. Which of the following statements is wrong (A).

    A. Except for dictionary types , All standard objects can be used for Boolean testing

    B. The Boolean value of an empty string is False

    C. The Boolean value of an empty list object is False

    D. The value is 0 The Boolean value of any numeric object of is False

  3. Python Unsupported data types are (A).

    A. char

    B. int

    C. float

    D. list

  4. ( multi-select )n = 6784, You can get 7 The way to do this is (CD).

    A. n / 1000 % 100

    B. n % 1000 / 100

    C. n // 100 % 10

    D. n // 10 % 100 // 10

  5. Run the following program , When entering... From the keyboard 12, The result of the operation is (A).

    x = (input())
    print(type(x))
    

    A. <class 'str'>

    B. <class 'int'>

    C. error

    D. class 'dict'

  6. The operation result of the following expression is ( D ) .

    a = 100
    b = False
    print(a * b > -1)
    

    A. False

    B. 1

    C. 0

    D.True

Completion

  1. stay Python The empty type is (None).

  2. The function name to view the type of data in the variable is (type).

  3. It is known that x = 3 == 3, After execution , Variable x The value of is (True).

  4. It is known that x = 3, Then execute the statement x += 6 after ,x The value of is (9).

  5. expression 3 ** 2 The value of is (9), expression 3 * 2 The value of is (6), expression 4 ** 0.5 The value of is (2.0).

Programming questions

  1. Write to determine whether a number can be simultaneously 3 and 7 Divisible conditional statement , And print the corresponding results .

     for example : Input 21 Print True, Input 9 Print False.
    num = int(input(' Please enter a number :'))
    print(num%21 == 0)
    
  2. Write to determine whether a number can be 3 perhaps 7 to be divisible by , But not both 3 perhaps 7 Divisible conditional statement , And print the corresponding results .

     for example : Input 14 Print True, Input 4 Print False, Input 21 Print False.
    num = int(input(' Please enter a number :'))
    print((num%3 == 0 or num%7==0)and not num%21==0)
    
  3. Enter year , Write code to judge whether the entered year is a leap year , And print the corresponding results .( It's a leap year condition : Can be 4 Divisible but not by 100 Divisible or capable of being divisible by 400 Divisible year )

     for example : Input 2020 Print True, Input 2011 Print False
    year=int(input(' Please enter the year :'))
    print((year%4==0 and year%100!=0) or year%400==0)
    
  4. Suppose today's class time is 15678 second , Programming to calculate the class time today is how many hours , How many minutes? , How many seconds ; With ‘XX when XX branch XX second ’ It's expressed in a different way .

     for example : Time 67 second —> 0 when 1 branch 7 second
    time_1=int(input(' Today's class is '))
    hours=time_1//3600
    mins=(time_1%3600)//60
    sec=time_1%60
    print(hours,' when ',mins,' branch ',sec,' second ')
    
  5. Define two variables to save a person's height and weight , Programming to determine whether the person's body is normal !

    The formula : weight (kg)/ height (m) The square value of stay 18.5 ~ 24.9 It's normal .

     for example : Enter the weight : 55, Enter the height :1.55, Output : True
    high=float(input(' Please enter height (m):'))
    weight=float(input(' Please enter the weight (kg):'))
    bmi=weight/high**2
    print(18.5<=bmi<=24.9)
    

Short answer

  1. Python What are the built-in data types ?

    • Digital data , Plastic surgery (int) And floating point (float)
    • str class , It is used to represent the data corresponding to text information
    • bool class , The two results are True and False, They correspond to each other 0 and 1
    • None Empty space ([]、{}、()、0)
  2. Write your thoughts about today ⽇ Where there are questions in the teaching content of the day ⽅ Fang ( Or knowledge points that feel difficult ).

    nothing


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