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

Python learning (II) variables and operators

編輯:Python

python Variable

1. Defining variables ( Create variables )

A variable is a variable used to Container for saving data , Using variables is to use the data stored in variables
grammar : Variable name = data

  1. Variable name :

     requirement : Is an identifier but not a keyword
    standard :
    1. See the name and know the meaning ( When you see the variable name, you know what data is stored in the variable )
    2. You cannot use system function names 、 Class name and module name
    3. All letters need lowercase , Multiple words are separated by underscores
    
  2. = : Assignment operator

  3. data : It can be any expression with a result
    for example : A specific data 、 Calculation results 、 Variables that have been assigned 、 Function call expression, etc .

2. Using variables

 age = 20
name = "wlstory"
num = 3 * 5
print(age, name, num)

Be careful : Variables must be defined before using

3. Re assign a variable

grammar : Variable name = The new data

a = 100
print(a,id(a))
# 100 140704131793792
a = 200
print(a,id(a))
# 200 140704131796992

id( Variable ) : Get the memory address of the variable
The new data is used in the variable after re assignment

4. Define multiple variables at the same time

  1. Define multiple variables at the same time and assign the same value
    grammar : Variable name 1 = Variable name 2 = Variable name 3 = ... = data
     a = b = c = 20
    
  2. Define multiple variables at the same time and assign different values
    grammar : Variable name 1, Variable name 2, ..., Variable name n = data 1, data 2, ..., data n.—> ( The number of variable names must match the number of data The number is the same ).
     a,b = 18,20
    

5. The principle of defining variables and reassigning variables

python To define a variable, you need to apply for memory , The size of the memory request is determined according to the needs of the saved data .
When reassigning , Will reapply for memory , How big the new memory is depends on the new data , Then bind the variables , Free the original memory .

Operator

1. Mathematical operators

+( Add ), -( reduce ), *( ride ), /( except ), %( Remainder ), //( to be divisible by ), **( Power operation

  1. Add, subtract, multiply and divide :
    python Medium +、 -、 .、 / And in mathematics +、-、×、÷ The function is as like as two peas 、
     print(1+1) # 2
    print(2-1) # 1
    print(2*1) # 2
    print(1/2) # 0.5
    
    Be careful : / The result of the operation must be float; +、-、 * The type of the result of the operation depends on Is there a Floating point numbers
  2. Remainder ( modulus ) --> %
    Modulus is to find the remainder
    The rules : x % y —> seek x Divide y The remainder of
    Application scenarios :
    1. Determine whether there is an integer division relationship between two numbers
    2. Take the lower digits of a number
 # Extract the last digit of a five digit number 
num = 235941
print(num % 10) # 1
  1. to be divisible by --> //
    function : Seeking quotient , Rounding down
 a = 50
print(a//10) # 5
  1. Power operation --> **
    The rules : x ** y —> seek x Of y Power
 a = 15
print(a ** 2) # 225
 Calculation 1000 Number of daffodils in
for i in range(100, 1000):
bw = i // 100
sw = i // 10 % 10
gw = i % 10
if ((bw**3) + (sw**3) + (gw**3)) == i:
print(i)
# 153
# 370
# 371
# 407

2. Comparison operator :

>( Greater than ), <( Less than ), ==( be equal to ), >=( Greater than or equal to ), <=( Less than or equal to ), !=( It's not equal to )
Be careful :

  1. The result of all comparison operators is Boolean
  2. It can be like math , Concatenation representation range Small scale <= x <= large-scale

3. Logical operators :

and( Logic and ), or( Logic or ), not( Logic is not )

  1. Logic and —> and
    Application scenarios : Equivalent to... In life also , Used to connect two requirements , Two requirements At the same time, the conditions for establishment are met
    Operational rules : All two are True The result is True, As long as one is False The result is False
 # Judge whether a number can be 3 and 7 to be divisible by 
num = 27
print(num % 7 == 0 and num % 3 == 0) # False
  1. Logic or —> or
    Application scenarios : Equivalent to... In life perhaps , Used to connect multiple conditions It can be established if one condition is met
    Operational rules : One of them Ture Namely Ture, All are False Namely False
 # Judgement of leap year 
year = 2000
print(year % 4 == 0 and year % 100 != 0 or year % 400 == 0) # True
  1. Logic is not —> not
    Application scenarios : Do sth on a condition no ; The positive conditions are complex , The reverse is simple , Then write the condition backwards and add not
    Operational rules : Only the specified condition can be negated .
  2. The result of a logical operation is only Boolean True and False

4. Assignment operator :

=、 +=、-=、*=、/=、//=、**=

 a = 15
a *= 2
print(a) # 30
a /= 5
print(a) # 6.0
a //= 2
print(a) # 3.0
a **= 3
print(a) # 27.0

Conclusion : The function of all assignment operators is to Data is stored in variables
Assignment statement has no result , Data cannot be provided directly to the program

5. Operator precedence

In mixed operations , Those with higher priority shall be calculated first , Low priority post computation
Mathematical operators > Comparison operator > Logical operators > Assignment operator ( The minimum )
Power operator > *、/、//、% > +、- ( The minimum )
If there are brackets, count them first


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