Blog 03

Getting Started with Python

1. Variables

  1. Variable is a variable quantity, attributes used to describe certain things. It is to describe the nature and role of receiving variable values
  2. Define a variable way:

Variable name = variable value

  1. Variable name rules:

    • Variable names must be descriptive sense
    • Variable names with letters, numbers and underscores.
    • Variable names can not be named keyword
  2. Two types of variables name representation:

    Underline and hump body (to solve the problem of multiple words variable name)

  3. 3 ways to print variable

x = 257
print(x,id(x),type(x))
#分别打印x的值,值的内存地址,值的数据类型
  1. The concept of variables is provided by Python, and if you do not run the program code, there is no variable

2. Python program in two ways

(1) Interactive

  • Pros: write a line translator row, the ability to detect bug

  • Disadvantages: execution tedious, time-consuming

(2) command line

  • Advantages: high efficiency

  • Disadvantages: difficult to find a bug

3. Python garbage collection

  1. Python variable value defined in the variable name has a reference value, each of which has a reference to the variable name, the value of the variable reference count is incremented when the reference count is zero, the value of this variable will send recovered, corresponding to memory space will be reclaimed.

Concept 4. DESCRIPTION Python small integer pool

  • concept:

    When the pool is small integers Python Python when activated, it will automatically define an integer variable between [-5,256], their memory address has been written dead, any integer variable references within this range, the memory address will not change

  • For x = 10, print variable values ​​are Python code, the memory address of the variable value, the variable value data type

    x=10
    print(x,id(x),type(x))
    
    # 打印结果:10 1490972416 <class 'int'>
  • The following code, determining x, y, z values ​​are the same variables? x, y, z where the same memory address, please elaborate why use Python code?

    x = 257
    y = x
    z = 257
    
    print(x,y,z)#打印x、y、z的值
    #打印结果:257 257 257
    print(id(x),id(y),id(z))#打印x、y、z值的内存地址
    #打印结果:5811920 5811920 5811920
    

    A: x, y, z are the same value, x, the same memory address y, z and x, y, different memory addresses because the x, y values ​​are the same references, and z = 257 defines a new variable, z is another reference value. Therefore, x, y, the same memory address, z and x, y different memory address.

5. Description of numeric type

1. Digital types include integer and floating point

(1) integer - "int

  • Integer is the integer type, floating-point values ​​can be cast into an integer type, but not rounded, rounding would want to use round ().
salary2 = int(3.7)
print(salary2)
#打印结果:3  不会四舍五入
print(round(salary2))
#打印结果:4   会四舍五入

== round () brackets will float rounding ==

  • Usage: + - * / ** //%

(2) Float - "float

  • Is a decimal floating-point number type, the float may be cast to integer value of type float.

    height = float(4)    # 强制类型转换
    print(height)
    #打印结果:4.0
  • Usage: + - * / ** //%

6. Description of string type

1. Definition of the character string of:

  1. Single quotes (string can not have single quotes)
  2. Double quotes (string can not have double quotes)
  3. Three single quotes (string can not have three single quotes)
  4. Three pairs of quotes (string can not have three pairs of quotation marks)

2. Connection String

str1 = 'nick'
str2 = 'handsome'
print(str1 +' ' + str2 ) # 字符串不能和数字相加
#打印结果:nickhandsome

3. The output string n times

str1 = 'nick'
print(str1 * 5)#相当于乘法,打印5次字符串,中间会用空格隔开
#打印结果:nick nick nick nick nick

Guess you like

Origin www.cnblogs.com/Mcoming/p/11492654.html
03
03