Variables and Data Type

Integer

Python doesn't like Java, C, or C++, it can handle any size of integer. Like 100, -123, 0 in decimal, octal, or hexadecimal.
example

//Code==========
print(-123)
print(0x12fd2)
//Result==========
-123
77778

Float

Python doesn't like Java, C, or C++, it doesn't care about double or float. Like 1.23, 1.2e-20, -3.5e7
example

//Code==========
print(1.2e-20)
print(-3.5e7)
//Result==========
1.2e-20
-35000000.0

String

Both '' and "" are working with string type, and allow using them by nested each other.
example

//Code==========
print("Hello World")
print('Hello Python')
//Result==========
Hello World
Hello Python

Escape character
Python also allow to print ' or ", then you need to escape each charactor in use.
example

//Code==========
print("\'Hello\' \"World\"")
//Result==========
'Hello' "World"

\n new line
\t tab
\\ backslash

r operator provides another way to make string escaping easier, wring as normal words within the r statement
example

//Code==========
print(r'\(~_~)/ \(~_~)/')
//Result==========
\(~_~)/ \(~_~)/

Multi-line string
'''...''' provides the way to writing a string in multiple lines, also can using comment to become multi-line comments
example(for markdown editor, code viewr doesn't support multi-line operator)

//Code==========
print r'''"To be, or not to be": that is the question.Whether it's nobler in the mind to suffer.'''
#'''"To be, or not to be": that is the question.Whether it's nobler in the mind to suffer.'''
//Result==========
"To be, or not to be": that is the question.Whether it's nobler in the mind to suffer.

Boolean

Python is Case Sensitive, there are only two value are valid for boolean type, True and False
Logic Operators
it doesn't like other OOP languages, it uses and, or, and not instead of using &&, ||, and !
example

//Code==========
print(100<99)
print((1<2) and (3<4))
//Result==========
False
True

Empty value

It uses None, but in other OOP languages, they use null

Print statement

  • in Python2, you can writing without parentheses, but in Python3 you have to use them, because in Python3, print is a function and no longer is a command
  • using , will generate a empty space
    example
//Code==========
print('hello python')
print('hello', 'python')
//Result==========
hello python
hello python

Comment

using # for line in Python is similar using // in other OOP languages
example

//Code==========
#print('hello python')
//Result==========

Variable

Consider variable as a reference for the address of the data in memory
example

//Code==========
a = 'ABC'
b = a
a = 'XYZ'
print(b)
//Result==========
ABC
create the space for 'ABC' in memory
create a reference named a in memory, point to the address of `ABC`, create another reference named b and point to same address as a pointed
new space for 'XYZ' is created, and a point to the address of `XYZ`

Unicode String

Generally, Unicode is using for international words. When using unicode, please add # -*- coding: utf-8 -*- as the first line.
example

//Code==========
# -*- coding: utf-8 -*-
print('''我现在是有点儿饿了,你呢?''')
//Result==========
我现在是有点儿饿了,你呢?

Arithmetic

Same to other OOP languages, +, -, *, /, %, ^, etc. But in Python3, / is special. / in Python2 is dividing as integer, but in Python3, it will automatically switch integer to float, but if you still need dividing as integer, you need to use //.
example

//Code==========
print(1/2)
print(1//2)
//Result==========
0.5
0

你可能感兴趣的:(Variables and Data Type)