Python 基础语法(runoob)

Python 基础语法(runoob)

多行语句

可使用反斜杠()来实现多行语句:

total = item_one + \
        item_two + \
        item_three

在 [], {}, 或 () 中的多行语句,不需要使用反斜杠()

数字(Number)类型

int (整数), 如 1, 只有一种整数类型 int,表示为长整型,没有 python2 中的 Long。
bool (布尔), 如 True。
float (浮点数), 如 1.23、3E-2
complex (复数), 如 1 + 2j、 1.1 + 2.2j

字符串(String)

python中单引号和双引号使用完全相同。
使用三引号(’’'或""")可以指定一个多行字符串。

print('''
... geek
... geek
... ''')

转义符 \,使用r可以让反斜杠不发生转义。如 r"this is a line with \n" 则\n会显示,并不是换行。
按字面意义级联字符串,如"this " "is " "string"会被自动转换为this is string。
字符串可以用 + 运算符连接在一起,用 * 运算符重复。

print('geek'*2)
geekgeek

Python中的字符串不能改变。
Python 没有单独的字符类型,一个字符就是长度为 1 的字符串。
字符串的截取的语法格式如下:变量[头下标:尾下标:步长]

等待用户输入 input()

input('\n\nPress enter to exit:')


Press enter to exit:this is a string
'this is a string'

同一行显示多条语句

Python可以在同一行中使用多条语句,语句之间使用分号(;)分割

import sys; x = 'runoob'; sys.stdout.write(x + '\n')
runoob
7 #7表示字符数

Print 输出
print 默认输出是换行的,如果要实现不换行需要在变量末尾加上 end="":

print( 'x', end=" " )
print( 'y', end="" )
x y

import 与 from…import

在 python 用 import 或者 from…import 来导入相应的模块。

将整个模块(somemodule)导入,格式为: import somemodule

从某个模块中导入某个函数,格式为: from somemodule import somefunction

从某个模块中导入多个函数,格式为: from somemodule import firstfunc, secondfunc, thirdfunc

将某个模块中的全部函数导入,格式为: from somemodule import *

导入 sys 模块:

import sys
print('================Python import mode==========================')
print ('命令行参数为:')
for i in sys.argv:
    print (i)
print ('\n python 路径为',sys.path)

导入 sys 模块的 argv,path 成员:

from sys import argv,path  #  导入特定的成员
 
print('================python from import===================================')
print('path:',path) # 因为已经导入path成员,所以此处引用时不需要加sys.path
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值