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
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi阅读 7,486评论 0 10
  • 太子湖畔柳色淡,凤凰岛上鹭影翩。 闲来泛舟碧波上,小橹轻摇渔歌晚。 草庐深深掩柴扉,空卧船头听雨眠。
    金指尖的花园阅读 428评论 0 4
  • Sublime Text3 当前版本号 Sublime Text3156,鉴于上次插件整理太过敷衍,这次老夫将奉上...
    飞球大的波波阅读 557评论 0 1
  • -webkit-user-select:none; -moz-user-select:none; -o-user-...
    魔王恩阅读 234评论 0 0
  • 上一个蓝夜,是今年的第一天。真快,过去二十天了。还记得当时在读书会里不知道自己kin刚接触历法,听小伙伴们说图腾但...
    零尘1113阅读 210评论 0 0