python starting from scratch - 05 data types and variables

Commonly used data types in Python fall into the following categories:

    1. Integer Example: 1, 100, -1897, 0

    2. Floating point number Example: 3.1415926, -98.7, 1.33e8

    3. String example: "sdfdf", 'kkkg'  

        a) Use \ to escape, such as print('C:\\desktop\\newfolder'), the printed result is C:\desktop\newfolder 

        b) Use r to escape, such as print(r'C:\desktop\newfolder'), the printed result is also C:\desktop\newfolder

        c) Use u to represent a unicode string, such as print(u'This is a unicode string')

   d) \n means newline, \t means tab, which is consistent with other languages

        e) Multi-line characters use """xxxxx""" or '''xxxxx'''     

print('''This
is
a
pig
''')

    4. Boolean values ​​can be connected with and, or, not, many print(not 1 > 2) are used in the code; if n>100 and t==50: xxxxx

    5. None, the use of None in python

           a) for assert, such as

char_list = ['a', 'b', 'm', 'd']
assert char_list is not None

           b) if...else...None is equivalent to False, and other non-None variables are equivalent to True (should be one with no address specified and one with an address)

var1 = 'ddd'
var2 = None

if var1:
    print("var1 is not None")
else:
    print("var1 is None")

if var2:
    print("var2 is not None")
else:
    print("var2 is None")

          c) If the function has no return, the return is None

def add1(a,b):
    return a+b
a1=add1(1,2)
print a1
# will output 3, because there is return, there is a return value
 
def add2(a,b):
    print a+b
a2 = add2(1,2)
print a2
# will output None, because there is no return, then add2 is None

  6. Variables: Python is a dynamic language, and its variable type is not fixed. This is different from static languages ​​such as C#, so the following is correct.

var = True
var = 100
print (var)

The Python interpreter does the following:

a) creates a boolean value of True in memory ;

b) Created a variable called var in memory and pointed it to this True .

c) and then create an integer of 100 in memory

d) point the var variable to the integer 100

  7. Constants Python does not actually have real constants, but uses uppercase variable representation to agree that it is a constant, such as PI = 3.1415


Others also write detailed knowledge about data types, which are not commonly used, such as the expression of complex numbers, refer to http://www.runoob.com/python/python-variable-types.html

        

Guess you like

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