a byte of python

Python解释性

Python不需要编译为二进制码,而是转换为中间码,不用考量库的链接和编译,更加适合迁移

Hello World

print ("Hello World")
Python 3.7.2 (tags/v3.7.2:9a3ffc0492, Dec 23 2018, 23:09:28) [MSC v.1916 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> print ("Hello World")
Hello World

注释

print('hello world') #注意到print是一个函数
  • 解释假设
  • 说明重要的决定
  • 解释重要的细节
  • 说明你想要解决的问题
  • 说明你想要在程序中克服的问题,等等。

字面常量

数字:123

字符串:“This is a string”

数字

int: 2 没有long int 可以表示任意大小

float 2.2

字符串

单引号

’Quote me on this ‘

引号内空间 如:空格,制表符 原样保留

双引号

与单引号相同

三引号

’‘’

三引号内指定多行字符串,三引号之间自由使用单双引号

‘’‘

字符串是不可变的

格式化方法

age = 20
name = 'Swaroop'
print('{0} was {1} years old when he wrote this book'.format(name, age))
print('Why is {0} playing with that python?'.format(name))
$ python str_format.py
Swaroop was 20 years old when he wrote this book
Why is Swaroop playing with that python?

一个字符串可以使用某些特定的格式(Specification),随后, format 方法将被调用,使用这一方法中与之相应的参数替换这些格式。在这里要注意我们第一次应用这一方法的地方,此处 {0} 对应的是变量 name ,它是该格式化方法中的第一个参数。与之类似,第二个格式 {1} 对应的是变量 age ,它是格式化方法
中的第二个参数。请注意,Python 从 0 开始计数,这意味着索引中的第一位是 0,第二位是1,以此类推。

# 对于浮点数 '0.333' 保留小数点(.)后三位
print('{0:.3f}'.format(1.0/3))
# 使用下划线填充文本,并保持文字处于中间位置
# 使用 (^) 定义 '___hello___'字符串长度为 11
print('{0:_^11}'.format('hello'))
# 基于关键词输出 'Swaroop wrote A Byte of Python'
print('{name} wrote {book}'.format(name='Swaroop', book='A Byte of Python'))

print 总是会以一个不可见的“新一行”字符( \n )结尾,因此重复调用 print 将会在相互独立的一行中分别打印。为防止打印过程中出现这一换行符,你可以通过 end 指定其应以空白结尾:

print('a', end='')
print('b', end='')

转义序列

“What’s your name?”

  • 使 Python 对于何处是字符串的开始、何处又是结束而感到困惑

转义序列(Escape Sequence) 来实现

‘What’s your name?’

在一个字符串中,一个放置在末尾的反斜杠表示字符串将在下一行继续,但不会添加新的一行

"This is the first sentence. \
This is the second sentence."
"This is the first sentence. This is the second sentence."

原始字符串

如果你需要指定一些未经过特殊处理的字符串,比如转义序列,那么你需要在字符串前增加r 或 R 来指定一个 原始(Raw) 字符串

r"Newlines are indicated by \n"

变量

标识符命名

开头字母下划线

其余数字字母下划线

大小写区分

# 文件名:var.py
i = 5
print(i)
i = i + 1
print(i)
s = '''This is a multi-line string.
This is the second line.'''
print(s)
5
6
This is a multi-line string.
This is the second line.

如果你有一行非常长的代码,你可以通过使用反斜杠将其拆分成多个物理行。这被称作显式行连接(Explicit Line Joining)

i = \
5

在某些情况下,会存在一个隐含的假设,允许你不使用反斜杠。这一情况即逻辑行以括号开始,它可以是方括号或花括号,但不能是右括号。这被称作 隐式行连接(Implicit Line Joining)

缩进

在逻辑行的开头留下空白区(使用空格或制表符)用以确定各逻辑行的缩进级别,而后者又可用于确定语句的分组。

有一件事你需要记住:错误的缩进可能会导致错误

i = 5
# 下面将发生错误,注意行首有一个空格
print('Value is', i)
print('I repeat, the value is', i)
File "whitespace.py", line 3
print('Value is', i)
^
IndentationError: unexpected indent
# 缩进错误:意外缩进

运算符

  • +(加)
    两个对象相加。
    3+5 则输出 8 。 ‘a’ + ‘b’ 则输出 ‘ab’ 。
  • -(减)
    从一个数中减去另一个数,如果第一个操作数不存在,则假定为零。
    -5.2 将输出一个负数, 50 - 24 输出 26 。
  • *(乘)
    给出两个数的乘积,或返回字符串重复指定次数后的结果。
    2 * 3 输出 6 。 ‘la’ * 3 输出 ‘lalala’ 。

  • ** (乘方)

    返回 x 的 y 次方。
    3 ** 4 输出 81 (即 3 * 3 * 3 * 3 )。

  • / 除

    浮点除

  • // 整除

  • % (取模)

  • << (左移)

  • ‘>>’(右移)

  • | (按位或)

  • ^ (按位异或)

  • ~ (按位取反)

  • < (小于)

  • ‘>’(大于)

  • = (大于等于)

  • == (等于)

  • != (不等于)

  • not (布尔“非”)

  • and (布尔“与”)

  • or (布尔“或”)

一种比较常见的操作是对一个变量进行一项数学运算并将运算得出的结果返回给这个变量,因此对于这类运算通常有如下的快捷表达方式:

a = 2
a = a * 2
a= 2
a*=2

求值顺序

  • lambda :Lambda 表达式

  • if - else :条件表达式

  • or :布尔“或”

  • and :布尔“与”

  • not :布尔“非”

  • in, not in, is, is not, <, <=, >, >=, !=, == :

  • |

  • ^

  • &

  • <<>>

  • +=

  • */

控制流

guess = int(input('Enter an integer : '))
if guess == number:
    # 新块从这里开始
    print('Congratulations, you guessed it.')
    print('(but you do not win any prizes!)')
# 新块在这里结束
elif guess < number:
    # 另一代码块
    print('No, it is a little higher than that')
    # 你可以在此做任何你希望在该代码块内进行的事情
else:
    print('No, it is a little lower than that')
    # 你必须通过猜测一个大于(>)设置数的数字来到达这里。
print('Done')
# 这最后一句语句将在
# if 语句执行完毕后执行。

我们为内置的 input 函数提供一串打印到屏幕上的字符串并等待用户的输入。一旦我们输入了某些内容并按下键盘上的 enter 键, input() 函数将以字符串的形式返回我们所输入的内容。然后我们通过 int 将这个字符串转换成一个整数并将其储存在变量 guess 中。实际上, int 是一个类(Class),但你现在你所需要知道的就是你可以使用它将一串字符串转换成一个整数(假设这个字符串的文本中含有一个有效的整数)。

if 语句在结尾处包含一个冒号

elif 和 else 同样都必须有一个冒号在其逻辑行的末尾,后面跟着与它们相应的语句块

while 语句

while 语句能够让你在条件为真的前提下重复执行某块语句。 while 语句是 循环(Looping) 语句的一种。 while 语句同样可以拥有 else 子句作为可选选项。

number = 23
running = True
while running:
    guess = int(input('Enter an integer : '))
    if guess == number:
        print('Congratulations, you guessed it.')
        # 这将导致 while 循环中止
        running = False
    elif guess < number:
    	print('No, it is a little higher than that.')
	else:
    	print('No, it is a little lower than that.')
else:
    print
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值